From 69c225c00f5fb298d8eec1ff9cbb256bab217dad Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 17 Nov 2019 17:22:21 +0000 Subject: [PATCH] Fix //vis --- .../adapter/mc1_14/MapChunkUtil_1_14.java | 2 +- .../com/boydti/fawe/beta/CombinedBlocks.java | 2 +- .../java/com/boydti/fawe/beta/IBlocks.java | 33 ++++++------------- .../implementation/packet/ChunkPacket.java | 3 +- .../processors/ChunkSendProcessor.java | 20 ++++++----- .../PersistentChunkSendProcessor.java | 30 ++++++++++++----- .../cfi/HeightMapMCAGenerator.java | 2 -- .../object/changeset/DiskStorageHistory.java | 4 --- .../fawe/object/changeset/FaweChangeSet.java | 8 ++++- .../worldedit/command/RegionCommands.java | 3 +- .../worldedit/command/tool/BrushTool.java | 23 +++++++++++-- .../platform/PlatformCommandManager.java | 1 + 12 files changed, 77 insertions(+), 54 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/MapChunkUtil_1_14.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/MapChunkUtil_1_14.java index 783fc2824..ecd88d2c4 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/MapChunkUtil_1_14.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/MapChunkUtil_1_14.java @@ -80,7 +80,7 @@ public class MapChunkUtil_1_14 { try { PacketPlayOutMapChunk nmsPacket; int bitMask = packet.getChunk().getBitMask(); - if (bitMask == 0) { + if (bitMask == 0) { // TODO remove once sending tiles/entities is fixed nmsPacket = Fawe.get().getQueueHandler().sync((Callable) () -> { Chunk nmsChunk = world.getChunkAt(packet.getChunkX(), packet.getChunkZ()); PacketPlayOutMapChunk nmsPacket1 = new PacketPlayOutMapChunk(nmsChunk, 65535); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/CombinedBlocks.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/CombinedBlocks.java index 95d4dfca1..6e3066c1b 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/CombinedBlocks.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/CombinedBlocks.java @@ -28,7 +28,7 @@ public class CombinedBlocks implements IBlocks { public CombinedBlocks(IBlocks secondary, IBlocks primary, int addMask) { this.secondary = secondary; this.primary = primary; - this.addMask = addMask; + this.addMask = addMask == 0 ? 0 : addMask & secondary.getBitMask(); } @Override diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/IBlocks.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/IBlocks.java index 0c242f620..575ee1178 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/IBlocks.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/IBlocks.java @@ -8,8 +8,10 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.biome.BiomeType; +import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.block.BlockID; import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.registry.BlockRegistry; import java.io.IOException; @@ -45,11 +47,11 @@ public interface IBlocks extends Trimable { IBlocks reset(); - default byte[] toByteArray(int bitMask) { - return toByteArray(null, bitMask); + default byte[] toByteArray(boolean full) { + return toByteArray(null, getBitMask(), full); } - default byte[] toByteArray(byte[] buffer, int bitMask) { + default byte[] toByteArray(byte[] buffer, int bitMask, boolean full) { if (buffer == null) { buffer = new byte[1024]; } @@ -58,11 +60,11 @@ public interface IBlocks extends Trimable { FastByteArrayOutputStream sectionByteArray = new FastByteArrayOutputStream(buffer); FaweOutputStream sectionWriter = new FaweOutputStream(sectionByteArray); - System.out.println("Bitmask " + getBitMask()); - try { for (int layer = 0; layer < FaweCache.IMP.CHUNK_LAYERS; layer++) { - if (!this.hasSection(layer)) continue; + if (!this.hasSection(layer) || (bitMask & (1 << layer)) == 0) continue; + + System.out.println("Write section " + layer); char[] ids = this.load(layer); @@ -83,7 +85,7 @@ public interface IBlocks extends Trimable { sectionWriter.writeShort(nonEmpty); // non empty -// if (false) { +// if (false) { // short palette // sectionWriter.writeByte(14); // globalPaletteBitsPerBlock // BitArray4096 bits = new BitArray4096(14); // globalPaletteBitsPerBlock // bits.setAt(0, 0); @@ -123,22 +125,7 @@ public interface IBlocks extends Trimable { } // } } - -// if (writeBiomes) { -// for (int x = 0; x < 16; x++) { -// for (int z = 0; z < 16; z++) { -// BiomeType biome = this.getBiomeType(x, z); -// if (biome == null) { -// if (writeBiomes) { -// break; -// } else { -// biome = BiomeTypes.FOREST; -// } -// } -// } -// } -// } - if (bitMask == Character.MAX_VALUE) { + if (full) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { BiomeType biome = getBiomeType(x, z); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/packet/ChunkPacket.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/packet/ChunkPacket.java index 6fcfefa48..b904c5cd6 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/packet/ChunkPacket.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/packet/ChunkPacket.java @@ -63,7 +63,8 @@ public class ChunkPacket implements Function, Supplier { synchronized (this) { if (sectionBytes == null) { IBlocks tmpChunk = getChunk(); - sectionBytes = tmpChunk.toByteArray(FaweCache.IMP.BYTE_BUFFER_8192.get(), tmpChunk.getBitMask()); + byte[] buf = FaweCache.IMP.BYTE_BUFFER_8192.get(); + sectionBytes = tmpChunk.toByteArray(buf, tmpChunk.getBitMask(), this.full); } tmp = sectionBytes; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/ChunkSendProcessor.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/ChunkSendProcessor.java index 687d358af..00951a626 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/ChunkSendProcessor.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/ChunkSendProcessor.java @@ -10,21 +10,20 @@ import com.boydti.fawe.beta.implementation.packet.ChunkPacket; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.world.World; -import com.sk89q.worldedit.world.biome.BiomeType; +import java.util.Collection; import java.util.function.Supplier; -import java.util.stream.Stream; public class ChunkSendProcessor implements IBatchProcessor { - private final Supplier> players; + private final Supplier> players; private final World world; private final boolean full; - public ChunkSendProcessor(World world, Supplier> players) { + public ChunkSendProcessor(World world, Supplier> players) { this(world, players, false); } - public ChunkSendProcessor(World world, Supplier> players, boolean full) { + public ChunkSendProcessor(World world, Supplier> players, boolean full) { this.players = players; this.world = world; this.full = full; @@ -34,7 +33,7 @@ public class ChunkSendProcessor implements IBatchProcessor { return world; } - public Supplier> getPlayers() { + public Supplier> getPlayers() { return players; } @@ -53,12 +52,15 @@ public class ChunkSendProcessor implements IBatchProcessor { } } ChunkPacket packet = new ChunkPacket(chunkX, chunkZ, () -> blocks, full); - Stream stream = this.players.get(); + Collection stream = this.players.get(); if (stream == null) { world.sendFakeChunk(null, packet); } else { - stream.filter(player -> player.getWorld().equals(world)) - .forEach(player -> world.sendFakeChunk(player, packet)); + for (Player player : stream) { + if (player.getWorld().equals(world)) { + world.sendFakeChunk(player, packet); + } + } } return set; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/PersistentChunkSendProcessor.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/PersistentChunkSendProcessor.java index 5aca69b2e..f639c1cb5 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/PersistentChunkSendProcessor.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/processors/PersistentChunkSendProcessor.java @@ -7,6 +7,7 @@ import com.boydti.fawe.beta.IChunk; import com.boydti.fawe.beta.IChunkGet; import com.boydti.fawe.beta.IChunkSet; import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.beta.implementation.IChunkExtent; import com.boydti.fawe.beta.implementation.packet.ChunkPacket; import com.boydti.fawe.util.MathMan; import com.sk89q.worldedit.entity.Player; @@ -14,19 +15,25 @@ import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.world.World; import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import org.jetbrains.annotations.Nullable; +import java.util.Collection; import java.util.function.Supplier; import java.util.stream.Stream; public class PersistentChunkSendProcessor extends ChunkSendProcessor { private final Long2ObjectLinkedOpenHashMap current; - private final IQueueExtent queue; + @Nullable private Long2ObjectLinkedOpenHashMap previous; + private IChunkExtent queue; - public PersistentChunkSendProcessor(IQueueExtent queue, World world, PersistentChunkSendProcessor previous, Supplier> players) { + public PersistentChunkSendProcessor(World world, PersistentChunkSendProcessor previous, Supplier> players) { super(world, players); this.current = new Long2ObjectLinkedOpenHashMap<>(); - this.previous = previous.current; + this.previous = previous != null ? previous.current : null; + } + + public void init(IChunkExtent queue) { this.queue = queue; } @@ -35,10 +42,15 @@ public class PersistentChunkSendProcessor extends ChunkSendProcessor { int chunkX = chunk.getX(); int chunkZ = chunk.getZ(); long pair = MathMan.pairInt(chunkX, chunkZ); - Character bitMask = (char) (set.hasBiomes() ? Character.MAX_VALUE : set.getBitMask()); - current.put(pair, bitMask); - Character lastValue = previous.remove(pair); - return new CombinedBlocks(get, set, lastValue == null ? 0 : lastValue); + char bitMask = (char) (set.hasBiomes() ? Character.MAX_VALUE : set.getBitMask()); + synchronized (this) { + current.put(pair, (Character) bitMask); + if (previous != null) { + Character lastValue = previous.remove(pair); + if (lastValue != null) bitMask |= lastValue; + } + } + return new CombinedBlocks(get, set, bitMask); } public void flush() { @@ -47,13 +59,15 @@ public class PersistentChunkSendProcessor extends ChunkSendProcessor { } public void clear() { + if (queue == null) throw new IllegalStateException("Queue is not provided"); clear(current); current.clear(); + queue = null; } public void clear(Long2ObjectLinkedOpenHashMap current) { if (current != null && !current.isEmpty()) { - Stream players = getPlayers().get(); + Collection players = getPlayers().get(); for (Long2ObjectMap.Entry entry : current.long2ObjectEntrySet()) { long pair = entry.getLongKey(); int chunkX = MathMan.unpairIntX(pair); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/visualization/cfi/HeightMapMCAGenerator.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/visualization/cfi/HeightMapMCAGenerator.java index 9077ef0e3..14b7d8d98 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/visualization/cfi/HeightMapMCAGenerator.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/visualization/cfi/HeightMapMCAGenerator.java @@ -1187,8 +1187,6 @@ public class HeightMapMCAGenerator extends MCAWriter implements StreamChange, Dr char combined = block.getDefaultState().getOrdinalChar(); mainArr[index] = combined; floorArr[index] = combined; - } else { - System.out.println("Block is null: " + color); } } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java b/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java index b16d36266..f29934f14 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java @@ -193,7 +193,6 @@ public class DiskStorageHistory extends FaweStreamChangeSet { public void close() throws IOException { super.close(); synchronized (this) { - boolean flushed = osBD != null || osBIO != null || osNBTF != null || osNBTT != null && osENTCF != null || osENTCT != null; try { if (osBD != null) { osBD.close(); @@ -222,9 +221,6 @@ public class DiskStorageHistory extends FaweStreamChangeSet { } catch (Exception e) { e.printStackTrace(); } - if (!flushed) { - throw new IOException("Empty"); - } } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java b/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java index cf92dfd38..a9c85af62 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java @@ -48,6 +48,7 @@ public abstract class FaweChangeSet implements ChangeSet, IBatchProcessor, Close private final String worldName; protected AtomicInteger waitingCombined = new AtomicInteger(0); protected AtomicInteger waitingAsync = new AtomicInteger(0); + private boolean closed; public static FaweChangeSet getDefaultChangeSet(World world, UUID uuid) { if (Settings.IMP.HISTORY.USE_DISK) { @@ -80,6 +81,8 @@ public abstract class FaweChangeSet implements ChangeSet, IBatchProcessor, Close } public void closeAsync() { + if (closed) return; + closed = true; waitingAsync.incrementAndGet(); TaskManager.IMP.async(() -> { waitingAsync.decrementAndGet(); @@ -116,7 +119,10 @@ public abstract class FaweChangeSet implements ChangeSet, IBatchProcessor, Close @Override public void close() throws IOException { - flush(); + if (!closed) { + closed = true; + flush(); + } } public abstract void add(int x, int y, int z, int combinedFrom, int combinedTo); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java index 8a0b91e87..2e9d9698b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java @@ -77,6 +77,7 @@ import org.enginehub.piston.annotation.param.Switch; import org.enginehub.piston.inject.InjectedValueAccess; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.stream.Stream; @@ -151,7 +152,7 @@ public class RegionCommands { @Logging(REGION) public void test(Player player, EditSession editSession, @Selection Region region, @Arg(desc = "hello there") BiomeType biome) throws WorldEditException { System.out.println("Test start"); - editSession.addProcessor(new ChunkSendProcessor(editSession.getWorld(), () -> Stream.of(player))); + editSession.addProcessor(new ChunkSendProcessor(editSession.getWorld(), () -> Collections.singleton(player))); editSession.addProcessor(NullProcessor.INSTANCE); FlatRegionFunction replace = new BiomeReplace(editSession, biome); FlatRegionVisitor visitor = new FlatRegionVisitor(Regions.asFlatRegion(region), replace); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java index 47c1458f8..ecd3216b1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java @@ -23,6 +23,8 @@ import static com.google.common.base.Preconditions.checkNotNull; import static org.slf4j.LoggerFactory.getLogger; import com.boydti.fawe.Fawe; +import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.beta.implementation.IChunkExtent; import com.boydti.fawe.beta.implementation.processors.NullProcessor; import com.boydti.fawe.beta.implementation.processors.PersistentChunkSendProcessor; import com.boydti.fawe.config.BBC; @@ -40,6 +42,7 @@ import com.boydti.fawe.object.mask.MaskedTargetBlock; import com.boydti.fawe.object.pattern.PatternTraverser; import com.boydti.fawe.util.BrushCache; import com.boydti.fawe.util.EditSessionBuilder; +import com.boydti.fawe.util.ExtentTraverser; import com.boydti.fawe.util.MaskTraverser; import com.boydti.fawe.util.StringMan; import com.boydti.fawe.util.TaskManager; @@ -58,6 +61,7 @@ import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Platform; +import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.MaskIntersection; @@ -74,6 +78,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.reflect.Type; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.Lock; @@ -653,11 +659,20 @@ public class BrushTool implements DoubleActionTraceTool, ScrollTool, MovableTool .combineStages(true); EditSession editSession = builder.build(); - PersistentChunkSendProcessor previous = null; World world = editSession.getWorld(); - Supplier> players = () -> Stream.of(player); + Supplier> players = () -> Collections.singleton(player); - PersistentChunkSendProcessor newVisualExtent = new PersistentChunkSendProcessor(world, previous, players); + PersistentChunkSendProcessor newVisualExtent = new PersistentChunkSendProcessor(world, this.visualExtent, players); + ExtentTraverser traverser = new ExtentTraverser<>(editSession).find(IChunkExtent.class); + if (traverser == null) { + throw new IllegalStateException("No queue found"); + } + + IChunkExtent chunkExtent = traverser.get(); + if (this.visualExtent != null) { + this.visualExtent.init(chunkExtent); + } + newVisualExtent.init(chunkExtent); editSession.addProcessor(newVisualExtent); editSession.addProcessor(NullProcessor.INSTANCE); @@ -675,6 +690,8 @@ public class BrushTool implements DoubleActionTraceTool, ScrollTool, MovableTool } } } + editSession.flushQueue(); + if (visualExtent != null) { // clear old data visualExtent.flush(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 9ef4e4d0d..2d10a8bb6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -457,6 +457,7 @@ public final class PlatformCommandManager { c -> { c.accept(BrushCommandsRegistration.builder(), new BrushCommands(worldEdit)); c.accept(ToolCommandsRegistration.builder(), new ToolCommands(worldEdit)); + c.accept(ToolUtilCommandsRegistration.builder(), new ToolUtilCommands(worldEdit)); }, manager -> { PaintBrushCommands.register(commandManagerService, manager, registration);