Lambda's and References and Cleanups! Oh My!

This commit is contained in:
matt 2019-03-27 12:17:05 -04:00
parent 1424998327
commit acc8eb0a99
12 changed files with 291 additions and 462 deletions

View File

@ -5,7 +5,7 @@ import org.bukkit.World;
public abstract class PaperChunkCallback { public abstract class PaperChunkCallback {
public PaperChunkCallback(World world, int x, int z) { public PaperChunkCallback(World world, int x, int z) {
world.getChunkAtAsync(x, z, chunk -> PaperChunkCallback.this.onLoad(chunk)); world.getChunkAtAsync(x, z, PaperChunkCallback.this::onLoad);
} }
public abstract void onLoad(Chunk chunk); public abstract void onLoad(Chunk chunk);

View File

@ -152,10 +152,10 @@ public class Fawe {
* The platform specific implementation * The platform specific implementation
*/ */
private final IFawe IMP; private final IFawe IMP;
private Thread thread = Thread.currentThread(); private Thread thread;
private Fawe(final IFawe implementation) { private Fawe(final IFawe implementation) {
this.INSTANCE = this; INSTANCE = this;
this.IMP = implementation; this.IMP = implementation;
this.thread = Thread.currentThread(); this.thread = Thread.currentThread();
/* /*
@ -164,26 +164,18 @@ public class Fawe {
this.setupConfigs(); this.setupConfigs();
TaskManager.IMP = this.IMP.getTaskManager(); TaskManager.IMP = this.IMP.getTaskManager();
TaskManager.IMP.async(new Runnable() { TaskManager.IMP.async(() -> {
@Override MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.HISTORY), TimeUnit.DAYS.toMillis(Settings.IMP.HISTORY.DELETE_AFTER_DAYS), false);
public void run() { MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.CLIPBOARD), TimeUnit.DAYS.toMillis(Settings.IMP.CLIPBOARD.DELETE_AFTER_DAYS), false);
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.HISTORY), TimeUnit.DAYS.toMillis(Settings.IMP.HISTORY.DELETE_AFTER_DAYS), false);
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.CLIPBOARD), TimeUnit.DAYS.toMillis(Settings.IMP.CLIPBOARD.DELETE_AFTER_DAYS), false);
}
}); });
if (Settings.IMP.METRICS) { if (Settings.IMP.METRICS) {
try { try {
this.stats = new BStats(); this.stats = new BStats();
this.IMP.startMetrics(); this.IMP.startMetrics();
TaskManager.IMP.later(new Runnable() { TaskManager.IMP.later(() -> stats.start(), 1);
@Override } catch (Throwable throwable) {
public void run() { throwable.printStackTrace();
stats.start();
}
}, 1);
} catch (Throwable ignore) {
ignore.printStackTrace();
} }
} }
/* /*
@ -216,7 +208,7 @@ public class Fawe {
WEManager.IMP.managers.addAll(Fawe.this.IMP.getMaskManagers()); WEManager.IMP.managers.addAll(Fawe.this.IMP.getMaskManagers());
WEManager.IMP.managers.add(new PlotSquaredFeature()); WEManager.IMP.managers.add(new PlotSquaredFeature());
Fawe.debug("Plugin 'PlotSquared' found. Using it now."); Fawe.debug("Plugin 'PlotSquared' found. Using it now.");
} catch (Throwable e) {} } catch (Throwable ignored) {}
}, 0); }, 0);
TaskManager.IMP.repeat(timer, 1); TaskManager.IMP.repeat(timer, 1);
@ -224,8 +216,8 @@ public class Fawe {
if (!Settings.IMP.UPDATE.equalsIgnoreCase("false")) { if (!Settings.IMP.UPDATE.equalsIgnoreCase("false")) {
// Delayed updating // Delayed updating
updater = new Updater(); updater = new Updater();
TaskManager.IMP.async(() -> update()); TaskManager.IMP.async(this::update);
TaskManager.IMP.repeatAsync(() -> update(), 36000); TaskManager.IMP.repeatAsync(this::update, 36000);
} }
} }
@ -452,16 +444,13 @@ public class Fawe {
final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
final NotificationEmitter ne = (NotificationEmitter) memBean; final NotificationEmitter ne = (NotificationEmitter) memBean;
ne.addNotificationListener(new NotificationListener() { ne.addNotificationListener((notification, handback) -> {
@Override final long heapSize = Runtime.getRuntime().totalMemory();
public void handleNotification(final Notification notification, final Object handback) { final long heapMaxSize = Runtime.getRuntime().maxMemory();
final long heapSize = Runtime.getRuntime().totalMemory(); if (heapSize < heapMaxSize) {
final long heapMaxSize = Runtime.getRuntime().maxMemory(); return;
if (heapSize < heapMaxSize) {
return;
}
MemUtil.memoryLimitedTask();
} }
MemUtil.memoryLimitedTask();
}, null, null); }, null, null);
final List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans(); final List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans();

View File

@ -111,12 +111,7 @@ public abstract class MappedFaweQueue<WORLD, CHUNK, CHUNKSECTIONS, SECTION> impl
map.forEachChunk(new RunnableVal<FaweChunk>() { map.forEachChunk(new RunnableVal<FaweChunk>() {
@Override @Override
public void run(final FaweChunk chunk) { public void run(final FaweChunk chunk) {
pool.submit(new Runnable() { pool.submit(chunk::optimize);
@Override
public void run() {
chunk.optimize();
}
});
} }
}); });
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS); pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
@ -300,8 +295,8 @@ public abstract class MappedFaweQueue<WORLD, CHUNK, CHUNKSECTIONS, SECTION> impl
@Override @Override
public boolean supports(Capability capability) { public boolean supports(Capability capability) {
switch (capability) { if (capability == Capability.CHANGE_TASKS) {
case CHANGE_TASKS: return true; return true;
} }
return false; return false;
} }
@ -416,12 +411,7 @@ public abstract class MappedFaweQueue<WORLD, CHUNK, CHUNKSECTIONS, SECTION> impl
public boolean queueChunkLoad(final int cx, final int cz) { public boolean queueChunkLoad(final int cx, final int cz) {
CHUNK chunk = getCachedChunk(getWorld(), cx, cz); CHUNK chunk = getCachedChunk(getWorld(), cx, cz);
if (chunk == null) { if (chunk == null) {
SetQueue.IMP.addTask(new Runnable() { SetQueue.IMP.addTask(() -> loadChunk(getWorld(), cx, cz, true));
@Override
public void run() {
loadChunk(getWorld(), cx, cz, true);
}
});
return true; return true;
} }
return false; return false;
@ -430,12 +420,9 @@ public abstract class MappedFaweQueue<WORLD, CHUNK, CHUNKSECTIONS, SECTION> impl
public boolean queueChunkLoad(final int cx, final int cz, RunnableVal<CHUNK> operation) { public boolean queueChunkLoad(final int cx, final int cz, RunnableVal<CHUNK> operation) {
operation.value = getCachedChunk(getWorld(), cx, cz); operation.value = getCachedChunk(getWorld(), cx, cz);
if (operation.value == null) { if (operation.value == null) {
SetQueue.IMP.addTask(new Runnable() { SetQueue.IMP.addTask(() -> {
@Override operation.value = loadChunk(getWorld(), cx, cz, true);
public void run() { if (operation.value != null) TaskManager.IMP.async(operation);
operation.value = loadChunk(getWorld(), cx, cz, true);
if (operation.value != null) TaskManager.IMP.async(operation);
}
}); });
return true; return true;
} else { } else {

View File

@ -40,14 +40,11 @@ public abstract class NMSMappedFaweQueue<WORLD, CHUNK, CHUNKSECTION, SECTION> ex
public void runTasks() { public void runTasks() {
super.runTasks(); super.runTasks();
if (!getRelighter().isEmpty()) { if (!getRelighter().isEmpty()) {
TaskManager.IMP.async(new Runnable() { TaskManager.IMP.async(() -> {
@Override if (getSettings().IMP.LIGHTING.REMOVE_FIRST) {
public void run() { getRelighter().removeAndRelight(hasSky());
if (getSettings().IMP.LIGHTING.REMOVE_FIRST) { } else {
getRelighter().removeAndRelight(hasSky()); getRelighter().fixLightingSafe(hasSky());
} else {
getRelighter().fixLightingSafe(hasSky());
}
} }
}); });
} }

View File

@ -133,39 +133,33 @@ public class SchematicStreamer extends NBTStreamer {
addReader("Schematic.Biomes.#", biomeReader); // FAWE stores as a byte[] (4x smaller) addReader("Schematic.Biomes.#", biomeReader); // FAWE stores as a byte[] (4x smaller)
// Tiles // Tiles
addReader("Schematic.TileEntities.#", new BiConsumer<Integer, CompoundTag>() { addReader("Schematic.TileEntities.#", (BiConsumer<Integer, CompoundTag>) (index, value) -> {
@Override if (fc == null) {
public void accept(Integer index, CompoundTag value) { setupClipboard(0);
if (fc == null) {
setupClipboard(0);
}
int x = value.getInt("x");
int y = value.getInt("y");
int z = value.getInt("z");
fc.setTile(x, y, z, value);
} }
int x = value.getInt("x");
int y = value.getInt("y");
int z = value.getInt("z");
fc.setTile(x, y, z, value);
}); });
// Entities // Entities
addReader("Schematic.Entities.#", new BiConsumer<Integer, CompoundTag>() { addReader("Schematic.Entities.#", (BiConsumer<Integer, CompoundTag>) (index, compound) -> {
@Override if (fc == null) {
public void accept(Integer index, CompoundTag compound) { setupClipboard(0);
if (fc == null) { }
setupClipboard(0); String id = compound.getString("id");
} if (id.isEmpty()) {
String id = compound.getString("id"); return;
if (id.isEmpty()) { }
return; ListTag positionTag = compound.getListTag("Pos");
} ListTag directionTag = compound.getListTag("Rotation");
ListTag positionTag = compound.getListTag("Pos"); EntityType type = EntityTypes.parse(id);
ListTag directionTag = compound.getListTag("Rotation"); if (type != null) {
EntityType type = EntityTypes.parse(id); compound.getValue().put("Id", new StringTag(type.getId()));
if (type != null) { BaseEntity state = new BaseEntity(type, compound);
compound.getValue().put("Id", new StringTag(type.getId())); fc.createEntity(clipboard, positionTag.asDouble(0), positionTag.asDouble(1), positionTag.asDouble(2), (float) directionTag.asDouble(0), (float) directionTag.asDouble(1), state);
BaseEntity state = new BaseEntity(type, compound); } else {
fc.createEntity(clipboard, positionTag.asDouble(0), positionTag.asDouble(1), positionTag.asDouble(2), (float) directionTag.asDouble(0), (float) directionTag.asDouble(1), state); Fawe.debug("Invalid entity: " + id);
} else {
Fawe.debug("Invalid entity: " + id);
}
} }
}); });
} }
@ -345,60 +339,23 @@ public class SchematicStreamer extends NBTStreamer {
} }
public void addDimensionReaders() { public void addDimensionReaders() {
addReader("Schematic.Height", new BiConsumer<Integer, Short>() { addReader("Schematic.Height",
@Override (BiConsumer<Integer, Short>) (index, value) -> height = (value));
public void accept(Integer index, Short value) { addReader("Schematic.Width", (BiConsumer<Integer, Short>) (index, value) -> width = (value));
height = (value); addReader("Schematic.Length",
} (BiConsumer<Integer, Short>) (index, value) -> length = (value));
}); addReader("Schematic.WEOriginX",
addReader("Schematic.Width", new BiConsumer<Integer, Short>() { (BiConsumer<Integer, Integer>) (index, value) -> originX = (value));
@Override addReader("Schematic.WEOriginY",
public void accept(Integer index, Short value) { (BiConsumer<Integer, Integer>) (index, value) -> originY = (value));
width = (value); addReader("Schematic.WEOriginZ",
} (BiConsumer<Integer, Integer>) (index, value) -> originZ = (value));
}); addReader("Schematic.WEOffsetX",
addReader("Schematic.Length", new BiConsumer<Integer, Short>() { (BiConsumer<Integer, Integer>) (index, value) -> offsetX = (value));
@Override addReader("Schematic.WEOffsetY",
public void accept(Integer index, Short value) { (BiConsumer<Integer, Integer>) (index, value) -> offsetY = (value));
length = (value); addReader("Schematic.WEOffsetZ",
} (BiConsumer<Integer, Integer>) (index, value) -> offsetZ = (value));
});
addReader("Schematic.WEOriginX", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
originX = (value);
}
});
addReader("Schematic.WEOriginY", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
originY = (value);
}
});
addReader("Schematic.WEOriginZ", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
originZ = (value);
}
});
addReader("Schematic.WEOffsetX", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
offsetX = (value);
}
});
addReader("Schematic.WEOffsetY", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
offsetY = (value);
}
});
addReader("Schematic.WEOffsetZ", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
offsetZ = (value);
}
});
} }
private int height; private int height;

View File

@ -91,50 +91,47 @@ public class MCAChunk extends FaweChunk<Void> {
public void write(NBTOutputStream nbtOut) throws IOException { public void write(NBTOutputStream nbtOut) throws IOException {
nbtOut.writeNamedTagName("", NBTConstants.TYPE_COMPOUND); nbtOut.writeNamedTagName("", NBTConstants.TYPE_COMPOUND);
nbtOut.writeLazyCompoundTag("Level", new NBTOutputStream.LazyWrite() { nbtOut.writeLazyCompoundTag("Level", out -> {
@Override out.writeNamedTag("V", (byte) 1);
public void write(NBTOutputStream out) throws IOException { out.writeNamedTag("xPos", getX());
out.writeNamedTag("V", (byte) 1); out.writeNamedTag("zPos", getZ());
out.writeNamedTag("xPos", getX()); out.writeNamedTag("LightPopulated", (byte) 0);
out.writeNamedTag("zPos", getZ()); out.writeNamedTag("TerrainPopulated", (byte) 1);
out.writeNamedTag("LightPopulated", (byte) 0); if (entities.isEmpty()) {
out.writeNamedTag("TerrainPopulated", (byte) 1); out.writeNamedEmptyList("Entities");
if (entities.isEmpty()) { } else {
out.writeNamedEmptyList("Entities"); out.writeNamedTag("Entities", new ListTag(CompoundTag.class, new ArrayList<>(entities.values())));
} else { }
out.writeNamedTag("Entities", new ListTag(CompoundTag.class, new ArrayList<>(entities.values()))); if (tiles.isEmpty()) {
} out.writeNamedEmptyList("TileEntities");
if (tiles.isEmpty()) { } else {
out.writeNamedEmptyList("TileEntities"); out.writeNamedTag("TileEntities", new ListTag(CompoundTag.class,
} else { new ArrayList<>(tiles.values())));
out.writeNamedTag("TileEntities", new ListTag(CompoundTag.class, }
new ArrayList<>(tiles.values()))); out.writeNamedTag("InhabitedTime", inhabitedTime);
} out.writeNamedTag("LastUpdate", lastUpdate);
out.writeNamedTag("InhabitedTime", inhabitedTime); if (biomes != null) {
out.writeNamedTag("LastUpdate", lastUpdate); out.writeNamedTag("Biomes", biomes);
if (biomes != null) { }
out.writeNamedTag("Biomes", biomes); out.writeNamedTag("HeightMap", heightMap);
} out.writeNamedTagName("Sections", NBTConstants.TYPE_LIST);
out.writeNamedTag("HeightMap", heightMap); nbtOut.getOutputStream().writeByte(NBTConstants.TYPE_COMPOUND);
out.writeNamedTagName("Sections", NBTConstants.TYPE_LIST); int len = 0;
nbtOut.getOutputStream().writeByte(NBTConstants.TYPE_COMPOUND); for (int layer = 0; layer < ids.length; layer++) {
int len = 0; if (ids[layer] != null) len++;
for (int layer = 0; layer < ids.length; layer++) { }
if (ids[layer] != null) len++; nbtOut.getOutputStream().writeInt(len);
} for (int layer = 0; layer < ids.length; layer++) {
nbtOut.getOutputStream().writeInt(len); byte[] idLayer = ids[layer];
for (int layer = 0; layer < ids.length; layer++) { if (idLayer == null) {
byte[] idLayer = ids[layer]; continue;
if (idLayer == null) {
continue;
}
out.writeNamedTag("Y", (byte) layer);
out.writeNamedTag("BlockLight", blockLight[layer]);
out.writeNamedTag("SkyLight", skyLight[layer]);
out.writeNamedTag("Blocks", idLayer);
out.writeNamedTag("Data", data[layer]);
out.writeEndTag();
} }
out.writeNamedTag("Y", (byte) layer);
out.writeNamedTag("BlockLight", blockLight[layer]);
out.writeNamedTag("SkyLight", skyLight[layer]);
out.writeNamedTag("Blocks", idLayer);
out.writeNamedTag("Data", data[layer]);
out.writeEndTag();
} }
}); });
nbtOut.writeEndTag(); nbtOut.writeEndTag();
@ -464,74 +461,43 @@ public class MCAChunk extends FaweChunk<Void> {
skyLight = new byte[16][]; skyLight = new byte[16][];
blockLight = new byte[16][]; blockLight = new byte[16][];
NBTStreamer streamer = new NBTStreamer(nis); NBTStreamer streamer = new NBTStreamer(nis);
streamer.addReader(".Level.InhabitedTime", new BiConsumer<Integer, Long>() { streamer.addReader(".Level.InhabitedTime",
@Override (BiConsumer<Integer, Long>) (index, value) -> inhabitedTime = value);
public void accept(Integer index, Long value) { streamer.addReader(".Level.LastUpdate",
inhabitedTime = value; (BiConsumer<Integer, Long>) (index, value) -> lastUpdate = value);
} streamer.addReader(".Level.Sections.#", (BiConsumer<Integer, CompoundTag>) (index, tag) -> {
int layer = tag.getByte("Y");
ids[layer] = tag.getByteArray("Blocks");
data[layer] = tag.getByteArray("Data");
skyLight[layer] = tag.getByteArray("SkyLight");
blockLight[layer] = tag.getByteArray("BlockLight");
}); });
streamer.addReader(".Level.LastUpdate", new BiConsumer<Integer, Long>() { streamer.addReader(".Level.TileEntities.#",
@Override (BiConsumer<Integer, CompoundTag>) (index, tile) -> {
public void accept(Integer index, Long value) { int x1 = tile.getInt("x") & 15;
lastUpdate = value;
}
});
streamer.addReader(".Level.Sections.#", new BiConsumer<Integer, CompoundTag>() {
@Override
public void accept(Integer index, CompoundTag tag) {
int layer = tag.getByte("Y");
ids[layer] = tag.getByteArray("Blocks");
data[layer] = tag.getByteArray("Data");
skyLight[layer] = tag.getByteArray("SkyLight");
blockLight[layer] = tag.getByteArray("BlockLight");
}
});
streamer.addReader(".Level.TileEntities.#", new BiConsumer<Integer, CompoundTag>() {
@Override
public void accept(Integer index, CompoundTag tile) {
int x = tile.getInt("x") & 15;
int y = tile.getInt("y"); int y = tile.getInt("y");
int z = tile.getInt("z") & 15; int z1 = tile.getInt("z") & 15;
short pair = MathMan.tripleBlockCoord(x, y, z); short pair = MathMan.tripleBlockCoord(x1, y, z1);
tiles.put(pair, tile); tiles.put(pair, tile);
} });
}); streamer.addReader(".Level.Entities.#",
streamer.addReader(".Level.Entities.#", new BiConsumer<Integer, CompoundTag>() { (BiConsumer<Integer, CompoundTag>) (index, entityTag) -> {
@Override
public void accept(Integer index, CompoundTag entityTag) {
if (entities == null) { if (entities == null) {
entities = new HashMap<>(); entities = new HashMap<>();
} }
long least = entityTag.getLong("UUIDLeast"); long least = entityTag.getLong("UUIDLeast");
long most = entityTag.getLong("UUIDMost"); long most = entityTag.getLong("UUIDMost");
entities.put(new UUID(most, least), entityTag); entities.put(new UUID(most, least), entityTag);
} });
}); streamer.addReader(".Level.Biomes",
streamer.addReader(".Level.Biomes", new BiConsumer<Integer, byte[]>() { (BiConsumer<Integer, byte[]>) (index, value) -> biomes = value);
@Override streamer.addReader(".Level.HeightMap",
public void accept(Integer index, byte[] value) { (BiConsumer<Integer, int[]>) (index, value) -> heightMap = value);
biomes = value;
}
});
streamer.addReader(".Level.HeightMap", new BiConsumer<Integer, int[]>() {
@Override
public void accept(Integer index, int[] value) {
heightMap = value;
}
});
if (readPos) { if (readPos) {
streamer.addReader(".Level.xPos", new BiConsumer<Integer, Integer>() { streamer.addReader(".Level.xPos",
@Override (BiConsumer<Integer, Integer>) (index, value) -> MCAChunk.this.setLoc(getParent(), value, getZ()));
public void accept(Integer index, Integer value) { streamer.addReader(".Level.zPos",
MCAChunk.this.setLoc(getParent(), value, getZ()); (BiConsumer<Integer, Integer>) (index, value) -> MCAChunk.this.setLoc(getParent(), getX(), value));
}
});
streamer.addReader(".Level.zPos", new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer index, Integer value) {
MCAChunk.this.setLoc(getParent(), getX(), value);
}
});
} }
streamer.readFully(); streamer.readFully();
} }

View File

@ -9,7 +9,6 @@ import com.boydti.fawe.object.brush.visualization.VirtualWorld;
import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard; import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard;
import com.boydti.fawe.object.exception.FaweException; import com.boydti.fawe.object.exception.FaweException;
import com.boydti.fawe.object.task.SimpleAsyncNotifyQueue; import com.boydti.fawe.object.task.SimpleAsyncNotifyQueue;
import com.boydti.fawe.object.task.ThrowableSupplier;
import com.boydti.fawe.regions.FaweMaskManager; import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.util.*; import com.boydti.fawe.util.*;
import com.boydti.fawe.wrappers.FakePlayer; import com.boydti.fawe.wrappers.FakePlayer;
@ -17,8 +16,7 @@ import com.boydti.fawe.wrappers.LocationMaskedPlayerWrapper;
import com.boydti.fawe.wrappers.PlayerWrapper; import com.boydti.fawe.wrappers.PlayerWrapper;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.command.tool.BrushTool;
import com.sk89q.worldedit.command.tool.Tool;
import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.extension.platform.*; import com.sk89q.worldedit.extension.platform.*;
@ -41,7 +39,6 @@ import java.text.NumberFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
public abstract class FawePlayer<T> extends Metadatable { public abstract class FawePlayer<T> extends Metadatable {
@ -164,18 +161,10 @@ public abstract class FawePlayer<T> extends Metadatable {
private void setConfirmTask(@Nullable Runnable task, CommandContext context, String command) { private void setConfirmTask(@Nullable Runnable task, CommandContext context, String command) {
if (task != null) { if (task != null) {
Runnable newTask = new Runnable() { Runnable newTask = () -> CommandManager.getInstance().handleCommandTask(() -> {
@Override task.run();
public void run() { return null;
CommandManager.getInstance().handleCommandTask(new ThrowableSupplier<Throwable>() { }, context.getLocals());
@Override
public Object get() throws Throwable {
task.run();
return null;
}
}, context.getLocals());
}
};
setMeta("cmdConfirm", newTask); setMeta("cmdConfirm", newTask);
} else { } else {
setMeta("cmdConfirm", new CommandEvent(getPlayer(), command)); setMeta("cmdConfirm", new CommandEvent(getPlayer(), command));
@ -365,15 +354,15 @@ public abstract class FawePlayer<T> extends Metadatable {
} }
} catch (EmptyClipboardException e) { } catch (EmptyClipboardException e) {
} }
if (player != null && session != null) { if (player != null) {
Clipboard clip = doc.toClipboard(); Clipboard clip = doc.toClipboard();
ClipboardHolder holder = new ClipboardHolder(clip); ClipboardHolder holder = new ClipboardHolder(clip);
getSession().setClipboard(holder); getSession().setClipboard(holder);
} }
} }
} catch (Exception ignore) { } catch (Exception event) {
Fawe.debug("====== INVALID CLIPBOARD ======"); Fawe.debug("====== INVALID CLIPBOARD ======");
MainUtil.handleError(ignore, false); MainUtil.handleError(event, false);
Fawe.debug("===============---============="); Fawe.debug("===============---=============");
Fawe.debug("This shouldn't result in any failure"); Fawe.debug("This shouldn't result in any failure");
Fawe.debug("File: " + file.getName() + " (len:" + file.length() + ")"); Fawe.debug("File: " + file.getName() + " (len:" + file.length() + ")");

View File

@ -73,120 +73,117 @@ public class SetQueue {
public SetQueue() { public SetQueue() {
tasks = new ConcurrentLinkedDeque<>(); tasks = new ConcurrentLinkedDeque<>();
activeQueues = new ConcurrentLinkedDeque(); activeQueues = new ConcurrentLinkedDeque<>();
inactiveQueues = new ConcurrentLinkedDeque<>(); inactiveQueues = new ConcurrentLinkedDeque<>();
if (TaskManager.IMP == null) return; if (TaskManager.IMP == null) return;
TaskManager.IMP.repeat(new Runnable() { TaskManager.IMP.repeat(() -> {
@Override try {
public void run() { long now = System.currentTimeMillis();
try { boolean empty = (inactiveQueues.isEmpty() && activeQueues.isEmpty());
long now = System.currentTimeMillis(); boolean emptyTasks = tasks.isEmpty();
boolean empty = (inactiveQueues.isEmpty() && activeQueues.isEmpty()); if (emptyTasks && empty) {
boolean emptyTasks = tasks.isEmpty(); last = now;
if (emptyTasks && empty) { runEmptyTasks();
last = now; return;
runEmptyTasks(); }
return;
}
targetTPS = 18 - Math.max(Settings.IMP.QUEUE.EXTRA_TIME_MS * 0.05, 0); targetTPS = 18 - Math.max(Settings.IMP.QUEUE.EXTRA_TIME_MS * 0.05, 0);
long diff = (50 + SetQueue.this.last) - (SetQueue.this.last = now); long diff = (50 + SetQueue.this.last) - (SetQueue.this.last = now);
long absDiff = Math.abs(diff); long absDiff = Math.abs(diff);
if (diff == 0) { if (diff == 0) {
allocate = Math.min(50, allocate + 1); allocate = Math.min(50, allocate + 1);
} else if (diff < 0) { } else if (diff < 0) {
allocate = Math.max(5, allocate + diff); allocate = Math.max(5, allocate + diff);
} else if (!Fawe.get().getTimer().isAbove(targetTPS)) { } else if (!Fawe.get().getTimer().isAbove(targetTPS)) {
allocate = Math.max(5, allocate - 1); allocate = Math.max(5, allocate - 1);
} }
long currentAllocate = allocate - absDiff; long currentAllocate = allocate - absDiff;
if (!emptyTasks) { if (!emptyTasks) {
long taskAllocate = activeQueues.isEmpty() ? currentAllocate : 1 + (currentAllocate >> 1); long taskAllocate = activeQueues.isEmpty() ? currentAllocate : 1 + (currentAllocate >> 1);
long used = 0; long used = 0;
boolean wait = false; boolean wait = false;
do { do {
Runnable task = tasks.poll(); Runnable task = tasks.poll();
if (task == null) { if (task == null) {
if (wait) { if (wait) {
synchronized (tasks) { synchronized (tasks) {
tasks.wait(1); tasks.wait(1);
}
task = tasks.poll();
wait = false;
} else {
break;
} }
} task = tasks.poll();
if (task != null) { wait = false;
task.run();
wait = true;
}
} while ((used = System.currentTimeMillis() - now) < taskAllocate);
currentAllocate -= used;
}
if (empty) {
runEmptyTasks();
return;
}
if (!MemUtil.isMemoryFree()) {
final int mem = MemUtil.calculateMemory();
if (mem != Integer.MAX_VALUE) {
allocate = Math.max(5, allocate - 1);
if ((mem <= 1) && Settings.IMP.PREVENT_CRASHES) {
for (FaweQueue queue : getAllQueues()) {
queue.saveMemory();
}
return;
}
if (SetQueue.this.forceChunkSet()) {
System.gc();
} else { } else {
SetQueue.this.runEmptyTasks(); break;
}
}
if (task != null) {
task.run();
wait = true;
}
} while ((used = System.currentTimeMillis() - now) < taskAllocate);
currentAllocate -= used;
}
if (empty) {
runEmptyTasks();
return;
}
if (!MemUtil.isMemoryFree()) {
final int mem = MemUtil.calculateMemory();
if (mem != Integer.MAX_VALUE) {
allocate = Math.max(5, allocate - 1);
if ((mem <= 1) && Settings.IMP.PREVENT_CRASHES) {
for (FaweQueue queue : getAllQueues()) {
queue.saveMemory();
} }
return; return;
} }
} if (SetQueue.this.forceChunkSet()) {
System.gc();
FaweQueue queue = getNextQueue(); } else {
if (queue == null) { SetQueue.this.runEmptyTasks();
}
return; return;
} }
}
long time = (long) Settings.IMP.QUEUE.EXTRA_TIME_MS + currentAllocate - System.currentTimeMillis() + now; FaweQueue queue = getNextQueue();
// Disable the async catcher as it can't discern async vs parallel if (queue == null) {
boolean parallel = Settings.IMP.QUEUE.PARALLEL_THREADS > 1; return;
queue.startSet(parallel); }
try {
if (!queue.next(Settings.IMP.QUEUE.PARALLEL_THREADS, time) && queue.getStage() == QueueStage.ACTIVE) { long time = (long) Settings.IMP.QUEUE.EXTRA_TIME_MS + currentAllocate - System.currentTimeMillis() + now;
queue.setStage(QueueStage.NONE); // Disable the async catcher as it can't discern async vs parallel
queue.runTasks(); boolean parallel = Settings.IMP.QUEUE.PARALLEL_THREADS > 1;
} queue.startSet(parallel);
} catch (Throwable e) { try {
pool.awaitQuiescence(Settings.IMP.QUEUE.DISCARD_AFTER_MS, TimeUnit.MILLISECONDS); if (!queue.next(Settings.IMP.QUEUE.PARALLEL_THREADS, time) && queue.getStage() == QueueStage.ACTIVE) {
completer = new ExecutorCompletionService(pool); queue.setStage(QueueStage.NONE);
e.printStackTrace(); queue.runTasks();
} }
if (pool.getQueuedSubmissionCount() != 0 || pool.getRunningThreadCount() != 0 || pool.getQueuedTaskCount() != 0) { } catch (Throwable e) {
pool.awaitQuiescence(Settings.IMP.QUEUE.DISCARD_AFTER_MS, TimeUnit.MILLISECONDS);
completer = new ExecutorCompletionService(pool);
e.printStackTrace();
}
if (pool.getQueuedSubmissionCount() != 0 || pool.getRunningThreadCount() != 0 || pool.getQueuedTaskCount() != 0) {
// if (Fawe.get().isJava8()) // if (Fawe.get().isJava8())
{ {
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS); pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} }
// else { // else {
// pool.shutdown(); // pool.shutdown();
// pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); // pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
// pool = new ForkJoinPool(); // pool = new ForkJoinPool();
// completer = new ExecutorCompletionService(pool); // completer = new ExecutorCompletionService(pool);
// } // }
}
queue.endSet(parallel);
} catch (Throwable e) {
e.printStackTrace();
} }
queue.endSet(parallel);
} catch (Throwable e) {
e.printStackTrace();
} }
}, 1); }, 1);
} }
@ -264,7 +261,7 @@ public class SetQueue {
public void flush(FaweQueue queue) { public void flush(FaweQueue queue) {
int parallelThreads; int parallelThreads;
if (Fawe.get().isMainThread()) { if (Fawe.isMainThread()) {
parallelThreads = Settings.IMP.QUEUE.PARALLEL_THREADS; parallelThreads = Settings.IMP.QUEUE.PARALLEL_THREADS;
Settings.IMP.QUEUE.PARALLEL_THREADS = 1; Settings.IMP.QUEUE.PARALLEL_THREADS = 1;
} else { } else {

View File

@ -124,14 +124,10 @@ public abstract class TaskManager {
} }
for (i = 0; i < threads.length; i++) { for (i = 0; i < threads.length; i++) {
final Runnable[] toRun = split[i]; final Runnable[] toRun = split[i];
Thread thread = threads[i] = new Thread(new Runnable() { Thread thread = threads[i] = new Thread(() -> {
@Override for (Runnable run : toRun) {
public void run() { if (run != null) {
for (int j = 0; j < toRun.length; j++) { run.run();
Runnable run = toRun[j];
if (run != null) {
run.run();
}
} }
} }
}); });
@ -420,7 +416,7 @@ public abstract class TaskManager {
} catch (InterruptedException e) { } catch (InterruptedException e) {
MainUtil.handleError(e); MainUtil.handleError(e);
} }
if (run.value != null && run.value instanceof RuntimeException) { if (run.value instanceof RuntimeException) {
throw (RuntimeException) run.value; throw (RuntimeException) run.value;
} }
return (T) run.value; return (T) run.value;

View File

@ -124,7 +124,7 @@ public class WEManager {
} }
} }
} }
if (!removed) return regions.toArray(new Region[regions.size()]); if (!removed) return regions.toArray(new Region[0]);
masks.clear(); masks.clear();
} }
} }
@ -146,7 +146,7 @@ public class WEManager {
} }
if (!tmpMasks.isEmpty()) { if (!tmpMasks.isEmpty()) {
masks = tmpMasks; masks = tmpMasks;
regions = masks.stream().map(mask -> mask.getRegion()).collect(Collectors.toSet()); regions = masks.stream().map(FaweMask::getRegion).collect(Collectors.toSet());
} else { } else {
regions.addAll(backupRegions); regions.addAll(backupRegions);
} }
@ -155,7 +155,7 @@ public class WEManager {
} else { } else {
player.deleteMeta("lastMask"); player.deleteMeta("lastMask");
} }
return regions.toArray(new Region[regions.size()]); return regions.toArray(new Region[0]);
} }
@ -179,36 +179,22 @@ public class WEManager {
public boolean delay(final FawePlayer<?> player, final String command) { public boolean delay(final FawePlayer<?> player, final String command) {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
return this.delay(player, new Runnable() { return this.delay(player, () -> {
@Override try {
public void run() { if ((System.currentTimeMillis() - start) > 1000) {
try { BBC.WORLDEDIT_RUN.send(FawePlayer.wrap(player));
if ((System.currentTimeMillis() - start) > 1000) {
BBC.WORLDEDIT_RUN.send(FawePlayer.wrap(player));
}
TaskManager.IMP.task(new Runnable() {
@Override
public void run() {
final long start = System.currentTimeMillis();
player.executeCommand(command.substring(1));
TaskManager.IMP.later(new Runnable() {
@Override
public void run() {
SetQueue.IMP.addEmptyTask(new Runnable() {
@Override
public void run() {
if ((System.currentTimeMillis() - start) > 1000) {
BBC.WORLDEDIT_COMPLETE.send(FawePlayer.wrap(player));
}
}
});
}
}, 2);
}
});
} catch (final Exception e) {
MainUtil.handleError(e);
} }
TaskManager.IMP.task(() -> {
final long start1 = System.currentTimeMillis();
player.executeCommand(command.substring(1));
TaskManager.IMP.later(() -> SetQueue.IMP.addEmptyTask(() -> {
if ((System.currentTimeMillis() - start1) > 1000) {
BBC.WORLDEDIT_COMPLETE.send(FawePlayer.wrap(player));
}
}), 2);
});
} catch (final Exception e) {
MainUtil.handleError(e);
} }
}, false, false); }, false, false);
} }

View File

@ -167,45 +167,33 @@ public class TaskBuilder extends Metadatable {
} }
public TaskBuilder abortIfTrue(final Runnable run) { public TaskBuilder abortIfTrue(final Runnable run) {
tasks.add(RunnableTask.adapt(new Task<Boolean, Boolean>() { tasks.add(RunnableTask.adapt((Task<Boolean, Boolean>) previous -> {
@Override if (previous == Boolean.TRUE) run.run();
public Boolean run(Boolean previous) { return previous == Boolean.TRUE;
if (previous == Boolean.TRUE) run.run();
return previous == Boolean.TRUE;
}
}, TaskType.ABORT)); }, TaskType.ABORT));
return this; return this;
} }
public TaskBuilder abortIfNull(final Runnable run) { public TaskBuilder abortIfNull(final Runnable run) {
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() { tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
@Override if (previous == null) run.run();
public Boolean run(Object previous) { return previous == null;
if (previous == null) run.run();
return previous == null;
}
}, TaskType.ABORT)); }, TaskType.ABORT));
return this; return this;
} }
public TaskBuilder abortIfEqual(final Runnable run, final Object other) { public TaskBuilder abortIfEqual(final Runnable run, final Object other) {
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() { tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
@Override if (Objects.equals(previous, other)) run.run();
public Boolean run(Object previous) { return Objects.equals(previous, other);
if (Objects.equals(previous, other)) run.run();
return Objects.equals(previous, other);
}
}, TaskType.ABORT)); }, TaskType.ABORT));
return this; return this;
} }
public TaskBuilder abortIfNotEqual(final Runnable run, final Object other) { public TaskBuilder abortIfNotEqual(final Runnable run, final Object other) {
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() { tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
@Override if (!Objects.equals(previous, other)) run.run();
public Boolean run(Object previous) { return !Objects.equals(previous, other);
if (!Objects.equals(previous, other)) run.run();
return !Objects.equals(previous, other);
}
}, TaskType.ABORT)); }, TaskType.ABORT));
return this; return this;
} }
@ -215,12 +203,7 @@ public class TaskBuilder extends Metadatable {
* - As opposed to trying to using the current thread * - As opposed to trying to using the current thread
*/ */
public void buildAsync() { public void buildAsync() {
TaskManager.IMP.async(new Runnable() { TaskManager.IMP.async(this::build);
@Override
public void run() {
build();
}
});
} }
/** /**
@ -240,20 +223,10 @@ public class TaskBuilder extends Metadatable {
case SYNC: case SYNC:
case ABORT: case ABORT:
case SYNC_PARALLEL: case SYNC_PARALLEL:
TaskManager.IMP.later(new Runnable() { TaskManager.IMP.later(this::build, task.getDelay(result));
@Override
public void run() {
build();
}
}, task.getDelay(result));
return; return;
default: default:
TaskManager.IMP.laterAsync(new Runnable() { TaskManager.IMP.laterAsync(this::build, task.getDelay(result));
@Override
public void run() {
build();
}
}, task.getDelay(result));
return; return;
} }
} }
@ -274,12 +247,7 @@ public class TaskBuilder extends Metadatable {
case ASYNC: case ASYNC:
case ASYNC_PARALLEL: case ASYNC_PARALLEL:
if (Fawe.isMainThread()) { if (Fawe.isMainThread()) {
TaskManager.IMP.async(new Runnable() { TaskManager.IMP.async(this::build);
@Override
public void run() {
build();
}
});
return; return;
} }
break; break;
@ -484,24 +452,21 @@ public class TaskBuilder extends Metadatable {
public Object execSplit(final Object previous) { public Object execSplit(final Object previous) {
this.value = previous; this.value = previous;
final Thread thread = new Thread(new Runnable() { final Thread thread = new Thread(() -> {
@Override try {
public void run() { synchronized (asyncWaitLock) {
try { asyncWaitLock.notifyAll();
synchronized (asyncWaitLock) { asyncWaitLock.wait(Long.MAX_VALUE);
asyncWaitLock.notifyAll();
asyncWaitLock.wait(Long.MAX_VALUE);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
exec(previous);
finished = true;
waitingAsync = true;
waitingSync = false;
synchronized (syncWaitLock) {
syncWaitLock.notifyAll();
} }
} catch (InterruptedException e) {
e.printStackTrace();
}
exec(previous);
finished = true;
waitingAsync = true;
waitingSync = false;
synchronized (syncWaitLock) {
syncWaitLock.notifyAll();
} }
}); });
try { try {

View File

@ -166,7 +166,7 @@ public class RegionVisitor implements Operation {
} catch (Throwable ignore) { } catch (Throwable ignore) {
} }
try { try {
for (; ; ) { while (true) {
apply(trailIter.next()); apply(trailIter.next());
apply(trailIter.next()); apply(trailIter.next());
} }