Solved Inv from file not working - must be multiple of 9

Discussion in 'Plugin Development' started by i3ick, Aug 5, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    i3ick

    I'm getting a "Chests must have a size that is a multiple of 9!" but how can that be? I .getSize() which is 41 so why can't I recreate it?. This code used to work in previous versions. Original code from https://bukkit.org/threads/util-saving-getting-inventories-from-a-file.164145/


    Code:
        public Inventory getInventoryFromFile(File file, Player player) {
            if (file == null) return null;
            if (!file.exists() || file.isDirectory() || !file.getAbsolutePath().endsWith(".invsave")) return null;
            try {
                FileConfiguration invConfig = YamlConfiguration.loadConfiguration(file);
                Inventory inventory = null;
                String invTitle = invConfig.getString("Title", "Inventory");
                String gm = (String) invConfig.get("GameMode");
                int invSize = invConfig.getInt("Size");
                int invMaxStackSize = invConfig.getInt("Max stack size", 64);
                InventoryHolder invHolder = null;
                if (invConfig.contains("Holder")) invHolder = Bukkit.getPlayer(invConfig.getString("Holder"));
                inventory = Bukkit.getServer().createInventory(invHolder, invSize, ChatColor.translateAlternateColorCodes('&', invTitle));
                inventory.setMaxStackSize(invMaxStackSize);
                try{
                    System.out.println("entered try");
                    ItemStack[] invContents = new ItemStack[invSize];
                    for (int i = 0; i < invSize; i++) {
                        if (invConfig.contains("Slot " + i)) invContents[i] = invConfig.getItemStack("Slot " + i);
                        else invContents[i] = new ItemStack(Material.AIR);
                    }
                    player.getInventory().setContents(invContents);
                } catch (Exception ex) {
                    System.out.println("entered try" + ex);
                }
                this.getLogger().info("util");
                try{
                    System.out.println("entered try");
                    ItemStack[] invContents = new ItemStack[invSize];
                    for (int i = 0; i < invSize; i++) {
                        if (invConfig.contains("Armor " + i)) invContents[i] = invConfig.getItemStack("Armor " + i);
                        else invContents[i] = new ItemStack(Material.AIR);
                    }
                } catch (Exception ex) {
                    System.out.println("entered try" + ex);
                }
                this.getLogger().info("util");
                String gamemode = gm;
                switch (gamemode){
                    case "SURVIVAL":
                        player.setGameMode(GameMode.SURVIVAL);
                        break;
                    case "CREATIVE":
                        player.setGameMode(GameMode.CREATIVE);
                        break;
                    case "ADVENTURE":
                        player.setGameMode(GameMode.ADVENTURE);
                        break;
                }
                System.out.println("entered try");
                System.out.println(inventory);
                this.getLogger().info("" + inventory);
                this.getLogger().info("util");
    
                return inventory;
            } catch (Exception ex) {
                System.out.println("entered try" + ex);
                return null;
            }
        }
     
  2. 41 is not a multiple of 9. Print the variable invSize to make sure it's one of the following.

    Available sizes are: 9, 18, 27, 36, 45, 54.
    (any more (63, 72, etc.) and the slot rendering will mess up)
     
    Last edited: Aug 5, 2016
    i3ick likes this.
  3. @i3ick If you'r going to do bukkit, learn basic math.
     
  4. If you're writing something on the internet, learn basic english. :p (you're*)
     
  5. If you're writing something on the internet, learn basic grammar. (on not in) :p
     
  6. Offline

    i3ick

    @FisheyLP Hmm but when I do
    invConfig.set("Size", inventory.getSize()); it sets a size of 41 which is giving the error. That must mean that it counts the armors slots as well but when I create an inventory I am creating it without the armor slots?
    Interesting. Something must have changed from the previous versions in the way slots are counted
     
  7. You are probably saving the player's inventory (36 slots + 4x armor + 1x offhand = 41 slots) instead of the one you created.
     
Thread Status:
Not open for further replies.

Share This Page