Map keeps resetting ??? (HELP)

Discussion in 'Spigot Plugin Development' started by Drawethree, Jun 8, 2016.

  1. So im making a own minigame plugin and im stucked at one thing.. Everything in my minigame works fine, i added a multiarena team points support. At the start of arena game im putting to the hashmap arena ID, and startpoints.
    Code (Text):
                        redpoints.put(ArenaManager.getManager().getArena(i).getId(), 0);
                        bluepoints.put(ArenaManager.getManager().getArena(i).getId(), 0);
    When im adding a debug, it will clearly show it.
    The problem is when im using a playerinterractevent. If player is trying to add item to collector, it may add point for team, but it doesnt. It is saying that, the hashmap is null. Im a bit confused how to fix this.

    PlayerInterractEvent :
    Code (Text):
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (p.getInventory().contains(plugin.gulasik)) {
                            if (block.getType() == plugin.collector.getType()) {
                                if (ArenaManager.getManager().getTeam(p) == "RED") {
                                    p.getInventory().removeItem(plugin.gulasik);
                                    redpoints.put(ArenaManager.getManager().getArena(i).getId(), redpoints.get(ArenaManager.getManager().getArena(i).getId()) + 1);
                                } else if (ArenaManager.getManager().getTeam(p) == "BLUE") {
                                    p.getInventory().removeItem(plugin.gulasik);
                                    bluepoints.put(ArenaManager.getManager().getArena(i).getId(), redpoints.get(ArenaManager.getManager().getArena(i).getId()) + 1);
                                }
                                for (String name : ArenaManager.getManager().getArena(i).getPlayers()) {
                                    plugin.getServer().getPlayer(name)
                                            .sendMessage(plugin.PREFIX + ChatColor.GREEN + "Hrac " + ChatColor.YELLOW
                                                    + p.getName() + ChatColor.GREEN + " odovzdal GULASIK pre team "
                                                    + ArenaManager.getManager().getTeam(p));
                                    plugin.getServer().getPlayer(name).playSound(p.getLocation(),
                                            Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
                                    scoreboard(i);
                                }
                                if (redpoints.get(i) == plugin.getConfig().getInt("Arenas." + i + ".required_gulas")
                                        || bluepoints.get(i) == plugin.getConfig()
                                                .getInt("Arenas." + i + ".required_gulas")) {
                                    endGame(i);
                                }
                            }
                        }
                    }
     
  2. It would be nice if you showed the entire class so we can see how and when you are instantiating your Map.
     
  3. You should try making your collections static. Preferably private static since you have no reason to make them public static.

    You should also be setting "am" to the current instance in your constructor so plugin doesn't turn out to be null.
     
    • Like Like x 1
  4. @Shortninja66 thx, it worked, i forget to make them static :/
     
  5. Also the plugin is making one warn..
    When im calling a removePlayer method when the game ends.. It will some players remove and some will left in arena and warn will appear :
    WARN :
    Code (Text):
    [23:26:19 WARN]: [GulasGame] Task #906 for GulasGame v1.0 generated an exception
    java.util.ConcurrentModificationException
            at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.8.0_73]
            at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_73]
            at me.drawe.game.ArenaManager$2.run(ArenaManager.java:267) ~[?:?]
            at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:729) [spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot1.9.2.jar:git-Spigot-5a40365-b70058a]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    endGame method :
    Code (Text):
        public void endGame(final int i) {
            plugin.getServer().getScheduler().cancelTask(dropgulas);
            plugin.getServer().getScheduler().cancelTask(dropkorma);
            plugin.getServer().getScheduler().cancelTask(poweruptaskid);
            clearItems(i);
            fireworks(i);
            for (String name : ArenaManager.getManager().getArena(i).getPlayers()) {
                plugin.getServer().getPlayer(name).playSound(plugin.getServer().getPlayer(name).getLocation(),
                        Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
                plugin.getServer().getPlayer(name).sendMessage(
                        plugin.PREFIX + ChatColor.GREEN + "Team " + ArenaManager.getManager().getWinner(i) + " vyhral !");
                new TitleObject("§eTeam " + ArenaManager.getManager().getWinner(i) + " vyhral !", "§aDakujeme za hranie !")
                        .setFadeIn(20).setFadeOut(20).setStay(100).send(plugin.getServer().getPlayer(name));
                plugin.getServer().getPlayer(name).setScoreboard(manager.getNewScoreboard());
                plugin.getServer().getPlayer(name).getInventory().clear();
            }
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @Override
                public void run() {
                    for (String name : ArenaManager.getManager().getArena(i).getPlayers()) {
                        if (ArenaManager.getManager().isInGame(plugin.getServer().getPlayer(name))) {
                            ArenaManager.getManager().removePlayer(plugin.getServer().getPlayer(name), i);
                        }
                    }
                }
            }, 100);
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

                @Override
                public void run() {
                    for (String name : ArenaManager.getManager().getArena(i).getPlayers()) {
                        if (ArenaManager.getManager().isInGame(plugin.getServer().getPlayer(name))) {
                            ArenaManager.getManager().removePlayer(plugin.getServer().getPlayer(name), i);
                        }

                    }
                }
            }, 200);
        }
    removePlayer method :
    Code (Text):
        public void removePlayer(Player p, int i) {
            manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
            Arena a = null;// make an arena
            for (Arena arena : arenas) {
                if (arena.getPlayers().contains(p.getName())) {
                    a = arena;// if the arena has the player, the arena field would
                                // be the arena containing the player
                }
                // if none is found, the arena will be null
            }
            if (a == null || !a.getPlayers().contains(p.getName())) {// make sure it
                                                                        // is not
                                                                        // null
                p.sendMessage(plugin.PREFIX
                        + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("arena_not_in_game")));
                return;
            }

            removeFromTeam(p, i);
            p.setScoreboard(board);
            a.getPlayers().remove(p.getName());// remove from arena

            p.getInventory().clear();
            p.getInventory().setArmorContents(null);

            p.getInventory().setContents(inv.get(p.getName()));// restore inventory
            p.getInventory().setArmorContents(armor.get(p.getName()));
            p.teleport(locs.get(p.getName()));
            new TitleObject("§eDakujeme za hranie GulasGame !", "§cOpustil si arenu " + i).setStay(60).setFadeIn(20)
                    .setFadeOut(20).send(p);

            inv.remove(p.getName());// remove entries from hashmaps
            armor.remove(p.getName());
            locs.remove(p.getName());
            p.setFireTicks(0);
            for (String name : a.getPlayers()) {
                plugin.getServer().getPlayer(name)
                        .sendMessage(plugin.PREFIX + ChatColor.GREEN + "Hrac " + p.getName() + " sa odpojil z hry !"
                                + ChatColor.YELLOW + " (" + a.getPlayers().size() + "/"
                                + plugin.getConfig().getInt("Arenas." + a.getId() + ".max") + ")");
            }

            if (am.getManager().getArena(i).getPlayers().size() < plugin.getConfig().getInt("Arenas." + i + ".min")) {
                ArenaManager.getManager().cancelGame(i);
            }
        }
     
  6. You can't remove a variable in a List while iterating over it with a for-each loop. Use a for with index or an iterator instead.