Config yml, ArrayList or HashMap, Set or create Player list

Discussion in 'Plugin Development' started by Invokers, Aug 16, 2022.

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

    Invokers

    what is the best way to create a list of players for the arena in a custom config? Via ArrayList? or a hashmap? set? Is it possible to do this ?

    How to add and remove a player correctly afterwards?
    Code:
    public class ArenaManager implements Listener
    {
    
        private Location min;
        private Location max;
        private String name;
        private int minPlayer;
        private int maxPlayer;
        private int time;
        private World world;
        private GameStatus status;
        private String power;
        private Location gamelobby;
        public static ArrayList<String> players;
    
        public ArenaManager(String name, Location min, Location max, int minPlayer, int maxPlayer, int time, World world, GameStatus status, Location gamelobby, String power, ArrayList<String> players)
        {
           this.setMin(min);
           this.setMax(max);
           this.setName(name);
           this.setMinPlayer(minPlayer);
           this.setMaxPlayer(maxPlayer);
           this.setTime(time);
           this.setWorld(world);
           this.setStatus(status);
           this.setPower(power);
           this.setGamelobby(gamelobby);
    
        }
    
    
        public Location getMin() {
            return min;
        }
    
        public void setMin(Location min) {
            this.min = min;
        }
    
        public Location getMax() {
            return max;
        }
    
        public void setMax(Location max) {
            this.max = max;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getMinPlayer() {
            return minPlayer;
        }
    
        public void setMinPlayer(int minPlayer) {
            this.minPlayer = minPlayer;
        }
    
        public int getMaxPlayer() {
            return maxPlayer;
        }
    
        public void setMaxPlayer(int maxPlayer) {
            this.maxPlayer = maxPlayer;
        }
    
        public int getTime() {
            return time;
        }
    
        public void setTime(int time) {
            this.time = time;
        }
    
        public World getWorld() {
            return world;
        }
    
        public void setWorld(World world) {
            this.world = world;
        }
    
        public GameStatus getStatus() {
            return status;
        }
    
        public void setStatus(GameStatus status) {
            this.status = status;
        }
    
        public String getPower() {
            return power;
        }
    
        public void setPower(String power) {
            this.power = power;
        }
    
        public Location getGamelobby() {
            return gamelobby;
        }
    
        public void setGamelobby(Location gamelobby) {
            this.gamelobby = gamelobby;
        }
    
        public ArrayList<String> getPlayers() {
            return players;
        }
    
        public void setPlayers(ArrayList<String> players) {
            this.players = players;
        }
    
    }
     
  2. Online

    timtower Administrator Administrator Moderator

    @Invokers Why do you need to save them in a config? Config is more long-term storage.
     
  3. Offline

    Invokers

    And then where to store them?

    Is it possible to add via set Player? will they be stored relative to a specific arena?

    What about the option to store players in HashMap?

    Code:
    HashMap<String, Player> players = new HashMap<String, Player>
    
    players.put("Arena_name", Player);
    and then check for the number of players and the team. from here, write further code.

    I haven't come up with a solution to the problem, Does it make sense to rewrite the logic of the manager's Arena?
     
    Last edited: Aug 16, 2022
  4. Offline

    Invokers

    Are there any options?
     
  5. Online

    timtower Administrator Administrator Moderator

    @Invokers HashMap<String, Vector<UUID>>
    List of players.
     
  6. Offline

    Invokers

    Thanks
     
  7. Offline

    Invokers

    When trying to save the team in the config, it was saved like this How to fix it?


    Code:
    public class Game {
    
        private ColorControl plugin;
    
        private String name;
    
        private GameState state;
        private GameState power;
    
        private List<Player> Players;
    
        private int minPlayer;
        private int maxPlayer;
    
        private long time;
    
        private Location min;
        private Location max;
        private Location lobby;
    
        private HashMap<String, Teams> teams;
        private List<Teams> playingTeams;
    
        private World world;
    
        private Scoreboard scoreboard;
    
        public Game(String name) {
            this.name = name;
            this.Players = new ArrayList<>();
            this.teams = new HashMap<>();
    
        }
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public boolean playerJoins(Player p) {
            if (getState() == GameState.Game || getState() == GameState.Reset) {
                Utils.Message("Арена уже запущена", p);
               return false;
            } else {
                if (getState() == GameState.RUNNING) {
                    Utils.Message("Арена уже запущена", p);
                    return false;
                } else {
                    if (getPlayers().size() >= getMaxPlayer()) {
                        if (p.isOp() || p.hasPermission("*")) {
                            p.teleport(getLobby());
                        }
                        return false;
                    } else {
                        Players.add(p);
                        p.teleport(getLobby());
                    }
                }
            }
            return true;
        }
    
        public void setState(GameState state) {
            this.state = state;
        }
    
        public GameState getState() {
            return state;
        }
    
        public GameState getPower() {
            return power;
        }
    
        public void setPower(GameState power) {
            this.power = power;
        }
    
        public List<Player> getPlayers() {
            return Players;
        }
    
        public void setPlayers(List<Player> players) {
            Players = players;
        }
    
        public int getMinPlayer() {
            return minPlayer;
        }
    
        public void setMinPlayer(int minPlayer) {
            this.minPlayer = minPlayer;
        }
    
        public int getMaxPlayer() {
            return maxPlayer;
        }
    
        public void setMaxPlayer(int maxPlayer) {
            this.maxPlayer = maxPlayer;
        }
    
        public Location getMin() {
            return min;
        }
    
        public void setMin(Location min) {
            this.min = min;
        }
    
        public Location getMax() {
            return max;
        }
    
        public void setMax(Location max) {
            this.max = max;
        }
    
        public World getWorld() {
            return world;
        }
    
        public void setWorld(World world) {
            this.world = world;
        }
    
        public Location getLobby() {
            return lobby;
        }
    
        public void setLobby(Location lobby) {
            this.lobby = lobby;
        }
    
        public List<Teams> getPlayingTeams() {
            return playingTeams;
        }
    
        public void setPlayingTeams(List<Teams> playingTeams) {
            this.playingTeams = playingTeams;
        }
    
        public void addTeam(String name, TeamColor color, int maxPlayers, Location location) {
            Teams theTeam = new Teams(name, color, maxPlayers, location);
            this.teams.put(name, theTeam);
        }
    
        public Scoreboard getScoreboard() {
            return scoreboard;
        }
    
        public void setScoreboard(Scoreboard scoreboard) {
            this.scoreboard = scoreboard;
        }
    
        public long getTime() {
            return time;
        }
    
        public void setTime(long time) {
            this.time = time;
        }
    
        public Teams getTeam(String name) {
            return this.teams.get(name);
        }
    
        public void setTeam(HashMap<String, Teams> teams) {
            this.teams = teams;
        }
    
        public int getMaxPlayers() {
            int max = 0;
            for (Teams t : this.teams.values())
                max += t.getMaxPlayers();
            return max;
        }
    
        public int getPlayerAmount() {
            return getPlayers().size();
        }
    
        public boolean isFull() {
            return (getMaxPlayers() <= getPlayerAmount());
        }
    
    
    
        public void SaveGame() {
            Utils.Arena.set(name+".name", name);
            Utils.Arena.set(name+".minPlayer", getMinPlayer());
            Utils.Arena.set(name+".maxPlayer", getMaxPlayer());
            Utils.Arena.set(name+".state", getState().name());
            Utils.Arena.set(name+".time", getTime());
            Utils.Arena.set(name+".power", getPower().name());
            Utils.Arena.set(name+".world", getWorld().getName());
            Utils.Arena.createSection(name+".team", this.teams);
    
           //Location min
            Utils.Arena.set(name+".min", getMin().serialize());
    
            //Location max
            Utils.Arena.set(name+".max", getMax().serialize());
    
            //Location Lobby
            Utils.Arena.set(name+".lobby", getLobby().serialize());
    
            try {
                Utils.Arena.save(Utils.File_Arena);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    Code:
    public class Teams implements ConfigurationSerializable {
    
        private TeamColor color;
        private String name;
        private int maxPlayers;
        private Location spawnLocation;
    
        public Teams(String name, TeamColor color, int maxPlayers, Location location) {
                setName(name);
                setColor(color);
                setMaxPlayers(maxPlayers);
                setSpawnLocation(location);
        }
    
    
        public TeamColor getColor() {
            return color;
        }
    
        public void setColor(TeamColor color) {
            this.color = color;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getMaxPlayers() {
            return maxPlayers;
        }
    
        public void setMaxPlayers(int maxPlayers) {
            this.maxPlayers = maxPlayers;
        }
    
        public Location getSpawnLocation() {
            return spawnLocation;
        }
    
        public void setSpawnLocation(Location spawnLocation) {
            this.spawnLocation = spawnLocation;
        }
    
        @Override
        public Map<String, Object> serialize() {
            HashMap<String, Object> team = new HashMap<>();
            team.put("name", getName());
            team.put("color", getColor().toString());
            team.put("max_players", Integer.valueOf(getMaxPlayers()));
            team.put("spawn", getSpawnLocation().serialize());
            return team;
        }
     

    Attached Files:

  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    Invokers

    The error was in loading the config back, but I fixed the error

    Code:
    ConfigurationSerialization.registerClass(Teams.class);
     
  10. Online

    timtower Administrator Administrator Moderator

    Then please provide your errors next time.
    Makes it very difficult to help without them.
     
Thread Status:
Not open for further replies.

Share This Page