optimizations + more sponge work

This commit is contained in:
boy0001
2015-08-02 21:56:18 +10:00
parent 8e1d42d4c5
commit b77c2d1049
11 changed files with 567 additions and 217 deletions

View File

@ -60,8 +60,6 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
return null;
}
final Set<String> tabOptions = new HashSet<>();
System.out.print(MainCommand.getInstance() + " mc");
System.out.print(MainCommand.getInstance().getCommands() + " gc");
ArrayList<Command<PlotPlayer>> commands = MainCommand.getInstance().getCommands();
String best = new StringComparison(strings[0], commands).getBestMatch();
tabOptions.add(best);

View File

@ -2,10 +2,8 @@ package com.plotsquared.bukkit.util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
@ -17,39 +15,25 @@ import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
import org.bukkit.plugin.Plugin;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.generator.ClassicPlotWorld;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotAnalysis;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.TaskManager;
import com.plotsquared.bukkit.BukkitMain;
public class BukkitHybridUtils extends HybridUtils {
public static List<ChunkLoc> regions;
public static List<ChunkLoc> chunks = new ArrayList<>();
public static String world;
private static boolean UPDATE = false;
public int task;
private long last;
@Override
public void analyzePlot(final Plot plot, final RunnableVal<PlotAnalysis> whenDone) {
// TODO Auto-generated method stub
// int diff, int variety, int verticies, int rotation, int height_sd
/*
@ -267,60 +251,6 @@ public class BukkitHybridUtils extends HybridUtils {
TaskManager.tasks.put(currentIndex, task);
}
public void checkModified(final Plot plot, final RunnableVal<Integer> whenDone) {
TaskManager.index.incrementAndGet();
final Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1);
final Location top = MainUtil.getPlotTopLoc(plot.world, plot.id);
int bx = bot.getX() >> 4;
int bz = bot.getZ() >> 4;
int tx = top.getX() >> 4;
int tz = top.getZ() >> 4;
World world = BukkitUtil.getWorld(plot.world);
final HashSet<Chunk> chunks = new HashSet<>();
for (int X = bx; X <= tx; X++) {
for (int Z = bz; Z <= tz; Z++) {
chunks.add(world.getChunkAt(X,Z));
}
}
PlotWorld plotworld = PS.get().getPlotWorld(plot.world);
if (!(plotworld instanceof ClassicPlotWorld)) {
whenDone.value = -1;
TaskManager.runTaskLater(whenDone, 1);
return;
}
final ClassicPlotWorld cpw = (ClassicPlotWorld) plotworld;
final AtomicInteger count = new AtomicInteger(0);
final Integer currentIndex = TaskManager.index.get();
final Integer task = TaskManager.runTaskRepeat(new Runnable() {
@Override
public void run() {
if (chunks.size() == 0) {
whenDone.value = count.intValue();
TaskManager.runTaskLater(whenDone, 1);
Bukkit.getScheduler().cancelTask(TaskManager.tasks.get(currentIndex));
TaskManager.tasks.remove(currentIndex);
return;
}
Iterator<Chunk> iter = chunks.iterator();
final Chunk chunk = iter.next();
iter.remove();
int bx = Math.max(chunk.getX() << 4, bot.getX());
int bz = Math.max(chunk.getZ() << 4, bot.getZ());
int ex = Math.min((chunk.getX() << 4) + 15, top.getX());
int ez = Math.min((chunk.getZ() << 4) + 15, top.getZ());
// count changes
count.addAndGet(checkModified(plot.world, bx, ex, 1, cpw.PLOT_HEIGHT - 1, bz, ez, cpw.MAIN_BLOCK));
count.addAndGet(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, cpw.TOP_BLOCK));
count.addAndGet(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez, new PlotBlock[] { new PlotBlock((short) 0, (byte) 0) }));
}
}, 1);
TaskManager.tasks.put(currentIndex, task);
}
@Override
public int checkModified(final String worldname, final int x1, final int x2, final int y1, final int y2, final int z1, final int z2, final PlotBlock[] blocks) {
final World world = BukkitUtil.getWorld(worldname);
@ -365,127 +295,4 @@ public class BukkitHybridUtils extends HybridUtils {
}
return ey;
}
public void regenerateChunkChunk(final String worldname, final ChunkLoc loc) {
final World world = BukkitUtil.getWorld(worldname);
final int sx = loc.x << 5;
final int sz = loc.z << 5;
for (int x = sx; x < (sx + 32); x++) {
for (int z = sz; z < (sz + 32); z++) {
final Chunk chunk = world.getChunkAt(x, z);
chunk.load(false);
}
}
final ArrayList<Chunk> chunks2 = new ArrayList<>();
for (int x = sx; x < (sx + 32); x++) {
for (int z = sz; z < (sz + 32); z++) {
final Chunk chunk = world.getChunkAt(x, z);
chunks2.add(chunk);
regenerateRoad(worldname, new ChunkLoc(x, z), 0);
MainUtil.update(world.getName(), new ChunkLoc(chunk.getX(), chunk.getZ()));
}
}
}
public final ArrayList<ChunkLoc> getChunks(ChunkLoc region) {
ArrayList<ChunkLoc> chunks = new ArrayList<ChunkLoc>();
final int sx = region.x << 5;
final int sz = region.z << 5;
for (int x = sx; x < (sx + 32); x++) {
for (int z = sz; z < (sz + 32); z++) {
chunks.add(new ChunkLoc(x, z));
}
}
return chunks;
}
@Override
public boolean scheduleRoadUpdate(final String world, int extend) {
if (BukkitHybridUtils.UPDATE) {
return false;
}
BukkitHybridUtils.UPDATE = true;
final List<ChunkLoc> regions = ChunkManager.manager.getChunkChunks(world);
return scheduleRoadUpdate(world, regions, extend);
}
public boolean scheduleRoadUpdate(final String world, final List<ChunkLoc> rgs, final int extend) {
BukkitHybridUtils.regions = rgs;
BukkitHybridUtils.world = world;
chunks = new ArrayList<ChunkLoc>();
final Plugin plugin = BukkitMain.THIS;
final AtomicInteger count = new AtomicInteger(0);
this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override
public void run() {
count.incrementAndGet();
if (count.intValue() % 20 == 0) {
PS.debug("PROGRESS: " + ((100 * (2048 - chunks.size())) / 2048) + "%");
}
if (regions.size() == 0 && chunks.size() == 0) {
BukkitHybridUtils.UPDATE = false;
PS.debug(C.PREFIX.s() + "Finished road conversion");
Bukkit.getScheduler().cancelTask(BukkitHybridUtils.this.task);
return;
} else {
try {
if (chunks.size() < 1024) {
if (regions.size() > 0) {
final ChunkLoc loc = regions.get(0);
PS.debug("&3Updating .mcr: " + loc.x + ", " + loc.z + " (aprrox 1024 chunks)");
PS.debug(" - Remaining: " + regions.size());
chunks.addAll(getChunks(loc));
regions.remove(0);
System.gc();
}
}
if (chunks.size() > 0) {
long diff = System.currentTimeMillis() + 25;
if (System.currentTimeMillis() - last > 1200 && last != 0) {
last = 0;
PS.debug(C.PREFIX.s() + "Detected low TPS. Rescheduling in 30s");
while (chunks.size() > 0) {
ChunkLoc chunk = chunks.get(0);
chunks.remove(0);
regenerateRoad(world, chunk, extend);
ChunkManager.manager.unloadChunk(world, chunk, true, true);
}
Bukkit.getScheduler().cancelTask(BukkitHybridUtils.this.task);
TaskManager.runTaskLater(new Runnable() {
@Override
public void run() {
scheduleRoadUpdate(world, regions, extend);
}
}, 600);
return;
}
if (System.currentTimeMillis() - last < 1000) {
while (System.currentTimeMillis() < diff && chunks.size() > 0) {
ChunkLoc chunk = chunks.get(0);
chunks.remove(0);
regenerateRoad(world, chunk, extend);
ChunkManager.manager.unloadChunk(world, chunk, true, true);
}
}
last = System.currentTimeMillis();
}
} catch (final Exception e) {
e.printStackTrace();
final ChunkLoc loc = regions.get(0);
PS.debug("&c[ERROR]&7 Could not update '" + world + "/region/r." + loc.x + "." + loc.z + ".mca' (Corrupt chunk?)");
final int sx = loc.x << 5;
final int sz = loc.z << 5;
for (int x = sx; x < (sx + 32); x++) {
for (int z = sz; z < (sz + 32); z++) {
ChunkManager.manager.unloadChunk(world, new ChunkLoc(x, z), true, true);
}
}
PS.debug("&d - Potentially skipping 1024 chunks");
PS.debug("&d - TODO: recommend chunkster if corrupt");
}
}
}
}, 20, 20);
return true;
}
}

View File

@ -10,6 +10,11 @@ public class BukkitTaskManager extends TaskManager {
public int taskRepeat(final Runnable r, final int interval) {
return BukkitMain.THIS.getServer().getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, r, interval, interval);
}
@Override
public int taskRepeatAsync(final Runnable r, final int interval) {
return BukkitMain.THIS.getServer().getScheduler().scheduleAsyncRepeatingTask(BukkitMain.THIS, r, interval, interval);
}
@Override
public void taskAsync(final Runnable r) {