diff --git a/build.gradle b/build.gradle index e943242..46ead1a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,15 @@ +buildscript { + repositories { + maven { url = 'https://repo.spongepowered.org/repository/maven-public/' } + } +} + plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' + id 'org.spongepowered.mixin' version '0.7.+' } version = mod_version @@ -15,6 +22,9 @@ base { // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) +sourceCompatibility = JavaVersion.VERSION_17 +targetCompatibility = JavaVersion.VERSION_17 + println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { // The mappings can be changed at any time and must be in the following format. @@ -76,6 +86,7 @@ minecraft { property("mixin.env.remapRefMap", "true") property("mixin.env.refMapRemappingFile", "${projectDir}/build/createSrgToMcp/output.srg") + args '-mixin.config=contraptionsaresafe.mixins.json' mods { "${mod_id}" { @@ -148,6 +159,8 @@ dependencies { // The group id is ignored when searching -- in this case, it is "blank" // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") + annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' + implementation(fg.deobf("com.simibubi.create:create-${minecraft_version}:${create_version}:slim") { transitive = false }) implementation(fg.deobf("net.createmod.ponder:Ponder-Forge-${minecraft_version}:${ponder_version}")) compileOnly(fg.deobf("dev.engine-room.flywheel:flywheel-forge-api-${minecraft_version}:${flywheel_version}")) @@ -190,7 +203,9 @@ tasks.named('jar', Jar).configure { 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, - 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), + "TSRGFx" : "true", + "MixinConfigs" : "contraptionsaresafe.mixins.json" ]) } @@ -203,6 +218,11 @@ tasks.named('jar', Jar).configure { // dependsOn 'reobfJar' // } +mixin { + add sourceSets.main, "contraptionsaresafe.refmap.json" + config "contraptionsaresafe.mixins.json" +} + // Example configuration to allow publishing using the maven-publish plugin publishing { publications { diff --git a/src/main/java/com/bipolarexpedition/java/contraptions_are_safe/mixin/MobTargetingMixin.java b/src/main/java/com/bipolarexpedition/java/contraptions_are_safe/mixin/MobTargetingMixin.java new file mode 100644 index 0000000..a4a2561 --- /dev/null +++ b/src/main/java/com/bipolarexpedition/java/contraptions_are_safe/mixin/MobTargetingMixin.java @@ -0,0 +1,33 @@ +package com.bipolarexpedition.java.contraptions_are_safe.mixin; + +import com.bipolarexpedition.java.contraptions_are_safe.ContraptionsAreSafe; +import com.simibubi.create.content.contraptions.AbstractContraptionEntity; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Mob; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Mob.class) +public class MobTargetingMixin { + + @Inject(method = "canAttack(Lnet/minecraft/world/entity/LivingEntity;)Z", at = @At("HEAD"), cancellable = true) + private void preventContraptionTargeting(LivingEntity target, CallbackInfoReturnable cir) { + if (target != null && isOnContraption(target)) { + cir.setReturnValue(false); + } + } + + private boolean isOnContraption(Entity entity) { + Entity vehicle = entity.getVehicle(); + while (vehicle != null) { + if (vehicle instanceof AbstractContraptionEntity) { + return true; + } + vehicle = vehicle.getVehicle(); + } + return false; + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index ac353f8..f2733b2 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -41,6 +41,8 @@ authors="${mod_authors}" #optional # IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself. #displayTest="MATCH_VERSION" # if nothing is specified, MATCH_VERSION is the default when clientSideOnly=false, otherwise IGNORE_ALL_VERSION when clientSideOnly=true (#optional) +mixinConfig = "contraptionsaresafe.mixins.json" + # Access Transformers # Optional, if not specified Forge will attempt to find the default META-INF/accesstransformer.cfg and silently continue if not found. May default to an empty list in a future MC. # If you specify AT path strings in this list, Forge will attempt to load each of them and throw errors for any that aren't found. diff --git a/src/main/resources/contraptionsaresafe.mixins.json b/src/main/resources/contraptionsaresafe.mixins.json new file mode 100644 index 0000000..11ee5f6 --- /dev/null +++ b/src/main/resources/contraptionsaresafe.mixins.json @@ -0,0 +1,12 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "com.bipolarexpedition.java.contraptions_are_safe.mixin", + "compatibilityLevel": "JAVA_17", + "refmap": "contraptionsaresafe.refmap.json", + "mixins": [ + "MobTargetingMixin" + ], + "client": [], + "server": [] +} \ No newline at end of file