Searching an object in HashSet (Arena)

Discussion in 'Spigot Plugin Development' started by KCVault, Aug 13, 2019.

  1. So I want to get an Arena that i've on an ArrayList:
    Code (Java):
    public Arena getArena(String key) {
            return this.arenas.stream().filter(arena -> arena.getName().equals(key)).findFirst().orElse(null);
        }
    On the command's class, I've this:
    Code (Java):
    if(args[0].equalsIgnoreCase("setspawn1")) {
                     
                        String arenaName = args[1];
         
                        Arena arenaloc = arenaManager.getArena(arenaName);
                     
                        if (arenaManager.checkIfArenaExists(arenaloc) == true) {
                            arena.setPos1(player.getLocation(), arenaloc.getGroup(), arenaloc.getName());
                            player.sendMessage(ChatColor.GREEN + "Spawn " + ChatColor.AQUA + "#1" + ChatColor.GREEN + " of arena "
                                    + ChatColor.AQUA + arenaName + ChatColor.GREEN + " has been set!");
                        }
                         
                        else {
                            player.sendMessage(adminprefix + ChatColor.RED + "This arena doesn't exist!");
                        }
                    }
    This code doesn't work. The error says that arenaloc is null (and yes, I've debuged it and it sends the correct arena name)

    Any ideas getting an arena?

    Thanks!
     
  2. try using equalsIgnoreCase instead of equals
     
  3. Code (Text):
    > [01:29:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sf' in plugin d v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_212]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_212]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    Caused by: java.lang.NullPointerException
    at kcvault.ArenaClasses.ArenaManager.checkIfArenaExists(ArenaManager.java:54) ~[?:?]
    at kcvault.Cmd.onCommand(Cmd.java:58) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-start-1.8.jar:git-Spigot-db6de12-d3e0b6f]
    ... 15 more
    Code (Java):
    if (arenaManager.checkIfArenaExists(arenaloc) == true) { //line 58
    The problem is arenaloc. I've debuged checkIfArenaExists and it works.
     
  4. Yeah, right, sorry. Make sure the arenas list in ArenaManager isn’t empty or null.
     
  5. Exactly the same.

    I've printed the list of arenas and the arena that I'm targeting exists.

    I can't understand this.
     
  6. Can you post the method checkIfArenaExists ?
     
  7. Code (Java):
    public boolean checkIfArenaExists(Arena arena) {
         if(plugin.getArenaConfig().getConfigurationSection("arenas."+arena.getGroup()+"."+arena.getName()) == null) {
              return false;
        }

         else return true;
    }
     
  8. You could shorten that to
    return configurationSection!=null

    Where are you defining arenaManager in your command class?
     
  9. Here:

    Code (Java):
    private ArenaManager arenaManager = new ArenaManager();
    It is between the first method and the public class Cmd...
     
  10. Bumpd?

    {need mroe chars}
     
  11. How/where are you creating arenas?
     
  12. Well, I use 2 classes. Arena and ArenaManager:
    Code (Java):
    package kcvault.ArenaClasses;

    import java.util.HashMap;
    import java.util.HashSet;

    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;

    import kcvault.Main;

    public class Arena {

        private Main plugin = Main.plugin;
     
        private final String name;
        private final String group;
        private Location pos1, pos2;
        private boolean gamestarted;
        private HashSet<Player> inQueuePlayers;
        private HashSet<Player> inGamePlayers;
        private HashMap<Player, Integer> numberOfKicks;
     
        private ConfigurationSection config = plugin.getArenaConfig();

        public Arena(String groupofarena, String name, Location location1, Location location2) {
            this.group = groupofarena;
            this.name = name;
            this.pos1 = location1;
            this.pos2 = location2;
            this.inQueuePlayers = new HashSet<>();
            this.inGamePlayers = new HashSet<>();
            this.numberOfKicks = new HashMap<>();
        }

        public String getGroup() {
            return this.group;
        }
     
        public String getName() {
            return this.name;
        }
     
        public Location getPos1() {
            return this.pos1;
        }
     
        public Location getPos2() {
            return this.pos2;
        }
     
        public void setPos1(Location loc, String groupName, String arenaName) {
            this.pos1 = loc;
            this.config.set("arenas."+groupName+"."+arenaName+".location.pos1", loc);
            plugin.saveArenaConfig();
        }
     
        public void setPos2(Location loc, String groupName, String arenaName) {
            this.pos2 = loc;
            this.config.set("arenas."+groupName+"."+arenaName+".location.pos2", loc);
            plugin.saveArenaConfig();
        }
     
        public boolean isGameStarted() {
            if(this.gamestarted == true) {
                return true;
            } else {
                return false;
            }
        }
     
        public void setGameStarted(boolean bool) {
            this.gamestarted = bool;
        }
     
        public int getNumberOfKicks(Player player) {
            return this.numberOfKicks.get(player);
        }
     
        public void setNumberOfKicks(Player player, int number) {
            this.numberOfKicks.put(player, number);
        }
     
        public HashSet<Player> getQueuedPlayers() {
            return this.inQueuePlayers;
        }
     
        public void addQueuedPlayers(Player player) {
            this.inQueuePlayers.add(player);
        }
     
        public void removeQueuedPlayers(Player player) {
            this.inQueuePlayers.remove(player);
        }
     
        public HashSet<Player> getPlayersInGame() {
            return this.inGamePlayers;
        }
     
        public void addPlayersIntoGame(Player player) {
            this.inGamePlayers.add(player);
        }
     
    }

    Code (Java):
    package kcvault.ArenaClasses;

    import java.util.HashSet;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    /*import java.util.Collections;
    import java.util.HashSet;
    import java.util.Set;

    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;*/

    import org.bukkit.configuration.file.FileConfiguration;

    import kcvault.Main;

    public class ArenaManager {

        private Main plugin = Main.plugin;
        private final FileConfiguration config = plugin.getArenaConfig();
        public HashSet<Arena> arenas = new HashSet<>();
        public void deserialise() {
            ConfigurationSection configSection = config.getConfigurationSection("arenas");
            if(configSection == null) return;
            for(String group : configSection.getKeys(false)){
                for(String arName : config.getConfigurationSection("arenas."+group).getKeys(false)) {
                    arenas.add(new Arena(group, arName, (Location) config.get("arenas." + group + "." + arName + ".location.pos1"),
                            (Location) config.get("arenas." + group + "." + arName + ".location.pos2")));
                }
            }
         
            for(Arena s : arenas) {
                Bukkit.getLogger().info(s.getName());
            }
         
        }

        public Arena getArena(String key) {
            return this.arenas.stream().filter(arena -> arena.getName().equalsIgnoreCase(key)).findFirst().orElse(null);
        }
     
        public boolean checkIfArenaExists(Arena arena) {
            if(this.config.getConfigurationSection("arenas."+arena.getGroup()+"."+arena.getName()) == null) {
                return false;
            }
         
            else return true;
         
        }
     
        public void addArena(Arena arena) {
            this.arenas.add(arena);
            this.config.set("arenas."+arena.getGroup()+"."+arena.getName()+".location.pos1", arena.getPos1());
            this.config.set("arenas."+arena.getGroup()+"."+arena.getName()+".location.pos2", arena.getPos2());
            plugin.saveArenaConfig();
        }  

        public void removeArena(Arena arena) {
            this.arenas.remove(arena);
            this.config.set("arenas."+arena.getGroup()+"."+arena.getName(), null);
            this.config.set("arenas."+arena.getGroup()+"."+arena.getName(), null);
        }
    }

    Arena class create de arena and the data (like players, locations, etc).
    ArenaManager class some other features (serialization (or it should do this), list of arenas, etc).

    I've tried with serialization and deseralization moving the information from config to a HashSet, but it doesn't work and I do not remember what exactly method was used.
     
  13. So.... bumped?

    (need mroe chars)