How to deny placing of blocks if chunk contains presure plate?

Discussion in 'Spigot Plugin Development' started by FroZenKeyboard, Apr 26, 2017.

  1. So i got this code. But it won't work when i place blocks at in the chunk of the stone presure plate:

    Code (Text):
            Chunk chunk = e.getBlockPlaced().getLocation().getChunk();
            for(BlockState entity : chunk.getTileEntities()) {
                    if(entity.getType() == Material.STONE_PLATE && TeamInv.blue.contains(e.getPlayer())) {
                        e.setCancelled(true);
                        e.getPlayer().sendMessage("§c§oYou can't build here!");
     
  2. Choco

    Moderator

    That'd be because pressure plates are not tile entities :p They're blocks. What are you trying to do? There must be a better way than iterating over every single block in the chunk
     
    • Agree Agree x 2
  3. If the plates stay there once placed I would suggest writing their location to a file upon place / removing it when they're broken. Searching every block in every chunk every time a block is broken is not a good idea.

    Another way you could do this is to search loaded chunks at startup and when a plate is found add the plates location to a list, then when blocks are placed get the chunk, and check to see if any of the plates in the list .getChunk() is equal to the placed block chunk.




    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
  4. Choco

    Moderator

    This is exactly what I was going to recommend.

    This isn't any better than having to iterate over Chunks when called upon other than the fact that you would do it once rather than multiple times. Either way, pressure plates could be moved. If you were to use this method, I would recommend doing it asynchronously with a ChunkSnapshot to prevent any unnecessary blocking on the main thread
     
    • Agree Agree x 1
  5. I've found that using
    World.getBlockTypeID(location) on chunk load works pretty well without having to hang up the main thread, that method is deprecated but seems to be much faster than getBlockAt().

    But saving/loading the locations are definitely the way to go if these blocks need to persist through reboots. If not just track their locations and set them to air on disable


    Sent from my iPhone using Tapatalk