Solved Restoring Inventories on Player Quit

Discussion in 'Spigot Plugin Development' started by ACrispyCookie, Feb 10, 2020.

  1. I am trying to make a staff mode plugin which will save inventories when a staff member enters it and will restore them when it exits it. When a player toggles it with the command it works fine. Now when a player quits it should also disable their staff mode and therefore return their inventory but it doesn't happen.

    Code (Java):
        @EventHandler
        public void onLeave(PlayerQuitEvent e) {
            ModCommand command = new ModCommand();
            if(ModCommand.getPlayersList().contains(e.getPlayer())) {
                command.toggleStaffMode(e.getPlayer());
            }
        }

    Code (Java):
        public void toggleStaffMode(Player p) {
            if(players.contains(p)) {
                p.getInventory().setContents(inventories.get(p));
                p.getInventory().setArmorContents(armorcontents.get(p));
                p.teleport(locations.get(p));
                players.remove(players.indexOf(p));
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', " &5&l» &fYou have been teleported back to your original location with your original inventory!"));
            }
            else {
                players.add(p);
                ItemStack[] items = p.getInventory().getContents();
                ItemStack[] armor = p.getInventory().getArmorContents();
                ItemStack[] emptyarmor = {new ItemStack(Material.AIR, 1), new ItemStack(Material.AIR, 1), new ItemStack(Material.AIR, 1), new ItemStack(Material.AIR, 1)};
                inventories.put(p, items);
                armorcontents.put(p, armor);
                locations.put(p, p.getLocation());
                p.getInventory().setArmorContents(emptyarmor);
                p.getInventory().clear();
                p.getInventory().setItem(0, createItem(Material.DIAMOND_AXE, "&dPunish a player", 0));
                p.getInventory().setItem(1, createItem(Material.CHEST, "&dExamine a player", 0));
                p.getInventory().setItem(6, createItem(Material.INK_SACK, "&dChat Manager", 5));
                p.getInventory().setItem(7, createItem(Material.INK_SACK, "&dVanish yourself", 10));
                p.getInventory().setItem(8, createItem(Material.NETHER_STAR, "&dOnline staff", 0));
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', " &5&l» &fYou have entered the staff mode!"));
            }
        }

    [15:56:41 ERROR]: Could not pass event PlayerQuitEvent to ColonyStaffMode v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:517) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:502) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerList.disconnect(PlayerList.java:364) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:875) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.NetworkManager.l(NetworkManager.java:314) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:145) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:875) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:616) [server.jar:git-PaperSpigot-"4c7641d"]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.setContents(CraftInventory.java:63) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at org.colonymc.colonystaffmode.commands.ModCommand.toggleStaffMode(ModCommand.java:61) ~[?:?]
    at org.colonymc.colonystaffmode.listeners.ModListener.onLeave(ModListener.java:32) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[server.jar:git-PaperSpigot-"4c7641d"]
    ... 13 more
     
  2. Is this line
    Code (Java):
     p.sendMessage(ChatColor.translateAlternateColorCodes('&', " &5&l» &fYou have entered the staff mode!"));
    The one that gives the error?

    Probably it is that you are sending messages and modifying the inventory of an OfflinePlayer. Try restoring the inventory when they reconnect
     
  3. This is the line that gives the error
    Code (Java):
    p.getInventory().setContents(inventories.get(p));
    I also try to restore the inventories of all the players when the plugin disables but I get a similar error:
    [17:44:16 ERROR]: Error occurred while disabling ColonyStaffMode v1.0 (Is it up to date?)
    java.lang.NullPointerException
    at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.setContents(CraftInventory.java:63) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at org.colonymc.colonystaffmode.commands.ModCommand.toggleStaffMode(ModCommand.java:61) ~[?:?]
    at org.colonymc.colonystaffmode.Main.onDisable(Main.java:32) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) ~[server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:360) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:427) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:420) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:473) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:735) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.Bukkit.reload(Bukkit.java:556) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:146) [server.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:666) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1189) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1001) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-PaperSpigot-"4c7641d"]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:774) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [server.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:616) [server.jar:git-PaperSpigot-"4c7641d"]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
     
  4. You are trying to modify an OfflinePlayer but the offline player doesn't have an inventory
    You'll have to restore it when the player enters again
     
    • Agree Agree x 1
  5. It turns out that it was Quit event that couldn't access the "inventories" HashMap and it wasn't because I was trying to set the inventory of an OfflinePlayer. But thank you anyways.