1.8.8 Give items from Config

Discussion in 'Spigot Plugin Development' started by swissrust, Jul 16, 2021.

  1. I am trying to save an inventory to go in a "Staff Mode" and re-give the inventory after they exit the staff mode.

    This is my code for saving the inventory, which works.

    for (int i=0; i<player.getInventory().getSize(); i++) {
    if (player.getInventory().getItem(i) != null) {
    Bukkit.getPluginManager().getPlugin("StaffMode").getConfig().set("inventory."+player.getUniqueId().toString() + ".items."+i, player.getInventory().getItem(i));
    Bukkit.getPluginManager().getPlugin("StaffMode").saveConfig();
    }
    }

    But, my loading inventory doesn't. No error.

    for (int i=0; i<player.getInventory().getSize(); i++) {

    if(Bukkit.getPluginManager().getPlugin("StaffMode").getConfig().getItemStack("inventory."+player.getUniqueId().toString()+".items."+i) != null) {
    player.getInventory().addItem(Bukkit.getPluginManager().getPlugin("StaffMode").getConfig().getItemStack("inventory."+player.getUniqueId().toString()+".items."+i));
    }
    }
     
  2. Why don't you simply store the player's inventory in a variable instead of using a file? Only if you don't need to keep the inventory after a server restart, I mean
     
  3. I do need to keep the inventory after restart thats the thing...
     
  4. Is your plugin name StaffMode?
     
  5. Why are you using the Bukkit API to retrieve your own config? This isn’t related to your issue this is just not optimized.

    if there are no error then print your if statement to see if it is null or not. If no error then the code doesn’t pass the if statement meaning that it’s probably null.
     
  6. Yes
     
  7. ok I will, thanks.
     
  8. Then why use Bukkit.getPluginManager().getPlugin("StaffMode")?

    Just make instance.

    private static Main instance;

    public static Main getInstance() {
    return instance;

    You need add onEnable() {
    instance = this;
    }

    Main.getInstance().getConfig(). use this code..
     
  9. It doesn't work half the time when it comes to creating the file.
     
  10. This works. Learn Java first.
     
  11. Can you help me or not?
     
  12. So if you try to load your inventory again it does not work. Am I right?
     
  13. You can convert the inventory to an base64 cryped string and save this one in a file. when you need the inventory (e.g. when you're joining the server) decrypt it.
    This video (sorry i could only find a german one) shows how to convert inventories
     
  14. You could save players inventories using these methods I used long ago.

    If you don't want to use base64 I suggest this.
    Code (Java):
        public static String serializeItemStack(ItemStack itemStack) {
            YamlConfiguration config = new YamlConfiguration();
            config.set("i", itemStack);
            return config.saveToString();
        }

        public static ItemStack deserializeItemStack(String s) {
            YamlConfiguration config = new YamlConfiguration();
            try {
                config.loadFromString(s);
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }
            return config.getItemStack("i");
        }
     
  15. Isn' there a method called
    Code (Text):
    item.serialize()
    And
    Code (Java):
    ItemStack.deserialize()
    //I dont remember the param but your IDE should show that
     
  16. Code (Java):
    Map<UUID, ItemStack[]> equipment = new HashMap<>();
    Map<UUID, ItemStack[]> armor = new HashMap<>();
    equipment.put(p.getUniqueId(), p.getInventory.getContents());
    armor.put(p.getUniqueId(), p.getInventory.getArmorContents());

    p.getInventory().setContents(equipment.get(p.getUniqueId()));
    p.getInventory().setArmorContents(armor.get(p.getUniqueId()));
     
  17. Yes, that‘s exactly what YamlConfiguration#set and YamlConfiguration#getItemStack is using
     
  18. Ahh, sorry then