Solved Trigger when player reveals a block

Discussion in 'Spigot Plugin Development' started by Squidgie, Mar 1, 2020.

Thread Status:
Not open for further replies.
  1. How would I trigger something when a player reveals a block, but doesn't mine it.

    e.g Player is mining and discovers diamonds but doesn't mine it. Triggers alerts to staff
     
  2. Code (Java):
    @EventHandler
    public void blockBreak(BlockBreakEvent e) {
        Block b = e.getBlock();
        Block[] around = new Block[6];

        around[0] = b.getRelative(1, 0, 0);
        around[1] = b.getRelative(-1, 0, 0);
        around[2] = b.getRelative(0, 1, 0);
        around[3] = b.getRelative(0, -1, 0);
        around[4] = b.getRelative(0, 0, 1);
        around[5] = b.getRelative(0, 0, -1);

        for (Block block: around) {
            if (block.getType() == Material.DIAMOND_ORE) {
                // do actions
            }
        }
    }
    That should work
    EDIT: but be careful, if you have a lot of players, looping all these blocks may cause lags.
     
    • Useful Useful x 1
  3. Thank you!

    Is there a way, that is more efficient and less lag intensive?
     
  4. Looping 6 times is not intensive, what you do in each loop is what may be intensive.
     
  5. Sadly I don't have any other way to do that. But yes as robertilt said it, you will hundreds or thousands of simultaneous broken block to generate lags.
     
  6. looping within itself is a performance expenditure, the iterator has to run something on the processor too you know. You could wring out a tiny little bit more performance by unrolling the loop & checking everything manually. Besides that, array lookup is slower then LinkedList look up SHOULD you not access it by indexing (just iteration)
     
  7. And while we‘re at unnecessary optimization, why not create an ASIC to search through your array with 6 elements in parallel?
    Or actually write the whole thing in assembler. Since Java is inperformant af, just write a CPU specific, optimized code for all possible types and extensions.
     
  8. If you are lagging with the provided code snippet you’ve either done something very wrong or your hardware is not capable of running a Minecraft server.
     
Thread Status:
Not open for further replies.