From 2060a698bd253790e8d3bec3c4b742bd6e412edf Mon Sep 17 00:00:00 2001 From: Matsv Date: Sat, 3 Jun 2017 11:47:08 +0200 Subject: [PATCH] Fix Spawn eggs --- .../entities/blockitem/BlockItemSettings.java | 72 +++++++++++++++ .../entities/meta/MetaHandlerSettings.java | 4 +- .../api/rewriters/BlockItemRewriter.java | 92 ++++++++----------- .../protocol1_10to1_11/EntityTypeNames.java | 17 +--- .../packets/BlockItemPackets.java | 18 ++-- .../packets/ItemPackets.java | 2 +- .../packets/BlockItemPackets.java | 12 +-- 7 files changed, 131 insertions(+), 86 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/entities/blockitem/BlockItemSettings.java diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/blockitem/BlockItemSettings.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/blockitem/BlockItemSettings.java new file mode 100644 index 00000000..7f2dc329 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/blockitem/BlockItemSettings.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 Matsv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package nl.matsv.viabackwards.api.entities.blockitem; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import nl.matsv.viabackwards.utils.Block; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; + +@RequiredArgsConstructor +@Getter +public class BlockItemSettings { + private final int id; + private Item repItem; + private Block repBlock; + private BlockEntityHandler blockEntityHandler; + private ItemHandler itemHandler; + + public BlockItemSettings repItem(Item item) { + this.repItem = item; + return this; + } + + public BlockItemSettings repBlock(Block block) { + this.repBlock = block; + return this; + } + + public BlockItemSettings blockEntityHandler(BlockEntityHandler handler) { + this.blockEntityHandler = handler; + return this; + } + + public BlockItemSettings itemHandler(ItemHandler handler) { + this.itemHandler = handler; + return this; + } + + public boolean hasRepItem() { + return repItem != null; + } + + public boolean hasRepBlock() { + return repBlock != null; + } + + public boolean hasEntityHandler() { + return blockEntityHandler != null; + } + + public boolean hasItemTagHandler() { + return itemHandler != null; + } + + public interface BlockEntityHandler { + CompoundTag handleOrNewCompoundTag(int block, CompoundTag tag); + } + + public interface ItemHandler { + Item handle(Item i); + } + +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java index 0f24b558..8973b2fd 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java @@ -95,9 +95,7 @@ public class MetaHandlerSettings { return false; } } - if (hasIndex() && metadata.getId() != getFilterIndex()) - return false; - return true; + return !(hasIndex() && metadata.getId() != getFilterIndex()); } return false; } 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 dc23f7a5..03147b19 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 @@ -10,8 +10,12 @@ package nl.matsv.viabackwards.api.rewriters; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.entities.blockitem.BlockItemSettings; import nl.matsv.viabackwards.utils.Block; import nl.matsv.viabackwards.utils.ItemUtil; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -26,40 +30,39 @@ 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 ConcurrentHashMap<>(); - protected void rewriteItem(int oldItem, Item newItem) { - replacementData.put(oldItem, new BlockItemData(oldItem, newItem, null, null)); + protected BlockItemSettings rewrite(int itemId) { + BlockItemSettings settings = new BlockItemSettings(itemId); + replacementData.put(itemId, settings); + return settings; } - protected void rewriteBlockItem(int oldId, Item newItem, Block newBlock) { - rewriteBlockItem(oldId, newItem, newBlock, null); - } - - protected void rewriteBlockItem(int oldId, Item newItem, Block newBlock, BlockEntityHandler handler) { - replacementData.put(oldId, new BlockItemData(oldId, newItem, newBlock, handler)); - } - - protected Item handleItemToClient(Item item) { - if (item == null) + protected Item handleItemToClient(Item i) { + if (i == null) return null; - if (!replacementData.containsKey((int) item.getId())) - return item; - BlockItemData data = replacementData.get((int) item.getId()); - if (!data.hasRepItem()) - return item; + if (!replacementData.containsKey((int) i.getId())) + return i; + BlockItemSettings data = replacementData.get((int) i.getId()); - Item i = ItemUtil.copyItem(data.getRepItem()); + if (data.hasRepItem()) { + i = ItemUtil.copyItem(data.getRepItem()); - if (i.getTag() == null) - i.setTag(new CompoundTag("")); - i.getTag().put(createViaNBT(item)); + if (i.getTag() == null) + i.setTag(new CompoundTag("")); + i.getTag().put(createViaNBT(i)); - if (item.getTag() != null) - for (Tag ai : item.getTag()) - i.getTag().put(ai); + if (i.getTag() != null) + for (Tag ai : i.getTag()) + i.getTag().put(ai); + i.setAmount(i.getAmount()); + } + if (data.hasItemTagHandler()) { + if (!i.getTag().contains("ViaBackwards|" + getProtocolName())) + i.getTag().put(createViaNBT(i)); + data.getItemHandler().handle(i); + } - i.setAmount(item.getAmount()); return i; } @@ -79,9 +82,10 @@ public abstract class BlockItemRewriter extends Rew item.setData(data); item.setAmount(amount); item.setTag(converter.convert("", converter.convert(extras))); - // Remove data blockEntityHandler + // Remove data tag tag.remove("ViaBackwards|" + getProtocolName()); } + System.out.println("TO_SERVER: " + item); return item; } @@ -160,7 +164,11 @@ public abstract class BlockItemRewriter extends Rew } protected boolean hasBlockEntityHandler(int block) { - return replacementData.containsKey(block) && replacementData.get(block).hasHandler(); + return replacementData.containsKey(block) && replacementData.get(block).hasEntityHandler(); + } + + protected boolean hasItemTagHandler(int block) { + return replacementData.containsKey(block) && replacementData.get(block).hasItemTagHandler(); } private CompoundTag createViaNBT(Item i) { @@ -186,32 +194,6 @@ public abstract class BlockItemRewriter extends Rew return getProtocol().getClass().getSimpleName(); } - public interface BlockEntityHandler { - CompoundTag handleOrNewCompoundTag(int block, CompoundTag tag); - } - - @Data - @RequiredArgsConstructor - public class BlockItemData { - private final int id; - private final Item repItem; - private final Block repBlock; - private final BlockEntityHandler blockEntityHandler; - - public boolean hasRepItem() { - return repItem != null; - } - - public boolean hasRepBlock() { - return repBlock != null; - } - - public boolean hasHandler() { - return blockEntityHandler != null; - } - - } - @Data @AllArgsConstructor @ToString diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java index b59d4f9e..13f556f6 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java @@ -122,26 +122,13 @@ public class EntityTypeNames { } } - /*public static void toClientItem(Item item) { + public static void toClientItem(Item item) { if (hasEntityTag(item)) { - CompoundTag entityTag = item.getBlockEntityHandler().get("EntityTag"); + CompoundTag entityTag = item.getTag().get("EntityTag"); toClient(entityTag); } - if (item != null && item.getAmount() <= 0) item.setAmount((byte) 1); } - public static void toServerItem(Item item) { - if (hasEntityTag(item)) { - CompoundTag entityTag = item.getBlockEntityHandler().get("EntityTag"); - if (entityTag.get("id") instanceof StringTag) { - StringTag id = entityTag.get("id"); - if (newToOldNames.inverse().containsKey(id.getValue())) { - id.setValue(newToOldNames.inverse().get(id.getValue())); - } - } - } - }*/ - private static boolean hasEntityTag(Item item) { if (item != null && item.getId() == 383) { // Monster Egg CompoundTag tag = item.getTag(); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java index 5f3e793a..a753a664 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java @@ -260,21 +260,27 @@ public class BlockItemPackets extends BlockItemRewriter { protected void registerRewrites() { // ShulkerBoxes to chests for (int i = 219; i < 235; i++) - rewriteBlockItem(i, - new Item((short) 54, (byte) 1, (short) 0, getNamedTag("1.11 Shulker Box (Color #" + (i - 219) + ")")), - new Block(54, 1)); + rewrite(i) + .repItem(new Item((short) 54, (byte) 1, (short) 0, getNamedTag("1.11 Shulker Box (Color #" + (i - 219) + ")"))) + .repBlock(new Block(54, 1)); // Observer to Dispenser TODO facing position? - rewriteBlockItem(218, new Item((short) 23, (byte) 1, (short) 0, getNamedTag("1.11 Observer")), new Block(23, 0)); + rewrite(218).repItem(new Item((short) 23, (byte) 1, (short) 0, getNamedTag("1.11 Observer"))).repBlock(new Block(23, 0)); // Handle spawner block entity - rewriteBlockItem(52, null, null, (b, tag) -> { + rewrite(52).blockEntityHandler((b, tag) -> { EntityTypeNames.toClientSpawner(tag); return tag; }); + // Rewrite spawn eggs TODO maybe intercept / handle server for creative instead of ViaBackwards NBT + rewrite(383).itemHandler((i) -> { + EntityTypeNames.toClientItem(i); + return i; + }); + // Totem of Undying to Dead Bush - rewriteItem(449, new Item((short) 32, (byte) 1, (short) 0, getNamedTag("1.11 Totem of Undying"))); + rewrite(449).repItem(new Item((short) 32, (byte) 1, (short) 0, getNamedTag("1.11 Totem of Undying"))); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/ItemPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/ItemPackets.java index a33c31ba..73992f29 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/ItemPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/ItemPackets.java @@ -151,6 +151,6 @@ public class ItemPackets extends BlockItemRewriter { @Override protected void registerRewrites() { - rewriteItem(452, new Item((short) 265, (byte) 1, (short) 0, getNamedTag("1.11.2 Iron Nugget"))); + rewrite(452).repItem(new Item((short) 265, (byte) 1, (short) 0, getNamedTag("1.11.2 Iron Nugget"))); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets.java index 29ff4cf2..231ddc49 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets.java @@ -232,11 +232,11 @@ public class BlockItemPackets extends BlockItemRewriter { @Override protected void registerRewrites() { - rewriteItem(255, new Item((short) 166, (byte) 1, (short) 0, getNamedTag("1.10 Structure Block"))); // Structure block only item since the structure block is in 1.9 - rewriteBlockItem(217, new Item((short) 287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void")), new Block(287, 0)); // Structure void to string - rewriteBlockItem(213, new Item((short) 159, (byte) 1, (short) 1, getNamedTag("1.10 Magma Block")), new Block(159, 1)); // Magma block to orange clay - rewriteBlockItem(214, new Item((short) 159, (byte) 1, (short) 14, getNamedTag("1.10 Nether Ward Block")), new Block(159, 14)); // Nether wart block to red clay - rewriteBlockItem(215, new Item((short) 112, (byte) 1, (short) 0, getNamedTag("1.10 Red Nether Bricks")), new Block(112, 0)); // Red nether brick to nether brick - rewriteBlockItem(216, new Item((short) 155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block")), new Block(155, 0)); // Bone block to quartz + rewrite(255).repItem(new Item((short) 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((short) 287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void"))).repBlock(new Block(287, 0)); // Structure void to string + rewrite(213).repItem(new Item((short) 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((short) 159, (byte) 1, (short) 14, getNamedTag("1.10 Nether Ward Block"))).repBlock(new Block(159, 14)); // Nether wart block to red clay + rewrite(215).repItem(new Item((short) 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((short) 155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block"))).repBlock(new Block(155, 0)); // Bone block to quartz } }