Solved Place and delete blocks effectively

Discussion in 'Plugin Development' started by Xp10d3, Mar 4, 2021.

Thread Status:
Not open for further replies.
  1. Offline


    Hello. I am coding a Bridge plugin and have reached the point where I need to implement cages and have arena resetting. However I have quite a few issues. First one is arena resetting. I want to store all the blocks the player placed and broke then set the blocks the player placed to air and set the blocks that the player broke to the appropriate block (lets say STAINED_CLAY for simplicity). How would I achieve this? My own method I used was this:
    2. public static Map<Player, Block> placed = new HashMap<Player, Block>();
    3. public static Map<Player, Block> breaked = new HashMap<Player, Block>();
    5. @EventHandler
    6. public void onPlace(BlockPlaceEvent event) {
    7. Player player = event.getPlayer();
    8. Block block = event.getBlockPlaced();
    9. placed.put(player, block);
    10. }
    12. @EventHandler
    13. public void onBreak(BlockBreakEvent event) {
    14. Player player = event.getPlayer();
    15. Block block = event.getBlock();
    16. if (!placed.containsValue(block)) {
    17. return;
    18. } else {
    19. breaked.put(player, block);
    20. }
    21. }
    23. ...
    25. // In commands class:
    27. public void resetArena(Player player, Player player2) {
    28. Block block = PlayerListeners.placed.get(player);
    29. Block blocke = PlayerListeners.breaked.get(player);
    30. blocke.setType(Material.STAINED_CLAY);
    31. block.breakNaturally();
    32. }
    34. ...
    36. // When the game is done:
    38. resetArena(target, tPlayer);

    Store the blocks the player placed into a map, then remove those blocks. However I get a null error when doing this (, and I believe this isn't a good way to do achieve my goal. This might also cause a lot of TPS lag and slow the server.

    Finally, there is the issue of goals. I want to build a specific design, then remove it entirely and be able to essentially copy and paste it like WorldEdit. However, I don't know how the WorldEdit API works and don't think it would be a reliable way to copy and paste for the reason that I'd have to bank on schematics working. On top of that, positioning the builds would be problematic since there are multiple arenas and different players will have different cages. Anyways **what is a good way to place and remove blocks effectively**?

    Sorry that this was a bit messy and confusing.

    By the way I'm not entirely comfortable with sharing my code partly because this will be the core plugin for my Bridge server.
  2. Offline


    Don't use static. Create a class to store the maps and pass the class into the commands class and the listener.

    Don't store players, store their UUID.

    Why do you have a separate map for placed and broken? Why do you store the player?

    Your map will only keep track of one block per player.

    The way I would do this:

    Have a List of Blocks, the Block structure holds not only the material and data, but also its location. When a block is broken, add the original block to the List, when a block is placed, add the original block to the List (Not the block that was just placed!). There could be conflict here depending whether its stored by reference or not but nevertheless that can be solved easily if that's the case.

    When you call resetArena() just loop over each Block in the List and use world#getBlockAt(some location)#setType etc... to 'reset' the block.

    As for your second question, WorldEdit's API is perfectly valid for what you need, just read through the documentation a little.
  3. Offline


    I store player because there will be multiple games going at the same time, so I need to delete the blocks per-player. Thanks, though. I'll take a look at lists + WorldEdit API.
  4. Offline



    Create an Arena class that has an ID and a List of Blocks like previously suggested and store the Arena inside the List instead of the Blocks. Replace them the same way.
    Xp10d3 likes this.
  5. Offline


    Thanks; I think I got it :) Marking as solved
Thread Status:
Not open for further replies.

Share This Page