Skip to content

Commit

Permalink
disunreflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecdcaeb authored Dec 4, 2024
1 parent 1cfed39 commit c9a725f
Showing 1 changed file with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -292,26 +290,22 @@ private void registerStatistics()

static abstract class ConstructorFactory<E extends Entity> implements Function<World, E>
{
private final MethodHandle constructor;
private final Constructor<? extends E> constructor;

ConstructorFactory(final Class<? extends E> 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;
}
Expand Down

0 comments on commit c9a725f

Please sign in to comment.