From 911f613f2f9fd5f909dbfac394649579a23b0af9 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 8 Oct 2017 16:20:17 +1100 Subject: [PATCH] Convert nether portals --- .../jnbt/anvil/filters/DelegateMCAFilter.java | 4 +++ .../fawe/jnbt/anvil/filters/RemapFilter.java | 29 +++++++++++++++++++ .../core/converter/MCAFile2LevelDB.java | 23 +++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/DelegateMCAFilter.java b/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/DelegateMCAFilter.java index 30578377..c420cde0 100644 --- a/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/DelegateMCAFilter.java +++ b/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/DelegateMCAFilter.java @@ -97,4 +97,8 @@ public class DelegateMCAFilter extends MCAFilter { public DelegateMCAFilter(MCAFilter filter) { this.filter = filter; } + + public MCAFilter getFilter() { + return filter; + } } diff --git a/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/RemapFilter.java b/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/RemapFilter.java index 45be42c7..d742633e 100644 --- a/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/RemapFilter.java +++ b/core/src/main/java/com/boydti/fawe/jnbt/anvil/filters/RemapFilter.java @@ -6,21 +6,28 @@ import com.boydti.fawe.jnbt.anvil.MCAFile; import com.boydti.fawe.jnbt.anvil.MCAFilterCounter; import com.boydti.fawe.jnbt.anvil.MutableMCABackedBaseBlock; import com.boydti.fawe.object.clipboard.ClipboardRemapper; +import com.boydti.fawe.object.collection.BlockVectorSet; import com.boydti.fawe.object.number.MutableLong; import com.boydti.fawe.util.ReflectionUtils; import com.sk89q.jnbt.ByteTag; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.jnbt.IntTag; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.blocks.BaseBlock; import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; public class RemapFilter extends MCAFilterCounter { private final ClipboardRemapper remapper; private final ClipboardRemapper.RemapPlatform from; private boolean skipRemap; + private List portals = Collections.synchronizedList(new ArrayList<>()); + private BlockVectorSet pLocs = new BlockVectorSet(); + private int dimension; public RemapFilter(ClipboardRemapper remapper) { this.remapper = remapper; @@ -45,6 +52,14 @@ public class RemapFilter extends MCAFilterCounter { return super.applyChunk(chunk, cache); } + public List getPortals() { + return portals; + } + + public void setDimension(int dimension) { + this.dimension = dimension; + } + @Override public void applyBlock(int x, int y, int z, BaseBlock block, MutableLong cache) { int id = block.getId(); @@ -69,6 +84,20 @@ public class RemapFilter extends MCAFilterCounter { case PC: { int newLight = 0; switch (id) { + case 90: + pLocs.add(x, y, z); + if (pLocs.contains(x, y - 1, z) || pLocs.contains(x - 1, y, z) || pLocs.contains(x, y, z - 1)) break; + Map tag = new HashMap<>(); + tag.put("Span", new ByteTag((byte) 1)); + tag.put("TpX", new IntTag(x)); + tag.put("TpY", new IntTag(y)); + tag.put("TpZ", new IntTag(z)); + tag.put("DimId", new IntTag(dimension)); + int data = block.getData(); + tag.put("Xa", new ByteTag((byte) ((data == 2) ? 0 : 1))); + tag.put("Za", new ByteTag((byte) ((data == 2) ? 1 : 0))); + portals.add(new CompoundTag(tag)); + break; case 29: case 33: Map map = new HashMap<>(); diff --git a/nukkit/src/main/java/com/boydti/fawe/nukkit/core/converter/MCAFile2LevelDB.java b/nukkit/src/main/java/com/boydti/fawe/nukkit/core/converter/MCAFile2LevelDB.java index 0f92cbc5..d905ebd5 100644 --- a/nukkit/src/main/java/com/boydti/fawe/nukkit/core/converter/MCAFile2LevelDB.java +++ b/nukkit/src/main/java/com/boydti/fawe/nukkit/core/converter/MCAFile2LevelDB.java @@ -40,12 +40,14 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.LongAdder; @@ -69,6 +71,8 @@ public class MCAFile2LevelDB extends MapConverter { private boolean remap; + private ConcurrentLinkedQueue portals = new ConcurrentLinkedQueue<>(); + private ConcurrentHashMap batches = new ConcurrentHashMap() { @Override @@ -137,8 +141,9 @@ public class MCAFile2LevelDB extends MapConverter { } } - public MCAFilter toFilter(final int dimension) { + public DelegateMCAFilter toFilter(final int dimension) { RemapFilter filter = new RemapFilter(ClipboardRemapper.RemapPlatform.PC, ClipboardRemapper.RemapPlatform.PE); + filter.setDimension(dimension); DelegateMCAFilter delegate = new DelegateMCAFilter(filter) { @Override public void finishFile(MCAFile file, MutableLong cache) { @@ -170,17 +175,31 @@ public class MCAFile2LevelDB extends MapConverter { } } + List portals = new ArrayList<>(); String[] dimDirs = {"region", "DIM-1/region", "DIM1/region"}; for (int dim = 0; dim < 3; dim++) { File source = new File(folderFrom, dimDirs[dim]); if (source.exists()) { - MCAFilter filter = toFilter(dim); + DelegateMCAFilter filter = toFilter(dim); MCAQueue queue = new MCAQueue(null, source, true); MCAFilter result = queue.filterWorld(filter); + portals.addAll(((RemapFilter) filter.getFilter()).getPortals()); } } + // Portals + if (!portals.isEmpty()) { + CompoundTag portalData = new CompoundTag(Collections.singletonMap("PortalRecords", new ListTag(CompoundTag.class, portals))); + CompoundTag portalsTag = new CompoundTag(Collections.singletonMap("data", portalData)); + try { + db.put("portals".getBytes(), write(Arrays.asList(portalsTag))); + } catch (IOException e) { + e.printStackTrace(); + } + } + + try { flush(false); } catch (IOException e) {