How to save specific Blocks (e.g. Bedwars)?

Discussion in 'Plugin Development' started by lowlight, Dec 4, 2019 at 9:03 AM.

  1. Offline

    lowlight

    Hey guys, how would y'all protect specific Blocks, while still allowing to break other blocks?

    Usually I'd use something like

    Code:
    public void onBlockBreak(BlockBreakEvent event) {
        event.setCancelled(true);
    }
    but in a game like Bedwars you have to be able to place blocks.

    My only idea is to store all blocks I want to protect in a list and adjust the code like this

    Code:
    List<Block> blocks = new ArrayList<Block>();
    
    public void onBlockBreak(BlockBreakEvent event) {
        if(!blocks.contains(event.getBlock())
            event.setCancelled(true);
    }
    but I believe this would not be very performant.

    Do you have any other ideas how to implement this?
     
    Last edited: Dec 4, 2019 at 1:50 PM
  2. Offline

    CraftCreeper6

    @lowlight
    I am not sure what you're asking, could you phrase it better?

    By save do you mean protect, I.E, the player can't destroy it?
     
  3. Offline

    lowlight

    Yeah exactly, rephrased it.
     
  4. Offline

    CraftCreeper6

    @lowlight
    How many blocks are you looking to protect? Ish.
     
  5. Offline

    lowlight

    @CraftCreeper6 Well it depends on the map.. I'm not good at estimating, but probably a few 100s to 1000s... Probably not more than 5000, most of the time way less
     
  6. Offline

    CraftCreeper6

    @lowlight
    If your main priority is efficiency, then it's definitely possible to write an algorithm that takes in the 5000 block locations, and turns them into centre blocks with a radius.
    For example, if you had this (crude) shape which was an island. Obviously, with a distance calculation you could not protect all of this area, but if you made it easier on yourself and only saved a centre block, and a radius, this could work.
    [​IMG]

    You could save the whole middle section as just the centre block and a radius value like so:
    [​IMG]

    But honestly I'm not sure that's worth the effort. I would suggest you just store each block. It may not be efficient or quick, but it will work.
     
  7. Offline

    lowlight

    @CraftCreeper6 Yeah, I see what you mean, thank you! I'll try to use a normal list first and come back to your idea if it's not efficient enough.
     

Share This Page