diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/cuboid2/io/TSchemWriter.java b/BauSystem_Main/src/de/steamwar/bausystem/features/cuboid2/io/TSchemWriter.java index 5a901363..205db779 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/cuboid2/io/TSchemWriter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/cuboid2/io/TSchemWriter.java @@ -75,21 +75,59 @@ public class TSchemWriter implements ClipboardWriter { } out.writeNamedTag("SizeMinOffset", new ByteArrayTag(bsArray)); - Map blockMap = new HashMap<>(); - typedClipboard.getBlocks().forEach((blockState, blockVector3s) -> { - blockMap.put(blockState, new BitSet(volume)); - }); - clipboard.getRegion().forEach(blockVector3 -> { - int index = blockVector3.getBlockX() + blockVector3.getBlockY() * region.getWidth() + blockVector3.getBlockZ() * region.getWidth() * region.getHeight(); - for (Map.Entry> entry : typedClipboard.getBlocks().entrySet()) { - if (entry.getValue().contains(blockVector3)) { - blockMap.get(entry.getKey()).set(index); + Map blocks = new HashMap<>(); + for (Map.Entry> entry : typedClipboard.getBlocks().entrySet()) { + BitSet bitSet = new BitSet(volume); + for (BlockVector3 blockVector3 : entry.getValue()) { + int index = blockVector3.getBlockX() + blockVector3.getBlockY() * region.getWidth() + blockVector3.getBlockZ() * region.getWidth() * region.getHeight(); + bitSet.set(index); + } + byte[] bytes = bitSet.toByteArray(); + // RunLengthEncoding + List bytes2 = new ArrayList<>(); + int last = -1; + int count = 0; + for (int i = 0; i < bytes.length; i++) { + byte b = bytes[i]; + if (b == last) { + count++; + } else { + if (count > 0) { + writeVarInt((count - 1), bytes2); + bytes2.add((byte) last); + } + last = b; + count = 1; } } - }); + if (count > 0) { + writeVarInt((count - 1), bytes2); + bytes2.add((byte) last); + } + // Convert to byte array + bytes = new byte[bytes2.size()]; + for (int i = 0; i < bytes.length; i++) { + bytes[i] = bytes2.get(i); + } + System.out.println(entry.getKey().getAsString() + " " + bitSet.toByteArray().length + " -> " + bytes2.size()); + blocks.put(entry.getKey(), bytes); + } + + BlockState biggest = null; + int biggestSize = 0; + for (Map.Entry entry : blocks.entrySet()) { + if (entry.getValue().length > biggestSize) { + biggest = entry.getKey(); + biggestSize = entry.getValue().length; + } + } + System.out.println("Biggest: " + biggest.getAsString() + " " + biggestSize); + blocks.remove(biggest); + out.writeNamedTag("BaseBlock", biggest.getAsString()); + out.writeLazyCompoundTag("Blocks", out2 -> { - for (Map.Entry entry : blockMap.entrySet()) { - out2.writeNamedTag(entry.getKey().getAsString(), entry.getValue().toByteArray()); + for (Map.Entry entry : blocks.entrySet()) { + out2.writeNamedTag(entry.getKey().getAsString(), entry.getValue()); } }); });