1.8.8 Chests being cleared after items set?

Discussion in 'Spigot Plugin Development' started by Embrasure, Feb 23, 2020.

  1. I've got this code.

    Code (Java):
    private static void setupChests(World world) {
            if (world.getName().contains("_active")) {
                String arenaName = world.getName().replace("_active", "");
                Location arenaCornerOne = new Location(world, Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner1.x"), Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner1.y"), Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner1.z"));
                Location arenaCornerTwo = new Location(world, Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner2.x"), Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner2.y"), Files.getArenasConfig().getDouble("arenas." + arenaName + ".arenacorner2.z"));
                int x = arenaCornerOne.getBlockX();
                int y = arenaCornerOne.getBlockY();
                int z = arenaCornerOne.getBlockZ();
                int maxx = arenaCornerTwo.getBlockX();
                int maxy = arenaCornerTwo.getBlockY();
                int maxz = arenaCornerTwo.getBlockZ();
                for (int x1 = x; x1 < maxx; x1++) {
                    for (int y1 = y; y1 < maxy; y1++) {
                        for (int z1 = z; z1 < maxz; z1++) {
                            Location loc = new Location(world, x1, y1, x1);
                            if (loc.getBlock().getType() == Material.CHEST) {
                                fillChestTest(loc, false);
                            }
                        }
                    }
                }
        }
        private static void fillChestTest(Location loc, boolean middle) {
            Chest chest = (Chest) loc.getBlock().getState();
            chest.getInventory().setItem(0, new ItemBuilder(Material.DIAMOND).build());
            chest.update();
            Bukkit.getConsoleSender().sendMessage(chest.getInventory().getItem(0).getType().toString());
        }
    It gets the world of an arena, loops through 2 locations (the arena corners) and fills the chests. The Bukkit.getConsoleSender().sendMessage() prints out "DIAMOND", as expected, but when I enter a game and open a chest, the chest isn't filled and, I assume, has been cleared. There's no code anywhere in that class that uses Inventory#clear(), though. Chests are filled in Arena worlds on server start, so this happens to all chests within all arenas.
     
  2. Try add item instead of set item! hopefully that helps =) also try setting state after you add item, as i believe the inventory is only saved in memory until you set it to the updated version
     
    • Optimistic Optimistic x 1
  3. addItem didn’t work, and how do I set the state?
     
  4. Are you sure that the particular chest you checked in game was registered by the loop? (print out the locations of all chests which were filled and check them)
     
  5. I’m definitely sure it’s being handled by the loop.
     
  6. Not sure if this is applicable for mc 1.8.8 but I remember an issue similar to this was resolved using Chest#getBlockInventoty(). Just a vague guess thou
     
  7. This isn't working for me sadly.
     
  8. Is there any errors in console? you 100% sure that the chests are being registered?