From 9c92b3b1a8a76c0f9735ab6699dd7a6b1c51adbb Mon Sep 17 00:00:00 2001 From: Niklas Widmann Date: Thu, 31 Oct 2024 13:58:34 +0100 Subject: [PATCH] make ScaredOfGargoyleGoal more reliable --- .../galena/oreganized/mixin/MobMixin.java | 22 +++++++++++++++++++ .../world/ScaredOfGargoyleGoal.java | 17 +++++--------- src/main/resources/oreganized.mixins.json | 4 ++-- 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/main/java/galena/oreganized/mixin/MobMixin.java diff --git a/src/main/java/galena/oreganized/mixin/MobMixin.java b/src/main/java/galena/oreganized/mixin/MobMixin.java new file mode 100644 index 00000000..472cdae0 --- /dev/null +++ b/src/main/java/galena/oreganized/mixin/MobMixin.java @@ -0,0 +1,22 @@ +package galena.oreganized.mixin; + +import galena.oreganized.world.ScaredOfGargoyleGoal; +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.CallbackInfo; + +@Mixin(Mob.class) +public class MobMixin { + + @Inject( + method = "", + at = @At("RETURN") + ) + private void addScaredOfGargoyleGoal(CallbackInfo ci) { + var self = (Mob) (Object) this; + if(self.level() == null || self.level().isClientSide()) return; + ScaredOfGargoyleGoal.addGoal(self); + } +} diff --git a/src/main/java/galena/oreganized/world/ScaredOfGargoyleGoal.java b/src/main/java/galena/oreganized/world/ScaredOfGargoyleGoal.java index d7dcb7ba..616c3557 100644 --- a/src/main/java/galena/oreganized/world/ScaredOfGargoyleGoal.java +++ b/src/main/java/galena/oreganized/world/ScaredOfGargoyleGoal.java @@ -1,8 +1,7 @@ package galena.oreganized.world; -import galena.oreganized.Oreganized; import net.minecraft.nbt.NbtUtils; -import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.MobType; import net.minecraft.world.entity.PathfinderMob; import net.minecraft.world.entity.ai.goal.Goal; @@ -10,13 +9,10 @@ import net.minecraft.world.entity.ai.util.DefaultRandomPos; import net.minecraft.world.level.pathfinder.Path; import net.minecraft.world.phys.Vec3; -import net.minecraftforge.event.entity.EntityEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.Mod; import javax.annotation.Nullable; +import java.util.EnumSet; -@Mod.EventBusSubscriber(modid = Oreganized.MOD_ID) public class ScaredOfGargoyleGoal extends Goal { public static final String AVOID_TAG_KEY = "ScaredByGargoyle"; @@ -25,11 +21,9 @@ public class ScaredOfGargoyleGoal extends Goal { private final PathfinderMob mob; - @SubscribeEvent - public static void onMobSpawn(EntityEvent.EntityConstructing event) { - if(!(event.getEntity() instanceof Mob entity)) return; - if (entity.getMobType() == MobType.UNDEAD && entity instanceof PathfinderMob mob) { - entity.goalSelector.addGoal(1, new ScaredOfGargoyleGoal(mob)); + public static void addGoal(Entity entity) { + if (entity instanceof PathfinderMob mob && mob.getMobType() == MobType.UNDEAD) { + mob.goalSelector.addGoal(1, new ScaredOfGargoyleGoal(mob)); } } @@ -43,6 +37,7 @@ public static void onMobSpawn(EntityEvent.EntityConstructing event) { public ScaredOfGargoyleGoal(PathfinderMob mob) { this.mob = mob; this.navigation = mob.getNavigation(); + this.setFlags(EnumSet.of(Flag.MOVE, Flag.LOOK)); } @Override diff --git a/src/main/resources/oreganized.mixins.json b/src/main/resources/oreganized.mixins.json index 447de135..bd81a67b 100644 --- a/src/main/resources/oreganized.mixins.json +++ b/src/main/resources/oreganized.mixins.json @@ -5,8 +5,8 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "AbstractArrowMixin", "BlockMixin", "CrossbowMixin", "DispenserBlockMixin", "EnchantmentHelperMixin", "EntityMixin", - "FoodDataMixin", "IceBlockMixin", "LivingEntityMixin", "MobEffectInstanceMixin", "PillagerMixin", "PlayerMixin", - "compat.AirCurrentMixin", "compat.BellowBlockTileMixin", "compat.LogStrippingFakeRecipesMixin" + "FoodDataMixin", "IceBlockMixin", "LivingEntityMixin", "MobEffectInstanceMixin", "MobMixin", "PillagerMixin", + "PlayerMixin", "compat.AirCurrentMixin", "compat.BellowBlockTileMixin", "compat.LogStrippingFakeRecipesMixin" ], "client": [ "client.GuiMixin", "client.HumanoidArmorLayerAccessor", "client.HumanoidArmorLayerMixin",