From aa1928ba02348187c9986e068f70e723765afa0d Mon Sep 17 00:00:00 2001 From: maxanier Date: Sat, 5 Aug 2023 13:12:13 +0200 Subject: [PATCH] Add mixin for MobType --- .../vampirism/config/VampirismConfig.java | 2 ++ .../vampirism/mixin/MixinLivingEntity.java | 21 ++++++++++++++++++- .../vampirism/mixin/MixinPlayerEntity.java | 12 ++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/teamlapen/vampirism/config/VampirismConfig.java b/src/main/java/de/teamlapen/vampirism/config/VampirismConfig.java index e283376d2a..ceaa1da5fc 100644 --- a/src/main/java/de/teamlapen/vampirism/config/VampirismConfig.java +++ b/src/main/java/de/teamlapen/vampirism/config/VampirismConfig.java @@ -142,6 +142,7 @@ public static class Server { public final ForgeConfigSpec.BooleanValue infectCreaturesSanguinare; public final ForgeConfigSpec.BooleanValue preventRenderingDebugBoundingBoxes; public final ForgeConfigSpec.BooleanValue allowVillageDestroyBlocks; + public final ForgeConfigSpec.BooleanValue vampiresAreUndeadType; public final ForgeConfigSpec.BooleanValue sundamageUnknownDimension; public final ForgeConfigSpec.ConfigValue> sundamageDimensionsOverridePositive; @@ -179,6 +180,7 @@ public static class Server { preventRenderingDebugBoundingBoxes = builder.comment("Prevent players from enabling the rendering of debug bounding boxes. This can allow them to see certain entities they are not supposed to see (e.g. disguised hunter").define("preventDebugBoundingBoxes", false); batDimensionBlacklist = builder.comment("Prevent vampire players to transform into a bat").defineList("batDimensionBlacklist", Collections.singletonList(Level.END.location().toString()), string -> string instanceof String && UtilLib.isValidResourceLocation(((String) string))); allowVillageDestroyBlocks = builder.comment("Allow players to destroy point of interest blocks in faction villages if they no not have the faction village").define("allowVillageDestroyBlocks", false); + vampiresAreUndeadType = builder.comment("Change vampire players mob type to UNDEAD. May cause issues with other mods").define("vampiresAreUndeadType", false); builder.push("sundamage"); sundamageUnknownDimension = builder.comment("Whether vampires should receive sundamage in unknown dimensions").define("sundamageUnknownDimension", false); diff --git a/src/main/java/de/teamlapen/vampirism/mixin/MixinLivingEntity.java b/src/main/java/de/teamlapen/vampirism/mixin/MixinLivingEntity.java index 281f8e2156..f7e98c5cf2 100644 --- a/src/main/java/de/teamlapen/vampirism/mixin/MixinLivingEntity.java +++ b/src/main/java/de/teamlapen/vampirism/mixin/MixinLivingEntity.java @@ -7,6 +7,8 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.MobType; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Mixin; @@ -17,13 +19,17 @@ @Mixin(LivingEntity.class) public abstract class MixinLivingEntity extends Entity { - private MixinLivingEntity(@NotNull EntityType type, @NotNull Level worldIn) { + protected MixinLivingEntity(@NotNull EntityType type, @NotNull Level worldIn) { super(type, worldIn); } @Shadow public abstract boolean addEffect(MobEffectInstance effectInstanceIn); + @Shadow + public abstract ItemStack getMainHandItem(); // Needed in subclass + + @Inject(method = "checkTotemDeathProtection", at = @At("RETURN")) private void handleTotemOfUndying(DamageSource damageSourceIn, @NotNull CallbackInfoReturnable cir) { if (cir.getReturnValue() && Helper.isVampire(this)) { @@ -31,4 +37,17 @@ private void handleTotemOfUndying(DamageSource damageSourceIn, @NotNull Callback this.addEffect(new MobEffectInstance(ModEffects.SUNSCREEN.get(), 800, 4)); } } + + protected MobType vampirism$getMobType() { + return null; + } + + @Inject(method = "getMobType", at = @At("HEAD"), cancellable = true) + private void handleGetMobType(CallbackInfoReturnable cir) { + MobType t = vampirism$getMobType(); + if (t != null) { + cir.setReturnValue(t); + cir.cancel(); + } + } } diff --git a/src/main/java/de/teamlapen/vampirism/mixin/MixinPlayerEntity.java b/src/main/java/de/teamlapen/vampirism/mixin/MixinPlayerEntity.java index 593e030362..086caf0ef8 100644 --- a/src/main/java/de/teamlapen/vampirism/mixin/MixinPlayerEntity.java +++ b/src/main/java/de/teamlapen/vampirism/mixin/MixinPlayerEntity.java @@ -1,11 +1,13 @@ package de.teamlapen.vampirism.mixin; +import de.teamlapen.vampirism.config.VampirismConfig; import de.teamlapen.vampirism.entity.player.IVampirismPlayer; import de.teamlapen.vampirism.entity.player.VampirismPlayerAttributes; import de.teamlapen.vampirism.util.MixinHooks; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.MobType; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; @@ -14,7 +16,7 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable; @Mixin(Player.class) -public abstract class MixinPlayerEntity extends LivingEntity implements IVampirismPlayer { +public abstract class MixinPlayerEntity extends MixinLivingEntity implements IVampirismPlayer { private final VampirismPlayerAttributes vampirismPlayerAttributes = new VampirismPlayerAttributes(); @@ -31,4 +33,12 @@ public VampirismPlayerAttributes getVampAtts() { public float vampireSlayerEnchantment(float damage, Entity target) { return damage + MixinHooks.calculateVampireSlayerEnchantments(target, this.getMainHandItem()); } + + @Override + protected MobType vampirism$getMobType() { + if (getVampAtts().vampireLevel > 0 && VampirismConfig.SERVER.vampiresAreUndeadType.get()) { + return MobType.UNDEAD; + } + return null; + } }