Saving Inventory ArrayList

Discussion in 'Plugin Development' started by Calebizzthaman, Mar 30, 2020.

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

    Calebizzthaman

    I'm writing a random loot chest plugin. The plugin will spawn loot inside of chests based on what's in the config file. I can set a chest to spawn loot. I'm setting the inventory in an ArrayList. The only problem is when the server reloads or restarts the ArrayList is empty. How can I keep the inventories of the chests I set as LootChests in the ArrayList when reloading? When I try saving the list to the config it doesn't seem to work when trying to save Inventories. Any way around this?
     
  2. Offline

    KarimAKL

  3. Offline

    Calebizzthaman

    Code:
     public String prefix = ChatColor.RED + "[" + ChatColor.GOLD + "Chesty" + ChatColor.RED + "] ";
     
      public ArrayList<Player> chestymode = new ArrayList<>();
     
      public ArrayList<Inventory> lootchests = new ArrayList<>();
      String chests = lootchests.toString();
     
      public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("chesty") &&
          sender instanceof Player)
          if (sender.isOp()) {
            if (!this.chestymode.contains(sender)) {
              this.chestymode.add((Player)sender);
              sender.sendMessage(prefix + ChatColor.GOLD + "Chesty mode " + ChatColor.RED + ChatColor.BOLD + "enabled");
            } else {
              this.chestymode.remove(sender);
              sender.sendMessage(prefix + ChatColor.GOLD + "Chesty mode " + ChatColor.RED + ChatColor.BOLD + "disabled");
            }
          } else {
            sender.sendMessage(prefix + " You do not have access to do this!");
          } 
        return true;
      }
     
      @EventHandler
      public void onPlayerSetLootChest(InventoryOpenEvent e) {
        Player player = (Player)e.getPlayer();
        Inventory chest = e.getInventory();
        if(!(lootchests.contains(chest))) {
            if (chestymode.contains(player)) {
                if(player.isSneaking()) {
                    if(e.getInventory().getType() == InventoryType.CHEST) {
                        e.setCancelled(true);
                        player.sendMessage(prefix + ChatColor.DARK_GREEN + "Loot Chest created");
                        lootchests.add(chest);
                        getConfig().set("chests", chests);
                    }
                }
            }
           
        }
      }
     
      @EventHandler
      public void onPlayerOpenChest(InventoryOpenEvent e) {
        Inventory inventory = e.getInventory();
       
        Player player = (Player)e.getPlayer();
        List<String> configItems = getConfig().getStringList("Items");
       
        int index = new Random().nextInt(configItems.size());
        String items = configItems.get(index);
       
        ItemStack newItem = new ItemStack(Material.getMaterial(items.toUpperCase()));
       
        if(!(chestymode.contains(player))) {
            if(lootchests.contains(inventory)) {
                inventory.addItem(newItem);
                lootchests.remove(inventory);
                (new BukkitRunnable() {
                    public void run() {
                        inventory.clear();
                        lootchests.add(inventory);
                    }
                }).runTaskLater(Bukkit.getPluginManager().getPlugin("Chesty"), 1200);
           
       
      }
     
  4. Offline

    KarimAKL

    @Calebizzthaman You should save each ItemStack in the inventory individually, you should almost never save the result of toString().
     
Thread Status:
Not open for further replies.

Share This Page