From 8ffdd8a9e93d5e1f4395498fea11faf40c4f4bb6 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 28 Mar 2020 10:44:51 +0000 Subject: [PATCH] Better reading of schematic biomes. Use biomes in the P2 schematic handler and save as a .schem not .schematic (when it's enabled again) --- .../plotsquared/FaweSchematicHandler.java | 4 +- .../clipboard/io/FastSchematicReader.java | 61 +++++++++---------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/plotsquared/FaweSchematicHandler.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/plotsquared/FaweSchematicHandler.java index 0356c636a..bb423b84c 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/plotsquared/FaweSchematicHandler.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/regions/plotsquared/FaweSchematicHandler.java @@ -61,7 +61,7 @@ public class FaweSchematicHandler extends SchematicHandler { final CuboidRegion region = new CuboidRegion(BlockVector3.at(pos1.getX(), pos1.getY(), pos1.getZ()), BlockVector3.at(pos2.getX(), pos2.getY(), pos2.getZ())); final EditSession editSession = new EditSessionBuilder(adaptedWorld).checkMemory(false).fastmode(true).limitUnlimited().changeSetNull().autoQueue(false).build(); - ReadOnlyClipboard clipboard = ReadOnlyClipboard.of(editSession, region); + ReadOnlyClipboard clipboard = ReadOnlyClipboard.of(editSession, region, false, true); Clipboard holder = new BlockArrayClipboard(region, clipboard); CompressedSchematicTag tag = new CompressedSchematicTag(holder); @@ -120,7 +120,7 @@ public class FaweSchematicHandler extends SchematicHandler { whenDone.run(url); return; } - MainUtil.upload(uuid, file, "schematic", new RunnableVal() { + MainUtil.upload(uuid, file, "schem", new RunnableVal() { @Override public void run(OutputStream output) { try { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/FastSchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/FastSchematicReader.java index 80a4613df..d5da9201b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/FastSchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/FastSchematicReader.java @@ -20,7 +20,6 @@ package com.sk89q.worldedit.extent.clipboard.io; import com.boydti.fawe.FaweCache; -import com.boydti.fawe.jnbt.streamer.IntValueReader; import com.boydti.fawe.jnbt.streamer.StreamDelegate; import com.boydti.fawe.jnbt.streamer.ValueReader; import com.boydti.fawe.object.FaweInputStream; @@ -38,7 +37,6 @@ import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.world.DataFixer; import com.sk89q.worldedit.world.biome.BiomeType; @@ -112,6 +110,11 @@ public class FastSchematicReader extends NBTSchematicReader { return fixer.fixUp(DataFixer.FixTypes.ENTITY, tag, dataVersion); } + private String fixBiome(String biomePalettePart) { + if(fixer == null || dataVersion == -1) return biomePalettePart; + return fixer.fixUp(DataFixer.FixTypes.BIOME, biomePalettePart, dataVersion); + } + public StreamDelegate createDelegate() { StreamDelegate root = new StreamDelegate(); StreamDelegate schematic = root.add("Schematic"); @@ -147,6 +150,7 @@ public class FastSchematicReader extends NBTSchematicReader { blocks = new FaweOutputStream(new LZ4BlockOutputStream(blocksOut)); }); blockData.withInt((index, value) -> blocks.write(value)); + StreamDelegate tilesDelegate = schematic.add("TileEntities"); tilesDelegate.withInfo((length, type) -> tiles = new ArrayList<>(length)); tilesDelegate.withElem((ValueReader>) (index, tile) -> tiles.add(tile)); @@ -154,36 +158,28 @@ public class FastSchematicReader extends NBTSchematicReader { StreamDelegate entitiesDelegate = schematic.add("Entities"); entitiesDelegate.withInfo((length, type) -> entities = new ArrayList<>(length)); entitiesDelegate.withElem((ValueReader>) (index, entity) -> entities.add(entity)); + + StreamDelegate biomePaletteDelegate = schematic.add("BiomePalette"); + biomePaletteDelegate.withValue((ValueReader>) (ignore, v) -> { + biomePalette = new char[v.size()]; + for (Entry entry : v.entrySet()) { + BiomeType biome = null; + try { + String biomePalettePart = fixBiome(entry.getKey()); + biome = BiomeTypes.get(biomePalettePart); + } catch (InputParseException e) { + e.printStackTrace(); + } + int index = (int) entry.getValue(); + biomePalette[index] = (char) biome.getInternalId(); + } + }); StreamDelegate biomeData = schematic.add("BiomeData"); biomeData.withInfo((length, type) -> { biomesOut = new FastByteArrayOutputStream(); - biomes = new FaweOutputStream(new LZ4BlockOutputStream(blocksOut)); - }); - biomeData.withElem((IntValueReader) (index, value) -> { - try { - biomes.write(value); // byte of varInt - } catch (IOException e) { - e.printStackTrace(); - } - }); - StreamDelegate biomePaletteDelegate = schematic.add("BiomePalette"); - biomePaletteDelegate.withInfo((length, type) -> biomePalette = new char[length]); - biomePaletteDelegate.withElem(new ValueReader>() { - @Override - public void apply(int index, Map.Entry palettePart) { - String key = palettePart.getKey(); - if (fixer != null) { - key = fixer.fixUp(DataFixer.FixTypes.BIOME, key, dataVersion); - } - BiomeType biome = BiomeTypes.get(key); - if (biome == null) { - System. out.println("Unknown biome " + key); - biome = BiomeTypes.FOREST; - } - int paletteIndex = palettePart.getValue().intValue(); - biomePalette[paletteIndex] = (char) biome.getInternalId(); - } + biomes = new FaweOutputStream(new LZ4BlockOutputStream(biomesOut)); }); + biomeData.withInt((index, value) -> biomes.write(value)); return root; } @@ -193,8 +189,7 @@ public class FastSchematicReader extends NBTSchematicReader { private BiomeType getBiomeType(FaweInputStream fis) throws IOException { char biomeId = biomePalette[fis.readVarInt()]; - BiomeType biome = BiomeTypes.get(biomeId); - return biome; + return BiomeTypes.get(biomeId); } @Override @@ -255,12 +250,14 @@ public class FastSchematicReader extends NBTSchematicReader { LinearClipboard linear = (LinearClipboard) clipboard; int volume = width * length; for (int index = 0; index < volume; index++) { - linear.setBiome(index, getBiomeType(fis)); + BiomeType biome = getBiomeType(fis); + linear.setBiome(index, biome); } } else { for (int z = 0; z < length; z++) { for (int x = 0; x < width; x++) { - clipboard.setBiome(x, 0, z, getBiomeType(fis)); + BiomeType biome = getBiomeType(fis); + clipboard.setBiome(x, 0, z, biome); } } }