Solved Runnable Breaks After Server Restart or Plugin Reload

Discussion in 'Spigot Plugin Development' started by xUbalubex, Feb 18, 2020.

  1. I am doing a runnable to check if the player has armor on and I get no errors until I restart the server or do my reload command.

    Here is my code:

    This is when the plugin is enabled. startBackpackCheck() is where the loop is.
    Code (Java):
    BackpackStorageFile file = BackpackStorageFile.getInstance();
        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            file.setup(this);
           
            /*if(file.getData().get("backpacks") == null)
            {
                HashMap<ItemStack, Integer> allBackpackItems = new HashMap<ItemStack, Integer>();
                file.getData().set("backpacks", allBackpackItems);
                file.saveData();
            }*/


           
            startBackpackCheck();
           
           
            super.onEnable();
        }
    The startBackpackCheck() method looks like this:

    Code (Java):
    public void startBackpackCheck()
        {
            Bukkit.getScheduler().cancelTasks(this);
            BukkitScheduler scheduler2 = getServer().getScheduler();
            scheduler2.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    if(file.getData().get("backpacks") != null)
                    {
                        for(Player p : Bukkit.getOnlinePlayers())
                        {
                           
                            int pocketSpace = 0;
                           
                           
                           
                            HashMap<ItemStack, Integer> allBackpackItems = (HashMap<ItemStack, Integer>) file.getData().get("backpacks");
                           
                            if(allBackpackItems.containsKey(p.getInventory().getItemInOffHand()))
                            {
                                pocketSpace += allBackpackItems.get(p.getInventory().getItemInOffHand());
                            }
                           
                            ItemStack restrictedItem = new ItemStack(Material.getMaterial(getConfig().getString("RestrictedInventoryItem")));
                            ItemMeta meta = restrictedItem.getItemMeta();
                            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("RestrictedInventoryItemName")));
                            meta.addEnchant(Enchantment.VANISHING_CURSE, 1, true);
                            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                            restrictedItem.setItemMeta(meta);
                            for(int i = 9; i < 35; i++)
                            {
                                if(i > 35 - pocketSpace)
                                {
                                    if(p.getInventory().getItem(i) != null && p.getInventory().getItem(i).equals(restrictedItem))
                                    {
                                        p.getInventory().setItem(i, new ItemStack(Material.AIR));
                                    }
                                }
                                else
                                {
                                    if(p.getInventory().getItem(i) != null && p.getInventory().getItem(i).getType() != Material.getMaterial(getConfig().getString("RestrictedInventoryItem")))
                                    {
                                        Item itemDropped = p.getWorld().dropItemNaturally(p.getLocation(), p.getInventory().getItem(i));
                                        p.getInventory().clear(i);
                                        itemDropped.setPickupDelay(40);
                                    }
                                    p.getInventory().setItem(i, restrictedItem);
                                }
                            }
                        }
                    }
                }
            }, 0L, getConfig().getInt("BackpackCheckTime"));
        }
    The specific line of code that is breaking is this line here:
    Code (Java):
    HashMap<ItemStack, Integer> allBackpackItems = (HashMap<ItemStack, Integer>) file.getData().get("backpacks");
    The error reads:
    Code (Java):
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    [12:11:50] [Server thread/WARN]: [Craftpacks] Task #11 for Craftpacks v1.0 generated an exception
    java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.HashMap
            at com.xboxsignsout.craftpacks.Main$1.run(Main.java:62) ~[?:?]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftTask.run(CraftTask.java:81) ~[spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:394) [spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1008) [spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:396) [spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:956) [spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:801) [spigot-1.12.2.jar:git-Spigot-093165d-ba575a5]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    This line of code seems to break everytime I restart. I also have a command to add more backpacks and it uses this exact line of code. It will add code correctly error-less until I restart then everytime I try to add more backpacks it will send an error. Help would be greatly appreciated!
     
  2. Well it says right there
    Code (Text):
    java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.HashMap
    It can't properly cast the hashmap,try using some other method or manually do it
    This looks rly strange for me
    Code (Text):
    (HashMap<ItemStack, Integer>) file.getData().get("backpacks");
    Or might also be cause by improper file loading,try delaying this action by certain ticks
     
  3. Ah so apparently it would loop every 1. There are 20 ticks in a second but it was 1. I tried changing it to 20 but this is still happening.
     
  4. it tells you. you're trying to convert a memorysection (and instance of configsection) object into a hashmap. you cant do this. change the object type to a configsection.

    /e since you probably dont know what youre doing, youre going against the conventions of yaml. you should be using strings as keys, not itemstacks with integers. yaml storage isnt meant for this. rethink how you're using yaml and structure it differently.
     
  5. electronicboy

    IRC Staff

    When you store a hashmap in a configuration, it will be converted into a ConfigurationSection on loading, unless you're actually doing stuff like nested maps, e.g. what's returned by getMapList iirc it was called; Stick to storing configuration stuff in the config instead of raw collections
     
  6. So I ended up doing that and it works perfectly!