Search code examples
javabukkit

Java 2 new objects mess together


I have made a new object called Game:

public class Game {

    private String gamenaam;
    private String bungeenaam;
    private int poort;
    private int minplayers;
    private int maxplayers;
    private static GameState gamestate = GameState.Ingame;


    public Game(String naam) {
        this.gamenaam = naam;

        setAlles();
    }

    public String getGameNaam() {
        return this.gamenaam;
    }

    public String getBungeeNaam() {
        return bungeenaam;
    }

    public int getPoort() {
        return poort;
    }

    public int getMinPlayers() {
        return minplayers;
    }
    public int getMaxPlayers() {
        return maxplayers;
    }

    public GameState getCurrentState() {
        //System.out.print(gamenaam + ":" + MySQL.getGameState(getGameNaam()) + ":" + gamestate);
        return gamestate;
    }
    public void setCurrecntState(GameState state) {
        gamestate = state;
    }



    private void setAlles() {
        bungeenaam = MySQL.getBungeeNaam(this.gamenaam);
        poort = MySQL.getPoort(this.gamenaam);
        minplayers = MySQL.getMinPlayer(this.gamenaam); 
        maxplayers = MySQL.getMaxPlayer(this.gamenaam);
        //gamestate = MySQL.getGameState(this.gamenaam);
    }

}

I store everything in an public static HashMap<Location, Game> gameSigns = new HashMap<Location, Game>(); map

Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("SCGameHost"), new Runnable() {

    @Override
    public void run() {
        for(Map.Entry<Location, Game> entry: Main.gameSigns.entrySet()){
            Game game = entry.getValue();
            if(game.getGameNaam().equalsIgnoreCase("Heks")) {
                System.out.print(game.getGameNaam()+" is changed to FINISHED");
                game.setCurrecntState(GameState.Finished);
            }else {
                game.setCurrecntState(GameState.Maintenance);
            }
        }
    }
}, 10 *20L, 10 *20L);

I have 2 things in the gameSigns HashMap Heks and Snowball. When I change Heks to GameState.Finished snowball is also being changed.


Solution

  • It is because the variable gamestate is static, so it is shared between all the instances of the Game class. Remove the word static if you want separate values for different instances.