Solved Having my enum (state) being always set as "null"

Discussion in 'Plugin Development' started by xpaintall, Jan 28, 2022.

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

    xpaintall

    Okay don't make fun of me because only recently I got back into java coding after a couple of months of doing nothing. I have an Enum
    Code:
    public enum GameState {
        Waiting, Starting, Running
    
    }
    and a couple of methods in my GameManager class to change and check for the state of the enum
    Code:
        public GameState getState() {
            return state;
        }
    
        public void setState(GameState state) {
            this.state = state;
        }
    
    My problem is however, that the state of the enum is always null;
    Code:
    if(cmd.getName().equalsIgnoreCase("whatstate")) {
                player.sendMessage(ChatColor.AQUA + "The state right now is: " + manager.getState());
            }
    so when I execute the command, no matter if the state has been changed or not, it says: "The state right now is: null".
    I would've been fine with that, but there's no way for me to check if the villager (the main objective of the game) is killed or not.
    Code:
    public class GameRunningRunnable extends BukkitRunnable {
    
        VillagerMainClass f;
    
        GameManager manager;
    
        GameState state;
    
        public GameRunningRunnable(VillagerMainClass f) {
            this.f = f;
            manager = new GameManager(f);
        }
    
    
    
        @Override
        public void run() {
            if(manager.getState() == GameState.Running) {
                for(Player p : Bukkit.getOnlinePlayers()) {
                    p.sendMessage("Game is running!");
                }
                if(manager.getVillager().isDead()) {
                    manager.endGame();
                }
            }
        }
    }
    Whenever I wanted to change a state, I used the "manager.setState(gamestate)" method, but it just doesn't set it to anything.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @xpaintall You seem to have multiple GameState variables. Might want to clean those first.
    Do you give it a starting value?
     
  3. Offline

    xpaintall

    @timtower yes I do give it a starting value (in my onEnable void if that's what you mean), I also did clean the multiple gamestate variables.
    Code:
    public class VillagerMainClass extends JavaPlugin {
    
        GameManager manager = new GameManager(this);
        GameRunningRunnable grr = new GameRunningRunnable(this);
    
        @Override
        public void onEnable() {
            manager.setState(GameState.Waiting);
            //I know this method is deprecated dont bully me ok
            getServer().getScheduler().runTaskTimer(this, grr, 0, 1);
            getCommand("join").setExecutor(new Commands(this));
            getCommand("whatstate").setExecutor(new Commands(this));
        }
    
    
    
    }
    
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    xpaintall

    @timtower so you're saying that I should put my starting value in the same class as my manager?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @xpaintall I am saying that you should only have 1 manager. In your main class.
    Make a getter for it in your main class and use that one everywhere.
     
  7. Offline

    xpaintall

    @timtower Thank you kind sir that fixed my problem :)
     
Thread Status:
Not open for further replies.

Share This Page