diff --git a/.gitignore b/.gitignore index 09cd281..0fe0144 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,10 @@ bin/ .classpath .project +# Visual Studio probably + +*.vs + # macos *.DS_Store diff --git a/src/main/java/net/fabricmc/example/mixin/ModLivingEntity.java b/src/main/java/net/fabricmc/example/mixin/ModLivingEntity.java new file mode 100644 index 0000000..b76bee9 --- /dev/null +++ b/src/main/java/net/fabricmc/example/mixin/ModLivingEntity.java @@ -0,0 +1,27 @@ +package net.fabricmc.example.mixin; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.world.Difficulty; +import net.minecraft.world.World; +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(LivingEntity.class) +public abstract class ModLivingEntity extends Entity { + + public ModLivingEntity(EntityType type, World world) { + super(type, world); + } + + @Inject(method = "canTarget(Lnet/minecraft/entity/LivingEntity;)Z", at = @At("HEAD"), cancellable = true, expect = 1) + public void onCanTarget(LivingEntity target, CallbackInfoReturnable ci) { + // Can't target if target is a player and difficulty is peaceful or easy + boolean readable = this.world.getDifficulty() == Difficulty.PEACEFUL || this.world.getDifficulty() == Difficulty.EASY; + ci.setReturnValue((!(target instanceof PlayerEntity) || !readable) && target.canTakeDamage()); + } +} diff --git a/src/main/resources/modid.mixins.json b/src/main/resources/modid.mixins.json index 7c42cb4..fc7028f 100644 --- a/src/main/resources/modid.mixins.json +++ b/src/main/resources/modid.mixins.json @@ -4,6 +4,7 @@ "package": "net.fabricmc.example.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "ModLivingEntity" ], "client": [ "ExampleMixin"