From 2815e69407fe64523b6f4165a9fd965333634afd Mon Sep 17 00:00:00 2001 From: Brennan Ward Date: Thu, 23 Mar 2023 11:29:06 -0700 Subject: [PATCH] [1.19.4] Revamp and fix spawn events (#9133) --- build.gradle | 15 +- .../tasks/BytecodePredicateFinder.groovy | 32 +++ .../world/entity/EntityType.java.patch | 8 - .../net/minecraft/world/entity/Mob.java.patch | 114 ++++++++- .../world/entity/npc/CatSpawner.java.patch | 3 +- .../world/level/BaseSpawner.java.patch | 19 +- .../world/level/NaturalSpawner.java.patch | 19 +- .../level/levelgen/PatrolSpawner.java.patch | 10 - .../level/levelgen/PhantomSpawner.java.patch | 13 - .../net/minecraftforge/common/ForgeHooks.java | 5 - .../common/ForgeInternalHandler.java | 10 + .../event/ForgeEventFactory.java | 51 +++- .../event/entity/living/LivingSpawnEvent.java | 186 -------------- .../event/entity/living/MobSpawnEvent.java | 242 ++++++++++++++++++ src/main/resources/META-INF/coremods.json | 3 +- .../coremods/finalize_spawn_targets.json | 24 ++ .../resources/coremods/method_redirector.js | 77 ++++++ .../debug/entity/CheckSpawnEventTest.java | 47 ---- 18 files changed, 558 insertions(+), 320 deletions(-) create mode 100644 buildSrc/src/main/groovy/net/minecraftforge/forge/tasks/BytecodePredicateFinder.groovy delete mode 100644 patches/minecraft/net/minecraft/world/level/levelgen/PatrolSpawner.java.patch delete mode 100644 patches/minecraft/net/minecraft/world/level/levelgen/PhantomSpawner.java.patch delete mode 100644 src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java create mode 100644 src/main/java/net/minecraftforge/event/entity/living/MobSpawnEvent.java create mode 100644 src/main/resources/coremods/finalize_spawn_targets.json create mode 100644 src/main/resources/coremods/method_redirector.js delete mode 100644 src/test/java/net/minecraftforge/debug/entity/CheckSpawnEventTest.java diff --git a/build.gradle b/build.gradle index cc082e51d2..3f17f6134d 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ import net.minecraftforge.gradle.common.tasks.SignJar import net.minecraftforge.gradle.userdev.tasks.RenameJar import org.apache.tools.ant.filters.ReplaceTokens import org.gradle.plugins.ide.eclipse.model.SourceFolder +import org.objectweb.asm.Opcodes plugins { id 'org.cadixdev.licenser' version '0.6.1' @@ -1173,13 +1174,25 @@ project(':forge') { */ } } + + task findFinalizeSpawnTargets(type: BytecodePredicateFinder, dependsOn: ['createJoinedSRG']) { + jar = createJoinedSRG.output + output = rootProject.file('src/main/resources/coremods/finalize_spawn_targets.json') + predicate = { + parent, node, insn -> + 'net/minecraft/world/level/BaseSpawner' != parent.name // Ignore this class as we special case it. + && insn.getOpcode() == Opcodes.INVOKEVIRTUAL + && insn.name.equals('m_6518_') + && insn.desc.equals('(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/world/entity/SpawnGroupData;') + } + } tasks.register('checkAll') { dependsOn 'checkLicenses' group = 'checks' } tasks.register('checkAllAndFix') { - dependsOn 'findFieldInstanceChecks', 'checkLicenses' + dependsOn 'findFinalizeSpawnTargets', 'findFieldInstanceChecks', 'checkLicenses' group = 'checks' } diff --git a/buildSrc/src/main/groovy/net/minecraftforge/forge/tasks/BytecodePredicateFinder.groovy b/buildSrc/src/main/groovy/net/minecraftforge/forge/tasks/BytecodePredicateFinder.groovy new file mode 100644 index 0000000000..290b660ce2 --- /dev/null +++ b/buildSrc/src/main/groovy/net/minecraftforge/forge/tasks/BytecodePredicateFinder.groovy @@ -0,0 +1,32 @@ +package net.minecraftforge.forge.tasks + +import groovy.transform.CompileStatic +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Internal +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodNode + +@CompileStatic +abstract class BytecodePredicateFinder extends BytecodeFinder { + + @Internal + abstract Property> getPredicate() + + private final Set matches = new HashSet() + + @Override + protected process(ClassNode parent, MethodNode node) { + for (final current : node.instructions) { + if (predicate.get().call(parent, node, current)) { + matches.add(parent.name) + return + } + } + } + + @Internal + @Override + protected Object getData() { + return matches.toSorted(Comparator.naturalOrder()) ?: {throw new RuntimeException('Failed to find any targets, please ensure that method names and descriptors are correct.')}() + } +} \ No newline at end of file diff --git a/patches/minecraft/net/minecraft/world/entity/EntityType.java.patch b/patches/minecraft/net/minecraft/world/entity/EntityType.java.patch index 050b11ec5a..f4dbf3ed7c 100644 --- a/patches/minecraft/net/minecraft/world/entity/EntityType.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/EntityType.java.patch @@ -48,14 +48,6 @@ } @Nullable -@@ -380,6 +_,7 @@ - public T m_262455_(ServerLevel p_262704_, @Nullable CompoundTag p_262603_, @Nullable Consumer p_262621_, BlockPos p_262672_, MobSpawnType p_262644_, boolean p_262690_, boolean p_262590_) { - T t = this.m_262451_(p_262704_, p_262603_, p_262621_, p_262672_, p_262644_, p_262690_, p_262590_); - if (t != null) { -+ if (t instanceof net.minecraft.world.entity.Mob && net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn((net.minecraft.world.entity.Mob) t, p_262704_, p_262672_.m_123341_(), p_262672_.m_123342_(), p_262672_.m_123343_(), null, p_262644_)) return null; - p_262704_.m_47205_(t); - } - @@ -600,14 +_,23 @@ } diff --git a/patches/minecraft/net/minecraft/world/entity/Mob.java.patch b/patches/minecraft/net/minecraft/world/entity/Mob.java.patch index efe7e8a70b..371f7510dd 100644 --- a/patches/minecraft/net/minecraft/world/entity/Mob.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/Mob.java.patch @@ -1,5 +1,15 @@ --- a/net/minecraft/world/entity/Mob.java +++ b/net/minecraft/world/entity/Mob.java +@@ -116,6 +_,9 @@ + private CompoundTag f_21359_; + private BlockPos f_21360_ = BlockPos.f_121853_; + private float f_21341_ = -1.0F; ++ @Nullable ++ private MobSpawnType spawnType; ++ private boolean spawnCancelled = false; + + protected Mob(EntityType p_21368_, Level p_21369_) { + super(p_21368_, p_21369_); @@ -224,7 +_,11 @@ } @@ -13,6 +23,31 @@ } public boolean m_6549_(EntityType p_21399_) { +@@ -422,6 +_,9 @@ + p_21484_.m_128379_("NoAI", this.m_21525_()); + } + ++ if (this.spawnType != null) { ++ p_21484_.m_128359_("forge:spawn_type", this.spawnType.name()); ++ } + } + + public void m_7378_(CompoundTag p_21450_) { +@@ -474,6 +_,14 @@ + } + + this.m_21557_(p_21450_.m_128471_("NoAI")); ++ ++ if (p_21450_.m_128441_("forge:spawn_type")) { ++ try { ++ this.spawnType = MobSpawnType.valueOf(p_21450_.m_128461_("forge:spawn_type")); ++ } catch (Exception ex) { ++ p_21450_.m_128473_("forge:spawn_type"); ++ } ++ } + } + + protected void m_7625_(DamageSource p_21389_, boolean p_21390_) { @@ -513,9 +_,9 @@ public void m_8107_() { super.m_8107_(); @@ -28,7 +63,7 @@ this.m_146870_(); } else if (!this.m_21532_() && !this.m_8023_()) { Entity entity = this.f_19853_.m_45930_(this, -1.0D); -+ net.minecraftforge.eventbus.api.Event.Result result = net.minecraftforge.event.ForgeEventFactory.canEntityDespawn(this); ++ net.minecraftforge.eventbus.api.Event.Result result = net.minecraftforge.event.ForgeEventFactory.canEntityDespawn(this, (ServerLevel) this.f_19853_); + if (result == net.minecraftforge.eventbus.api.Event.Result.DENY) { + f_20891_ = 0; + entity = null; @@ -39,7 +74,31 @@ if (entity != null) { double d0 = entity.m_20280_(this); int i = this.m_6095_().m_20674_().m_21611_(); -@@ -1410,13 +_,23 @@ +@@ -1020,6 +_,15 @@ + + } + ++ /** ++ * Forge: Deprecated, call via ForgeEventFactory.onFinalizeSpawn.
++ * Overrides are fine. Super calls within overrides are fine and should not be wrapped (as that will cause stack overflows).
++ * Vanilla calls are replaced with a transformer, and are not visible in source. ++ *

++ * Be certain to either call super.finalizeSpawn or set the {@link #spawnType} field from within your override. ++ */ ++ @Deprecated ++ @org.jetbrains.annotations.ApiStatus.OverrideOnly + @Nullable + public SpawnGroupData m_6518_(ServerLevelAccessor p_21434_, DifficultyInstance p_21435_, MobSpawnType p_21436_, @Nullable SpawnGroupData p_21437_, @Nullable CompoundTag p_21438_) { + RandomSource randomsource = p_21434_.m_213780_(); +@@ -1030,6 +_,7 @@ + this.m_21559_(false); + } + ++ this.spawnType = p_21436_; + return p_21437_; + } + +@@ -1410,15 +_,25 @@ return false; } @@ -56,11 +115,54 @@ this.m_20256_(this.m_20184_().m_82520_(0.0D, 0.3D, 0.0D)); } -+ } -+ + } + + @Override + public void jumpInFluid(net.minecraftforge.fluids.FluidType type) { + this.jumpInLiquidInternal(() -> super.jumpInFluid(type)); - } - ++ } ++ public void m_147272_() { + this.m_262441_((p_262562_) -> { + return true; +@@ -1442,5 +_,40 @@ + public ItemStack m_142340_() { + SpawnEggItem spawneggitem = SpawnEggItem.m_43213_(this.m_6095_()); + return spawneggitem == null ? null : new ItemStack(spawneggitem); ++ } ++ ++ /** ++ * Returns the type of spawn that created this mob, if applicable. ++ * If it could not be determined, this will return null. ++ *

++ * This is set via {@link Mob#finalizeSpawn}, so you should not call this from within that method, instead using the parameter. ++ */ ++ @Nullable ++ public final MobSpawnType getSpawnType() { ++ return this.spawnType; ++ } ++ ++ /** ++ * This method exists so that spawns can be cancelled from the {@link net.minecraftforge.event.entity.living.MobSpawnEvent.FinalizeSpawn FinalizeSpawnEvent} ++ * without needing to hook up an additional handler for the {@link net.minecraftforge.event.entity.EntityJoinLevelEvent EntityJoinLevelEvent}. ++ * @return if this mob will be blocked from spawning during {@link Level#addFreshEntity(Entity)} ++ * @apiNote Not public-facing API. ++ */ ++ @org.jetbrains.annotations.ApiStatus.Internal ++ public final boolean isSpawnCancelled() { ++ return this.spawnCancelled; ++ } ++ ++ /** ++ * Marks this mob as being disallowed to spawn during {@link Level#addFreshEntity(Entity)}.

++ * @throws UnsupportedOperationException if this entity has already been {@link Entity#isAddedToWorld() added to the world}. ++ * @apiNote Not public-facing API. ++ */ ++ @org.jetbrains.annotations.ApiStatus.Internal ++ public final void setSpawnCancelled(boolean cancel) { ++ if (this.isAddedToWorld()) { ++ throw new UnsupportedOperationException("Late invocations of Mob#setSpawnCancelled are not permitted."); ++ } ++ this.spawnCancelled = cancel; + } + } diff --git a/patches/minecraft/net/minecraft/world/entity/npc/CatSpawner.java.patch b/patches/minecraft/net/minecraft/world/entity/npc/CatSpawner.java.patch index f53c7a527a..184c9aa2d2 100644 --- a/patches/minecraft/net/minecraft/world/entity/npc/CatSpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/npc/CatSpawner.java.patch @@ -1,11 +1,10 @@ --- a/net/minecraft/world/entity/npc/CatSpawner.java +++ b/net/minecraft/world/entity/npc/CatSpawner.java -@@ -86,8 +_,9 @@ +@@ -86,8 +_,8 @@ if (cat == null) { return 0; } else { + cat.m_20035_(p_35334_, 0.0F, 0.0F); // Fix MC-147659: Some witch huts spawn the incorrect cat -+ if(net.minecraftforge.common.ForgeHooks.canEntitySpawn(cat, p_35335_, p_35334_.m_123341_(), p_35334_.m_123342_(), p_35334_.m_123343_(), null, MobSpawnType.NATURAL) == -1) return 0; cat.m_6518_(p_35335_, p_35335_.m_6436_(p_35334_), MobSpawnType.NATURAL, (SpawnGroupData)null, (CompoundTag)null); - cat.m_20035_(p_35334_, 0.0F, 0.0F); p_35335_.m_47205_(cat); diff --git a/patches/minecraft/net/minecraft/world/level/BaseSpawner.java.patch b/patches/minecraft/net/minecraft/world/level/BaseSpawner.java.patch index cc6570f14d..fafc30bfe3 100644 --- a/patches/minecraft/net/minecraft/world/level/BaseSpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/level/BaseSpawner.java.patch @@ -1,21 +1,18 @@ --- a/net/minecraft/world/level/BaseSpawner.java +++ b/net/minecraft/world/level/BaseSpawner.java -@@ -131,11 +_,15 @@ - entity.m_7678_(entity.m_20185_(), entity.m_20186_(), entity.m_20189_(), randomsource.m_188501_() * 360.0F, 0.0F); - if (entity instanceof Mob) { - Mob mob = (Mob)entity; -+ net.minecraftforge.eventbus.api.Event.Result res = net.minecraftforge.event.ForgeEventFactory.canEntitySpawn(mob, p_151312_, (float)entity.m_20185_(), (float)entity.m_20186_(), (float)entity.m_20189_(), this, MobSpawnType.SPAWNER); -+ if (res == net.minecraftforge.eventbus.api.Event.Result.DENY) continue; -+ if (res == net.minecraftforge.eventbus.api.Event.Result.DEFAULT) - if (spawndata.m_186574_().isEmpty() && !mob.m_5545_(p_151312_, MobSpawnType.SPAWNER) || !mob.m_6914_(p_151312_)) { +@@ -135,8 +_,10 @@ continue; } - if (spawndata.m_186567_().m_128440_() == 1 && spawndata.m_186567_().m_128425_("id", 8)) { -+ if (!net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn(mob, (LevelAccessor)p_151312_, (float)entity.m_20185_(), (float)entity.m_20186_(), (float)entity.m_20189_(), this, MobSpawnType.SPAWNER)) - ((Mob)entity).m_6518_(p_151312_, p_151312_.m_6436_(entity.m_20183_()), MobSpawnType.SPAWNER, (SpawnGroupData)null, (CompoundTag)null); +- if (spawndata.m_186567_().m_128440_() == 1 && spawndata.m_186567_().m_128425_("id", 8)) { +- ((Mob)entity).m_6518_(p_151312_, p_151312_.m_6436_(entity.m_20183_()), MobSpawnType.SPAWNER, (SpawnGroupData)null, (CompoundTag)null); ++ // Forge: Patch in the spawn event for spawners so it may be fired unconditionally, instead of only when vanilla normally would trigger it. ++ var event = net.minecraftforge.event.ForgeEventFactory.onFinalizeSpawnSpawner(mob, p_151312_, p_151312_.m_6436_(entity.m_20183_()), null, compoundtag, this); ++ if (event != null && spawndata.m_186567_().m_128440_() == 1 && spawndata.m_186567_().m_128425_("id", 8)) { ++ ((Mob)entity).m_6518_(p_151312_, event.getDifficulty(), event.getSpawnType(), event.getSpawnData(), event.getSpawnTag()); } } + @@ -283,4 +_,12 @@ public double m_45474_() { return this.f_45446_; diff --git a/patches/minecraft/net/minecraft/world/level/NaturalSpawner.java.patch b/patches/minecraft/net/minecraft/world/level/NaturalSpawner.java.patch index 1421193737..9311d401fa 100644 --- a/patches/minecraft/net/minecraft/world/level/NaturalSpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/level/NaturalSpawner.java.patch @@ -17,16 +17,7 @@ if (mobcategory != MobCategory.MISC) { BlockPos blockpos = entity.m_20183_(); p_186527_.m_47103_(ChunkPos.m_151388_(blockpos), (p_275163_) -> { -@@ -171,13 +_,15 @@ - } - - mob.m_7678_(d0, (double)i, d1, p_47040_.f_46441_.m_188501_() * 360.0F, 0.0F); -- if (m_46991_(p_47040_, mob, d2)) { -+ int canSpawn = net.minecraftforge.common.ForgeHooks.canEntitySpawn(mob, p_47040_, d0, i, d1, null, MobSpawnType.NATURAL); -+ if (canSpawn != -1 && (canSpawn == 1 || m_46991_(p_47040_, mob, d2))) { -+ if (!net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn(mob, (LevelAccessor) p_47040_, (float)d0, (float)i, (float)d1, null, MobSpawnType.NATURAL)) - spawngroupdata = mob.m_6518_(p_47040_, p_47040_.m_6436_(mob.m_20183_()), MobSpawnType.NATURAL, spawngroupdata, (CompoundTag)null); - ++j; +@@ -177,7 +_,7 @@ ++l1; p_47040_.m_47205_(mob); p_47044_.m_47100_(mob, p_47041_); @@ -75,11 +66,3 @@ } } -@@ -366,6 +_,7 @@ - entity.m_7678_(d0, (double)blockpos.m_123342_(), d1, p_220454_.m_188501_() * 360.0F, 0.0F); - if (entity instanceof Mob) { - Mob mob = (Mob)entity; -+ if (net.minecraftforge.common.ForgeHooks.canEntitySpawn(mob, p_220451_, d0, blockpos.m_123342_(), d1, null, MobSpawnType.CHUNK_GENERATION) == -1) continue; - if (mob.m_5545_(p_220451_, MobSpawnType.CHUNK_GENERATION) && mob.m_6914_(p_220451_)) { - spawngroupdata = mob.m_6518_(p_220451_, p_220451_.m_6436_(mob.m_20183_()), MobSpawnType.CHUNK_GENERATION, spawngroupdata, (CompoundTag)null); - p_220451_.m_47205_(mob); diff --git a/patches/minecraft/net/minecraft/world/level/levelgen/PatrolSpawner.java.patch b/patches/minecraft/net/minecraft/world/level/levelgen/PatrolSpawner.java.patch deleted file mode 100644 index 473646ebea..0000000000 --- a/patches/minecraft/net/minecraft/world/level/levelgen/PatrolSpawner.java.patch +++ /dev/null @@ -1,10 +0,0 @@ ---- a/net/minecraft/world/level/levelgen/PatrolSpawner.java -+++ b/net/minecraft/world/level/levelgen/PatrolSpawner.java -@@ -104,6 +_,7 @@ - } - - patrollingmonster.m_6034_((double)p_224534_.m_123341_(), (double)p_224534_.m_123342_(), (double)p_224534_.m_123343_()); -+ if(net.minecraftforge.common.ForgeHooks.canEntitySpawn(patrollingmonster, p_224533_, p_224534_.m_123341_(), p_224534_.m_123342_(), p_224534_.m_123343_(), null, MobSpawnType.PATROL) == -1) return false; - patrollingmonster.m_6518_(p_224533_, p_224533_.m_6436_(p_224534_), MobSpawnType.PATROL, (SpawnGroupData)null, (CompoundTag)null); - p_224533_.m_47205_(patrollingmonster); - return true; diff --git a/patches/minecraft/net/minecraft/world/level/levelgen/PhantomSpawner.java.patch b/patches/minecraft/net/minecraft/world/level/levelgen/PhantomSpawner.java.patch deleted file mode 100644 index 7a3aea6c86..0000000000 --- a/patches/minecraft/net/minecraft/world/level/levelgen/PhantomSpawner.java.patch +++ /dev/null @@ -1,13 +0,0 @@ ---- a/net/minecraft/world/level/levelgen/PhantomSpawner.java -+++ b/net/minecraft/world/level/levelgen/PhantomSpawner.java -@@ -61,6 +_,10 @@ - Phantom phantom = EntityType.f_20509_.m_20615_(p_64576_); - if (phantom != null) { - phantom.m_20035_(blockpos1, 0.0F, 0.0F); -+ if (net.minecraftforge.common.ForgeHooks.canEntitySpawn(phantom, p_64576_, blockpos1.m_123341_(), blockpos1.m_123342_(), blockpos1.m_123343_(), null, MobSpawnType.NATURAL) == -1) { -+ i--; -+ continue; -+ } - spawngroupdata = phantom.m_6518_(p_64576_, difficultyinstance, MobSpawnType.NATURAL, spawngroupdata, (CompoundTag)null); - p_64576_.m_47205_(phantom); - ++i; diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 0866060c4d..527dece70e 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -1093,11 +1093,6 @@ public class ForgeHooks return event.getVanillaNoteId(); } - public static int canEntitySpawn(Mob entity, LevelAccessor world, double x, double y, double z, BaseSpawner spawner, MobSpawnType spawnReason) { - Result res = ForgeEventFactory.canEntitySpawn(entity, world, x, y, z, null, spawnReason); - return res == Result.DEFAULT ? 0 : res == Result.DENY ? -1 : 1; - } - public static boolean hasNoElements(Ingredient ingredient) { ItemStack[] items = ingredient.getItems(); diff --git a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java index d1c04820eb..d03b5e7ad9 100644 --- a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java @@ -6,6 +6,7 @@ package net.minecraftforge.common; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -135,5 +136,14 @@ public class ForgeInternalHandler event.addListener(TierSortingRegistry.getReloadListener()); event.addListener(CreativeModeTabRegistry.getReloadListener()); } + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void builtinMobSpawnBlocker(EntityJoinLevelEvent event) + { + if(event.getEntity() instanceof Mob mob && mob.isSpawnCancelled()) + { + event.setCanceled(true); + } + } } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index d6eeee6e9b..d25ceb4b04 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -25,6 +25,7 @@ import net.minecraft.server.players.PlayerList; import net.minecraft.util.RandomSource; import net.minecraft.util.random.WeightedRandomList; import net.minecraft.world.Container; +import net.minecraft.world.DifficultyInstance; import net.minecraft.world.entity.MobCategory; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.entity.projectile.ThrownEnderpearl; @@ -38,6 +39,7 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Pose; +import net.minecraft.world.entity.SpawnGroupData; import net.minecraft.world.entity.EntityDimensions; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; @@ -61,6 +63,7 @@ import net.minecraft.world.InteractionResult; import net.minecraft.core.Direction; import net.minecraft.world.InteractionHand; import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundSource; @@ -73,6 +76,7 @@ import net.minecraft.world.level.Explosion; import net.minecraft.world.level.GameRules; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; +import net.minecraft.world.level.ServerLevelAccessor; import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.storage.ServerLevelData; @@ -99,8 +103,8 @@ import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent; import net.minecraftforge.event.entity.living.LivingExperienceDropEvent; import net.minecraftforge.event.entity.living.LivingHealEvent; import net.minecraftforge.event.entity.living.LivingPackSizeEvent; -import net.minecraftforge.event.entity.living.LivingSpawnEvent; -import net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn; +import net.minecraftforge.event.entity.living.MobSpawnEvent; +import net.minecraftforge.event.entity.living.MobSpawnEvent.AllowDespawn; import net.minecraftforge.event.entity.living.ZombieEvent.SummonAidEvent; import net.minecraftforge.event.entity.player.AdvancementEvent.AdvancementEarnEvent; import net.minecraftforge.event.entity.player.AdvancementEvent.AdvancementProgressEvent; @@ -185,23 +189,46 @@ public class ForgeEventFactory MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, stack, hand)); } - public static Result canEntitySpawn(Mob entity, LevelAccessor level, double x, double y, double z, BaseSpawner spawner, MobSpawnType spawnReason) + /** + * Vanilla calls to {@link Mob#finalizeSpawn} are replaced with calls to this method via coremod.
+ * Mods should call this method in place of calling {@link Mob#finalizeSpawn}. Super calls (from within overrides) should not be wrapped. + *

+ * Returns the SpawnGroupData from this event, or null if it was canceled. + * @see MobSpawnEvent.FinalizeSpawn + * @see Mob#finalizeSpawn(ServerLevelAccessor, DifficultyInstance, MobSpawnType, SpawnGroupData, CompoundTag) + * @implNote Changes to the signature of this method must be reflected in the method redirector coremod. + */ + @Nullable + public static SpawnGroupData onFinalizeSpawn(Mob mob, ServerLevelAccessor level, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag) { - if (entity == null) - return Result.DEFAULT; - LivingSpawnEvent.CheckSpawn event = new LivingSpawnEvent.CheckSpawn(entity, level, x, y, z, spawner, spawnReason); - MinecraftForge.EVENT_BUS.post(event); - return event.getResult(); + var event = new MobSpawnEvent.FinalizeSpawn(mob, level, mob.getX(), mob.getY(), mob.getZ(), difficulty, spawnType, spawnData, spawnTag, null); + boolean cancel = MinecraftForge.EVENT_BUS.post(event); + + if (!cancel) + { + mob.finalizeSpawn(level, event.getDifficulty(), event.getSpawnType(), event.getSpawnData(), event.getSpawnTag()); + } + + return cancel ? null : event.getSpawnData(); } - public static boolean doSpecialSpawn(Mob entity, LevelAccessor level, float x, float y, float z, BaseSpawner spawner, MobSpawnType spawnReason) + /** + * Returns the FinalizeSpawn event instance, or null if it was canceled.
+ * This is separate since mob spawners perform special finalizeSpawn handling when NBT data is present, but we still want to fire the event.
+ * This overload is also the only way to pass through a {@link BaseSpawner} instance. + * @see MobSpawnEvent.FinalizeSpawn + */ + @Nullable + public static MobSpawnEvent.FinalizeSpawn onFinalizeSpawnSpawner(Mob mob, ServerLevelAccessor level, DifficultyInstance difficulty, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag, BaseSpawner spawner) { - return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, level, x, y, z, spawner, spawnReason)); + var event = new MobSpawnEvent.FinalizeSpawn(mob, level, mob.getX(), mob.getY(), mob.getZ(), difficulty, MobSpawnType.SPAWNER, spawnData, spawnTag, spawner); + boolean cancel = MinecraftForge.EVENT_BUS.post(event); + return cancel ? null : event; } - public static Result canEntityDespawn(Mob entity) + public static Result canEntityDespawn(Mob entity, ServerLevelAccessor level) { - AllowDespawn event = new AllowDespawn(entity); + AllowDespawn event = new AllowDespawn(entity, level); MinecraftForge.EVENT_BUS.post(event); return event.getResult(); } diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java deleted file mode 100644 index 18cebf3c7f..0000000000 --- a/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.minecraftforge.event.entity.living; - -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.Mob; -import net.minecraft.world.entity.MobSpawnType; -import net.minecraft.world.level.BaseSpawner; -import net.minecraft.world.level.LevelAccessor; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.eventbus.api.Cancelable; -import net.minecraftforge.fml.LogicalSide; -import org.jetbrains.annotations.Nullable; - -/** - * This event is fired whenever a {@link Mob} should do something spawn-related. - *

- * This event is not {@linkplain Cancelable cancellable} and does not {@linkplain HasResult have a result}. - *

- * This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus} - * only on the {@linkplain LogicalSide#SERVER logical server}. - */ -public class LivingSpawnEvent extends LivingEvent -{ - private final LevelAccessor level; - private final Mob mob; - private final double x; - private final double y; - private final double z; - - public LivingSpawnEvent(Mob mob, LevelAccessor level, double x, double y, double z) - { - super(mob); - this.mob = mob; - this.level = level; - this.x = x; - this.y = y; - this.z = z; - } - - /** - * {@return the mob that is causing a spawn action} - */ - @Override - public Mob getEntity() - { - return this.mob; - } - - /** - * {@return the level relating to the mob spawn action} - */ - public LevelAccessor getLevel() - { - return this.level; - } - - /** - * {@return the x-coordinate relating to the mob spawn action} - */ - public double getX() - { - return this.x; - } - - /** - * {@return the y-coordinate relating to the mob spawn action} - */ - public double getY() - { - return this.y; - } - - /** - * {@return the z-coordinate relating to the mob spawn action} - */ - public double getZ() - { - return this.z; - } - - /** - * This event is fired before a {@link Mob} spawns. - *

- * This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}. - * {@linkplain Result#DEFAULT DEFAULT} indicates that default spawn rules should be used. - * {@linkplain Result#ALLOW ALLOW} indicates that the mob should spawn regardless of default spawn rules. - * {@linkplain Result#DENY DENY} indicates that the mob should not spawn. - *

- * This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}, - * only on the {@linkplain LogicalSide#SERVER logical server}. - */ - @HasResult - public static class CheckSpawn extends LivingSpawnEvent - { - @Nullable - private final BaseSpawner spawner; - private final MobSpawnType spawnReason; - - public CheckSpawn(Mob mob, LevelAccessor level, double x, double y, double z, @Nullable BaseSpawner spawner, MobSpawnType spawnReason) - { - super(mob, level, x, y, z); - this.spawner = spawner; - this.spawnReason = spawnReason; - } - - public boolean isSpawner() - { - return spawner != null; - } - - @Nullable - public BaseSpawner getSpawner() - { - return spawner; - } - - public MobSpawnType getSpawnReason() - { - return spawnReason; - } - } - - /** - * This event is fired whenever a {@link Mob} is set to be spawned, to allow for mod-specific initializers. - *

- * This event is {@linkplain Cancelable cancellable} and does not {@linkplain HasResult have a result}. - * If the event is canceled, the mob will not spawn. - *

- * This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}, - * only on the {@linkplain LogicalSide#SERVER logical server}. - */ - @Cancelable - public static class SpecialSpawn extends LivingSpawnEvent - { - @Nullable - private final BaseSpawner spawner; - private final MobSpawnType spawnReason; - - public SpecialSpawn(Mob entity, LevelAccessor level, double x, double y, double z, @Nullable BaseSpawner spawner, MobSpawnType spawnReason) - { - super(entity, level, x, y, z); - this.spawner = spawner; - this.spawnReason = spawnReason; - } - - @Nullable - public BaseSpawner getSpawner() - { - return spawner; - } - - public MobSpawnType getSpawnReason() - { - return spawnReason; - } - } - - /** - * This event is fired for a {@link Mob} that can despawn each mob tick. - * This event only fires if a mob can be allowed to despawn and will not - * otherwise fire if a despawn is certain. - *

- * This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}. - * {@link Result#DEFAULT} indicates that default despawn mechanics should be used. - * {@link Result#ALLOW} indicates that the mob should forcefully despawn. - * {@link Result#DENY} indicates that the mob should forcefully stay spawned. - *

- * This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}, - * only on the {@linkplain LogicalSide#SERVER logical server}. - * - * @see LivingEntity#checkDespawn() - * @author cpw - */ - @HasResult - public static class AllowDespawn extends LivingSpawnEvent - { - public AllowDespawn(Mob mob) - { - super(mob, mob.level, mob.getX(), mob.getY(), mob.getZ()); - } - } -} diff --git a/src/main/java/net/minecraftforge/event/entity/living/MobSpawnEvent.java b/src/main/java/net/minecraftforge/event/entity/living/MobSpawnEvent.java new file mode 100644 index 0000000000..1b0ca03554 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/MobSpawnEvent.java @@ -0,0 +1,242 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.event.entity.living; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.DifficultyInstance; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.entity.SpawnGroupData; +import net.minecraft.world.level.BaseSpawner; +import net.minecraft.world.level.ServerLevelAccessor; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.ForgeEventFactory; +import net.minecraftforge.event.entity.EntityEvent; +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.fml.LogicalSide; + +/** + * The subclasses of this event are fired whenever a mob performs a spawning-related action. + */ +public abstract class MobSpawnEvent extends EntityEvent +{ + private final ServerLevelAccessor level; + private final double x; + private final double y; + private final double z; + + /** + * @apiNote Do not construct directly. Access via {@link ForgeEventFactory#onFinalizeSpawn}. + */ + @ApiStatus.Internal + public MobSpawnEvent(Mob mob, ServerLevelAccessor level, double x, double y, double z) + { + super(mob); + this.level = level; + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public Mob getEntity() + { + return (Mob) super.getEntity(); + } + + /** + * @return The level relating to the mob spawn action + */ + public ServerLevelAccessor getLevel() + { + return this.level; + } + + /** + * @return The x-coordinate relating to the mob spawn action + */ + public double getX() + { + return this.x; + } + + /** + * @return The y-coordinate relating to the mob spawn action + */ + public double getY() + { + return this.y; + } + + /** + * @return The z-coordinate relating to the mob spawn action + */ + public double getZ() + { + return this.z; + } + + /** + * This event is fired before {@link Mob#finalizeSpawn(ServerLevelAccessor, DifficultyInstance, MobSpawnType, SpawnGroupData, CompoundTag)} is called.
+ * This allows mods to control mob initialization.
+ * In vanilla code, this event is injected by a transformer and not via patch, so calls cannot be traced via call hierarchy. + *

+ * Canceling this event will result in {@link Mob#finalizeSpawn} not being called, and the returned value always being null, instead of propagating the SpawnGroupData.
+ * The entity will still be spawned. If you want to prevent the spawn, use {@link FinalizeSpawn#cancelSpawn()} + *

+ * This event is fired on {@link MinecraftForge#EVENT_BUS}, and is only fired on the logical server. + */ + @Cancelable + public static class FinalizeSpawn extends MobSpawnEvent + { + private final MobSpawnType spawnType; + @Nullable private final BaseSpawner spawner; + + private DifficultyInstance difficulty; + @Nullable private SpawnGroupData spawnData; + @Nullable private CompoundTag spawnTag; + + public FinalizeSpawn(Mob entity, ServerLevelAccessor level, double x, double y, double z, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag, @Nullable BaseSpawner spawner) + { + super(entity, level, x, y, z); + this.difficulty = difficulty; + this.spawnType = spawnType; + this.spawnData = spawnData; + this.spawnTag = spawnTag; + this.spawner = spawner; + } + + /** + * Retrieves the {@link DifficultyInstance} for the chunk where the mob is about to be spawned. + * @return The local difficulty instance + */ + public DifficultyInstance getDifficulty() + { + return this.difficulty; + } + + /** + * Sets the difficulty instance for this event, which will be propagated to {@link Mob#finalizeSpawn} unless cancelled. + * The difficulty instance controls how likely certain random effects are to occur, or if certain mob abilities are enabled. + * @param inst The new difficulty instance. + */ + public void setDifficulty(DifficultyInstance inst) + { + this.difficulty = inst; + } + + /** + * Retrieves the type of mob spawn that happened (the event that caused the spawn). The enum names are self-explanatory. + * @return The mob spawn type. + * @see MobSpawnType + */ + public MobSpawnType getSpawnType() + { + return this.spawnType; + } + + /** + * Retrieves the {@link SpawnGroupData} for this entity. When spawning mobs in a loop, this group data is used for the entire group and impacts future spawns. + * This is how entities like horses ensure that the whole group spawns as a single variant. How this is used varies on a per-entity basis. + * @return The spawn group data. + */ + @Nullable + public SpawnGroupData getSpawnData() + { + return this.spawnData; + } + + /** + * Sets the spawn data for this entity. If this event is cancelled, this value is not used, since {@link Mob#finalizeSpawn} will not be called. + * @param data The new spawn data + * @see FinalizeSpawn#getSpawnData + */ + public void setSpawnData(@Nullable SpawnGroupData data) + { + this.spawnData = data; + } + + /** + * This is the NBT data the entity was loaded from, if applicable. It is unknown if the entity has already been loaded from this data, or if it will be loaded later. + * Callers should not modify this data. If you need to change the data, you can create a copy, modify it, and set it via {@link FinalizeSpawn#setSpawnTag} + * @return The spawn data this entity was or will be loaded from, if any. + */ + @Nullable + public CompoundTag getSpawnTag() + { + return this.spawnTag; + } + + /** + * Sets the spawn data for this event, which will be propagated to {@link Mob#finalizeSpawn} unless cancelled. + * The only vanilla mob known to use this tag for anything in finalize is tropical fish for setting the variant when spawned via bucket. + * @param tag The new spawn tag + */ + public void setSpawnTag(@Nullable CompoundTag tag) + { + this.spawnTag = tag; + } + + /** + * Retrieves the underlying {@link BaseSpawner} instance if this mob was created by a Mob Spawner of some form. + * This is always null unless {@link #getSpawnType()} is {@link MobSpawnType#SPAWNER}, and may still be null even then. + * @return The BaseSpawner responsible for triggering the spawn, or null if none is available. + */ + @Nullable + public BaseSpawner getSpawner() + { + return spawner; + } + + /** + * This method can be used to cancel the spawn of this mob.

+ * This method must be used if you want to block the spawn, as canceling the event only blocks the call to {@link Mob#finalizeSpawn}.

+ * Note that if the spawn is cancelled, but the event is not, then {@link Mob#finalizeSpawn} will still be called, but the entity will not be spawned. + * Usually that has no side effects, but callers should be aware. + * @param cancel If the spawn should be cancelled (or not). + */ + public void setSpawnCancelled(boolean cancel) { + this.getEntity().setSpawnCancelled(cancel); + } + + /** + * Returns the current spawn cancellation status, which can be changed via {@link FinalizeSpawn#setSpawnCancelled(boolean)}. + * @return If this mob's spawn is cancelled or not. + */ + public boolean isSpawnCancelled() { + return this.getEntity().isSpawnCancelled(); + } + } + + /** + * This event is fired from {@link Mob#checkDespawn()}.
+ * It fires once per tick per mob that is attempting to despawn.
+ * It is not fired if the mob is persistent (meaning it may not despawn).
+ *

+ * This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}. + * {@link Result#DEFAULT} indicates that default despawn mechanics should be used. + * {@link Result#ALLOW} indicates that the mob should forcefully despawn. + * {@link Result#DENY} indicates that the mob should forcefully stay spawned. + *

+ * This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}, + * only on the {@linkplain LogicalSide#SERVER logical server}. + * + * @see LivingEntity#checkDespawn() + * @author cpw + */ + @HasResult + public static class AllowDespawn extends MobSpawnEvent + { + public AllowDespawn(Mob mob, ServerLevelAccessor level) + { + super(mob, level, mob.getX(), mob.getY(), mob.getZ()); + } + } +} diff --git a/src/main/resources/META-INF/coremods.json b/src/main/resources/META-INF/coremods.json index 445d251cbd..0d4634501d 100644 --- a/src/main/resources/META-INF/coremods.json +++ b/src/main/resources/META-INF/coremods.json @@ -2,5 +2,6 @@ "field_to_method": "coremods/field_to_method.js", "field_to_instanceof": "coremods/field_to_instanceof.js", "add_bouncer_method": "coremods/add_bouncer_method.js", - "intrinsic_tag_appender_binary_compat": "coremods/intrinsic_tag_appender_binary_compat.js" + "intrinsic_tag_appender_binary_compat": "coremods/intrinsic_tag_appender_binary_compat.js", + "method_redirector": "coremods/method_redirector.js" } diff --git a/src/main/resources/coremods/finalize_spawn_targets.json b/src/main/resources/coremods/finalize_spawn_targets.json new file mode 100644 index 0000000000..494cd63bc2 --- /dev/null +++ b/src/main/resources/coremods/finalize_spawn_targets.json @@ -0,0 +1,24 @@ +[ + "net/minecraft/server/commands/RaidCommand", + "net/minecraft/server/commands/SummonCommand", + "net/minecraft/world/entity/EntityType", + "net/minecraft/world/entity/ai/village/VillageSiege", + "net/minecraft/world/entity/animal/frog/Tadpole", + "net/minecraft/world/entity/animal/horse/SkeletonTrapGoal", + "net/minecraft/world/entity/monster/Evoker$EvokerSummonSpellGoal", + "net/minecraft/world/entity/monster/Spider", + "net/minecraft/world/entity/monster/Strider", + "net/minecraft/world/entity/monster/Zombie", + "net/minecraft/world/entity/monster/ZombieVillager", + "net/minecraft/world/entity/npc/CatSpawner", + "net/minecraft/world/entity/npc/Villager", + "net/minecraft/world/entity/raid/Raid", + "net/minecraft/world/level/NaturalSpawner", + "net/minecraft/world/level/levelgen/PatrolSpawner", + "net/minecraft/world/level/levelgen/PhantomSpawner", + "net/minecraft/world/level/levelgen/structure/structures/OceanMonumentPieces$OceanMonumentPiece", + "net/minecraft/world/level/levelgen/structure/structures/OceanRuinPieces$OceanRuinPiece", + "net/minecraft/world/level/levelgen/structure/structures/SwampHutPiece", + "net/minecraft/world/level/levelgen/structure/structures/WoodlandMansionPieces$WoodlandMansionPiece", + "net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate" +] \ No newline at end of file diff --git a/src/main/resources/coremods/method_redirector.js b/src/main/resources/coremods/method_redirector.js new file mode 100644 index 0000000000..b70912ab85 --- /dev/null +++ b/src/main/resources/coremods/method_redirector.js @@ -0,0 +1,77 @@ +var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI'); +var Opcodes = Java.type('org.objectweb.asm.Opcodes'); +var Handle = Java.type('org.objectweb.asm.Handle'); +var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode'); + +function finalizeSpawnNode(node){ + return new MethodInsnNode( + Opcodes.INVOKESTATIC, + "net/minecraftforge/event/ForgeEventFactory", + "onFinalizeSpawn", + "(Lnet/minecraft/world/entity/Mob;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/world/entity/SpawnGroupData;", + false); +} + +function contains(list, target) { + for(var i = 0; i < list.length; i++) { + if(list[i] == target) return true; + } + return false; +} + +function search(className, node, replacements) { + for(var i = 0; i < replacements.length; i++){ + var r = replacements[i]; + if(contains(r.targets, className) && node.getOpcode() == r.opcode && node.name == r.name && node.desc == r.desc) { + return r; + } + } + return null; +} + +var replacements = [ + { + 'opcode': Opcodes.INVOKEVIRTUAL, + 'name': ASMAPI.mapMethod('m_6518_'), + 'desc': '(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/world/entity/SpawnGroupData;', + 'targets': 'coremods/finalize_spawn_targets.json', + 'factory': finalizeSpawnNode + } +]; + +function initializeCoreMod() { + var mergedTargets = []; + for(var i = 0; i < replacements.length; i++){ + var r = replacements[i]; + r.targets = ASMAPI.loadData(r.targets); + for(var k = 0; k < r.targets.length; k++){ + mergedTargets.push(r.targets[k]); + } + } + + return { + 'forge_method_redirector': { + 'target': { + 'type': 'CLASS', + 'names': function(listofclasses) { + return mergedTargets + } + }, + 'transformer': function(classNode) { + var methods = classNode.methods; + var count = 0; + for(var i = 0; i < methods.size(); i++){ + var instr = methods.get(i).instructions; + for(var ix = 0; ix < instr.size(); ix++){ + var node = instr.get(ix); + var temp = search(classNode.name, node, replacements); + if (temp != null) { + instr.set(node, temp.factory(node)); + } + } + } + return classNode; + } + } + } +} \ No newline at end of file diff --git a/src/test/java/net/minecraftforge/debug/entity/CheckSpawnEventTest.java b/src/test/java/net/minecraftforge/debug/entity/CheckSpawnEventTest.java deleted file mode 100644 index 9097803f16..0000000000 --- a/src/test/java/net/minecraftforge/debug/entity/CheckSpawnEventTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.minecraftforge.debug.entity; - -import net.minecraft.world.entity.vehicle.MinecartSpawner; -import net.minecraft.world.level.block.entity.SpawnerBlockEntity; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.entity.living.LivingSpawnEvent; -import net.minecraftforge.eventbus.api.Event; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.Mod; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -@Mod("check_spawn_event_test") -public class CheckSpawnEventTest -{ - public static final boolean ENABLE = true; - public static final Logger LOGGER = LogManager.getLogger(); - - public CheckSpawnEventTest() - { - if (ENABLE) - MinecraftForge.EVENT_BUS.register(this); - } - - @SubscribeEvent - public void checkSpawn(LivingSpawnEvent.CheckSpawn event) - { - if (event.isSpawner()) - { - if (event.getSpawner().getSpawnerBlockEntity() instanceof SpawnerBlockEntity) - { - LOGGER.info("Stopped {} from spawning from Spawner Block Entity : {}", event.getEntity(), event.getSpawner().getSpawnerBlockEntity()); - event.setResult(Event.Result.DENY); - } - if (event.getSpawner().getSpawnerEntity() instanceof MinecartSpawner) - { - LOGGER.info("Stopped {} from spawning from Minecart Spawner : {}", event.getEntity(), event.getSpawner().getSpawnerEntity()); - event.setResult(Event.Result.DENY); - } - } - } -} \ No newline at end of file