From 2a1f6602bae89109e3227804ab5a6d76c818f7fd Mon Sep 17 00:00:00 2001 From: Matsv Date: Sat, 10 Jun 2017 16:15:37 +0200 Subject: [PATCH] Fix the Item Rewriter, implement 1.12 block rewrites & handle fallingblocks --- .../api/entities/types/EntityType1_12.java | 4 +- .../api/rewriters/BlockItemRewriter.java | 21 +- .../Protocol1_11_1To1_12.java | 4 +- .../packets/BlockItemPackets1_12.java | 245 ++++++++++++++++++ .../packets/ChangedPacketIds.java | 6 +- .../packets/EntityPackets1_12.java | 24 +- .../nl/matsv/viabackwards/utils/Block.java | 4 + 7 files changed, 289 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/types/EntityType1_12.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/types/EntityType1_12.java index ec814273..bbaa53a2 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/types/EntityType1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/types/EntityType1_12.java @@ -72,8 +72,8 @@ public class EntityType1_12 { ENTITY_ILLAGER_ABSTRACT(-1, ENTITY_INSENTIENT), EVOCATION_ILLAGER(34, ENTITY_ILLAGER_ABSTRACT), VEX(35, ENTITY_INSENTIENT), - VINDICATION_ILLAGER(36, ENTITY_INSENTIENT), - ILLUSION_ILLAGER(37, ENTITY_ILLAGER_ABSTRACT), + VINDICATION_ILLAGER(36, ENTITY_ILLAGER_ABSTRACT), + ILLUSION_ILLAGER(37, EVOCATION_ILLAGER), // Vehicles MINECART_ABSTRACT(-1, ENTITY), 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 22ff1771..84bc228a 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 @@ -45,21 +45,22 @@ public abstract class BlockItemRewriter extends Rew return i; BlockItemSettings data = replacementData.get((int) i.getId()); + Item original = ItemUtil.copyItem(i); if (data.hasRepItem()) { i = ItemUtil.copyItem(data.getRepItem()); if (i.getTag() == null) i.setTag(new CompoundTag("")); - i.getTag().put(createViaNBT(i)); + i.getTag().put(createViaNBT(original)); - if (i.getTag() != null) - for (Tag ai : i.getTag()) - i.getTag().put(ai); - i.setAmount(i.getAmount()); + i.setAmount(original.getAmount()); + // Keep original data when -1 + if (i.getData() == -1) + i.setData(original.getData()); } if (data.hasItemTagHandler()) { if (!i.getTag().contains("ViaBackwards|" + getProtocolName())) - i.getTag().put(createViaNBT(i)); + i.getTag().put(createViaNBT(original)); data.getItemHandler().handle(i); } @@ -88,7 +89,7 @@ public abstract class BlockItemRewriter extends Rew return item; } - protected int handleBlockID(int idx) { + public int handleBlockID(int idx) { int type = idx >> 4; int meta = idx & 15; @@ -99,13 +100,13 @@ public abstract class BlockItemRewriter extends Rew return (b.getId() << 4 | (b.getData() & 15)); } - protected Block handleBlock(int block, int data) { + public Block handleBlock(int block, int data) { if (!containsBlock(block)) return null; - Block b = replacementData.get(block).getRepBlock(); + Block b = replacementData.get(block).getRepBlock().clone(); // For some blocks, the data can still be useful (: - if (b.getData() != -1) + if (b.getData() == -1) b.setData(data); return b; } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/Protocol1_11_1To1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/Protocol1_11_1To1_12.java index 083fff48..e4454c4f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/Protocol1_11_1To1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/Protocol1_11_1To1_12.java @@ -22,14 +22,16 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @Getter public class Protocol1_11_1To1_12 extends BackwardsProtocol { + // TODO store all rewriters and make them easy accessible? private EntityPackets1_12 entityPackets; + private BlockItemPackets1_12 blockItemPackets; @Override protected void registerPackets() { new ChangedPacketIds().register(this); (entityPackets = new EntityPackets1_12()).register(this); + (blockItemPackets = new BlockItemPackets1_12()).register(this); new SoundPackets1_12().register(this); - new BlockItemPackets1_12().register(this); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/BlockItemPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/BlockItemPackets1_12.java index a55cc6c8..ac678c6a 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/BlockItemPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/BlockItemPackets1_12.java @@ -12,15 +12,241 @@ package nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.packets; import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter; import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.Protocol1_11_1To1_12; +import nl.matsv.viabackwards.utils.Block; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_12; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.chunks.Chunk1_9_3_4; +import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; +import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class BlockItemPackets1_12 extends BlockItemRewriter { @Override protected void registerPackets(Protocol1_11_1To1_12 protocol) { + /* Item packets */ + + // Set slot packet + protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.BYTE); // 0 - Window ID + map(Type.SHORT); // 1 - Slot ID + map(Type.ITEM); // 2 - Slot Value + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item stack = wrapper.get(Type.ITEM, 0); + wrapper.set(Type.ITEM, 0, handleItemToClient(stack)); + } + }); + } + }); + + // Window items packet + protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); // 0 - Window ID + map(Type.ITEM_ARRAY); // 1 - Window Values + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0); + for (int i = 0; i < stacks.length; i++) + stacks[i] = handleItemToClient(stacks[i]); + } + }); + } + }); + + // Entity Equipment Packet + protocol.registerOutgoing(State.PLAY, 0x3E, 0x3C, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + map(Type.VAR_INT); // 1 - Slot ID + map(Type.ITEM); // 2 - Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item stack = wrapper.get(Type.ITEM, 0); + wrapper.set(Type.ITEM, 0, handleItemToClient(stack)); + } + }); + } + }); + + // Plugin message Packet -> Trading + protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); // 0 - Channel + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID + + int size = wrapper.passthrough(Type.BYTE); + for (int i = 0; i < size; i++) { + wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Input Item + wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Output Item + + boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item + if (secondItem) + wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Second Item + + wrapper.passthrough(Type.BOOLEAN); // Trade disabled + wrapper.passthrough(Type.INT); // Number of tools uses + wrapper.passthrough(Type.INT); // Maximum number of trade uses + } + } + } + }); + } + }); + + // Click window packet + protocol.registerIncoming(State.PLAY, 0x08, 0x07, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); // 0 - Window ID + map(Type.SHORT); // 1 - Slot + map(Type.BYTE); // 2 - Button + map(Type.SHORT); // 3 - Action number + map(Type.VAR_INT); // 4 - Mode + map(Type.ITEM); // 5 - Clicked Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item item = wrapper.get(Type.ITEM, 0); + handleItemToServer(item); + } + }); + } + } + ); + + // Creative Inventory Action + protocol.registerIncoming(State.PLAY, 0x1B, 0x18, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.SHORT); // 0 - Slot + map(Type.ITEM); // 1 - Clicked Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item item = wrapper.get(Type.ITEM, 0); + handleItemToServer(item); + } + }); + } + } + ); + + /* Block packets */ + + // Chunk packet + protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { + @Override + public void registerMap() { + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + + Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.9.4 Chunk type since nothing changed. + Chunk1_9_3_4 chunk = (Chunk1_9_3_4) wrapper.passthrough(type); + + handleChunk(chunk); + } + }); + } + } + ); + + // Block Change Packet + protocol.registerOutgoing(State.PLAY, 0x0B, 0x0B, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.POSITION); // 0 - Block Position + map(Type.VAR_INT); // 1 - Block + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int idx = wrapper.get(Type.VAR_INT, 0); + wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); + } + }); + } + } + ); + + // Multi Block Change Packet + protocol.registerOutgoing(State.PLAY, 0x10, 0x10, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Chunk X + map(Type.INT); // 1 - Chunk Z + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int count = wrapper.passthrough(Type.VAR_INT); // Array length + + for (int i = 0; i < count; i++) { + wrapper.passthrough(Type.UNSIGNED_BYTE); // Horizontal position + wrapper.passthrough(Type.UNSIGNED_BYTE); // Y coords + + int id = wrapper.read(Type.VAR_INT); // Block ID + wrapper.write(Type.VAR_INT, handleBlockID(id)); + } + } + }); + } + } + ); + + // Update Block Entity + protocol.registerOutgoing(State.PLAY, 0x09, 0x09, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.POSITION); // 0 - Position + map(Type.UNSIGNED_BYTE); // 1 - Action + map(Type.NBT); // 2 - NBT + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + // Remove bed color + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) + wrapper.cancel(); + } + }); + } + }); + + protocol.getEntityPackets().registerMetaHandler().handle(e -> { + Metadata data = e.getData(); + + if (data.getMetaType().equals(MetaType1_12.Slot)) // Is Item + data.setValue(handleItemToClient((Item) data.getValue())); + + return data; + }); + // Client Status protocol.registerIncoming(State.PLAY, 0x04, 0x03, new PacketRemapper() { @Override @@ -41,6 +267,25 @@ public class BlockItemPackets1_12 extends BlockItemRewriter Stained clay? (Also got a new name Terracota?) + rewrite(251) + .repItem(new Item((short) 159, (byte) 1, (short) -1, getNamedTag("1.12 Concrete"))) + .repBlock(new Block(159, -1)); // TODO color provided by name + + // Concrete Powder -> Wool + rewrite(252) + .repItem(new Item((short) 35, (byte) 1, (short) -1, getNamedTag("1.12 Concrete Powder"))) + .repBlock(new Block(35, -1)); // TODO color provided by name + + // Knowledge book -> book + rewrite(453) + .repItem(new Item((short) 340, (byte) 1, (short) 0, getNamedTag("1.12 Knowledge Book"))); // TODO glow + + // Glazed Terracotta -> Stained Clay + for (int i = 235; i < 250; i++) { + rewrite(i).repItem(new Item((short) 159, (byte) 1, (short) (i - 235), getNamedTag("1.12 Glazed Terracotta"))) + .repBlock(new Block(159, (i - 235))); // TODO color provided by name + } } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/ChangedPacketIds.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/ChangedPacketIds.java index d0de0f71..5435a90e 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/ChangedPacketIds.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/ChangedPacketIds.java @@ -58,7 +58,7 @@ public class ChangedPacketIds extends Rewriter { // 0x3B -> 0x39 Entity Metadata handled in EntityPackets1_12.java p.registerOutgoing(State.PLAY, 0x3C, 0x3A); // Attach Entity p.registerOutgoing(State.PLAY, 0x3D, 0x3B); // Entity Velocity - p.registerOutgoing(State.PLAY, 0x3E, 0x3C); // Entity Equipment + // 0x3E -> 0x3C Entity Equipment handled in BlockItemPackets1_12.java p.registerOutgoing(State.PLAY, 0x3F, 0x3D); // Set Experience p.registerOutgoing(State.PLAY, 0x40, 0x3E); // Update Health p.registerOutgoing(State.PLAY, 0x41, 0x3F); // ScoreBoard Objective @@ -93,7 +93,7 @@ public class ChangedPacketIds extends Rewriter { p.registerIncoming(State.PLAY, 0x05, 0x04); // Client Settings p.registerIncoming(State.PLAY, 0x06, 0x05); // Confirm Transaction (Serverbound) p.registerIncoming(State.PLAY, 0x07, 0x06); // Enchant Item - p.registerIncoming(State.PLAY, 0x08, 0x07); // Click Window + // 0x08 -> 0x07 Click Window handled in BlockItemPackets1_12.java p.registerIncoming(State.PLAY, 0x09, 0x08); // Close Window (Serverbound) p.registerIncoming(State.PLAY, 0x0A, 0x09); // Plugin message (Serverbound) p.registerIncoming(State.PLAY, 0x0B, 0x0A); // Use Entity @@ -112,7 +112,7 @@ public class ChangedPacketIds extends Rewriter { p.registerIncoming(State.PLAY, 0x18, 0x16); // Resource Pack Status p.registerIncoming(State.PLAY, 0x19, -1); // Advancement Tab p.registerIncoming(State.PLAY, 0x1A, 0x17); // Held Item Change (Serverbound) - p.registerIncoming(State.PLAY, 0x1B, 0x18); // Creative Inventory Action + // 0x1B -> 0x18 Creative Inventory Action handled in BlockItemPackets.java p.registerIncoming(State.PLAY, 0x1C, 0x19); // Update Sign p.registerIncoming(State.PLAY, 0x1D, 0x1A); // Animatin (Serverbound) p.registerIncoming(State.PLAY, 0x1E, 0x1B); // Spectate diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/EntityPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/EntityPackets1_12.java index 98f65530..6f9ac943 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/EntityPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12to1_11_1/packets/EntityPackets1_12.java @@ -17,6 +17,7 @@ import nl.matsv.viabackwards.api.entities.types.AbstractEntityType; import nl.matsv.viabackwards.api.entities.types.EntityType1_12; import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.Protocol1_11_1To1_12; +import nl.matsv.viabackwards.utils.Block; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -81,6 +82,23 @@ public class EntityPackets1_12 extends EntityRewriter { } } }); + + // Replace falling blocks + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Optional type = ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (type.isPresent() && type.get().equals(ObjectType.FALLING_BLOCK)) { + int objectData = wrapper.get(Type.INT, 0); + int objType = objectData & 4095; + int data = objectData >> 12 & 15; + + Block block = getProtocol().getBlockItemPackets().handleBlock(objType, data); + + wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); + } + } + }); } }); @@ -344,9 +362,9 @@ public class EntityPackets1_12 extends EntityRewriter { regEntType(EntityType.PARROT, EntityType.BAT).spawnMetadata(storage -> storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); regEntType(EntityType.ILLUSION_ILLAGER, EntityType.EVOCATION_ILLAGER); - // Handle Illager TODO wtf does this metadata do? - registerMetaHandler().filter(EntityType.ENTITY_ILLAGER_ABSTRACT, true, 12).removed(); - registerMetaHandler().filter(EntityType.ENTITY_ILLAGER_ABSTRACT, true, 13).handleIndexChange(12); + // Handle Illager TODO wtf does this metadata do? Is aggresive it is a bitmask? + registerMetaHandler().filter(EntityType.EVOCATION_ILLAGER, true, 12).removed(); + registerMetaHandler().filter(EntityType.EVOCATION_ILLAGER, true, 13).handleIndexChange(12); // Parrot remove animal metadata registerMetaHandler().filter(EntityType.PARROT, 12).removed(); // Is baby 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 c3fce2df..70797560 100644 --- a/core/src/main/java/nl/matsv/viabackwards/utils/Block.java +++ b/core/src/main/java/nl/matsv/viabackwards/utils/Block.java @@ -20,4 +20,8 @@ import lombok.EqualsAndHashCode; public class Block { private int id; private int data; + + public Block clone() { + return new Block(id, data); + } }