Hey Guys, Im trying to create a Multiple Arena System API, I know how to write it but the only thing I dont know is how can I create ArrayLists with the Players for each arena?
I would create an object with the name 'Arena' and store a List<UUID> in it to identify the players that are in the arena. Code (Text): public class Arena { private final List<UUID> players = new ArrayList<UUID>(); public List<Player> getPlayers() { List<Player> convertedPlayers = new ArrayList<Player>(); for(UUID uniqueId : players) { Player player = Bukkit.getServer().getPlayer(uniqueId); if((player != null) && player.isOnline()) { convertedPlayers.add(player); } } return convertedPlayers; } public void addPlayer(Player player) { if(!inArena(player)) { players.add(player.getUniqueId()); } } public void removePlayer(Player player) { if(inArena(player)) { players.remove(player.getUniqueId()); } } public boolean inArena(Player player) { return players.contains(player.getUniqueId()); } }
^ Like he said. Create a class, preferably called Arena, something easy. Then inside this Arena class you can store the Arena Name/ID, and other information, INCLUDING the lists of players. Here's an example: PHP: public class Arena { private List<String> players; // our players variable public Arena() { // constructor players = new ArrayList<String>(); // set the list to a new arraylist } public List<String> getPlayers() { // method to get players return this.players; // return players list } } Hopefully you can add the other "Add player" and "Remove player" methods yourself. Hope I helped
Thank you now my Problem is I get a NullPointerExeption and I dont Know why? Exeption: Line: 54 Code (Text): public void addPlayer(Player p, String name){ Arena a = getArena(name); Thats line 54 --> a.getPlayers().add(p.getName()); } the Variable (getArena...) etc works but I cant add a player... here my Arena class: Code (Text): public class Arena { String n; List<String> players = new ArrayList<String>(); public Arena(String name){ this.n = name; } public List<String> getPlayers(){ return this.players; } public int getPlayersSize(){ return this.players.size(); }
Sorry my bad, I forgot to load the games in the Enable method ... -.- EDIT: Problem is back ... Arena Method: Code (Text): List<Arena> arenas = new ArrayList<Arena>(); public Arena getArena(String name){ for(Arena n : arenas){ if(n.getArena() == name){ return n; } } return null; } create Method: Code (Text): public Arena createArena(String pathname, String name, Player p){ File file = new File(pathname, "arenas.yml"); FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); List<String> list = cfg.getStringList("Arenen.Arenen"); Arena a = new Arena(name, prefix, lobbytime, gametime, schutzzeit, maxplayers, false); arenas.add(a); list.add(name); cfg.set("Arenen.Arenen", list); saveCfg(cfg, file); return a; } addPlayer Method Code (Text): public void addPlayer(Player p, String name){ Arena a = getArena(name); a.getPlayers().remove(p.getName()); } NullPointerExeption: I can create an arena it works perfekt, but if I try too add players it dont work.
I think I know why it dont work... but I dont know how to fix it: Code (Text): public Arena getArena(String name){ for(Arena n : arenas){ for(int i : n.toString()) if(n.getArena() == name){ return n; } } return null; } I need a int for that method but I want a string now is my question how can I find an arena in the List<Arena> with a String? Because it save it as int
Save a name variable in your Arena class and make a getter for it, after that you can loop through all Arena objects in the List<Arena> and compare the output from arena#getName(); with the given name in the parameters.
@nflug Why a List instead of a Map? Why does your toString() method return an int iterable (array or collection)? Strings should be compared using equals rather than ==, as they are not primitives and likely don't have the same memory address (== checks if two objects refer to the same part in memory) getArena(String) returns null if the Arena does not exist. It does that in the last line. Thus, why don't you check if it's null before trying to add players? Your fields in the Arena class should be private and final whenever possible. Only if they should be accessible in subclasses or the same package, consider package-private (no access modifier) or protected. Your getter shouldn't expose the List (pass the reference directly). It should pass an unmodifiable one. ... and you should create addPlayer/removePlayer methods for modifying the list instead. (Your last post made me doubt whether you actually know enough Java to continue with the Spigot API - consider spending more time with Java and object oriented Java before continuing with the API)
Next Problem: I have 3 Players and one Countdown of 10 now the first player see only 1 then 3 then 5 the second 2, 4, 6 and the last 3, 5 , 7 Why? Code (Text): lobby = new BukkitRunnable() { File file = new File(plugin.getDataFolder().getPath(), "arenas.yml"); FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); @SuppressWarnings("deprecation") @Override public void run() { for(Player all : mg.getArena(id).getPlayers()) { if(getTime(id, "Lobby") != 0 ) { int lobbytime = getTime(id, "Lobby"); String time = "§7Start in §a"+getTime(id, "Lobby"); ActionBarAPI.sendActionBar(all, time); lobbytime--; setTime(id, "Lobby", lobbytime); } else { etc.... } } } }.runTaskTimer(plugin, 20, 15); [code]