1.12.2 why does saving still happen?

Discussion in 'Spigot Plugin Development' started by Ginger_14, Jan 12, 2020.

  1. Code (Text):
            if (1000 >= number) {
                for (int i = 0; i < 36; i++) {
                    ItemStack is = inv.getItem(i);
                    if (is == null || is.getType() == Material.AIR)
                        continue;
                    String slot = path + "Items." + i;
                    config.set(slot + ".Type", is.getType().toString());
                    config.set(slot + ".Amount", is.getAmount());
                    if (!is.hasItemMeta())
                        continue;
                    if (is.getItemMeta().hasDisplayName())
                        config.set(slot + ".Name", is.getItemMeta().getDisplayName());
                    if (is.getItemMeta().hasLore())
                        config.set(slot + ".Lore", is.getItemMeta().getLore());
                    if (is.getItemMeta().hasEnchants()) {
                        Map<Enchantment, Integer> enchants = is.getEnchantments();
                        List<String> enchantList = new ArrayList<String>();
                        for (Enchantment e : is.getEnchantments().keySet()) {
                            int level = enchants.get(e);
                            enchantList.add(e.getName() + ":" + level);
                        }
                        config.set(slot + ".Enchants", enchantList);
                        plugin.saveConfig();
                        p.sendMessage("Completed!");
                    }
                    continue;
                }
            }else if (1000 < number){
                p.sendMessage("Failed!");
                return;
            }
     
  2. Please, give more info about what do you wanna do and in what is a problem
     
    • Like Like x 1
  3. When a player enters a command, plugin compares the items in the inventory with their prices, if the sum of all items exceeds 1000, the software must stop working, if the amount is less than or equal to 1000, perform the saving of his inventory in the config
     
  4. You know that ItemStack ConfigurationSerializable is and you can simple use:
    Code (Java):
    config.set(slot, is);
    // and
    config.getItemStack(slot);
    // to get the ItemStack
    And how do you get the variable number? Have you try to print it out and see if it is the right number?
     
  5. 1) If you want to save anything by yourself you should either
    a) Implement ConfigurationSerializable (ItemStack already does that)
    b) Write a new class the serializes the object
    instead of stacking code in your command method.
    (My commands are generally around 10 lines. [ACF helps a lot ^^])

    2)
     
  6. The above suggestions for saving are good advice, but despite that, are you trying to stop saving after you have found just one item?
    You have "continue;" at the very end of your loop, this is unnecessary, the loop will continue on anyway. If you change that to "break;" it will stop looping once you have saved your first item and not continue searching the rest of the inventory.
    Is that what your problem was? It was saving more than one?
    Also your else if statement can simply be replaced by an else statement...