How to get PlayerType using enum

Discussion in 'Plugin Development' started by MDev, Jul 28, 2022.

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

    MDev

    Hello, I need advice on how to get a player type using enum. I am trying to make 3 groups for players (BRONZE) (IRON) (GOLD). I wanted to do this using enum PlayerType and a switch (or is there a better solution?). Each time a player would join that group I would gain value based on that. For example, the player "Player" is PlayerType BRONZE.

    PlayerType class
    Code:
    public enum PlayerType {
    BRONZE, IRON, GOLD
    }
    

    PlayerManager class
    Code:
    private final Bronze bronzes = Bronze.getInstance();
    private final Iron irons = Iron.getInstance();
    private final Gold golds= Gold.getInstance();
    
    public void setPlayerType(Player player, PlayerType playerType) {
    switch(playerType) {
    case BRONZE:
    bronzes.addPlayer(player);
    irons.removePlayer(player);
    golds.removePlayer(player);
    break;
    case IRON:
    irons.addPlayer(player);
    bronzes.removePlayer(player);
    golds.removePlayer(player);
    break;
    case GOLD:
    golds.addPlayer(player);
    irons.removePlayer(player);
    bronzes.removePlayer(player);
    break;
    }
    }
    
    public boolean getPlayerType(Player player, PlayerType playerType) {
    ?? Now how can I return the value in which group the given player is?
    return false;
    }
    
     
    Last edited: Jul 28, 2022
  2. Offline

    timtower Administrator Administrator Moderator

    @MDev Why is it not a HashMap?
    Then it is a single list, can set and get the values right away.
    And store the players using uuid, not Player instances please.
     
  3. Offline

    MDev

    I am using HashSets to store the UUIDs of the players. This is what addPlayer(player) is used for.

    Code:
    private Set<UUID> bronzes = new HashSet<>();
    
    addPlayer(Player player) {
    bronzes.add(player.getUniqueId());
    }
    
     
  4. Offline

    timtower Administrator Administrator Moderator

    But why are there multiple sets instead of a single hashmap?
    No more issues removing or adding people.
    No weird switch cases either as it changes into a single line call.
     
  5. Offline

    MDev

    Ohh, that's right. So something like that?
    Code:
    private final HashMap<UUID, PlayerType> playerList = new HashMap<>();
    
    public void setPlayerType(Player player, PlayerType playerType) {
    playerList.put(player.getUniqueId(), playerType);
    }
    
    public boolean getPlayerType(Player player, PlayerType playerType) {
    
    }
    
    And how can I use getPlayerType to get the individual player type and send a message to it? For example, if the player is a BRONZE, send him a message. So that I can access it from other classes like:
    Code:
    if (playerManager.getPlayerType(player.getUniqueId(), PlayerType.BRONZE)) { 
    player.sendMessage("");
    
     
    Last edited: Jul 28, 2022
  6. Offline

    timtower Administrator Administrator Moderator

    @MDev Why not get the player type and then check with the enum?
     
  7. Offline

    MDev

    Do you mean it like this? So if the value is for example BRONZE it will return true.
    Code:
    public boolean getPlayerType(Player player, PlayerType playerType) {
    return playerList.get(player.getUniqueId()) == playerType;
    }
    
     
  8. Offline

    timtower Administrator Administrator Moderator

    @MDev getPlayerType(Player){
    return playerList.get(player.getUniqueId())
    }
    Then just check in the normal function?
    And do check if the player exists.
     
  9. Offline

    MDev

    So:
    Code:
    public PlayerType getPlayerType(Player player) {
    return playerList.get(player.getUniqueId());
    }
    
    
    Code:
    if (playerManager.getPlayerType(player) == PlayerType.BRONZE)
    //do something
    
     
    Last edited: Jul 28, 2022
  10. Offline

    timtower Administrator Administrator Moderator

    Yes, together with a return value when the player is not in the list.
     
  11. Offline

    MDev

    When I tried to send to the player ActionBar it shows me an error:
    Caused by: java.lang.NullPointerException: Cannot invoke "me.MDEV.Plugin.PlayerManager.getPlayerType(org.bukkit.entity.Player)" because "this .playerManager" is null

    Here is the code:

    PlayerManager
    Code:
    private static final PlayerManager instance = new PlayerManager();
        private final HashMap<UUID, PlayerType> playerList = new HashMap<>();
        ActionBar actionBar = ActionBar.getInstance();
    
        public static PlayerManager getInstance() {
            return instance;
        }
    
    public void setPlayerType(Player player, PlayerType playerType) {
            playerList.put(player.getUniqueId(), playerType);
            actionBar.create(player);
        }
    
    ActionBar
    Code:
    
    private static final ActionBar instance = new ActionBar();
    PlayerManager playerManager = PlayerManager.getInstance();
    
    public static ActionBar getInstance() {
    return instance;
    }
    
    if (playerManager.getPlayerType(player) == PlayerType.BRONZE)
                    player.spigot().sendMessage(ChatMessageType.ACTION_BAR,
                            TextComponent.fromLegacyText("BRONZE PLAYER");
    
    Why is playerManager null?
     
Thread Status:
Not open for further replies.

Share This Page