Enum Help

Discussion in 'Spigot Plugin Development' started by Domboing, Jun 12, 2018.

  1. Basically in my plugin when a player clicks i kit i make it change the state which changes the scoreboard to pvp mode but its not separate to the player

    here is my code of trying to set a individual player state

    Code (Text):
    package gg.kisurimc.kitpvp.state;

    import java.util.UUID;

    public enum PlayerState
    {

        LOBBY, FIGHTING,
        PRAC_LOBBY, PRAC_FIGHTING;

        private static PlayerState currentState;

        public static PlayerState setType(PlayerState state, UUID uuid)
        {
            return PlayerState.currentState = state;
        }
       
        public static PlayerState getState(UUID uuid)
        {
            return currentState;
        }
    }
     
     
  2. Do you know the meaning of the word static ?
    https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    private static PlayerState currentState;
    This will be shared across all instances it it will be the same.

    If i was you i would have 2 classes.. Your enum and a "Manager Class"

    In the manager class add a NON static hashmap called "States" storing <PlayerUUID,StateEnum>
    When when you need to set a player state ad it to the hashmap with there uuid and the state
    you can also then get the state back after the event.
     
  3. Ive tried using hashmaps and it still made it so both player have a the same scoreboard and its not separate
     
  4. Like this.. ?


    Code (Java):
    public enum States {
    One,Two,Three;
    }

    public class Manager {

    HashMap<UUID, States> playerStates = new HashMap<>();


    public void SetState(UUID playerUUID, States state) {
    playerStates.put(playerUUID, state );
    }

    public void GetState(UUID playerUUID) {
    playerStates.getOrDefault(playerUUID, States.One);
    //States.One in the above line is the DEFAULT value if the player has not had a state set.
    }

    }
     
  5. Ok would this would when doing something

    like if(Manager.playerstates.getState(playernlaha) == State.one) {
    //do this?
    }
     
  6. How about test it. And dont make "playerstates" static.
     
  7. I really don't want to start I fight as I know there are many think I shouldn't say this, but please learn JAVA: HashMaps and the meaning of the word static are basic concepts.
     
  8. Run.getInstance().getPState().playerStates.put(player.getUniqueId(), PlayerState.FIGHTING);
     
  9. Prove me I'm wrong what does Run.getINstance() do?? What is Run? What is .getInstance()?
     
  10. Run is the main class and getInstance is so i can access the getter and setter of the PlayerState