From 46342a7ff852aec72cc06c60ccbf810ed3f22829 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 24 Sep 2014 13:09:45 +1000 Subject: [PATCH] Async plot clear? I got plot clearing down to around 100 ms for a 256x256 plot without async. --- .../intellectualcrafters/plot/PlotHelper.java | 194 ++++++------------ .../plot/WorldGenerator.java | 4 +- 2 files changed, 63 insertions(+), 135 deletions(-) diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java index 8a5791162..eeafad6af 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java @@ -9,6 +9,7 @@ package com.intellectualcrafters.plot; +import com.intellectualcrafters.plot.Logger.LogLevel; import com.intellectualcrafters.plot.database.DBFunc; import org.bukkit.*; @@ -17,8 +18,11 @@ import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.entity.Player; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.*; +import static com.intellectualcrafters.plot.PlotMain.connection; import static com.intellectualcrafters.plot.Settings.*; /** @@ -31,14 +35,22 @@ public class PlotHelper { private static double calculateNeededTime(double blocks, double blocks_per_second) { return (blocks / blocks_per_second); } + static long state; + public static final long nextLong() { + long a=state; + state = xorShift64(a); + return a; + } - public static Short[] getRandom(Random random, Object[] filling) { - int len= ((Short[]) filling[0]).length; - if (len==1) { - return new Short[] {((Short[]) filling[0])[0],((Short[]) filling[1])[0]}; - } - int index = random.nextInt(len); - return new Short[] {((Short[]) filling[0])[index],((Short[]) filling[1])[index]}; + public static final long xorShift64(long a) { + a ^= (a << 21); + a ^= (a >>> 35); + a ^= (a << 4); + return a; + } + public static final int random(int n) { + long r = ((nextLong()>>>32)*n)>>32; + return (int) r; } public static void removeSign(Player plr, Plot p) { @@ -440,21 +452,21 @@ public class PlotHelper { } public static void clear(final Player requester, final Plot plot) { - PlotWorld plotworld = PlotMain.getWorldSettings(Bukkit.getWorld(plot.world)); - long start = System.nanoTime(); + final PlotWorld plotworld = PlotMain.getWorldSettings(Bukkit.getWorld(plot.world)); + final long start = System.nanoTime(); PlotHelper.setBiome(requester.getWorld(), plot, Biome.FOREST); PlotHelper.removeSign(requester, plot); PlayerFunctions.sendMessage(requester, C.CLEARING_PLOT); - World world = requester.getWorld(); - Location pos1 = getPlotBottomLoc(world, plot.id).add(1,0,1); - Location pos2 = getPlotTopLoc(world, plot.id); + final World world = requester.getWorld(); + final Location pos1 = getPlotBottomLoc(world, plot.id).add(1,0,1); + final Location pos2 = getPlotTopLoc(world, plot.id); SetBlockFast setBlockClass = null; - Short[] plotfloors = new Short[plotworld.TOP_BLOCK.length]; - Short[] plotfloors_data = new Short[plotworld.TOP_BLOCK.length]; + final short[] plotfloors = new short[plotworld.TOP_BLOCK.length]; + final short[] plotfloors_data = new short[plotworld.TOP_BLOCK.length]; - Short[] filling = new Short[plotworld.MAIN_BLOCK.length]; - Short[] filling_data = new Short[plotworld.MAIN_BLOCK.length]; + final short[] filling = new short[plotworld.MAIN_BLOCK.length]; + final short[] filling_data = new short[plotworld.MAIN_BLOCK.length]; for (int i = 0; i < plotworld.TOP_BLOCK.length; i++) { Short[] result = getBlock(plotworld.TOP_BLOCK[i]); @@ -469,126 +481,42 @@ public class PlotHelper { try { - setBlockClass = new SetBlockFast(); - regenerateCuboid(pos1, pos2,requester,plotworld, new Object[] {plotfloors,plotfloors_data}, new Object[] {filling, filling_data}); - PlayerFunctions.sendMessage(requester, C.CLEARING_DONE.s().replaceAll("%time%", ""+((System.nanoTime()-start)/1000000.0))); - SetBlockFast.update(requester); - PlayerFunctions.sendMessage(requester, C.CLEARING_DONE_PACKETS.s().replaceAll("%time%", ""+((System.nanoTime()-start)/1000000.0))); + final int prime = 31; + int h = 1; + h = prime * h + pos1.getBlockX(); + h = prime * h + pos1.getBlockZ(); + state = h; + PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), (new Runnable() { + @Override + public void run() { + for (int y = 0; y plotworld.PLOT_HEIGHT && y < world.getMaxHeight()) { - if(type != 0) - id = 0; - else - change = false; - } - else { - change = false; - } - if(change) { - if (type!=id) { - block.setTypeIdAndData(id, (byte) d, true); - } - } - } - } + PlayerFunctions.sendMessage(requester, C.PREFIX.s() + "&cFast plot clearing is currently not enabled."); } } diff --git a/PlotSquared/src/com/intellectualcrafters/plot/WorldGenerator.java b/PlotSquared/src/com/intellectualcrafters/plot/WorldGenerator.java index 4af329f3d..00a6b240e 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/WorldGenerator.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/WorldGenerator.java @@ -22,7 +22,7 @@ import static com.intellectualcrafters.plot.PlotWorld.*; * */ public class WorldGenerator extends ChunkGenerator { - private long state = 100; + private long state; public final long nextLong() { long a=state; state = xorShift64(a); @@ -35,11 +35,11 @@ public class WorldGenerator extends ChunkGenerator { a ^= (a << 4); return a; } - public final int random(int n) { long r = ((nextLong()>>>32)*n)>>32; return (int) r; } + PlotWorld plotworld; short[][] result; int plotsize;