mercredi 10 août 2016

Reflection Config Loading

I am trying to load a config and just to make it easier I have decided to make the fields the exact same as the properties in the config. This makes it easier to add more properties later. When trying to load the config it appears to be able to read the first two properties which are both ints but once it hits the third which is also an int it spits out this error:

IV
80
int
CP
500
int
MinEvolveUseXpEgg
0
int
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.pokemongobot.PokemonGoBot.main(PokemonGoBot.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.IllegalArgumentException: Can not set int field com.pokemongobot.Config.MinEvolveUseXpEgg to java.lang.Class
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.setInt(UnsafeIntegerFieldAccessorImpl.java:128)
    at java.lang.reflect.Field.setInt(Field.java:949)
    at com.pokemongobot.Config.loadConfig(Config.java:65)
    at com.pokemongobot.Config.<clinit>(Config.java:38)
    ... 6 more

I have put in some debug lines in order to see what it fails on

public static void loadConfig(Properties config) throws IllegalAccessException {
        Field[] allFields = Config.class.getDeclaredFields();
        for (Field field : allFields) {
            if(field.getType().isPrimitive()) {
                if(field.getType().isAssignableFrom(int.class)) {
                    System.out.println(field.getName());
                    System.out.println(config.getProperty(field.getName()));
                    System.out.println(field.getType().toString());
                    field.setInt(Config.class, Integer.parseInt(config.getProperty(field.getName())));
                } else if(field.getType().isAssignableFrom(double.class)) {
                    field.setDouble(Config.class, Double.parseDouble(config.getProperty(field.getName())));
                } else if(field.getType().isAssignableFrom(boolean.class)) {
                    field.setBoolean(Config.class, Boolean.parseBoolean(config.getProperty(field.getName())));
                }
            } else {
                if(field.getType().isAssignableFrom(String.class)) {
                    field.set(Config.class, config.getProperty(field.getName()));
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

Config:

# Percentage of perfect
IV=80
CP=500

AutoDrop=true

# Inventory

Potion=0
SuperPotion=10
HyperPotion=50
Revive=20
Pokeball=10
GreatBall=40
UltraBall=50
RazzBerry=150

Evolve=true
XPEggEvolve=false
MinEvolveUseXpEgg=0

HandleSoftBan=false





Aucun commentaire:

Enregistrer un commentaire