samedi 10 septembre 2022

(Java) using reflection to add elements to an arrayList from another class

I'm trying to make a mod, so I have been looking for a way to access a protected arrayList from another class. So far I have managed to use my new basic understanding of java reflection in order to do so. I have been able to find the field I am looking for but so far I have not yet been able to find a way to modify its elements because I believe that it is the casting that leads to multiple errors, any suggestions for a way to solve this:

My code (repository https://github.com/dilosir22/BTAprodject/tree/master/src/main/java/com/example/examplemod):

public class LivingEntityHelper {

   static Field monsterList;

    static {
        try{
            Field[] feilds = BiomeGenBase.class.getDeclaredFields();
            for(Field feild : feilds) {
                    if(feild.getName().equals("spawnableMonsterList")) {
                        monsterList = feild;
                    }

            }
            if(monsterList == null) throw new RuntimeException("Could not find spawnableMonsterList feild!");
            monsterList.setAccessible(true);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @SuppressWarnings("unchecked")
    public static void addMonster(SpawnListEntry monster, Object biome){
        try {
            ((List<SpawnListEntry>) monsterList.get(biome)).add(monster);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

biomeGenBase contains:

    protected List<SpawnListEntry> spawnableMonsterList;

as well as multiple static instances of itself (what the biome parameter in addMonster() is meant to specify when used) this in its constructor:

        this.spawnableMonsterList = new ArrayList();

the errors recieved:

[18:57:32] [Minecraft main thread] java.lang.RuntimeException: java.lang.IllegalArgumentException: Can not set java.util.List field net.minecraft.src.BiomeGenBase.spawnableMonsterList to java.lang.Class [18:57:32] [Minecraft main thread] at com.example.examplemod.util.LivingEntityHelper.addMonster(LivingEntityHelper.java:35) [18:57:32] [Minecraft main thread] at com.example.examplemod.ExampleMod.init(ExampleMod.java:15) [18:57:32] [Minecraft main thread] at bta.ModLoader.loadMod(ModLoader.java:120) [18:57:32] [Minecraft main thread] at bta.ModLoader.init(ModLoader.java:76) [18:57:32] [Minecraft main thread] at net.minecraft.client.Minecraft.startGame(Minecraft.java:309) [18:57:32] [Minecraft main thread] at net.minecraft.client.Minecraft.run(Minecraft.java:566) [18:57:32] [Minecraft main thread] at java.base/java.lang.Thread.run(Thread.java:833) [18:57:32] [Minecraft main thread] Caused by: java.lang.IllegalArgumentException: Can not set java.util.List field net.minecraft.src.BiomeGenBase.spawnableMonsterList to java.lang.Class [18:57:32] [Minecraft main thread] at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) [18:57:32] [Minecraft main thread] at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) [18:57:32] [Minecraft main thread] at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) [18:57:32] [Minecraft main thread] at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) [18:57:32] [Minecraft main thread] at java.base/java.lang.reflect.Field.get(Field.java:425) [18:57:32] [Minecraft main thread] at com.example.examplemod.util.LivingEntityHelper.addMonster(LivingEntityHelper.java:33) [18:57:32] [Minecraft main thread] ... 6 more





Aucun commentaire:

Enregistrer un commentaire