setBlockFast() extremely slow

Discussion in 'Plugin Development' started by LucasEmanuel, Jun 3, 2013.

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

    LucasEmanuel

    I have been playing around with the setBlockFast() method that desht created and found it to be incredibly slow during my tests.

    In my tests it took around 65-75 seconds to update 1,000,000 blocks with the fast method and around 10 seconds longer with the "normal" method using the bukkit API.

    I have tried this several times now and i keep getting the same result, I just want to know if I have screwed up somewhere or something else is going on:

    Testing code:
    Code:
    import java.util.HashSet;
     
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class Commands implements CommandExecutor {
     
        @SuppressWarnings("unused")
        private Main plugin;
        private ConsoleLogger logger;
     
        public Commands(Main instance) {
            plugin = instance;
            logger = new ConsoleLogger(instance, "CommandExecutor");
         
            logger.debug("Initiated");
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            String command = cmd.getName();
         
            switch(command) {
                case "test": return test(sender, args);
            }
         
            return false;
        }
     
        private boolean test(CommandSender sender, String[] args) {
         
            if(!(sender instanceof Player)) {
                sender.sendMessage("This command can only be used by players!");
                return true;
            }
         
            final Player player = (Player) sender;
         
            Location pl = player.getEyeLocation();
         
            int px = pl.getBlockX();
            int py = pl.getBlockY();
            int pz = pl.getBlockZ();
         
            HashSet<Block> blocks = new HashSet<Block>();
         
            System.out.println("Loading blocks");
            for(int x = -50 ; x < 50 ; x++) {
                for(int y = -50 ; y < 50 ; y++) {
                    for(int z = -50 ; z < 50 ; z++) {
                        blocks.add(pl.getWorld().getBlockAt(px+x, py+y, pz+z));
                    }
                }
            }
         
            System.out.println("Starting fast:");
            long start = System.currentTimeMillis();
            for(Block b : blocks) {
                setBlockFast(b, 1, (byte) 0);
            }
            long stop = System.currentTimeMillis();
         
            System.out.println("Fast time: " + (stop-start)/1000);
         
            System.out.println("Resetting blocks");
            for(Block b : blocks) {
                b.setType(Material.AIR);
            }
         
            System.out.println("Starting slow:");
            start = System.currentTimeMillis();
            for(Block b : blocks) {
                b.setType(Material.STONE);
            }
            stop = System.currentTimeMillis();
         
            System.out.println("Slow time: " + (stop-start)/1000);
         
         
            return true;
        }
     
        public boolean setBlockFast(Block b, int typeId, byte data) {
            Chunk c = b.getChunk();
            net.minecraft.server.v1_5_R3.Chunk chunk = ((org.bukkit.craftbukkit.v1_5_R3.CraftChunk) c).getHandle();
            return chunk.a(b.getX() & 15, b.getY(), b.getZ() & 15, typeId, data);
        }
    }
    Output:
    Code:
    2013-06-04 01:02:53 [INFO] Loading blocks
    2013-06-04 01:02:55 [INFO] Starting fast:
    2013-06-04 01:04:09 [INFO] Fast time: 73
    2013-06-04 01:04:09 [INFO] Resetting blocks
    2013-06-04 01:04:30 [INFO] Starting slow:
    2013-06-04 01:05:55 [INFO] Slow time: 85
    Even down at 125,000 blocks, it took around 10 seconds for it to set the blocks and around 1 second longer for the "normal" method.
     
Thread Status:
Not open for further replies.

Share This Page