ArrayList of Objects

Discussion in 'Plugin Development' started by Weiyuane, Sep 12, 2015.

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

    Weiyuane

    Solved: I realized I made the members of my Foo class static, so there can only be one at a time.


    Hey, so I'm trying to create an ArrayList of Foo objects, but it seems to overwrite itself:

    Code:
    //Where the problem seems to occur, I'm replacing class names and variables with Foo and Bar
    public class startGame {
    
        static boolean game = false;
        public static ArrayList<Foo> Foos = new ArrayList<Foo>();
    
        public static void newGame(){
            game = true;
            Foos = new ArrayList<Foo>();
            for(Player p : Bukkit.getOnlinePlayers()){
                Foo f = new Foo(p.getName());
                Bukkit.broadcastMessage("Set Foo: " + f);
                Foos.add(f);
                Bukkit.broadcastMessage("Added Foo: " + Foos.get(Foos.size() - 1));
                f.tpToSpawn();
            }
            Bukkit.broadcastMessage("Starting game! Foos: " + Foos.toString());
        }
    //Methods and whatnot
    }
    
    //Full Foo class
    package com.weiyuane.pluginName;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import com.weiyuane.pluginName.pluginName;
    
    public class Foo {
    
        int barRegen = 25;
        static int maxBar = 100;
        static String playerName;
    
        public Foo(String playerName){
            Foo.playerName = playerName;
            getPlayer().setLevel(maxBar);
            getPlayer().setDisplayName("§3" + getPlayer().getName());
            Bukkit.broadcastMessage("Created Foo: " + getPlayer().getDisplayName());
        }
    
        @SuppressWarnings("deprecation")
        public static Player getPlayer(){
            return Bukkit.getPlayer(playerName);
        }
    
        public void tpToSpawn(){
            getPlayer().teleport(pluginName.fooSpawnPoint);
        }
    
        public void regenBar(){
            if(getPlayer().getLevel() >= maxBar - barRegen) {
                getPlayer().setLevel(maxBar);
            } else {
                getPlayer().setLevel(getPlayer().getLevel() + barRegen);
            }
            getPlayer().setExp((float) getPlayer().getExp() / maxBar);
        }
    
        public static void death(){
            startGame.fooDeath(getPlayer());
            getPlayer().setDisplayName("§7" + getPlayer().getDisplayName());
            getPlayer().getWorld().setSpawnLocation((int) pluginName.lobbySpawnPoint.getX(), (int) pluginName.lobbySpawnPoint.getY(), (int) pluginName.lobbySpawnPoint.getZ());
        }
    
        public static void takeBar(Player player, int cost){
            getPlayer().setLevel(player.getLevel() - cost);
            player.setExp((float) player.getExp() / maxBar);
        }
    
        public String toString(){
            String str = "";
            str += "Name: " + getPlayer().getDisplayName() + " ";
            str += "Bar: " + getPlayer().getLevel() + "|";
            return str;
        }
    
    }
    
    
    

    //In a server with alts Weiyuane and Thomas
    When I send the command that executes startGame, the following is broadcast:
    -
    Created Foo: Weiyuane
    Set Foo: Name: Weiyuane, Bar: 100 |
    Added Foo: Name: Weiyuane, Bar: 100 |
    Created Foo: Thomas
    Set Foo: Name: Thomas, Bar: 100 |
    Added Foo: Name: Thomas, Bar: 100 |
    Starting game! Foo: {Name: Thomas, Bar: 100 | Thomas, Bar: 100}
    -
    Then everything that iterates through Foo runs twice for Thomas and doesn't run for Weiyuane.

    Both are teleported.

    (Another test with 5 alts has the same results.)

    //In a server with alts Weiyuane, Thomas, Timmy, Trevor, Essund
    When I send the command that executes startGame, the following is broadcast:
    -
    Created Foo: Weiyuane
    Set Foo: Name: Weiyuane, Bar: 100 |
    Added Foo: Name: Weiyuane, Bar: 100 |
    Created Foo: Thomas
    Set Foo: Name: Thomas, Bar: 100 |
    Added Foo: Name: Thomas, Bar: 100 |
    Created Foo: Timmy
    Set Foo: Name: Timmy, Bar: 100 |
    Added Foo: Name: Timmy, Bar: 100 |
    Created Foo: Trevor
    Set Foo: Name: Trevor, Bar: 100 |
    Added Foo: Name: Trevor, Bar: 100 |
    Created Foo: Essund
    Set Foo: Name: Essund, Bar: 100 |
    Added Foo: Name: Essund, Bar: 100 |
    Starting game! Foo: {Name: Essund, Bar: 100 | Essund, Bar: 100 | Essund, Bar: 100 | Essund, Bar: 100 | Essund, Bar: 100}
    -
    Then everything that iterates through Foo runs 5x for Essund and doesn't run for anyone else.

    Everyone is teleported.

    There are no error messages.

    I don't understand why Foos is overwriting Weiyuane with Thomas, any help would be greatly appreciated. :)


    EDIT: Further testing shows that with each .add, every value is being set to that Foo.
     
    Last edited: Sep 12, 2015
  2. Offline

    Gater12

    @Weiyuane
    Please post full Foo class please.
     
  3. Offline

    Weiyuane

    //Where the problem was
    Code:
    public class Foo {
    
        int barRegen = 25;
        static int maxBar = 100; //Not supposed to be static. >.<
        static String playerName;
    
    I'm pretty sure I figured it out, I made playerName and maxBar static; so yeah...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page