Home > Enterprise >  Is there a way to check if a block has already been broken before and a player hasn't just repl
Is there a way to check if a block has already been broken before and a player hasn't just repl

Time:11-27

I'm creating a plugin with Spiggot that every time you break a certain block or kill and entity it expands the border(The border is gonna start of being small). I have realised that after someone breaking a block they can replace in and repeat meaning the border will become infinite does anyone know a way I can prevent this I thought about using persistent variables inside the items but they were blocks that had been broken not items so I couldn't I don't think this question needs any code but for some reason if you need the main part here it is:

@EventHandler
public void onBlockBreak(BlockBreakEvent e)
{
    if (e.getBlock().getType() == Material.DIAMOND_ORE)
    {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "worldborder add 6 1");
    }
    if (e.getBlock().getType() == Material.IRON_ORE)
    {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "worldborder add 0.5 1");
    }
    if (e.getBlock().getType() == Material.GOLD_ORE)
    {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "worldborder add 1 1");
    }
    if (e.getBlock().getType() == Material.ANCIENT_DEBRIS)
    {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "worldborder add 0.5 1");
    }
}

CodePudding user response:

Firstly, I'm assuming the blocks that grow the worldborder are ore-types like Diamond ore and Iron ore. An idea to prevent this exploit would be to prevent players from placing that ore-type entirely, hopefully that won't be a problem.

You could use the Class BlockPlaceEvent, and method getBlockPlaced() to achieve this.

Also, I'd recommend using switch statements rather than multiple ifs.

CodePudding user response:

You can create a global variable containing the coordinates of blocks that have been broken. In order for the most efficient solution, I'd recommend this variable be a HashSet, as there is an O(1) time complexity for checking for the presence of an element.

private final Set<Location> blocksBroken = new HashSet<>();

Then in your onBlockBreak method, at the start you can check if the location of the block is in the Set - if it is, don't expand the border:

if (blocksBroken.contains(e.getBlock().getLocation())) return;

But in the case that the border is expanded, simply add the location of the broken block to the Set:

blocksBroken.add(e.getBlock().getLocation());

It is worth noting that this solution is really only efficient assuming there will not be a ridiculously high number of blocks broken by players, because for every block broken, the location of the block is broken in memory. This means that the amount of memory used by the solution is proportional to the number of blocks broken (O(n) space complexity). Personally, I'd really only concern about this after ~10,000 blocks.

CodePudding user response:

You could set the owner of the item and prevent placing if an owner is set.

  • Related