Solved Null ArrayList Problem.

Discussion in 'Plugin Development' started by zlToxicNetherlz, Oct 12, 2015.

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

    zlToxicNetherlz

    Sorry for my bad english.

    I created a arraylist collection where locations are added.
    But When I try to use the collection in an event. I get an empty collection.
    Please help.

    My code: (Line 66 to 69)
    ArenaManager.java (open)
    Code:
    public class ArenaManager implements Listener{
     
        Plugin plugin;
     
        /**
         * @category Game Locations
         */
        ArrayList<Location> pspawns = new ArrayList<Location>();
        ArrayList<Location> powerup = new ArrayList<Location>();
        ArrayList<Location> ocupped = new ArrayList<Location>();
     
        /**
         * @category Players On Game
         */
        ArrayList<Player> live = new ArrayList<Player>();
        ArrayList<Player> spec = new ArrayList<Player>();
     
        public void startArena(World w, Plugin pl){
            plugin = pl;
            try{
                Location _lspawn = w.getSpawnLocation();
             
                Location game_l1 = new Location(w, _lspawn.getX() + 100, _lspawn.getY() + 100, _lspawn.getZ() + 100);
                Location game_l2 = new Location(w, _lspawn.getX() - 100, _lspawn.getY() - 100, _lspawn.getZ() - 100);
             
                Cuboid game_area = new Cuboid(game_l1, game_l2);
             
                 if(comprobateArena(game_area) == true){
                 
                    for(Block pblock : game_area.getBlocks()){
                        if(pblock.getType() == Material.SPONGE){
                            pblock.setType(Material.AIR);
                            pspawns.add(pblock.getLocation().add(0.5, 0.5, 0.5));
                            Bukkit.broadcastMessage("size = " + pspawns.size());
                        }
                        if(pblock.getType() == Material.FLOWER_POT){
                            pblock.setType(Material.AIR);
                            powerup.add(pblock.getLocation().add(0.5, 0.5, 0.5));
                        }
                    }
                 
                }
             
            } catch(Exception ex){
                plugin.getServer().getConsoleSender().sendMessage(TextData.prefix + "§4ERROR §7Se ha definido mal el spawn principal.");
            }
        }
     
     
        public boolean comprobateArena(Cuboid c){
            for(Chunk chunk : c.getChunks()){
                chunk.load();
            }
            for(Block b : c.getBlocks()){
                if(b.getType() == Material.SPONGE){
                    return true;
                }
            }
            return false;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onJoin(PlayerJoinEvent e){
            Player player = e.getPlayer();
            ArrayList<Location> _fix = pspawns;
            Bukkit.broadcastMessage("text = " + pspawns.toString()); // print "text = []" in console. ¿WHAT!?
            Bukkit.broadcastMessage("text = " + _fix); // print "text = []" in console. ¿WHAT?
            // ranPlayer(player, _fix); or ranPlayer(player, pspawns) <- ERROR.
            //
        }
     
        public void ranPlayer(Player p, ArrayList<Location> spawn){
            RangeInt ri = new RangeInt();
            Bukkit.broadcastMessage("text = " + spawn);
            Bukkit.broadcastMessage("size = " + spawn.size());
            int random = ri.getRandomInt(0, 15);
            Location r_spawn = spawn.get(random);
            if(!ocupped.contains(r_spawn)){
                ocupped.add(r_spawn);
                live.add(p);
                p.teleport(r_spawn);
            } else {
                ranPlayer(p, spawn);
            }
        }
     
    }
    


    Screenshot:
    [​IMG]

    Please help :(
     
  2. Offline

    dlange

  3. Offline

    Xerox262

    I don't see you would expect this to NOT be empty? You only call pspawn.add(Location) in your start Arena method.
     
  4. Offline

    mcdorli

    @zlToxicNetherlz I could assume, that you created 2 listener class, and you try to reach the variable from tthe another one. Always create tge lsitener class as a variable, and then pass instead if creating a new one.
     
  5. Offline

    zlToxicNetherlz

    @mcdorli
    Thanks, I tried to create another class where I keep the lists, And I called the class to add and receive the list.
    Screenshot:
    [​IMG]
    ArenaList.java

    Code:
    package ic.arcade.games.Canibal.arena;
    
    import java.util.ArrayList;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class ArenaList {
        /**
         * @category Game Locations
         */
        public static ArrayList<Location> pspawns = new ArrayList<Location>();
        public static ArrayList<Location> powerup = new ArrayList<Location>();
        public static ArrayList<Location> ocupped = new ArrayList<Location>();
       
        /**
         * @category Players On Game
         */
        public static ArrayList<Player> live = new ArrayList<Player>();
        public static ArrayList<Player> spec = new ArrayList<Player>();
    }
    
    Thank you so much! :)
     
Thread Status:
Not open for further replies.

Share This Page