getting a String List from config problem

Discussion in 'Plugin Development' started by pkt, Jan 8, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    pkt

    So, for my Giants plugin, I have been removing the wrappers that the previous dev had made and instead using bukkits implementation. All of it works exept getting the World list from the config. The source code is on GitHub: https://github.com/pkt77/Giants/tree/master/src/me/pkt77/giants
    I get this error when a Giant trys to spawn
    Code:
    2013-01-08 17:34:14 [SEVERE] Could not pass event CreatureSpawnEvent to Giants v5.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_4_6.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:227)
        at net.minecraft.server.v1_4_6.World.addEntity(World.java:903)
        at net.minecraft.server.v1_4_6.SpawnerCreature.spawnEntities(SpawnerCreature.java:158)
        at net.minecraft.server.v1_4_6.WorldServer.doTick(WorldServer.java:157)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:572)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
        at me.pkt77.giants.events.Listeners.GiantSpawnEvent(Listeners.java:60)
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 12 more
    btw the source on GitHub is old, so line 60 is actually
    Code:
            if (!_giants.getConfig().getStringList("Giants Configuration.Spawn Settings.Worlds").contains(entity.getWorld().getName())) {
                return;
            }
    The error also will sometimes occur in the SpawnEvent class line 23 which is also similar to the code above ^
     
  2. Offline

    fireblast709

    • _giants is null
    • getStringList(path), path is nonexistant (maybe the whitespace in the path breaks it
    either one
     
  3. Offline

    pkt

    Why world _giants be null?
    Its not the white spaces.. I already tested that
     
  4. Offline

    skipperguy12

    You could make a List<String> and then get the config path Giants Configuration.Spawn Settings.Worlds

    and then loop through it and broadcast it to console. If nothing comes up, its null, and then we can try to help you from there.
     
  5. Offline

    pkt

    Code:
            for (String w : _giants.getConfig().getStringList("Giants Configuration.Spawn Settings.Worlds")) {
                System.out.println(w);
            }
    printed out correctly:
    08.01 20:18:38 [Server] INFO Test_the_end
    08.01 20:18:38 [Server] INFO Test_nether
    08.01 20:18:38 [Server] INFO Test
     
  6. Offline

    CubixCoders

    I would recommend making a List like so
    Code:java
    1.  
    2. //Make the list
    3. public List<String> worldList;
    4. //And in onEnable add this
    5. worldList = _giants.getConfig().getStringList("Giants Configuration.Spawn Settings.Worlds");
    6. _giants.getConfig().set("Giants Configuration.Spawn Settings.Worlds", worldList);
    7. _giants.saveConfig();
    8. //Then use
    9. if(_giants.worldList.contains("")){
    10. }
    11.  
     
  7. Offline

    pkt

    Well I already set the list... I get need to get it..
     
  8. Offline

    chasechocolate

    Code:java
    1. if(worldList.contains("world")){
    2. //Code
    3. }
     
  9. Offline

    CubixCoders

    My code is more reliable. also use this
    Code:java
    1.  
    2. if(_giants.getConfig().contains("Giants Configuration.Spawn Settings.Worlds"){
    3. }
    4.  
     
  10. Offline

    pkt

    Neither of those will work...
     
  11. Offline

    fireblast709

    the code in the OP would work perfectly fine. Its just that _giants is null (highly likely).
     
  12. Offline

    pkt

    but why would that be? It returned the worlds correctly in my for loop.. so it can't be null...
     
  13. Offline

    fireblast709

    pkt well it might be that it would get updated somewhere between your test and the actual code. Can you show me how you have tested that (like: the full code, not just the snippet posted before)
     
  14. Offline

    pkt

    :confused: wait what other code do you want? The whole class?
     
  15. Offline

    fireblast709

    the whole class including the debug code you have used before
     
  16. Offline

    pkt

    fireblast709
    The SpawnEvent class:
    Code:
    package me.pkt77.giants.events;
     
    import org.bukkit.Location;
    import org.bukkit.block.Biome;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import me.pkt77.giants.Giants;
     
    public class SpawnEvent extends Event {
        private Giants _giants;
        private static boolean cancelled = false;
        private Entity entity;
        private Location location;
        private static final HandlerList handlers = new HandlerList();
     
        public SpawnEvent(Location loc) {
            location = loc;
            Biome biome = loc.getWorld().getBiome(loc.getBlockX(), loc.getBlockZ());
     
    // This is where the debug code was
            if (!_giants.getConfig().getString("Giants Configuration.Spawn Settings.Worlds").equals(loc.getWorld().getName())) {
                setCancelled(true);
            }
     
            if (!isCancelled()) {
                if (biome == Biome.FOREST) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Forest") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.DESERT) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Desert") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.PLAINS) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Plains") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.SWAMPLAND) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Swampland") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.JUNGLE) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Jungle") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.ICE_PLAINS) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Ice Plains") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.TAIGA) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Taiga") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.EXTREME_HILLS) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Extreme Hills") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.ICE_MOUNTAINS) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Ice Mountains") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if (biome == Biome.MUSHROOM_ISLAND) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Mushroom Island") == true) {
                        entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                    }
                }
                if ((biome == Biome.BEACH) || (biome == Biome.FROZEN_OCEAN) || (biome == Biome.FROZEN_RIVER) || (biome == Biome.MUSHROOM_SHORE) || (biome == Biome.OCEAN) || (biome == Biome.RIVER)) {
                    if (_giants.getConfig().getBoolean("Giants Configuration.Spawn Settings.Biomes.Other") == true) {
     
                    }
                }
                if (biome == Biome.HELL) {
                    entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                }
                if (biome == Biome.SKY) {
                    entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
                }
            }
        }
     
        public void setCancelled(boolean cancel) {
            cancelled = cancel;
        }
     
        public static boolean isCancelled() {
            return cancelled;
        }
     
        public Entity getEntity() {
            return entity;
        }
     
        public Location getLocation() {
            return location;
        }
     
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
    }
    and where it is called upon:
    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void GiantSpawnEvent(CreatureSpawnEvent event) {
            Entity entity = event.getEntity();
            EntityType type = event.getEntityType();
            SpawnReason spawnReason = event.getSpawnReason();
     
            if (!_giants.getConfig().getString("Giants Configuration.Spawn Settings.Worlds").equals(entity.getWorld().getName())) {
                return;
            }
     
            if (spawnReason == SpawnReason.NATURAL) {
                if ((type == EntityType.ZOMBIE) || (type == EntityType.COW) || (type == EntityType.MUSHROOM_COW) || (type == EntityType.PIG_ZOMBIE) || (type == EntityType.ENDERMAN)) {
                    int sRate = _giants.getConfig().getInt("Giants Configuration.Spawn Settings.Spawn Chance");
                    int chance = 100 - sRate;
     
                    Random rand = new Random();
                    double choice = rand.nextInt(100) < chance ? 1 : 0;
                    if (choice == 0) {
                        Location location = event.getEntity().getLocation();
                        double x = location.getX();
                        double y = location.getY();
                        double z = location.getZ();
     
                        int x2 = (int) x;
                        int y2 = (int) y;
                        int z2 = (int) z;
     
                        int spawngiant = 1;
                        double checkcount = 0.01;
                        while (checkcount < 10) {
                            y2 += checkcount;
     
                            if (entity.getWorld().getBlockTypeIdAt(x2, y2, z2) != 0) {
                                spawngiant = 0;
                            }
                            checkcount++;
                        }
                        if (spawngiant == 1) {
                            SpawnEvent SE = new SpawnEvent(location);
                            Bukkit.getServer().getPluginManager().callEvent(SE);
                        }
                    }
                }
            }
        }
     
  17. Offline

    fireblast709

    _giants in the first block should throw a NullPointerException (as it is undefined at that point)
     
  18. Offline

    pkt

    How do I re-define it after its null?
     
  19. Offline

    fireblast709

    use the constructor to pass along the object
     
  20. Offline

    pkt

    Apparently, bukkits implementation does not work for me...it cant even get an int from the config... looks like I'm going back to using the wrappers... :/
     
Thread Status:
Not open for further replies.

Share This Page