[SOLVED] Scan all chest in world.

Discussion in 'Spigot Plugin Development' started by ProxyBite, May 31, 2015.

  1. Hello,
    I need scanner to find all the chests in the world and when you find one or more keep them in a YML and after obtaining the location of the YML and fill chest.

    Sorry my english bad.
     
  2. You would want to check chunks as they are loaded with a ChunkLoadEvent for chests and store their locations in your yml. Next, you would want to listen to the BlockPlaceEvent and BlockBreakEvent for new chests and removed chests. Storing and removing chests by location.
     
    • Agree Agree x 1
  3. Example?
     
  4. When start game fill all chest in world, I use with loadedChunks on world
    but I need to have loaded the chunks which some chest are not filled correctly
     
  5. Why don't you make it so that when someone opens the chest it fills it? Reduces lag to a minimum :)
     
  6. Because what would make is that every time you open it fills and is not what I want , just a filling while starting the game and ready
     
  7. Well add the open chest location to an arraylist, and don't allow chests to be renewed once opened again.
     
    • Agree Agree x 1
  8. Okay I try now.
     
  9. Load all the world chunks, and iterate through the tile entities in each chunk that are instanceof Chest or DoubleChest
     
  10. Yeah i use this method
    Code (Text):
      public static void fill()
      {
        World w = Bukkit.getWorld(TSW.getInstance().getConfig().getString("mapa"));
        for (Chunk c : w.getLoadedChunks()) {
          for (BlockState block : c.getTileEntities()) {
            if ((block instanceof Chest))
            {
              Chest chest = (Chest)block;
              TSW.getManager().fill(chest);
            }
          }
        }
      }
     
    but some chest are not filled
     
  11. Possibly because those chests are in nonloaded chunks
     
    • Like Like x 1
  12. Yeah any method for load all chunks with command ?
     
    • Useful Useful x 1
  13. HashSet > ArrayList, you don't want to iterate over a lot of chests and compare each one (which is what List #contains does)
     
    • Agree Agree x 1
  14. Thanks, I solved this
    My code
    Code (Text):
     @EventHandler(priority = EventPriority.NORMAL)
            public void onPlayerInteract(PlayerInteractEvent event) {
                if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getClickedBlock().getType().equals(Material.CHEST)){
                  if(!fill.contains(event.getClickedBlock().getLocation())){
                      BlockState block = event.getClickedBlock().getState();
                   
                      if(block instanceof Chest){
                      Chest chest = (Chest) block;
                      if(chest.getInventory().getName().equalsIgnoreCase("Nothing")){
                          return;
                      }
                      TSW.getManager().fill(chest);
                      fill.add(event.getClickedBlock().getLocation());
                      }
                      }
                }
            }
     
    • Like Like x 1
    • Useful Useful x 1
  15. You could also consider to just do it on ChunkLoadEvent
     
  16. It had already answered above :)
     
    • Like Like x 2