From e8f62a631027af62ad989793c2da1a8bc5ea6893 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Mon, 2 Dec 2024 13:16:08 +0800 Subject: [PATCH 1/6] handler and Deprecated --- .../common/registry/EntityEntryBuilder.java | 21 ++++++++++++------- .../fml/common/registry/EntityRegistry.java | 6 ++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java index 0b191a269..4fe85494d 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java @@ -34,6 +34,7 @@ import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import net.minecraftforge.fml.relauncher.ReflectionHelper.UnknownConstructorException; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -290,22 +291,26 @@ private void registerStatistics() static abstract class ConstructorFactory implements Function { - private final Constructor constructor; + private final MethodHandle constructor; ConstructorFactory(final Class entity) { - this.constructor = ObfuscationReflectionHelper.findConstructor(entity, World.class); + constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); + try { + this.constructor = MethodHandles.lookup().unreflectConstructor(constructorIn); + } catch (IllegalAccessException e) { + throw new UnknownConstructorException("Could not unreflect constructor '" + constructorIn.toString()); + } } @Override + @SuppressWarnings("unchecked") public E apply(final World world) { - try - { - return this.constructor.newInstance(world); - } - catch (final IllegalAccessException | InstantiationException | InvocationTargetException e) - { + + try { + return (T)this.constructor.invoke(world); + } catch (Throwable e) { FMLLog.log.error("Encountered an exception while constructing entity '{}'", this.describeEntity(), e); return null; } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java index 8ba7a978c..cbfb6e3b9 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java @@ -167,7 +167,10 @@ private EntityRegistry() * @param trackingRange The range at which MC will send tracking updates * @param updateFrequency The frequency of tracking updates * @param sendsVelocityUpdates Whether to send velocity information packets as well + *

+ * Deprecated, use {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ + @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates) { instance().doModEntityRegistration(registryName, entityClass, entityName, id, mod, trackingRange, updateFrequency, sendsVelocityUpdates); @@ -186,7 +189,10 @@ public static void registerModEntity(ResourceLocation registryName, Class + * Deprecated, use {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ + @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates, int eggPrimary, int eggSecondary) { instance().doModEntityRegistration(registryName, entityClass, entityName, id, mod, trackingRange, updateFrequency, sendsVelocityUpdates); From 2e05e0855dec27f3728dd16ed5d8a9123f0ca896 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:07:49 +0800 Subject: [PATCH 2/6] fix words --- .../fml/common/registry/EntityEntryBuilder.java | 7 ++++--- .../minecraftforge/fml/common/registry/EntityRegistry.java | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java index 4fe85494d..99ab03bc0 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java @@ -35,6 +35,7 @@ import net.minecraftforge.fml.relauncher.ReflectionHelper.UnknownConstructorException; import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -109,7 +110,7 @@ public final EntityEntryBuilder entity(@Nonnull final Class enti } /** - * Sets the factory of the entity. + * Sets the factory of the entity. Used for the client-side sync. * * @param factory The entity factory * @return This builder @@ -295,7 +296,7 @@ static abstract class ConstructorFactory implements Function entity) { - constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); + Constructor constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); try { this.constructor = MethodHandles.lookup().unreflectConstructor(constructorIn); } catch (IllegalAccessException e) { @@ -309,7 +310,7 @@ public E apply(final World world) { try { - return (T)this.constructor.invoke(world); + return (E)this.constructor.invoke(world); } catch (Throwable e) { FMLLog.log.error("Encountered an exception while constructing entity '{}'", this.describeEntity(), e); return null; diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java index cbfb6e3b9..99f6c06ec 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java @@ -168,7 +168,7 @@ private EntityRegistry() * @param updateFrequency The frequency of tracking updates * @param sendsVelocityUpdates Whether to send velocity information packets as well *

- * Deprecated, use {@link net.minecraftforge.event.RegistryEvent.Register} instead. + * Deprecated, subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates) @@ -190,7 +190,7 @@ public static void registerModEntity(ResourceLocation registryName, Class - * Deprecated, use {@link net.minecraftforge.event.RegistryEvent.Register} instead. + * Deprecated, subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates, int eggPrimary, int eggSecondary) From 1cfed39e65764ffe8f71a3a896fe11cc93293874 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:27:08 +0800 Subject: [PATCH 3/6] Stupid Generics --- .../minecraftforge/fml/common/registry/EntityEntryBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java index 99ab03bc0..f8e0f5384 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java @@ -296,7 +296,7 @@ static abstract class ConstructorFactory implements Function entity) { - Constructor constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); + var constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); try { this.constructor = MethodHandles.lookup().unreflectConstructor(constructorIn); } catch (IllegalAccessException e) { From c9a725fa66ea5d8f61fcc6142b9448e6b21c7012 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:12:08 +0800 Subject: [PATCH 4/6] disunreflection --- .../common/registry/EntityEntryBuilder.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java index f8e0f5384..5c0530679 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java @@ -34,8 +34,6 @@ import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import net.minecraftforge.fml.relauncher.ReflectionHelper.UnknownConstructorException; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -292,26 +290,22 @@ private void registerStatistics() static abstract class ConstructorFactory implements Function { - private final MethodHandle constructor; + private final Constructor constructor; ConstructorFactory(final Class entity) { - var constructorIn = ObfuscationReflectionHelper.findConstructor(entity, World.class); - try { - this.constructor = MethodHandles.lookup().unreflectConstructor(constructorIn); - } catch (IllegalAccessException e) { - throw new UnknownConstructorException("Could not unreflect constructor '" + constructorIn.toString()); - } + this.constructor = ObfuscationReflectionHelper.findConstructor(entity, World.class); } @Override - @SuppressWarnings("unchecked") public E apply(final World world) { - - try { - return (E)this.constructor.invoke(world); - } catch (Throwable e) { + try + { + return this.constructor.newInstance(world); + } + catch (final IllegalAccessException | InstantiationException | InvocationTargetException e) + { FMLLog.log.error("Encountered an exception while constructing entity '{}'", this.describeEntity(), e); return null; } From 8ce58d1eb758d85f4ba9123e1cf3d42d8566946a Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:15:27 +0800 Subject: [PATCH 5/6] format deprecated --- .../minecraftforge/fml/common/registry/EntityRegistry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java index 99f6c06ec..660323cc7 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityRegistry.java @@ -168,7 +168,7 @@ private EntityRegistry() * @param updateFrequency The frequency of tracking updates * @param sendsVelocityUpdates Whether to send velocity information packets as well *

- * Deprecated, subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. + * @deprecated subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates) @@ -190,7 +190,7 @@ public static void registerModEntity(ResourceLocation registryName, Class - * Deprecated, subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. + * @deprecated subscribe {@link net.minecraftforge.event.RegistryEvent.Register} instead. */ @Deprecated public static void registerModEntity(ResourceLocation registryName, Class entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates, int eggPrimary, int eggSecondary) From c66b475d78b4d0a6c8dd8e8f904ab38d03bc5388 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:27:02 +0800 Subject: [PATCH 6/6] full docs --- .../minecraftforge/fml/common/registry/EntityEntryBuilder.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java index 5c0530679..320defea9 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntryBuilder.java @@ -109,6 +109,8 @@ public final EntityEntryBuilder entity(@Nonnull final Class enti /** * Sets the factory of the entity. Used for the client-side sync. + * Here, we just construct an instance. Datas will be passed elsewhere. + * {@see IEntityAdditionalSpawnData} * * @param factory The entity factory * @return This builder