Best way to change a lot of blocks?

Discussion in 'Plugin Development' started by TehHypnoz, Mar 15, 2015.

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

    TehHypnoz

    I'm probably going to create a minigame plugin where players can build and break blocks in an arena, and after the game the arena needs to be reset. I have been experimenting with setting lots of blocks at once and came up with this (only changes material):

    Code:
    final ArrayList<Location> list = selection.getContents();
            int delay = 0;
    
            p.sendMessage(ChatColor.GREEN + "Changing " + list.size() + " blocks.");
    
            for (int i = 0; i < list.size(); i++) {
                final int index = i;
                new BukkitRunnable() {
                    public void run() {
                        list.get(index).getBlock().setType(mat);
                    }
                }.runTaskLater(Main.plugin, delay);
                if (i % (perSecond / 20) == 0) {
                    delay++;
                }
            }
    
    This basically just limits the amount of blocks per second. Does anyone know a better way?

    EDIT: I know that just putting a static plugin field in the main class isn't good practice, I only did this because I wanted to quickly test my method.
     
  2. When it's okay to restart the server (for example you use something like a proxy to connect multiple servers) you can restart the server when the game ends and in the plugin.yml you set the startup to before the world and you delete the world and replace it with a backup. I used this with a minigame network I was creating and it works perfectly, but when it comes to not having the server restart, I think the method you use right now is the best.
     
  3. Offline

    mine-care

    What i had done in the past in a minigame was that after game had ended, i teleported all players to lobby and i start the rebuild process by spawning an asynchronus thread that does the looping and fixing of the map, when this thread reports that it is done, i know i can continue with the next game.
     
    Last edited: Apr 5, 2015
  4. Offline

    Zombie_Striker

    In you public void run. Make a if statement that checks if that block is what it should be (saves memory), and you might also want to save the BlockData (if you're dealing with chests or coloured wool)

    Another step would be to divide your loop into four sections (topleft,topright,bottomleft,bottomright) and have a slight cool down between each section. Doing this will reduce the amount of area to cover (reducing the time it takes to do all this) and gives players a moment to make sure they won't TimeOut.
     
  5. Offline

    mine-care

    @Zombie_Striker also as I said above to avoid timeout hazard you can run task or thread asynchronously with craftbukkit sever thread
     
    Last edited: Apr 5, 2015
  6. Offline

    mythbusterma

    @TehHypnoz

    The way you have it is fine, you can tune it to more or less blocks per tick depending on the resources of the server you're running on.

    @mine-care

    Don't ever perform world changes on separate thread, you're asking for an unimaginable mess of trouble if there's atomicity issues.

    EVER.
     
    mine-care and nverdier like this.
  7. Offline

    sgavster

    I wanna know the same thing.. May I ask:

    Is there any sort of post for how you have the blocks selected?
     
  8. Offline

    Europia79

    @sgavster

    You can get the blocks from BlockPlaceEvent and BlockBreakEvent.

    I'm not exactly sure about chests tho... I'm guessing maybe InventoryOpenEvent ?
     
  9. Offline

    mine-care

    @mythbusterma Hmm just thought of it and yes you are right, it will prove a bit... or too much of a disaster :3
     
Thread Status:
Not open for further replies.

Share This Page