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 37420414..dd04b730 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 @@ -11,23 +11,27 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPackets1_13; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; 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_3to1_9_1_2.storage.ClientWorld; public class Protocol1_12_2To1_13 extends BackwardsProtocol { @Override protected void registerPackets() { + new BlockItemPackets1_13().register(this); + // Thanks to https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14150 out(State.PLAY, 0x00, 0x00, cancel()); - // Spawn Painting TODO MODIFIED - out(State.PLAY, 0x04, 0x04, cancel()); - // Statistics TODO MODIFIED - out(State.PLAY, 0x07, 0x07, cancel()); + out(State.PLAY, 0x04, 0x04, cancel());// Spawn Painting TODO MODIFIED + out(State.PLAY, 0x07, 0x07, cancel()); // Statistics TODO MODIFIED out(State.PLAY, 0x09, 0x09, cancel()); // Update Block Entity TODO MODIFIED out(State.PLAY, 0x0B, 0x0B, cancel()); // Block Change TODO MODIFIED out(State.PLAY, 0x0E, 0x0F); // Chat Message (clientbound) @@ -41,7 +45,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { out(State.PLAY, 0x16, 0x15, cancel()); // Window Property out(State.PLAY, 0x17, 0x16, cancel()); // Set Slot out(State.PLAY, 0x18, 0x17); // Set Cooldown - out(State.PLAY, 0x19, 0x18, cancel()); // Plugin Message (clientbound) TODO MODIFIED + out(State.PLAY, 0x19, 0x18); // Plugin Message (clientbound) TODO MODIFIED out(State.PLAY, 0x1A, 0x19, cancel()); // Named Sound Effect TODO MODIFIED out(State.PLAY, 0x1B, 0x1A); // Disconnect (play) out(State.PLAY, 0x1C, 0x1B); // Entity Status @@ -50,10 +54,30 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { out(State.PLAY, 0x1F, 0x1D); // Unload Chunk out(State.PLAY, 0x20, 0x1E); // Change Game State out(State.PLAY, 0x21, 0x1F); // Keep Alive (clientbound) - out(State.PLAY, 0x22, 0x20, cancel()); // Chunk Data TODO MODIFIED + + // Chunk Data -> moved to BlockItemPackets + + + out(State.PLAY, 0x23, 0x21, cancel()); // Effect TODO MODIFIED out(State.PLAY, 0x24, 0x22, cancel()); // Spawn Particle TODO MODIFIED - out(State.PLAY, 0x25, 0x23); // Join Game + out(State.PLAY, 0x25, 0x23, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Entity ID + map(Type.UNSIGNED_BYTE); // 1 - Gamemode + map(Type.INT); // 2 - Dimension + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 1); + clientChunks.setEnvironment(dimensionId); + } + }); + } + }); // Join Game out(State.PLAY, 0x26, 0x24, cancel()); // Map TODO MODIFIED out(State.PLAY, 0x27, 0x25); // Entity out(State.PLAY, 0x28, 0x26); // Entity Relative Move @@ -147,7 +171,10 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { } @Override - public void init(UserConnection userConnection) { + public void init(UserConnection user) { + // Register ClientWorld + if (!user.has(ClientWorld.class)) + user.put(new ClientWorld(user)); } @@ -164,4 +191,9 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { } }; } + + static { + BackwardsMappings.init(); + } + } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java new file mode 100644 index 00000000..34d146ca --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java @@ -0,0 +1,75 @@ +/* + * 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.data; + +import us.myles.ViaVersion.api.Via; +import us.myles.viaversion.libs.gson.JsonElement; +import us.myles.viaversion.libs.gson.JsonObject; + +import java.util.Arrays; +import java.util.Map; + +import static us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData.loadData; + +public class BackwardsMappings { + public static BlockMappings blockMappings; + + public static void init() { + us.myles.viaversion.libs.gson.JsonObject mapping1_12 = loadData("mapping-1.12.json"); + us.myles.viaversion.libs.gson.JsonObject mapping1_13 = loadData("mapping-1.13.json"); + + Via.getPlatform().getLogger().info("Loading block mapping..."); + blockMappings = new BlockMappingsShortArray(mapping1_13.getAsJsonObject("blocks"), mapping1_12.getAsJsonObject("blocks")); + } + + + private static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers) { + for (Map.Entry entry : oldIdentifiers.entrySet()) { + Map.Entry value = findValue(newIdentifiers, entry.getValue().getAsString()); + if (value == null) { + if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); + } + continue; + } + output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); + } + } + + + private static Map.Entry findValue(JsonObject object, String needle) { + for (Map.Entry entry : object.entrySet()) { + String value = entry.getValue().getAsString(); + if (value.equals(needle)) { + return entry; + } + } + return null; + } + + public interface BlockMappings { + int getNewBlock(int old); + } + + private static class BlockMappingsShortArray implements BlockMappings { + private short[] oldToNew = new short[4084 * 6]; + + private BlockMappingsShortArray(JsonObject oldMapping, JsonObject newMapping) { + Arrays.fill(oldToNew, (short) -1); + mapIdentifiers(oldToNew, oldMapping, newMapping); + } + + @Override + public int getNewBlock(int old) { + return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; + } + } +} 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 new file mode 100644 index 00000000..e3310d8b --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -0,0 +1,91 @@ +/* + * 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.packets; + +import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter; +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 us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.minecraft.chunks.Chunk; +import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.packets.State; +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; + +public class BlockItemPackets1_13 extends BlockItemRewriter { + @Override + protected void registerPackets(Protocol1_12_2To1_13 protocol) { + + // Chunk packet + protocol.out(State.PLAY, 0x22, 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_old = new Chunk1_9_3_4Type(clientWorld); + Chunk1_13Type type = new Chunk1_13Type(clientWorld); + Chunk chunk = wrapper.read(type); + + for (int i = 0; i < chunk.getSections().length; i++) { + ChunkSection section = chunk.getSections()[i]; + if (section == null) { + continue; + } + + for (int p = 0; p < section.getPalette().size(); p++) { + int old = section.getPalette().get(p); + if (old != 0) { + section.getPalette().set(p, toNewId(old)); + } + } + } + + // Rewrite biome id 255 to plains + if (chunk.isBiomeData()) { + for (int i = 0; i < 256; i++) { + chunk.getBiomeData()[i] = 1; // Plains + } + } + + chunk.getBlockEntities().clear(); + wrapper.write(type_old, chunk); + } + }); + } + } + ); + } + + public static int toNewId(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; + } + + @Override + protected void registerRewrites() { + + } +}