diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index 6e2dcea4..a41e4bfe 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -19,8 +19,11 @@ import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPack import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.EntityPackets1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.PlayerPacket1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.SoundPackets1_13; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.packets.State; @@ -44,7 +47,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { out(State.PLAY, 0x07, 0x07, cancel()); // Statistics TODO MODIFIED - out(State.PLAY, 0x09, 0x09, cancel()); // Update Block Entity TODO MODIFIED out(State.PLAY, 0x0E, 0x0F); // Chat Message (clientbound) out(State.PLAY, 0x11, -1, cancel()); // Declare Commands TODO NEW out(State.PLAY, 0x12, 0x11); // Confirm Transaction (clientbound) @@ -142,6 +144,15 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { // Init protocol in EntityTracker user.get(EntityTracker.class).initProtocol(this); + + // Register Block Storage + if (!user.has(BackwardsBlockStorage.class)) + user.put(new BackwardsBlockStorage(user)); + } + + @Override + protected void register(ViaProviders providers) { + providers.register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider()); } public PacketRemapper cancel() { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/BedHandler.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/BedHandler.java new file mode 100644 index 00000000..b05f7809 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/BedHandler.java @@ -0,0 +1,29 @@ +/* + * 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.protocol.protocol1_12_2to1_13.block_entity_handlers; + +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag; + +public class BedHandler implements BackwardsBlockEntityProvider.BackwardsBlockEntityHandler { + + @Override + public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) { + int offset = blockId - 748; + int color = offset >> 4; + + tag.put(new IntTag("color", color)); + + return tag; + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index 1f350fb7..97c1b064 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -10,12 +10,15 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; -import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter; +import nl.matsv.viabackwards.api.rewriters.Rewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; +import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -28,45 +31,119 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPacke import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; 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; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; + +public class BlockItemPackets1_13 extends Rewriter { + public static int toOldId(int oldId) { + if (oldId < 0) { + oldId = 0; // Some plugins use negative numbers to clear blocks, remap them to air. + } + int newId = BackwardsMappings.blockMappings.getNewBlock(oldId); + if (newId != -1) + return newId; + + Via.getPlatform().getLogger().warning("Missing block completely " + oldId); + // Default stone + return 1 << 4; + } + + //Basic translation for now. TODO remap new items; should probably use BlockItemRewriter#handleItemToClient/Server, but that needs some rewriting + public static void toClient(Item item) { + InventoryPackets.toServer(item); + } + + public static void toServer(Item item) { + InventoryPackets.toClient(item); + } -public class BlockItemPackets1_13 extends BlockItemRewriter { @Override protected void registerPackets(Protocol1_12_2To1_13 protocol) { - //Block Change - protocol.out(State.PLAY, 0x0B, 0x0B, new PacketRemapper() { + // Update Block Entity + protocol.out(State.PLAY, 0x09, 0x09, new PacketRemapper() { @Override public void registerMap() { - map(Type.POSITION); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int blockState = wrapper.read(Type.VAR_INT); - wrapper.write(Type.VAR_INT, toOldId(blockState)); - } - }); - } - }); + map(Type.POSITION); // 0 - Position + map(Type.UNSIGNED_BYTE); // 1 - Action + map(Type.NBT); // 2 - NBT Data - //Multi Block Change - protocol.out(State.PLAY, 0x0F, 0x10, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); - map(Type.INT); - map(Type.BLOCK_CHANGE_RECORD_ARRAY); handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - record.setBlockId(toOldId(record.getBlockId())); + BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class); + + switch (wrapper.get(Type.UNSIGNED_BYTE, 0)) { + case 11: + wrapper.set(Type.NBT, 0, + provider.transform( + wrapper.user(), + wrapper.get(Type.POSITION, 0), + wrapper.get(Type.NBT, 0) + )); + break; + default: + wrapper.cancel(); // TODO CONFIRM EVERYTHING WORKING BEFORE REMOVING THIS + break; } } }); } }); - //Windows Items + // Block Change + protocol.out(State.PLAY, 0x0B, 0x0B, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.POSITION); // 0 - Position + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int blockState = wrapper.read(Type.VAR_INT); + + // Store blocks for + BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); + storage.checkAndStore(wrapper.get(Type.POSITION, 0), blockState); + + wrapper.write(Type.VAR_INT, toOldId(blockState)); + } + }); + } + }); + + // Multi Block Change + protocol.out(State.PLAY, 0x0F, 0x10, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Chunk X + map(Type.INT); // 1 - Chunk Z + map(Type.BLOCK_CHANGE_RECORD_ARRAY); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); + + for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { + int chunkX = wrapper.get(Type.INT, 0); + int chunkZ = wrapper.get(Type.INT, 1); + int block = record.getBlockId(); + Position position = new Position( + (long) (record.getHorizontal() >> 4 & 15) + (chunkX * 16), + (long) record.getY(), + (long) (record.getHorizontal() & 15) + (chunkZ * 16)); + + // Store if needed + storage.checkAndStore(position, block); + + // Change to old id + record.setBlockId(toOldId(block)); + } + } + }); + } + }); + + // Windows Items protocol.out(State.PLAY, 0x15, 0x14, new PacketRemapper() { @Override public void registerMap() { @@ -84,7 +161,7 @@ public class BlockItemPackets1_13 extends BlockItemRewriter> 4; + ChunkSection section = chunk.getSections()[sectionIndex]; + + int x = (int) tag.get("x").getValue(); + int y = (int) tag.get("y").getValue(); + int z = (int) tag.get("z").getValue(); + Position position = new Position((long) x, (long) y, (long) z); + + int block = section.getFlatBlock(x & 0xF, y & 0xF, z & 0xF); + storage.checkAndStore(position, block); + + provider.transform(wrapper.user(), position, tag); + } + + // Rewrite new blocks to old blocks for (int i = 0; i < chunk.getSections().length; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) { @@ -135,7 +241,6 @@ public class BlockItemPackets1_13 extends BlockItemRewriter handlers = new ConcurrentHashMap<>(); + + public BackwardsBlockEntityProvider() { +// handlers.put("minecraft:flower_pot", ); + handlers.put("minecraft:bed", new BedHandler()); +// handlers.put("minecraft:banner", ); +// handlers.put("minecraft:skull", ); +// handlers.put("minecraft:mob_spawner", ); + } + + /** + * Check if a block entity handler is present + * + * @param key Id of the NBT data ex: minecraft:bed + * @return true if present + */ + public boolean isHandled(String key) { + return handlers.containsKey(key); + } + + /** + * Transform blocks to block entities! + * + * @param user The user + * @param position The position of the block entity + * @param tag The block entity tag + */ + public CompoundTag transform(UserConnection user, Position position, CompoundTag tag) throws Exception { + String id = (String) tag.get("id").getValue(); + BackwardsBlockEntityHandler handler = handlers.get(id); + if (handler == null) { + if (Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag); + } + return tag; + } + + BackwardsBlockStorage storage = user.get(BackwardsBlockStorage.class); + + if (!storage.contains(position)) { + if (Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("Handled BlockEntity does not have a stored block :( " + id + " full tag: " + tag); + } + return tag; + } + + return handler.transform(user, storage.get(position), tag); + } + + interface BackwardsBlockEntityHandler { + CompoundTag transform(UserConnection user, int blockId, CompoundTag tag); + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java new file mode 100644 index 00000000..1845b3b4 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java @@ -0,0 +1,81 @@ +/* + * 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.protocol.protocol1_12_2to1_13.storage; + +import com.google.common.collect.Sets; +import us.myles.ViaVersion.api.data.StoredObject; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.Position; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +public class BackwardsBlockStorage extends StoredObject { + // This BlockStorage is very exclusive (; + private static final Set whitelist = Sets.newConcurrentHashSet(); + + static { + // Flower pots +// whitelist.add(5266); + + // Add those beds + for (int i = 0; i < (16 * 16); i++) + whitelist.add(748 + i); + + // Add the white banners +// for (int i = 0; i < 20; i++) +// whitelist.add(6854 + i); +// + // Add the white wall banners +// for (int i = 0; i < 4; i++) { +// whitelist.add(7110 + i); +// } + + // Skeleton skulls +// for (int i = 0; i < 5; i++) +// whitelist.add(5447 + i); + } + + private Map blocks = new ConcurrentHashMap<>(); + + public BackwardsBlockStorage(UserConnection user) { + super(user); + } + + public void checkAndStore(Position position, int block) { + if (!whitelist.contains(block)) { + // Remove if not whitelisted + if (blocks.containsKey(position)) + blocks.remove(position); + return; + } + + blocks.put(position, block); + } + + public boolean isWelcome(int block) { + return whitelist.contains(block); + } + + public boolean contains(Position position) { + return blocks.containsKey(position); + } + + public int get(Position position) { + return blocks.get(position); + } + + public int remove(Position position) { + return blocks.remove(position); + } + +} 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 c0fa2cc6..1ceea5af 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 @@ -20,7 +20,7 @@ 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? + // TODO checkAndStore all rewriters and make them easy accessible? private EntityPackets1_12 entityPackets; private BlockItemPackets1_12 blockItemPackets;