diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java index 77761b1f..4db39228 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java @@ -29,12 +29,11 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.*; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; public abstract class BlockItemRewriter extends Rewriter { private static final CompoundTagConverter converter = new CompoundTagConverter(); - private final Map replacementData = new ConcurrentHashMap<>(); + private final Map replacementData = new HashMap<>(); protected String nbtTagName; protected boolean jsonNameFormat = true; @@ -60,6 +59,18 @@ public abstract class BlockItemRewriter extends Rew ItemUtil.copyItem(i, data.getRepItem()); if (i.getTag() == null) { i.setTag(new CompoundTag("")); + } else { + // Handle colors + CompoundTag tag = i.getTag().get("display"); + if (tag != null) { + StringTag nameTag = tag.get("Name"); + if (nameTag != null) { + String value = nameTag.getValue(); + if (value.contains("%vb_color%")) { + tag.put(new StringTag("Name", value.replace("%vb_color%", BlockColors.get(original.getData())))); + } + } + } } // Backup data for toServer @@ -72,16 +83,6 @@ public abstract class BlockItemRewriter extends Rew } } - // Handle colors - if (i.getTag().contains("display")) { - CompoundTag tag = i.getTag().get("display"); - if (tag.contains("Name")) { - String value = (String) tag.get("Name").getValue(); - tag.put(new StringTag("Name", - value.replaceAll("%viabackwards_color%", BlockColors.get((int) original.getData())))); - } - } - i.setAmount(original.getAmount()); // Keep original data when -1 if (i.getData() == -1) { @@ -125,22 +126,22 @@ public abstract class BlockItemRewriter extends Rew int type = idx >> 4; int meta = idx & 15; - if (!containsBlock(type)) - return idx; - Block b = handleBlock(type, meta); + if (b == null) return idx; + return (b.getId() << 4 | (b.getData() & 15)); } - public Block handleBlock(int block, int data) { - if (!containsBlock(block)) - return null; + public Block handleBlock(int blockId, int data) { + BlockItemSettings settings = replacementData.get(blockId); + if (settings == null || !settings.hasRepBlock()) return null; - Block b = replacementData.get(block).getRepBlock().clone(); + Block block = settings.getRepBlock(); // For some blocks, the data can still be useful (: - if (b.getData() == -1) - b.setData(data); - return b; + if (block.getData() == -1) { + return block.withData(data); + } + return block; } protected void handleChunk(Chunk chunk) { @@ -160,14 +161,16 @@ public abstract class BlockItemRewriter extends Rew if (section == null) continue; int block = section.getFlatBlock(pos.getX(), pos.getY() & 0xF, pos.getZ()); int btype = block >> 4; - if (!hasBlockEntityHandler(btype)) continue; - replacementData.get(btype).getBlockEntityHandler().handleOrNewCompoundTag(block, tag); + + BlockItemSettings settings = replacementData.get(btype); + if (settings != null && settings.hasEntityHandler()) { + settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag); + } } for (int i = 0; i < chunk.getSections().length; i++) { ChunkSection section = chunk.getSections()[i]; - if (section == null) - continue; + if (section == null) continue; boolean hasBlockEntityHandler = false; @@ -177,12 +180,18 @@ public abstract class BlockItemRewriter extends Rew int btype = block >> 4; int meta = block & 0xF; - if (containsBlock(btype)) { - Block b = handleBlock(btype, meta); + Block b = handleBlock(btype, meta); + if (b != null) { section.setPaletteEntry(j, (b.getId() << 4) | (b.getData() & 0xF)); } - hasBlockEntityHandler = hasBlockEntityHandler || hasBlockEntityHandler(btype); + // We already know that is has a handler + if (hasBlockEntityHandler) continue; + + BlockItemSettings settings = replacementData.get(btype); + if (section != null && settings.hasEntityHandler()) { + hasBlockEntityHandler = true; + } } if (!hasBlockEntityHandler) continue; @@ -195,7 +204,8 @@ public abstract class BlockItemRewriter extends Rew int btype = block >> 4; int meta = block & 15; - if (!hasBlockEntityHandler(btype)) continue; + BlockItemSettings settings = replacementData.get(btype); + if (settings == null || !settings.hasEntityHandler()) continue; Pos pos = new Pos(x, (y + (i << 4)), z); @@ -206,7 +216,8 @@ public abstract class BlockItemRewriter extends Rew tag.put(new IntTag("x", x + (chunk.getX() << 4))); tag.put(new IntTag("y", y + (i << 4))); tag.put(new IntTag("z", z + (chunk.getZ() << 4))); - replacementData.get(btype).getBlockEntityHandler().handleOrNewCompoundTag(block, tag); + + settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag); chunk.getBlockEntities().add(tag); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java index 4bf4dbdd..e9a0aebc 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java @@ -356,7 +356,7 @@ public class BlockItemPackets1_11 extends BlockItemRewriter // ShulkerBoxes to Dropper for (int i = 219; i < 235; i++) rewrite(i).repItem(new Item(158, (byte) 1, (short) 0, getNamedTag("1.11 " + BlockColors.get(i - 219) + " Shulker Box"))) - .repBlock(new Block(158, 0)); + .repBlock(new Block(158)); // Observer to Dispenser rewrite(218).repItem(new Item(23, (byte) 1, (short) 0, getNamedTag("1.11 Observer"))).repBlock(new Block(23, -1)); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/data/BlockColors.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/data/BlockColors.java index a8b182b0..53b5bde2 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/data/BlockColors.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/data/BlockColors.java @@ -10,41 +10,29 @@ package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.data; -import java.util.HashMap; -import java.util.Map; - public class BlockColors { - private static final Map COLORS = new HashMap<>(); - private static int count = 0; + private static final String[] COLORS = new String[16]; static { - add("White"); - add("Orange"); - add("Magenta"); - add("Light Blue"); - add("Yellow"); - add("Lime"); - add("Pink"); - add("Gray"); - add("Light Gray"); - add("Cyan"); - add("Purple"); - add("Blue"); - add("Brown"); - add("Green"); - add("Red"); - add("Black"); + COLORS[0] = "White"; + COLORS[1] = "Orange"; + COLORS[2] = "Magenta"; + COLORS[3] = "Light Blue"; + COLORS[4] = "Yellow"; + COLORS[5] = "Lime"; + COLORS[6] = "Pink"; + COLORS[7] = "Gray"; + COLORS[8] = "Light Gray"; + COLORS[9] = "Cyan"; + COLORS[10] = "Purple"; + COLORS[11] = "Blue"; + COLORS[12] = "Brown"; + COLORS[13] = "Green"; + COLORS[14] = "Red"; + COLORS[15] = "Black"; } - private static void add(String value) { - COLORS.put(count++, value); - } - - public static boolean has(Integer key) { - return COLORS.containsKey(key); - } - - public static String get(Integer key) { - return COLORS.getOrDefault(key, "Unknown color"); + public static String get(int key) { + return key >= 0 && key < COLORS.length ? COLORS[key] : "Unknown color"; } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java index c6944379..e4289b0c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java @@ -269,12 +269,12 @@ public class BlockItemPackets1_12 extends BlockItemRewriter Stained clay? (Also got a new name Terracota?) rewrite(251) - .repItem(new Item(159, (byte) 1, (short) -1, getNamedTag("1.12 %viabackwards_color% Concrete"))) + .repItem(new Item(159, (byte) 1, (short) -1, getNamedTag("1.12 %vb_color% Concrete"))) .repBlock(new Block(159, -1)); // Concrete Powder -> Wool rewrite(252) - .repItem(new Item(35, (byte) 1, (short) -1, getNamedTag("1.12 %viabackwards_color% Concrete Powder"))) + .repItem(new Item(35, (byte) 1, (short) -1, getNamedTag("1.12 %vb_color% Concrete Powder"))) .repBlock(new Block(35, -1)); // Knowledge book -> book @@ -296,6 +296,6 @@ public class BlockItemPackets1_12 extends BlockItemRewriter @Override protected void registerRewrites() { rewrite(255).repItem(new Item(166, (byte) 1, (short) 0, getNamedTag("1.10 Structure Block"))); // Structure block only item since the structure block is in 1.9 - rewrite(217).repItem(new Item(287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void"))).repBlock(new Block(287, 0)); // Structure void to string + rewrite(217).repItem(new Item(287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void"))).repBlock(new Block(287)); // Structure void to string rewrite(213).repItem(new Item(159, (byte) 1, (short) 1, getNamedTag("1.10 Magma Block"))).repBlock(new Block(159, 1)); // Magma block to orange clay rewrite(214).repItem(new Item(159, (byte) 1, (short) 14, getNamedTag("1.10 Nether Wart Block"))).repBlock(new Block(159, 14)); // Nether wart block to red clay - rewrite(215).repItem(new Item(112, (byte) 1, (short) 0, getNamedTag("1.10 Red Nether Bricks"))).repBlock(new Block(112, 0)); // Red nether brick to nether brick - rewrite(216).repItem(new Item(155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block"))).repBlock(new Block(155, 0)); // Bone block to quartz + rewrite(215).repItem(new Item(112, (byte) 1, (short) 0, getNamedTag("1.10 Red Nether Bricks"))).repBlock(new Block(112)); // Red nether brick to nether brick + rewrite(216).repItem(new Item(155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block"))).repBlock(new Block(155)); // Bone block to quartz } } diff --git a/core/src/main/java/nl/matsv/viabackwards/utils/Block.java b/core/src/main/java/nl/matsv/viabackwards/utils/Block.java index 70797560..323caf20 100644 --- a/core/src/main/java/nl/matsv/viabackwards/utils/Block.java +++ b/core/src/main/java/nl/matsv/viabackwards/utils/Block.java @@ -10,18 +10,32 @@ package nl.matsv.viabackwards.utils; -import lombok.AllArgsConstructor; -import lombok.Data; import lombok.EqualsAndHashCode; -@AllArgsConstructor @EqualsAndHashCode -@Data public class Block { - private int id; - private int data; + private final int id; + private final int data; - public Block clone() { - return new Block(id, data); + public Block(final int id, final int data) { + this.id = id; + this.data = data; + } + + public Block(final int id) { + this.id = id; + this.data = 0; + } + + public int getId() { + return id; + } + + public int getData() { + return data; + } + + public Block withData(final int data) { + return new Block(this.id, data); } }