From 82c01c4b2dc00734d6a86c85987e305315d091bf Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 12 Feb 2023 12:17:15 +0100 Subject: [PATCH] Greatly reduce the number of inner classes --- .../viabackwards/ViaBackwardsConfig.java | 3 +- .../api/ViaBackwardsPlatform.java | 1 - .../api/data/BackwardsMappings.java | 3 +- .../api/data/VBMappingDataLoader.java | 3 +- .../viabackwards/api/data/VBMappings.java | 1 - .../api/entities/storage/EntityData.java | 3 +- .../storage/EntityPositionHandler.java | 1 - .../api/entities/storage/WrappedMetadata.java | 3 +- .../api/rewriters/EnchantmentRewriter.java | 1 - .../api/rewriters/EntityRewriter.java | 14 +- .../api/rewriters/EntityRewriterBase.java | 4 +- .../api/rewriters/ItemRewriter.java | 6 +- .../rewriters/LegacyBlockItemRewriter.java | 3 +- .../rewriters/LegacyEnchantmentRewriter.java | 1 - .../api/rewriters/LegacyEntityRewriter.java | 17 +- .../api/rewriters/SoundRewriter.java | 10 +- .../api/rewriters/TranslatableRewriter.java | 40 +- .../packets/BlockItemPackets1_11.java | 252 ++--- .../packets/EntityPackets1_11.java | 65 +- .../packets/PlayerPackets1_11.java | 14 +- .../Protocol1_11_1To1_12.java | 16 +- .../packets/BlockItemPackets1_12.java | 160 ++- .../packets/ChatPackets1_12.java | 12 +- .../packets/EntityPackets1_12.java | 73 +- .../packets/SoundPackets1_12.java | 37 +- .../packets/EntityPackets1_11_1.java | 14 +- .../packets/ItemPackets1_11_1.java | 39 +- .../Protocol1_12_1To1_12_2.java | 44 +- .../block_entity_handlers/PistonHandler.java | 1 - .../data/EntityTypeMapping.java | 1 - .../data/NamedSoundMapping.java | 1 - .../data/ParticleMapping.java | 1 - .../packets/BlockItemPackets1_13.java | 508 +++++---- .../packets/EntityPackets1_13.java | 196 ++-- .../packets/PlayerPacket1_13.java | 986 +++++++++--------- .../packets/SoundPackets1_13.java | 74 +- .../storage/BackwardsBlockStorage.java | 1 - .../storage/TabCompleteStorage.java | 1 - .../Protocol1_13_1To1_13_2.java | 68 +- .../packets/EntityPackets1_13_2.java | 43 +- .../packets/InventoryPackets1_13_2.java | 127 +-- .../packets/WorldPackets1_13_2.java | 19 +- .../Protocol1_13_2To1_14.java | 173 ++- .../packets/BlockItemPackets1_14.java | 571 +++++----- .../packets/EntityPackets1_14.java | 205 ++-- .../packets/PlayerPackets1_14.java | 103 +- .../packets/SoundPackets1_14.java | 62 +- .../storage/ChunkLightStorage.java | 1 - .../Protocol1_13To1_13_1.java | 140 ++- .../packets/EntityPackets1_13_1.java | 61 +- .../packets/InventoryPackets1_13_1.java | 55 +- .../packets/WorldPackets1_13_1.java | 38 +- .../Protocol1_14_2To1_14_3.java | 88 +- .../Protocol1_14_3To1_14_4.java | 48 +- .../Protocol1_14_4To1_15.java | 6 +- .../packets/BlockItemPackets1_15.java | 116 +-- .../packets/EntityPackets1_15.java | 48 +- .../packets/EntityPackets1_14_1.java | 30 +- .../Protocol1_15_2To1_16.java | 112 +- .../chat/TagSerializer.java | 1 - .../data/BackwardsMappings.java | 1 - .../packets/BlockItemPackets1_16.java | 235 ++--- .../packets/EntityPackets1_16.java | 51 +- .../Protocol1_16_1To1_16_2.java | 79 +- .../data/BiomeMappings.java | 1 - .../packets/BlockItemPackets1_16_2.java | 118 +-- .../packets/EntityPackets1_16_2.java | 18 +- .../Protocol1_16_3To1_16_4.java | 17 +- .../Protocol1_16_4To1_17.java | 181 ++-- .../packets/BlockItemPackets1_17.java | 206 ++-- .../packets/EntityPackets1_17.java | 35 +- .../Protocol1_17_1To1_18.java | 22 +- .../packets/BlockItemPackets1_18.java | 167 ++- .../packets/EntityPackets1_18.java | 10 +- .../Protocol1_17To1_17_1.java | 188 ++-- .../Protocol1_18_2To1_19.java | 97 +- .../data/BackwardsMappings.java | 3 +- .../packets/BlockItemPackets1_19.java | 88 +- .../packets/EntityPackets1_19.java | 71 +- .../storage/DimensionRegistryStorage.java | 3 +- .../storage/StoredPainting.java | 1 - .../Protocol1_18To1_18_2.java | 21 +- .../Protocol1_19_1To1_19_3.java | 240 ++--- .../packets/BlockItemPackets1_19_3.java | 135 ++- .../packets/EntityPackets1_19_3.java | 183 ++-- .../storage/ChatSessionStorage.java | 1 - .../storage/NonceStorage.java | 2 +- .../Protocol1_19_3To1_19_4.java | 6 +- .../packets/EntityPackets1_19_4.java | 14 +- .../Protocol1_19To1_19_1.java | 162 ++- .../storage/NonceStorage.java | 2 +- .../storage/ReceivedMessagesStorage.java | 1 - .../Protocol1_9_4To1_10.java | 14 +- .../packets/BlockItemPackets1_10.java | 98 +- .../packets/EntityPackets1_10.java | 65 +- .../viabackwards/ViaFabricAddon.java | 5 +- .../viaversion/viabackwards/SpongePlugin.java | 7 +- .../viabackwards/VelocityPlugin.java | 1 - 98 files changed, 3210 insertions(+), 4064 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java index 7d947ef1..eeeb7d92 100644 --- a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java @@ -18,14 +18,13 @@ package com.viaversion.viabackwards; import com.viaversion.viaversion.util.Config; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.io.File; import java.net.URL; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; public class ViaBackwardsConfig extends Config implements com.viaversion.viabackwards.api.ViaBackwardsConfig { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java index 88e89425..628ebe68 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java @@ -55,7 +55,6 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.protocol.ProtocolManager; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.update.Version; - import java.io.File; import java.util.logging.Logger; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java index 3d16b433..98e30a34 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java @@ -26,10 +26,9 @@ import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.gson.JsonObject; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.Map; import java.util.logging.Logger; +import org.checkerframework.checker.nullness.qual.Nullable; public class BackwardsMappings extends MappingDataBase { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java index 48dd9725..94b96df4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java @@ -29,8 +29,6 @@ import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.libs.gson.JsonSyntaxException; import com.viaversion.viaversion.util.GsonUtil; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -38,6 +36,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; public final class VBMappingDataLoader { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java index 7c652605..6430fea4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java @@ -20,7 +20,6 @@ package com.viaversion.viabackwards.api.data; import com.viaversion.viaversion.api.data.IntArrayMappings; import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.Mappings; - import java.util.Arrays; public final class VBMappings extends IntArrayMappings { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java index a3b8c218..e117aecd 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java @@ -22,9 +22,8 @@ import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.Locale; +import org.checkerframework.checker.nullness.qual.Nullable; public class EntityData { private final BackwardsProtocol protocol; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java index 77db17b4..6bf6f13c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java @@ -24,7 +24,6 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; - import java.util.function.Supplier; public class EntityPositionHandler { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java index 1ac847ef..3e00577e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java @@ -19,9 +19,8 @@ package com.viaversion.viabackwards.api.entities.storage; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.List; +import org.checkerframework.checker.nullness.qual.Nullable; public final class WrappedMetadata { private final List metadataList; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EnchantmentRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EnchantmentRewriter.java index ec1345f2..c11087a5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EnchantmentRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EnchantmentRewriter.java @@ -25,7 +25,6 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; - import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java index 89074472..ac7d96de 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java @@ -24,7 +24,7 @@ import com.viaversion.viaversion.api.minecraft.metadata.MetaType; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_14; @@ -40,9 +40,9 @@ public abstract class EntityRewriter metadataList, UserConnection connection) { final TrackedEntity entity = tracker(connection).entity(entityId); final boolean initialMetadata = !(entity != null && entity.hasSentMetadata()); + super.handleMetadata(entityId, metadataList, connection); if (entity == null) { return; // Don't handle untracked entities - basically always the fault of a plugin sending virtual entities through concurrency-unsafe handling } - final EntityData entityData = entityDataForType(entity.entityType()); - // Set the mapped entity name if there is no custom name set already + final EntityData entityData = entityDataForType(entity.entityType()); if (entityData != null && entityData.mobName() != null) { final Metadata displayName = getMeta(displayNameIndex, metadataList); if (initialMetadata) { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java index 165f86fd..c2a49d5c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java @@ -22,7 +22,7 @@ import com.viaversion.viabackwards.api.data.MappedItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag; @@ -122,9 +122,9 @@ public class ItemRewriter type) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { handler(wrapper -> { wrapper.passthrough(Type.BOOLEAN); // Reset/clear final int size = wrapper.passthrough(Type.VAR_INT); // Mapping size diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java index a2d5be8b..60bf59d3 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java @@ -42,10 +42,9 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.HashMap; import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; public abstract class LegacyBlockItemRewriter> extends ItemRewriterBase { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java index 4922c606..71ce5c19 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java @@ -25,7 +25,6 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; - import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java index b450d5c9..7aea75bc 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java @@ -30,15 +30,14 @@ import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import org.checkerframework.checker.nullness.qual.Nullable; public abstract class LegacyEntityRewriter> extends EntityRewriterBase { private final Map objectTypes = new HashMap<>(); @@ -62,9 +61,9 @@ public abstract class LegacyEntityRewriter { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); @@ -75,9 +74,9 @@ public abstract class LegacyEntityRewriter> oldMetaType, Type> newMetaType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID if (oldMetaType != null) { map(oldMetaType, newMetaType); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java index 2fe62022..722fb19a 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java @@ -20,7 +20,7 @@ package com.viaversion.viabackwards.api.rewriters; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; public class SoundRewriter extends com.viaversion.viaversion.rewriter.SoundRewriter { @@ -33,9 +33,9 @@ public class SoundRewriter extends com.viaversi } public void registerNamedSound(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // Sound identifier handler(getNamedSoundHandler()); } @@ -43,9 +43,9 @@ public class SoundRewriter extends com.viaversi } public void registerStopSound(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { handler(getStopSoundHandler()); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java index 300389f6..f64a63a0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java @@ -22,12 +22,11 @@ import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.VBMappingDataLoader; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.rewriter.ComponentRewriter; - import java.util.HashMap; import java.util.Map; @@ -63,27 +62,17 @@ public class TranslatableRewriter extends Compo } public void registerPing() { - protocol.registerClientbound(State.LOGIN, 0x00, 0x00, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); + protocol.registerClientbound(State.LOGIN, 0x00, 0x00, wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); } public void registerDisconnect(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); + protocol.registerClientbound(packetType, wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); } public void registerLegacyOpenWindow(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // Id map(Type.STRING); // Window Type handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); @@ -92,9 +81,9 @@ public class TranslatableRewriter extends Compo } public void registerOpenWindow(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Id map(Type.VAR_INT); // Window Type handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); @@ -103,21 +92,16 @@ public class TranslatableRewriter extends Compo } public void registerTabList(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - processText(wrapper.passthrough(Type.COMPONENT)); - processText(wrapper.passthrough(Type.COMPONENT)); - }); - } + protocol.registerClientbound(packetType, wrapper -> { + processText(wrapper.passthrough(Type.COMPONENT)); + processText(wrapper.passthrough(Type.COMPONENT)); }); } public void registerCombatKill(C packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { + protocol.registerClientbound(packetType, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.INT); handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java index e5534274..30f49ef1 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java @@ -34,7 +34,7 @@ import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; @@ -45,7 +45,6 @@ import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1 import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; - import java.util.Arrays; import java.util.Optional; @@ -59,9 +58,9 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter { + Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0); + for (int i = 0; i < stacks.length; i++) + stacks[i] = handleItemToClient(stacks[i]); - if (isLlama(wrapper.user())) { - Optional horse = getChestedHorse(wrapper.user()); - if (!horse.isPresent()) - return; - ChestedHorseStorage storage = horse.get(); - stacks = Arrays.copyOf(stacks, !storage.isChested() ? 38 : 53); + if (isLlama(wrapper.user())) { + Optional horse = getChestedHorse(wrapper.user()); + if (!horse.isPresent()) + return; + ChestedHorseStorage storage = horse.get(); + stacks = Arrays.copyOf(stacks, !storage.isChested() ? 38 : 53); - for (int i = stacks.length - 1; i >= 0; i--) { - stacks[getNewSlotId(storage, i)] = stacks[i]; - stacks[i] = getNewItem(storage, i, stacks[i]); - } - wrapper.set(Type.ITEM_ARRAY, 0, stacks); + for (int i = stacks.length - 1; i >= 0; i--) { + stacks[getNewSlotId(storage, i)] = stacks[i]; + stacks[i] = getNewItem(storage, i, stacks[i]); } + wrapper.set(Type.ITEM_ARRAY, 0, stacks); } }); } @@ -120,40 +116,37 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter Trading - protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 + handler(wrapper -> { + if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_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 + int size = wrapper.passthrough(Type.UNSIGNED_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 + 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 } } }); } }); - protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.SHORT); // 1 - Slot map(Type.BYTE); // 2 - Button @@ -164,19 +157,16 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter horse = getChestedHorse(wrapper.user()); - if (!horse.isPresent()) - return; - ChestedHorseStorage storage = horse.get(); - int clickSlot = wrapper.get(Type.SHORT, 0); - int correctSlot = getOldSlotId(storage, clickSlot); + handler(wrapper -> { + if (isLlama(wrapper.user())) { + Optional horse = getChestedHorse(wrapper.user()); + if (!horse.isPresent()) + return; + ChestedHorseStorage storage = horse.get(); + int clickSlot = wrapper.get(Type.SHORT, 0); + int correctSlot = getOldSlotId(storage, clickSlot); - wrapper.set(Type.SHORT, 0, ((Integer) correctSlot).shortValue()); - } + wrapper.set(Type.SHORT, 0, ((Integer) correctSlot).shortValue()); } }); } @@ -184,151 +174,125 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.10 Chunk type since nothing changed. - Chunk chunk = wrapper.passthrough(type); + Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.10 Chunk type since nothing changed. + Chunk chunk = wrapper.passthrough(type); - handleChunk(chunk); + handleChunk(chunk); - // only patch it for signs for now - for (CompoundTag tag : chunk.getBlockEntities()) { - Tag idTag = tag.get("id"); - if (!(idTag instanceof StringTag)) continue; + // only patch it for signs for now + for (CompoundTag tag : chunk.getBlockEntities()) { + Tag idTag = tag.get("id"); + if (!(idTag instanceof StringTag)) continue; - String id = (String) idTag.getValue(); - if (id.equals("minecraft:sign")) { - ((StringTag) idTag).setValue("Sign"); - } - } - } - }); + String id = (String) idTag.getValue(); + if (id.equals("minecraft:sign")) { + ((StringTag) idTag).setValue("Sign"); + } } }); - protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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)); - } + handler(wrapper -> { + int idx = wrapper.get(Type.VAR_INT, 0); + wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); }); } }); - protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 { - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - record.setBlockId(handleBlockID(record.getBlockId())); - } + handler(wrapper -> { + for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { + record.setBlockId(handleBlockID(record.getBlockId())); } }); } }); - protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 on shulkerbox decleration - if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 10) { - wrapper.cancel(); - } - // Handler Spawners - if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1) { - CompoundTag tag = wrapper.get(Type.NBT, 0); - EntityIdRewriter.toClientSpawner(tag, true); - } + handler(wrapper -> { + // Remove on shulkerbox decleration + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 10) { + wrapper.cancel(); + } + // Handler Spawners + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1) { + CompoundTag tag = wrapper.get(Type.NBT, 0); + EntityIdRewriter.toClientSpawner(tag, true); } }); } }); - protocol.registerClientbound(ClientboundPackets1_9_3.OPEN_WINDOW, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.OPEN_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.STRING); // 1 - Window Type map(Type.COMPONENT); // 2 - Title map(Type.UNSIGNED_BYTE); // 3 - Slots - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = -1; - // Passthrough Entity ID - if (wrapper.get(Type.STRING, 0).equals("EntityHorse")) { - entityId = wrapper.passthrough(Type.INT); - } + handler(wrapper -> { + int entityId = -1; + // Passthrough Entity ID + if (wrapper.get(Type.STRING, 0).equals("EntityHorse")) { + entityId = wrapper.passthrough(Type.INT); + } - // Track Inventory - String inventory = wrapper.get(Type.STRING, 0); - WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); - windowTracker.setInventory(inventory); - windowTracker.setEntityId(entityId); + // Track Inventory + String inventory = wrapper.get(Type.STRING, 0); + WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); + windowTracker.setInventory(inventory); + windowTracker.setEntityId(entityId); - // Change llama slotcount to the donkey one - if (isLlama(wrapper.user())) { - wrapper.set(Type.UNSIGNED_BYTE, 1, (short) 17); - } + // Change llama slotcount to the donkey one + if (isLlama(wrapper.user())) { + wrapper.set(Type.UNSIGNED_BYTE, 1, (short) 17); } }); } }); - protocol.registerClientbound(ClientboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.CLOSE_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { // Inventory tracking - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); - windowTracker.setInventory(null); - windowTracker.setEntityId(-1); - } + handler(wrapper -> { + WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); + windowTracker.setInventory(null); + windowTracker.setEntityId(-1); }); } }); - protocol.registerServerbound(ServerboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_9_3.CLOSE_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { // Inventory tracking - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); - windowTracker.setInventory(null); - windowTracker.setEntityId(-1); - } + handler(wrapper -> { + WindowTracker windowTracker = wrapper.user().get(WindowTracker.class); + windowTracker.setInventory(null); + windowTracker.setEntityId(-1); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java index df6b62ef..8a4dfa2e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java @@ -31,13 +31,10 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; - import java.util.List; import java.util.Optional; @@ -49,9 +46,9 @@ public class EntityPackets1_11 extends LegacyEntityRewriter Entity1_11Types.ObjectType.findById(id).orElse(null))); // Handle FallingBlock blocks - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); - if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { - int objectData = wrapper.get(Type.INT, 0); - int objType = objectData & 4095; - int data = objectData >> 12 & 15; + handler(wrapper -> { + Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { + int objectData = wrapper.get(Type.INT, 0); + int objType = objectData & 4095; + int data = objectData >> 12 & 15; - Block block = protocol.getItemRewriter().handleBlock(objType, data); - if (block == null) - return; + Block block = protocol.getItemRewriter().handleBlock(objType, data); + if (block == null) + return; - wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); - } + wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); } }); } @@ -113,9 +107,9 @@ public class EntityPackets1_11 extends LegacyEntityRewriter { + byte b = wrapper.get(Type.BYTE, 0); - if (b == 35) { - wrapper.clearPacket(); - wrapper.setPacketType(ClientboundPackets1_9_3.GAME_EVENT); - wrapper.write(Type.UNSIGNED_BYTE, (short) 10); // Play Elder Guardian animation - wrapper.write(Type.FLOAT, 0F); - } + if (b == 35) { + wrapper.clearPacket(); + wrapper.setPacketType(ClientboundPackets1_9_3.GAME_EVENT); + wrapper.write(Type.UNSIGNED_BYTE, (short) 10); // Play Elder Guardian animation + wrapper.write(Type.FLOAT, 0F); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/PlayerPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/PlayerPackets1_11.java index 9bbed07d..831a3a9d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/PlayerPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/PlayerPackets1_11.java @@ -20,7 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_10to1_11.packets; import com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -39,9 +39,9 @@ public class PlayerPackets1_11 { }; public void register(Protocol1_10To1_11 protocol) { - protocol.registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Action handler(wrapper -> { @@ -67,9 +67,9 @@ public class PlayerPackets1_11 { } }); - protocol.registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Collected entity id map(Type.VAR_INT); // 1 - Collector entity id @@ -78,9 +78,9 @@ public class PlayerPackets1_11 { }); - protocol.registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION); // 0 - Location map(Type.VAR_INT); // 1 - Face map(Type.VAR_INT); // 2 - Hand diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java index 59b57a76..8f65dfdd 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java @@ -27,7 +27,6 @@ import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.EntityP import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -60,16 +59,11 @@ public class Protocol1_11_1To1_12 extends BackwardsProtocol { - int action = wrapper.passthrough(Type.VAR_INT); - if (action >= 0 && action <= 2) { - JsonElement component = wrapper.read(Type.COMPONENT); - wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(component.toString())); - } - }); + registerClientbound(ClientboundPackets1_12.TITLE, wrapper -> { + int action = wrapper.passthrough(Type.VAR_INT); + if (action >= 0 && action <= 2) { + JsonElement component = wrapper.read(Type.COMPONENT); + wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(component.toString())); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java index a09d5c39..a53c5bf0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java @@ -25,8 +25,7 @@ import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag; @@ -37,10 +36,9 @@ import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ServerboundPacke import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.Iterator; import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; public class BlockItemPackets1_12 extends LegacyBlockItemRewriter { @@ -50,9 +48,9 @@ public class BlockItemPackets1_12 extends LegacyBlockItemRewriter Trading - protocol.registerClientbound(ClientboundPackets1_12.PLUGIN_MESSAGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.PLUGIN_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 + handler(wrapper -> { + if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_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 + int size = wrapper.passthrough(Type.UNSIGNED_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 + 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 - } + wrapper.passthrough(Type.BOOLEAN); // Trade disabled + wrapper.passthrough(Type.INT); // Number of tools uses + wrapper.passthrough(Type.INT); // Maximum number of trade uses } } }); } }); - protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.SHORT); // 1 - Slot map(Type.BYTE); // 2 - Button @@ -127,102 +122,82 @@ public class BlockItemPackets1_12 extends LegacyBlockItemRewriter { + if (wrapper.get(Type.VAR_INT, 0) == 1) { // Shift click + // https://github.com/ViaVersion/ViaVersion/pull/754 + // Previously clients grab the item from the clicked slot *before* it has + // been moved however now they grab the slot item *after* it has been moved + // and send that in the packet. + wrapper.set(Type.ITEM, 0, null); // Set null item (probably will work) - // Apologize (may happen in some cases, maybe if inventory is full?) - PacketWrapper confirm = wrapper.create(ServerboundPackets1_12.WINDOW_CONFIRMATION); - confirm.write(Type.UNSIGNED_BYTE, wrapper.get(Type.UNSIGNED_BYTE, 0)); - confirm.write(Type.SHORT, wrapper.get(Type.SHORT, 1)); - confirm.write(Type.BOOLEAN, false); // Success - not used + // Apologize (may happen in some cases, maybe if inventory is full?) + PacketWrapper confirm = wrapper.create(ServerboundPackets1_12.WINDOW_CONFIRMATION); + confirm.write(Type.UNSIGNED_BYTE, wrapper.get(Type.UNSIGNED_BYTE, 0)); + confirm.write(Type.SHORT, wrapper.get(Type.SHORT, 1)); + confirm.write(Type.BOOLEAN, false); // Success - not used - wrapper.sendToServer(Protocol1_11_1To1_12.class); - wrapper.cancel(); - confirm.sendToServer(Protocol1_11_1To1_12.class); - return; + wrapper.sendToServer(Protocol1_11_1To1_12.class); + wrapper.cancel(); + confirm.sendToServer(Protocol1_11_1To1_12.class); + return; - } - Item item = wrapper.get(Type.ITEM, 0); - handleItemToServer(item); } + Item item = wrapper.get(Type.ITEM, 0); + handleItemToServer(item); }); } }); registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM); - protocol.registerClientbound(ClientboundPackets1_12.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + protocol.registerClientbound(ClientboundPackets1_12.CHUNK_DATA, wrapper -> { + 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. - Chunk chunk = wrapper.passthrough(type); + Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.9.4 Chunk type since nothing changed. + Chunk chunk = wrapper.passthrough(type); - handleChunk(chunk); - } - }); - } + handleChunk(chunk); }); - protocol.registerClientbound(ClientboundPackets1_12.BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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)); - } + handler(wrapper -> { + int idx = wrapper.get(Type.VAR_INT, 0); + wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); }); } }); - protocol.registerClientbound(ClientboundPackets1_12.MULTI_BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.MULTI_BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 { - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - record.setBlockId(handleBlockID(record.getBlockId())); - } + handler(wrapper -> { + for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { + record.setBlockId(handleBlockID(record.getBlockId())); } }); } }); - protocol.registerClientbound(ClientboundPackets1_12.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.BLOCK_ENTITY_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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(); - } + handler(wrapper -> { + // Remove bed color + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) + wrapper.cancel(); }); } }); @@ -232,18 +207,15 @@ public class BlockItemPackets1_12 extends LegacyBlockItemRewriter { + // Open Inventory + if (wrapper.get(Type.VAR_INT, 0) == 2) { + wrapper.cancel(); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java index e4c55718..1f9e7b72 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java @@ -20,7 +20,6 @@ package com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.AdvancementTranslations; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.rewriter.RewriterBase; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -46,14 +45,9 @@ public class ChatPackets1_12 extends RewriterBase { @Override protected void registerPackets() { - protocol.registerClientbound(ClientboundPackets1_12.CHAT_MESSAGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - JsonElement element = wrapper.passthrough(Type.COMPONENT); - componentRewriter.processText(element); - }); - } + protocol.registerClientbound(ClientboundPackets1_12.CHAT_MESSAGE, wrapper -> { + JsonElement element = wrapper.passthrough(Type.COMPONENT); + componentRewriter.processText(element); }); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java index 31be7ff3..c78a19dc 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java @@ -29,13 +29,11 @@ import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_12; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; - import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import java.util.Optional; @@ -47,9 +45,9 @@ public class EntityPackets1_12 extends LegacyEntityRewriter Entity1_12Types.ObjectType.findById(id).orElse(null))); // Handle FallingBlock blocks - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); - if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { - int objectData = wrapper.get(Type.INT, 0); - int objType = objectData & 4095; - int data = objectData >> 12 & 15; + handler(wrapper -> { + Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { + int objectData = wrapper.get(Type.INT, 0); + int objType = objectData & 4095; + int data = objectData >> 12 & 15; - Block block = protocol.getItemRewriter().handleBlock(objType, data); - if (block == null) { - return; - } - - wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); + Block block = protocol.getItemRewriter().handleBlock(objType, data); + if (block == null) { + return; } + + wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); } }); } @@ -89,9 +84,9 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { + ShoulderTracker tracker = wrapper.user().get(ShoulderTracker.class); + tracker.setEntityId(wrapper.get(Type.INT, 0)); }); // Send fake inventory achievement - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper packetWrapper) throws Exception { - PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.STATISTICS, packetWrapper.user()); + handler(packetWrapper -> { + PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.STATISTICS, packetWrapper.user()); - wrapper.write(Type.VAR_INT, 1); - wrapper.write(Type.STRING, "achievement.openInventory"); - wrapper.write(Type.VAR_INT, 1); + wrapper.write(Type.VAR_INT, 1); + wrapper.write(Type.STRING, "achievement.openInventory"); + wrapper.write(Type.VAR_INT, 1); - wrapper.scheduleSend(Protocol1_11_1To1_12.class); - } + wrapper.scheduleSend(Protocol1_11_1To1_12.class); }); } }); @@ -171,9 +160,9 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java index 095e4e7c..2647f1c4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java @@ -20,9 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets; import com.viaversion.viabackwards.api.rewriters.LegacySoundRewriter; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; @@ -34,9 +32,9 @@ public class SoundPackets1_12 extends LegacySoundRewriter @Override protected void registerPackets() { - protocol.registerClientbound(ClientboundPackets1_12.NAMED_SOUND, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.NAMED_SOUND, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // 0 - Sound name map(Type.VAR_INT); // 1 - Sound Category map(Type.INT); // 2 - x @@ -47,9 +45,9 @@ public class SoundPackets1_12 extends LegacySoundRewriter } }); - protocol.registerClientbound(ClientboundPackets1_12.SOUND, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_12.SOUND, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Sound name map(Type.VAR_INT); // 1 - Sound Category map(Type.INT); // 2 - x @@ -58,21 +56,18 @@ public class SoundPackets1_12 extends LegacySoundRewriter map(Type.FLOAT); // 5 - Volume map(Type.FLOAT); // 6 - Pitch - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int oldId = wrapper.get(Type.VAR_INT, 0); - int newId = handleSounds(oldId); - if (newId == -1) { - wrapper.cancel(); - return; - } - - if (hasPitch(oldId)) { - wrapper.set(Type.FLOAT, 1, handlePitch(oldId)); - } - wrapper.set(Type.VAR_INT, 0, newId); + handler(wrapper -> { + int oldId = wrapper.get(Type.VAR_INT, 0); + int newId = handleSounds(oldId); + if (newId == -1) { + wrapper.cancel(); + return; } + + if (hasPitch(oldId)) { + wrapper.set(Type.FLOAT, 1, handlePitch(oldId)); + } + wrapper.set(Type.VAR_INT, 0, newId); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java index f9aea092..6795b00b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java @@ -22,7 +22,7 @@ import com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter; import com.viaversion.viabackwards.protocol.protocol1_11to1_11_1.Protocol1_11To1_11_1; import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; @@ -35,9 +35,9 @@ public class EntityPackets1_11_1 extends LegacyEntityRewriter Trading - protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 + handler(wrapper -> { + if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_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 + int size = wrapper.passthrough(Type.UNSIGNED_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 + 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 } } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_1to1_12_2/Protocol1_12_1To1_12_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_1to1_12_2/Protocol1_12_1To1_12_2.java index 453ce071..60fdfeea 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_1to1_12_2/Protocol1_12_1To1_12_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_1to1_12_2/Protocol1_12_1To1_12_2.java @@ -20,9 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_12_1to1_12_2; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; @@ -35,36 +33,30 @@ public class Protocol1_12_1To1_12_2 extends BackwardsProtocol { + Long keepAlive = packetWrapper.read(Type.LONG); + packetWrapper.user().get(KeepAliveTracker.class).setKeepAlive(keepAlive); + packetWrapper.write(Type.VAR_INT, keepAlive.hashCode()); }); } }); - registerServerbound(ServerboundPackets1_12_1.KEEP_ALIVE, new PacketRemapper() { + registerServerbound(ServerboundPackets1_12_1.KEEP_ALIVE, new PacketHandlers() { @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper packetWrapper) throws Exception { - int keepAlive = packetWrapper.read(Type.VAR_INT); - long realKeepAlive = packetWrapper.user().get(KeepAliveTracker.class).getKeepAlive(); - if (keepAlive != Long.hashCode(realKeepAlive)) { - packetWrapper.cancel(); // Wrong data, cancel packet - return; - } - packetWrapper.write(Type.LONG, realKeepAlive); - // Reset KeepAliveTracker (to prevent sending same valid value in a row causing a timeout) - packetWrapper.user().get(KeepAliveTracker.class).setKeepAlive(Integer.MAX_VALUE); + public void register() { + handler(packetWrapper -> { + int keepAlive = packetWrapper.read(Type.VAR_INT); + long realKeepAlive = packetWrapper.user().get(KeepAliveTracker.class).getKeepAlive(); + if (keepAlive != Long.hashCode(realKeepAlive)) { + packetWrapper.cancel(); // Wrong data, cancel packet + return; } + packetWrapper.write(Type.LONG, realKeepAlive); + // Reset KeepAliveTracker (to prevent sending same valid value in a row causing a timeout) + packetWrapper.user().get(KeepAliveTracker.class).setKeepAlive(Integer.MAX_VALUE); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java index 808c48b9..33fa90dc 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java @@ -29,7 +29,6 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; - import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityTypeMapping.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityTypeMapping.java index 0070c5c0..2dd85076 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityTypeMapping.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityTypeMapping.java @@ -20,7 +20,6 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter; - import java.lang.reflect.Field; public class EntityTypeMapping { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java index be547829..18acce0b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java @@ -18,7 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.NamedSoundRewriter; - import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java index c3b44885..e236c60d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/ParticleMapping.java @@ -23,7 +23,6 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; - import java.util.List; public class ParticleMapping { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index 47743c6e..780cc7f0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -35,8 +35,7 @@ import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.conversion.ConverterRegistry; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag; @@ -57,7 +56,6 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRew import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -91,178 +89,153 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit @Override protected void registerPackets() { - protocol.registerClientbound(ClientboundPackets1_13.COOLDOWN, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - int itemId = wrapper.read(Type.VAR_INT); - int oldId = protocol.getMappingData().getItemMappings().get(itemId); - if (oldId == -1) { - wrapper.cancel(); - return; - } - - if (SpawnEggRewriter.getEntityId(oldId).isPresent()) { - wrapper.write(Type.VAR_INT, 383 << 4); - return; - } - - wrapper.write(Type.VAR_INT, oldId >> 4); - }); + protocol.registerClientbound(ClientboundPackets1_13.COOLDOWN, wrapper -> { + int itemId = wrapper.read(Type.VAR_INT); + int oldId = protocol.getMappingData().getItemMappings().get(itemId); + if (oldId == -1) { + wrapper.cancel(); + return; } + + if (SpawnEggRewriter.getEntityId(oldId).isPresent()) { + wrapper.write(Type.VAR_INT, 383 << 4); + return; + } + + wrapper.write(Type.VAR_INT, oldId >> 4); }); - protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION); // Location map(Type.UNSIGNED_BYTE); // Action Id map(Type.UNSIGNED_BYTE); // Action param map(Type.VAR_INT); // Block Id - /!\ NOT BLOCK STATE ID - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int blockId = wrapper.get(Type.VAR_INT, 0); + handler(wrapper -> { + int blockId = wrapper.get(Type.VAR_INT, 0); - if (blockId == 73) - blockId = 25; - else if (blockId == 99) - blockId = 33; - else if (blockId == 92) - blockId = 29; - else if (blockId == 142) - blockId = 54; - else if (blockId == 305) - blockId = 146; - else if (blockId == 249) - blockId = 130; - else if (blockId == 257) - blockId = 138; - else if (blockId == 140) - blockId = 52; - else if (blockId == 472) - blockId = 209; - else if (blockId >= 483 && blockId <= 498) - blockId = blockId - 483 + 219; + if (blockId == 73) + blockId = 25; + else if (blockId == 99) + blockId = 33; + else if (blockId == 92) + blockId = 29; + else if (blockId == 142) + blockId = 54; + else if (blockId == 305) + blockId = 146; + else if (blockId == 249) + blockId = 130; + else if (blockId == 257) + blockId = 138; + else if (blockId == 140) + blockId = 52; + else if (blockId == 472) + blockId = 209; + else if (blockId >= 483 && blockId <= 498) + blockId = blockId - 483 + 219; - wrapper.set(Type.VAR_INT, 0, blockId); - } + wrapper.set(Type.VAR_INT, 0, blockId); }); } }); - protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION); // 0 - Position map(Type.UNSIGNED_BYTE); // 1 - Action map(Type.NBT); // 2 - NBT Data - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class); + handler(wrapper -> { + BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class); - // TODO conduit handling - if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 5) { - wrapper.cancel(); - } - - wrapper.set(Type.NBT, 0, - provider.transform( - wrapper.user(), - wrapper.get(Type.POSITION, 0), - wrapper.get(Type.NBT, 0) - )); + // TODO conduit handling + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 5) { + wrapper.cancel(); } + + wrapper.set(Type.NBT, 0, + provider.transform( + wrapper.user(), + wrapper.get(Type.POSITION, 0), + wrapper.get(Type.NBT, 0) + )); }); } }); - protocol.registerClientbound(ClientboundPackets1_13.UNLOAD_CHUNK, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int chunkMinX = wrapper.passthrough(Type.INT) << 4; - int chunkMinZ = wrapper.passthrough(Type.INT) << 4; - int chunkMaxX = chunkMinX + 15; - int chunkMaxZ = chunkMinZ + 15; - BackwardsBlockStorage blockStorage = wrapper.user().get(BackwardsBlockStorage.class); - blockStorage.getBlocks().entrySet().removeIf(entry -> { - Position position = entry.getKey(); - return position.x() >= chunkMinX && position.z() >= chunkMinZ - && position.x() <= chunkMaxX && position.z() <= chunkMaxZ; - }); - } - }); - } + protocol.registerClientbound(ClientboundPackets1_13.UNLOAD_CHUNK, wrapper -> { + int chunkMinX = wrapper.passthrough(Type.INT) << 4; + int chunkMinZ = wrapper.passthrough(Type.INT) << 4; + int chunkMaxX = chunkMinX + 15; + int chunkMaxZ = chunkMinZ + 15; + BackwardsBlockStorage blockStorage = wrapper.user().get(BackwardsBlockStorage.class); + blockStorage.getBlocks().entrySet().removeIf(entry -> { + Position position = entry.getKey(); + return position.x() >= chunkMinX && position.z() >= chunkMinZ + && position.x() <= chunkMaxX && position.z() <= chunkMaxZ; + }); }); // Block Change - protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION); // 0 - Position - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int blockState = wrapper.read(Type.VAR_INT); - Position position = wrapper.get(Type.POSITION, 0); + handler(wrapper -> { + int blockState = wrapper.read(Type.VAR_INT); + Position position = wrapper.get(Type.POSITION, 0); - // Store blocks - BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); - storage.checkAndStore(position, blockState); + // Store blocks + BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); + storage.checkAndStore(position, blockState); - wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockState)); + wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockState)); - // Flower pot special treatment - flowerPotSpecialTreatment(wrapper.user(), blockState, position); - } + // Flower pot special treatment + flowerPotSpecialTreatment(wrapper.user(), blockState, position); }); } }); // Multi Block Change - protocol.registerClientbound(ClientboundPackets1_13.MULTI_BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.MULTI_BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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); + handler(wrapper -> { + 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( - record.getSectionX() + (chunkX * 16), - record.getY(), - record.getSectionZ() + (chunkZ * 16)); + 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( + record.getSectionX() + (chunkX * 16), + record.getY(), + record.getSectionZ() + (chunkZ * 16)); - // Store if needed - storage.checkAndStore(position, block); + // Store if needed + storage.checkAndStore(position, block); - // Flower pot special treatment - flowerPotSpecialTreatment(wrapper.user(), block, position); + // Flower pot special treatment + flowerPotSpecialTreatment(wrapper.user(), block, position); - // Change to old id - record.setBlockId(protocol.getMappingData().getNewBlockStateId(block)); - } + // Change to old id + record.setBlockId(protocol.getMappingData().getNewBlockStateId(block)); } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY); @@ -270,9 +243,9 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); map(Type.SHORT); map(Type.FLAT_ITEM, Type.ITEM); @@ -281,181 +254,170 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, wrapper -> { + 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); + Chunk1_9_3_4Type type_old = new Chunk1_9_3_4Type(clientWorld); + Chunk1_13Type type = new Chunk1_13Type(clientWorld); + Chunk chunk = wrapper.read(type); - // Handle Block Entities before block rewrite - BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class); - BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); - for (CompoundTag tag : chunk.getBlockEntities()) { - Tag idTag = tag.get("id"); - if (idTag == null) continue; + // Handle Block Entities before block rewrite + BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class); + BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); + for (CompoundTag tag : chunk.getBlockEntities()) { + Tag idTag = tag.get("id"); + if (idTag == null) continue; - String id = (String) idTag.getValue(); + String id = (String) idTag.getValue(); - // Ignore if we don't handle it - if (!provider.isHandled(id)) continue; + // Ignore if we don't handle it + if (!provider.isHandled(id)) continue; - int sectionIndex = ((NumberTag) tag.get("y")).asInt() >> 4; - if (sectionIndex < 0 || sectionIndex > 15) { - // 1.17 chunks - continue; - } + int sectionIndex = ((NumberTag) tag.get("y")).asInt() >> 4; + if (sectionIndex < 0 || sectionIndex > 15) { + // 1.17 chunks + continue; + } - ChunkSection section = chunk.getSections()[sectionIndex]; + ChunkSection section = chunk.getSections()[sectionIndex]; - int x = ((NumberTag) tag.get("x")).asInt(); - int y = ((NumberTag) tag.get("y")).asInt(); - int z = ((NumberTag) tag.get("z")).asInt(); - Position position = new Position(x, (short) y, z); + int x = ((NumberTag) tag.get("x")).asInt(); + int y = ((NumberTag) tag.get("y")).asInt(); + int z = ((NumberTag) tag.get("z")).asInt(); + Position position = new Position(x, (short) y, z); - int block = section.palette(PaletteType.BLOCKS).idAt(x & 0xF, y & 0xF, z & 0xF); - storage.checkAndStore(position, block); + int block = section.palette(PaletteType.BLOCKS).idAt(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) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - // Flower pots require a special treatment, they are no longer block entities :( - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - for (int x = 0; x < 16; x++) { - int block = palette.idAt(x, y, z); - - // Check if the block is a flower - if (FlowerPotHandler.isFlowah(block)) { - Position pos = new Position( - (x + (chunk.getX() << 4)), - (short) (y + (i << 4)), - (z + (chunk.getZ() << 4)) - ); - // Store block - storage.checkAndStore(pos, block); - - CompoundTag nbt = provider.transform(wrapper.user(), pos, "minecraft:flower_pot"); - - chunk.getBlockEntities().add(nbt); - } - } - } - } - - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); - } - } - - - if (chunk.isBiomeData()) { - for (int i = 0; i < 256; i++) { - int biome = chunk.getBiomeData()[i]; - int newId = -1; - switch (biome) { - case 40: // end biomes - case 41: - case 42: - case 43: - newId = 9; - break; - case 47: // deep ocean biomes - case 48: - case 49: - newId = 24; - break; - case 50: // deep frozen... let's just pick the frozen variant - newId = 10; - break; - case 44: // the other new ocean biomes - case 45: - case 46: - newId = 0; - break; - } - - if (newId != -1) { - chunk.getBiomeData()[i] = newId; - } - } - } - - wrapper.write(type_old, chunk); - }); + 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) { + continue; + } + + DataPalette palette = section.palette(PaletteType.BLOCKS); + // Flower pots require a special treatment, they are no longer block entities :( + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + int block = palette.idAt(x, y, z); + + // Check if the block is a flower + if (FlowerPotHandler.isFlowah(block)) { + Position pos = new Position( + (x + (chunk.getX() << 4)), + (short) (y + (i << 4)), + (z + (chunk.getZ() << 4)) + ); + // Store block + storage.checkAndStore(pos, block); + + CompoundTag nbt = provider.transform(wrapper.user(), pos, "minecraft:flower_pot"); + + chunk.getBlockEntities().add(nbt); + } + } + } + } + + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } + } + + + if (chunk.isBiomeData()) { + for (int i = 0; i < 256; i++) { + int biome = chunk.getBiomeData()[i]; + int newId = -1; + switch (biome) { + case 40: // end biomes + case 41: + case 42: + case 43: + newId = 9; + break; + case 47: // deep ocean biomes + case 48: + case 49: + newId = 24; + break; + case 50: // deep frozen... let's just pick the frozen variant + newId = 10; + break; + case 44: // the other new ocean biomes + case 45: + case 46: + newId = 0; + break; + } + + if (newId != -1) { + chunk.getBiomeData()[i] = newId; + } + } + } + + wrapper.write(type_old, chunk); }); - protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // Effect Id map(Type.POSITION); // Location map(Type.INT); // Data - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - int data = wrapper.get(Type.INT, 1); - if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4); - } else if (id == 2001) { // Block break + block break sound - data = protocol.getMappingData().getNewBlockStateId(data); - int blockId = data >> 4; - int blockData = data & 0xF; - wrapper.set(Type.INT, 1, (blockId & 0xFFF) | (blockData << 12)); - } + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + int data = wrapper.get(Type.INT, 1); + if (id == 1010) { // Play record + wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4); + } else if (id == 2001) { // Block break + block break sound + data = protocol.getMappingData().getNewBlockStateId(data); + int blockId = data >> 4; + int blockData = data & 0xF; + wrapper.set(Type.INT, 1, (blockId & 0xFFF) | (blockData << 12)); } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.BYTE); map(Type.BOOLEAN); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int iconCount = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < iconCount; i++) { - int type = wrapper.read(Type.VAR_INT); - byte x = wrapper.read(Type.BYTE); - byte z = wrapper.read(Type.BYTE); - byte direction = wrapper.read(Type.BYTE); - if (wrapper.read(Type.BOOLEAN)) { - wrapper.read(Type.COMPONENT); - } - if (type > 9) { - wrapper.set(Type.VAR_INT, 1, wrapper.get(Type.VAR_INT, 1) - 1); - continue; - } - wrapper.write(Type.BYTE, (byte) ((type << 4) | (direction & 0x0F))); - wrapper.write(Type.BYTE, x); - wrapper.write(Type.BYTE, z); + handler(wrapper -> { + int iconCount = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < iconCount; i++) { + int type = wrapper.read(Type.VAR_INT); + byte x = wrapper.read(Type.BYTE); + byte z = wrapper.read(Type.BYTE); + byte direction = wrapper.read(Type.BYTE); + if (wrapper.read(Type.BOOLEAN)) { + wrapper.read(Type.COMPONENT); } + if (type > 9) { + wrapper.set(Type.VAR_INT, 1, wrapper.get(Type.VAR_INT, 1) - 1); + continue; + } + wrapper.write(Type.BYTE, (byte) ((type << 4) | (direction & 0x0F))); + wrapper.write(Type.BYTE, x); + wrapper.write(Type.BYTE, z); } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.VAR_INT); map(Type.FLAT_ITEM, Type.ITEM); @@ -464,9 +426,9 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_13.WINDOW_PROPERTY, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.WINDOW_PROPERTY, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // Window Id map(Type.SHORT); // Property map(Type.SHORT); // Value @@ -482,9 +444,9 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit }); - protocol.registerServerbound(ServerboundPackets1_12_1.CREATIVE_INVENTORY_ACTION, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_12_1.CREATIVE_INVENTORY_ACTION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.SHORT); map(Type.ITEM, Type.FLAT_ITEM); @@ -492,9 +454,9 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerServerbound(ServerboundPackets1_12_1.CLICK_WINDOW, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_12_1.CLICK_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); map(Type.SHORT); map(Type.BYTE); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index 90ad0118..fc2cae92 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -32,8 +32,7 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_12; @@ -42,7 +41,6 @@ import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; - import java.util.Optional; public class EntityPackets1_13 extends LegacyEntityRewriter { @@ -53,38 +51,30 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { + if (!ViaBackwards.getConfig().isFix1_13FacePlayer()) return; - PlayerPositionStorage1_13 playerStorage = wrapper.user().get(PlayerPositionStorage1_13.class); - byte bitField = wrapper.get(Type.BYTE, 0); - playerStorage.setX(toSet(bitField, 0, playerStorage.getX(), wrapper.get(Type.DOUBLE, 0))); - playerStorage.setY(toSet(bitField, 1, playerStorage.getY(), wrapper.get(Type.DOUBLE, 1))); - playerStorage.setZ(toSet(bitField, 2, playerStorage.getZ(), wrapper.get(Type.DOUBLE, 2))); - } - - private double toSet(int field, int bitIndex, double origin, double packetValue) { - // If bit is set, coordinate is relative - return (field & (1 << bitIndex)) != 0 ? origin + packetValue : packetValue; - } + PlayerPositionStorage1_13 playerStorage = wrapper.user().get(PlayerPositionStorage1_13.class); + byte bitField = wrapper.get(Type.BYTE, 0); + playerStorage.setX(toSet(bitField, 0, playerStorage.getX(), wrapper.get(Type.DOUBLE, 0))); + playerStorage.setY(toSet(bitField, 1, playerStorage.getY(), wrapper.get(Type.DOUBLE, 1))); + playerStorage.setZ(toSet(bitField, 2, playerStorage.getZ(), wrapper.get(Type.DOUBLE, 2))); }); } }); - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.UUID); map(Type.BYTE); @@ -97,35 +87,32 @@ public class EntityPackets1_13 extends LegacyEntityRewriter optionalType = Entity1_13Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); - if (!optionalType.isPresent()) return; + handler(wrapper -> { + Optional optionalType = Entity1_13Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (!optionalType.isPresent()) return; - Entity1_13Types.ObjectType type = optionalType.get(); - if (type == Entity1_13Types.ObjectType.FALLING_BLOCK) { - int blockState = wrapper.get(Type.INT, 0); - int combined = Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(blockState); - combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12); - wrapper.set(Type.INT, 0, combined); - } else if (type == Entity1_13Types.ObjectType.ITEM_FRAME) { - int data = wrapper.get(Type.INT, 0); - switch (data) { - case 3: - data = 0; - break; - case 4: - data = 1; - break; - case 5: - data = 3; - break; - } - wrapper.set(Type.INT, 0, data); - } else if (type == Entity1_13Types.ObjectType.TRIDENT) { - wrapper.set(Type.BYTE, 0, (byte) Entity1_13Types.ObjectType.TIPPED_ARROW.getId()); + Entity1_13Types.ObjectType type = optionalType.get(); + if (type == Entity1_13Types.ObjectType.FALLING_BLOCK) { + int blockState = wrapper.get(Type.INT, 0); + int combined = Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(blockState); + combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12); + wrapper.set(Type.INT, 0, combined); + } else if (type == Entity1_13Types.ObjectType.ITEM_FRAME) { + int data = wrapper.get(Type.INT, 0); + switch (data) { + case 3: + data = 0; + break; + case 4: + data = 1; + break; + case 5: + data = 3; + break; } + wrapper.set(Type.INT, 0, data); + } else if (type == Entity1_13Types.ObjectType.TRIDENT) { + wrapper.set(Type.BYTE, 0, (byte) Entity1_13Types.ObjectType.TIPPED_ARROW.getId()); } }); } @@ -134,9 +121,9 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { + int type = wrapper.get(Type.VAR_INT, 1); + EntityType entityType = Entity1_13Types.getTypeFromId(type, false); + tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType); - int oldId = EntityTypeMapping.getOldId(type); - if (oldId == -1) { - if (!hasData(entityType)) { - ViaBackwards.getPlatform().getLogger().warning("Could not find 1.12 entity type for 1.13 entity type " + type + "/" + entityType); - } - } else { - wrapper.set(Type.VAR_INT, 1, oldId); + int oldId = EntityTypeMapping.getOldId(type); + if (oldId == -1) { + if (!hasData(entityType)) { + ViaBackwards.getPlatform().getLogger().warning("Could not find 1.12 entity type for 1.13 entity type " + type + "/" + entityType); } + } else { + wrapper.set(Type.VAR_INT, 1, oldId); } }); @@ -174,9 +158,9 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { + int motive = wrapper.read(Type.VAR_INT); + String title = PaintingMapping.getStringId(motive); + wrapper.write(Type.STRING, title); }); } }); registerJoinGame(ClientboundPackets1_13.JOIN_GAME, Entity1_13Types.EntityType.PLAYER); - protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // 0 - Dimension ID handler(getDimensionHandler(0)); @@ -224,48 +205,40 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { + wrapper.cancel(); - if (!ViaBackwards.getConfig().isFix1_13FacePlayer()) return; + if (!ViaBackwards.getConfig().isFix1_13FacePlayer()) return; - // We will just accept a possible, very minor mismatch between server and client position, - // and will take the server's one in both cases, else we would have to cache all entities' positions. - final int anchor = wrapper.read(Type.VAR_INT); // feet/eyes enum - final double x = wrapper.read(Type.DOUBLE); - final double y = wrapper.read(Type.DOUBLE); - final double z = wrapper.read(Type.DOUBLE); + // We will just accept a possible, very minor mismatch between server and client position, + // and will take the server's one in both cases, else we would have to cache all entities' positions. + final int anchor = wrapper.read(Type.VAR_INT); // feet/eyes enum + final double x = wrapper.read(Type.DOUBLE); + final double y = wrapper.read(Type.DOUBLE); + final double z = wrapper.read(Type.DOUBLE); - PlayerPositionStorage1_13 positionStorage = wrapper.user().get(PlayerPositionStorage1_13.class); + PlayerPositionStorage1_13 positionStorage = wrapper.user().get(PlayerPositionStorage1_13.class); - // Send teleport packet to client - PacketWrapper positionAndLook = wrapper.create(ClientboundPackets1_12_1.PLAYER_POSITION); - positionAndLook.write(Type.DOUBLE, 0D); - positionAndLook.write(Type.DOUBLE, 0D); - positionAndLook.write(Type.DOUBLE, 0D); + // Send teleport packet to client + PacketWrapper positionAndLook = wrapper.create(ClientboundPackets1_12_1.PLAYER_POSITION); + positionAndLook.write(Type.DOUBLE, 0D); + positionAndLook.write(Type.DOUBLE, 0D); + positionAndLook.write(Type.DOUBLE, 0D); - //TODO properly cache and calculate head position? - EntityPositionHandler.writeFacingDegrees(positionAndLook, positionStorage.getX(), - anchor == 1 ? positionStorage.getY() + 1.62 : positionStorage.getY(), - positionStorage.getZ(), x, y, z); + //TODO properly cache and calculate head position? + EntityPositionHandler.writeFacingDegrees(positionAndLook, positionStorage.getX(), + anchor == 1 ? positionStorage.getY() + 1.62 : positionStorage.getY(), + positionStorage.getZ(), x, y, z); - positionAndLook.write(Type.BYTE, (byte) 7); // bitfield, 0=absolute, 1=relative - x,y,z relative, yaw,pitch absolute - positionAndLook.write(Type.VAR_INT, -1); - positionAndLook.send(Protocol1_12_2To1_13.class); - } - }); - } + positionAndLook.write(Type.BYTE, (byte) 7); // bitfield, 0=absolute, 1=relative - x,y,z relative, yaw,pitch absolute + positionAndLook.write(Type.VAR_INT, -1); + positionAndLook.send(Protocol1_12_2To1_13.class); }); if (ViaBackwards.getConfig().isFix1_13FacePlayer()) { - PacketRemapper movementRemapper = new PacketRemapper() { + PacketHandlers movementRemapper = new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.DOUBLE); map(Type.DOUBLE); map(Type.DOUBLE); @@ -408,4 +381,9 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { @Override protected void registerPackets() { // Login Plugin Request - protocol.registerClientbound(State.LOGIN, 0x04, -1, new PacketRemapper() { + protocol.registerClientbound(State.LOGIN, 0x04, -1, new PacketHandlers() { @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper packetWrapper) throws Exception { - packetWrapper.cancel(); - packetWrapper.create(0x02, new PacketHandler() { // Plugin response - @Override - public void handle(PacketWrapper newWrapper) throws Exception { - newWrapper.write(Type.VAR_INT, packetWrapper.read(Type.VAR_INT)); // Packet id - newWrapper.write(Type.BOOLEAN, false); // Success - } - }).sendToServer(Protocol1_12_2To1_13.class); - } + public void register() { + handler(packetWrapper -> { + packetWrapper.cancel(); + packetWrapper.create(0x02, new PacketHandler() { // Plugin response + @Override + public void handle(PacketWrapper newWrapper) throws Exception { + newWrapper.write(Type.VAR_INT, packetWrapper.read(Type.VAR_INT)); // Packet id + newWrapper.write(Type.BOOLEAN, false); // Success + } + }).sendToServer(Protocol1_12_2To1_13.class); }); } }); - protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - String channel = wrapper.read(Type.STRING); - if (channel.equals("minecraft:trader_list")) { - wrapper.write(Type.STRING, "MC|TrList"); - wrapper.passthrough(Type.INT); //Passthrough Window ID + protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, wrapper -> { + String channel = wrapper.read(Type.STRING); + if (channel.equals("minecraft:trader_list")) { + wrapper.write(Type.STRING, "MC|TrList"); + wrapper.passthrough(Type.INT); //Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - //Input Item - Item input = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(input)); - //Output Item - Item output = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(output)); + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + //Input Item + Item input = wrapper.read(Type.FLAT_ITEM); + wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(input)); + //Output Item + Item output = wrapper.read(Type.FLAT_ITEM); + wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(output)); - boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item - if (secondItem) { - //Second Item - Item second = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(second)); - } + boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item + if (secondItem) { + //Second Item + Item second = wrapper.read(Type.FLAT_ITEM); + wrapper.write(Type.ITEM, protocol.getItemRewriter().handleItemToClient(second)); + } - wrapper.passthrough(Type.BOOLEAN); //Trade disabled - wrapper.passthrough(Type.INT); //Number of tools uses - wrapper.passthrough(Type.INT); //Maximum number of trade uses - } - } else { - String oldChannel = InventoryPackets.getOldPluginChannelId(channel); - if (oldChannel == null) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("Ignoring outgoing plugin message with channel: " + channel); - } - wrapper.cancel(); - return; - } - wrapper.write(Type.STRING, oldChannel); + wrapper.passthrough(Type.BOOLEAN); //Trade disabled + wrapper.passthrough(Type.INT); //Number of tools uses + wrapper.passthrough(Type.INT); //Maximum number of trade uses + } + } else { + String oldChannel = InventoryPackets.getOldPluginChannelId(channel); + if (oldChannel == null) { + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("Ignoring outgoing plugin message with channel: " + channel); + } + wrapper.cancel(); + return; + } + wrapper.write(Type.STRING, oldChannel); - if (oldChannel.equals("REGISTER") || oldChannel.equals("UNREGISTER")) { - String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); - List rewrittenChannels = new ArrayList<>(); - for (String s : channels) { - String rewritten = InventoryPackets.getOldPluginChannelId(s); - if (rewritten != null) { - rewrittenChannels.add(rewritten); - } else if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("Ignoring plugin channel in outgoing REGISTER: " + s); - } - } - wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(rewrittenChannels).getBytes(StandardCharsets.UTF_8)); - } + if (oldChannel.equals("REGISTER") || oldChannel.equals("UNREGISTER")) { + String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); + List rewrittenChannels = new ArrayList<>(); + for (String s : channels) { + String rewritten = InventoryPackets.getOldPluginChannelId(s); + if (rewritten != null) { + rewrittenChannels.add(rewritten); + } else if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("Ignoring plugin channel in outgoing REGISTER: " + s); } } - }); + wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(rewrittenChannels).getBytes(StandardCharsets.UTF_8)); + } } }); - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // 0 - Particle ID map(Type.BOOLEAN); // 1 - Long Distance map(Type.FLOAT); // 2 - X @@ -151,486 +139,448 @@ public class PlayerPacket1_13 extends RewriterBase { map(Type.FLOAT); // 7 - Offset Z map(Type.FLOAT); // 8 - Particle Data map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - ParticleMapping.ParticleData old = ParticleMapping.getMapping(wrapper.get(Type.INT, 0)); - wrapper.set(Type.INT, 0, old.getHistoryId()); - - int[] data = old.rewriteData(protocol, wrapper); - if (data != null) { - if (old.getHandler().isBlockHandler() && data[0] == 0) { - // Cancel air block particles - wrapper.cancel(); - return; - } - - for (int i : data) { - wrapper.write(Type.VAR_INT, i); - } - } - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.PLAYER_INFO, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper packetWrapper) throws Exception { - TabCompleteStorage storage = packetWrapper.user().get(TabCompleteStorage.class); - int action = packetWrapper.passthrough(Type.VAR_INT); - int nPlayers = packetWrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < nPlayers; i++) { - UUID uuid = packetWrapper.passthrough(Type.UUID); - if (action == 0) { // Add - String name = packetWrapper.passthrough(Type.STRING); - storage.usernames().put(uuid, name); - int nProperties = packetWrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < nProperties; j++) { - packetWrapper.passthrough(Type.STRING); - packetWrapper.passthrough(Type.STRING); - packetWrapper.passthrough(Type.OPTIONAL_STRING); - } - packetWrapper.passthrough(Type.VAR_INT); - packetWrapper.passthrough(Type.VAR_INT); - packetWrapper.passthrough(Type.OPTIONAL_COMPONENT); - } else if (action == 1) { // Update Game Mode - packetWrapper.passthrough(Type.VAR_INT); - } else if (action == 2) { // Update Ping - packetWrapper.passthrough(Type.VAR_INT); - } else if (action == 3) { // Update Display Name - packetWrapper.passthrough(Type.OPTIONAL_COMPONENT); - } else if (action == 4) { // Remove Player - storage.usernames().remove(uuid); - } - } - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.SCOREBOARD_OBJECTIVE, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.STRING); - map(Type.BYTE); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - byte mode = wrapper.get(Type.BYTE, 0); - if (mode == 0 || mode == 2) { - String value = wrapper.read(Type.COMPONENT).toString(); - value = ChatRewriter.jsonToLegacyText(value); - if (value.length() > 32) { - value = value.substring(0, 32); - } - - wrapper.write(Type.STRING, value); - int type = wrapper.read(Type.VAR_INT); - wrapper.write(Type.STRING, type == 1 ? "hearts" : "integer"); - } - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.TEAMS, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.STRING); - map(Type.BYTE); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - byte action = wrapper.get(Type.BYTE, 0); - if (action == 0 || action == 2) { - String displayName = wrapper.read(Type.STRING); - displayName = ChatRewriter.jsonToLegacyText(displayName); - displayName = ChatUtil.removeUnusedColor(displayName, 'f'); - if (displayName.length() > 32) { - displayName = displayName.substring(0, 32); - } - wrapper.write(Type.STRING, displayName); - - byte flags = wrapper.read(Type.BYTE); - String nameTagVisibility = wrapper.read(Type.STRING); - String collisionRule = wrapper.read(Type.STRING); - - int colour = wrapper.read(Type.VAR_INT); - if (colour == 21) { - colour = -1; - } - - JsonElement prefixComponent = wrapper.read(Type.COMPONENT); - JsonElement suffixComponent = wrapper.read(Type.COMPONENT); - - String prefix = prefixComponent == null || prefixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(prefixComponent.toString()); - if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) { - prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r"); - } - - prefix = ChatUtil.removeUnusedColor(prefix, 'f', true); - if (prefix.length() > 16) prefix = prefix.substring(0, 16); - if (prefix.endsWith("§")) prefix = prefix.substring(0, prefix.length() - 1); - - String suffix = suffixComponent == null || suffixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(suffixComponent.toString()); - suffix = ChatUtil.removeUnusedColor(suffix, '\0'); // Don't remove white coloring - if (suffix.length() > 16) suffix = suffix.substring(0, 16); - if (suffix.endsWith("§")) suffix = suffix.substring(0, suffix.length() - 1); - - wrapper.write(Type.STRING, prefix); - wrapper.write(Type.STRING, suffix); - - wrapper.write(Type.BYTE, flags); - wrapper.write(Type.STRING, nameTagVisibility); - wrapper.write(Type.STRING, collisionRule); - - wrapper.write(Type.BYTE, (byte) colour); - } - - if (action == 0 || action == 3 || action == 4) { - wrapper.passthrough(Type.STRING_ARRAY); //Entities - } - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.DECLARE_COMMANDS, null, new PacketRemapper() { - @Override - public void registerMap() { handler(wrapper -> { - wrapper.cancel(); + ParticleMapping.ParticleData old = ParticleMapping.getMapping(wrapper.get(Type.INT, 0)); + wrapper.set(Type.INT, 0, old.getHistoryId()); - TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); - - if (!storage.commands().isEmpty()) { - storage.commands().clear(); - } - - int size = wrapper.read(Type.VAR_INT); - boolean initialNodes = true; - for (int i = 0; i < size; i++) { - byte flags = wrapper.read(Type.BYTE); - wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices - if ((flags & 0x08) != 0) { - wrapper.read(Type.VAR_INT); // Redirect node index - } - - byte nodeType = (byte) (flags & 0x03); - if (initialNodes && nodeType == 2) { - initialNodes = false; - } - - if (nodeType == 1 || nodeType == 2) { // Literal/argument node - String name = wrapper.read(Type.STRING); - if (nodeType == 1 && initialNodes) { - storage.commands().add('/' + name); - } - } - - if (nodeType == 2) { // Argument node - commandRewriter.handleArgument(wrapper, wrapper.read(Type.STRING)); - } - - if ((flags & 0x10) != 0) { - wrapper.read(Type.STRING); // Suggestion type - } - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.TAB_COMPLETE, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); - if (storage.lastRequest() == null) { + int[] data = old.rewriteData(protocol, wrapper); + if (data != null) { + if (old.getHandler().isBlockHandler() && data[0] == 0) { + // Cancel air block particles wrapper.cancel(); return; } - if (storage.lastId() != wrapper.read(Type.VAR_INT)) wrapper.cancel(); - int start = wrapper.read(Type.VAR_INT); - int length = wrapper.read(Type.VAR_INT); - int lastRequestPartIndex = storage.lastRequest().lastIndexOf(' ') + 1; - if (lastRequestPartIndex != start) wrapper.cancel(); // Client only replaces after space - - if (length != storage.lastRequest().length() - lastRequestPartIndex) { - wrapper.cancel(); // We can't set the length in previous versions - } - - int count = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < count; i++) { - String match = wrapper.read(Type.STRING); - wrapper.write(Type.STRING, (start == 0 && !storage.isLastAssumeCommand() ? "/" : "") + match); - // Ignore tooltip - if (wrapper.read(Type.BOOLEAN)) { - wrapper.read(Type.STRING); - } + for (int i : data) { + wrapper.write(Type.VAR_INT, i); } } }); } }); - protocol.registerServerbound(ServerboundPackets1_12_1.TAB_COMPLETE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.PLAYER_INFO, new PacketHandlers() { @Override - public void registerMap() { - handler(wrapper -> { - TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); - List suggestions = new ArrayList<>(); - - String command = wrapper.read(Type.STRING); - boolean assumeCommand = wrapper.read(Type.BOOLEAN); - wrapper.read(Type.OPTIONAL_POSITION); - - if (!assumeCommand && !command.startsWith("/")) { - // Complete usernames for non-commands - String buffer = command.substring(command.lastIndexOf(' ') + 1); - for (String value : storage.usernames().values()) { - if (startsWithIgnoreCase(value, buffer)) { - suggestions.add(value); - } - } - } else if (!storage.commands().isEmpty() && !command.contains(" ")) { - // Complete commands names with values from 'Declare Commands' packet - for (String value : storage.commands()) { - if (startsWithIgnoreCase(value, command)) { - suggestions.add(value); + public void register() { + handler(packetWrapper -> { + TabCompleteStorage storage = packetWrapper.user().get(TabCompleteStorage.class); + int action = packetWrapper.passthrough(Type.VAR_INT); + int nPlayers = packetWrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < nPlayers; i++) { + UUID uuid = packetWrapper.passthrough(Type.UUID); + if (action == 0) { // Add + String name = packetWrapper.passthrough(Type.STRING); + storage.usernames().put(uuid, name); + int nProperties = packetWrapper.passthrough(Type.VAR_INT); + for (int j = 0; j < nProperties; j++) { + packetWrapper.passthrough(Type.STRING); + packetWrapper.passthrough(Type.STRING); + packetWrapper.passthrough(Type.OPTIONAL_STRING); } + packetWrapper.passthrough(Type.VAR_INT); + packetWrapper.passthrough(Type.VAR_INT); + packetWrapper.passthrough(Type.OPTIONAL_COMPONENT); + } else if (action == 1) { // Update Game Mode + packetWrapper.passthrough(Type.VAR_INT); + } else if (action == 2) { // Update Ping + packetWrapper.passthrough(Type.VAR_INT); + } else if (action == 3) { // Update Display Name + packetWrapper.passthrough(Type.OPTIONAL_COMPONENT); + } else if (action == 4) { // Remove Player + storage.usernames().remove(uuid); } } + }); + } + }); - if (!suggestions.isEmpty()) { - wrapper.cancel(); - PacketWrapper response = wrapper.create(ClientboundPackets1_12_1.TAB_COMPLETE); - response.write(Type.VAR_INT, suggestions.size()); - for (String value : suggestions) { - response.write(Type.STRING, value); + protocol.registerClientbound(ClientboundPackets1_13.SCOREBOARD_OBJECTIVE, new PacketHandlers() { + @Override + public void register() { + map(Type.STRING); + map(Type.BYTE); + handler(wrapper -> { + byte mode = wrapper.get(Type.BYTE, 0); + if (mode == 0 || mode == 2) { + String value = wrapper.read(Type.COMPONENT).toString(); + value = ChatRewriter.jsonToLegacyText(value); + if (value.length() > 32) { + value = value.substring(0, 32); } - response.scheduleSend(Protocol1_12_2To1_13.class); - storage.setLastRequest(null); + + wrapper.write(Type.STRING, value); + int type = wrapper.read(Type.VAR_INT); + wrapper.write(Type.STRING, type == 1 ? "hearts" : "integer"); + } + }); + } + }); + + protocol.registerClientbound(ClientboundPackets1_13.TEAMS, new PacketHandlers() { + @Override + public void register() { + map(Type.STRING); + map(Type.BYTE); + handler(wrapper -> { + byte action = wrapper.get(Type.BYTE, 0); + if (action == 0 || action == 2) { + String displayName = wrapper.read(Type.STRING); + displayName = ChatRewriter.jsonToLegacyText(displayName); + displayName = ChatUtil.removeUnusedColor(displayName, 'f'); + if (displayName.length() > 32) { + displayName = displayName.substring(0, 32); + } + wrapper.write(Type.STRING, displayName); + + byte flags = wrapper.read(Type.BYTE); + String nameTagVisibility = wrapper.read(Type.STRING); + String collisionRule = wrapper.read(Type.STRING); + + int colour = wrapper.read(Type.VAR_INT); + if (colour == 21) { + colour = -1; + } + + JsonElement prefixComponent = wrapper.read(Type.COMPONENT); + JsonElement suffixComponent = wrapper.read(Type.COMPONENT); + + String prefix = prefixComponent == null || prefixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(prefixComponent.toString()); + if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) { + prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r"); + } + + prefix = ChatUtil.removeUnusedColor(prefix, 'f', true); + if (prefix.length() > 16) prefix = prefix.substring(0, 16); + if (prefix.endsWith("§")) prefix = prefix.substring(0, prefix.length() - 1); + + String suffix = suffixComponent == null || suffixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(suffixComponent.toString()); + suffix = ChatUtil.removeUnusedColor(suffix, '\0'); // Don't remove white coloring + if (suffix.length() > 16) suffix = suffix.substring(0, 16); + if (suffix.endsWith("§")) suffix = suffix.substring(0, suffix.length() - 1); + + wrapper.write(Type.STRING, prefix); + wrapper.write(Type.STRING, suffix); + + wrapper.write(Type.BYTE, flags); + wrapper.write(Type.STRING, nameTagVisibility); + wrapper.write(Type.STRING, collisionRule); + + wrapper.write(Type.BYTE, (byte) colour); + } + + if (action == 0 || action == 3 || action == 4) { + wrapper.passthrough(Type.STRING_ARRAY); //Entities + } + }); + } + }); + + protocol.registerClientbound(ClientboundPackets1_13.DECLARE_COMMANDS, null, wrapper -> { + wrapper.cancel(); + + TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); + + if (!storage.commands().isEmpty()) { + storage.commands().clear(); + } + + int size = wrapper.read(Type.VAR_INT); + boolean initialNodes = true; + for (int i = 0; i < size; i++) { + byte flags = wrapper.read(Type.BYTE); + wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices + if ((flags & 0x08) != 0) { + wrapper.read(Type.VAR_INT); // Redirect node index + } + + byte nodeType = (byte) (flags & 0x03); + if (initialNodes && nodeType == 2) { + initialNodes = false; + } + + if (nodeType == 1 || nodeType == 2) { // Literal/argument node + String name = wrapper.read(Type.STRING); + if (nodeType == 1 && initialNodes) { + storage.commands().add('/' + name); + } + } + + if (nodeType == 2) { // Argument node + commandRewriter.handleArgument(wrapper, wrapper.read(Type.STRING)); + } + + if ((flags & 0x10) != 0) { + wrapper.read(Type.STRING); // Suggestion type + } + } + }); + + protocol.registerClientbound(ClientboundPackets1_13.TAB_COMPLETE, wrapper -> { + TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); + if (storage.lastRequest() == null) { + wrapper.cancel(); + return; + } + if (storage.lastId() != wrapper.read(Type.VAR_INT)) wrapper.cancel(); + int start = wrapper.read(Type.VAR_INT); + int length = wrapper.read(Type.VAR_INT); + + int lastRequestPartIndex = storage.lastRequest().lastIndexOf(' ') + 1; + if (lastRequestPartIndex != start) wrapper.cancel(); // Client only replaces after space + + if (length != storage.lastRequest().length() - lastRequestPartIndex) { + wrapper.cancel(); // We can't set the length in previous versions + } + + int count = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < count; i++) { + String match = wrapper.read(Type.STRING); + wrapper.write(Type.STRING, (start == 0 && !storage.isLastAssumeCommand() ? "/" : "") + match); + // Ignore tooltip + if (wrapper.read(Type.BOOLEAN)) { + wrapper.read(Type.STRING); + } + } + }); + + protocol.registerServerbound(ServerboundPackets1_12_1.TAB_COMPLETE, wrapper -> { + TabCompleteStorage storage = wrapper.user().get(TabCompleteStorage.class); + List suggestions = new ArrayList<>(); + + String command = wrapper.read(Type.STRING); + boolean assumeCommand = wrapper.read(Type.BOOLEAN); + wrapper.read(Type.OPTIONAL_POSITION); + + if (!assumeCommand && !command.startsWith("/")) { + // Complete usernames for non-commands + String buffer = command.substring(command.lastIndexOf(' ') + 1); + for (String value : storage.usernames().values()) { + if (startsWithIgnoreCase(value, buffer)) { + suggestions.add(value); + } + } + } else if (!storage.commands().isEmpty() && !command.contains(" ")) { + // Complete commands names with values from 'Declare Commands' packet + for (String value : storage.commands()) { + if (startsWithIgnoreCase(value, command)) { + suggestions.add(value); + } + } + } + + if (!suggestions.isEmpty()) { + wrapper.cancel(); + PacketWrapper response = wrapper.create(ClientboundPackets1_12_1.TAB_COMPLETE); + response.write(Type.VAR_INT, suggestions.size()); + for (String value : suggestions) { + response.write(Type.STRING, value); + } + response.scheduleSend(Protocol1_12_2To1_13.class); + storage.setLastRequest(null); + return; + } + + if (!assumeCommand && command.startsWith("/")) { + command = command.substring(1); + } + + int id = ThreadLocalRandom.current().nextInt(); + wrapper.write(Type.VAR_INT, id); + wrapper.write(Type.STRING, command); + + storage.setLastId(id); + storage.setLastAssumeCommand(assumeCommand); + storage.setLastRequest(command); + }); + + protocol.registerServerbound(ServerboundPackets1_12_1.PLUGIN_MESSAGE, wrapper -> { + String channel = wrapper.read(Type.STRING); + switch (channel) { + case "MC|BSign": + case "MC|BEdit": + wrapper.setPacketType(ServerboundPackets1_13.EDIT_BOOK); + Item book = wrapper.read(Type.ITEM); + wrapper.write(Type.FLAT_ITEM, protocol.getItemRewriter().handleItemToServer(book)); + boolean signing = channel.equals("MC|BSign"); + wrapper.write(Type.BOOLEAN, signing); + break; + case "MC|ItemName": + wrapper.setPacketType(ServerboundPackets1_13.RENAME_ITEM); + break; + case "MC|AdvCmd": + byte type = wrapper.read(Type.BYTE); + if (type == 0) { + //Information from https://wiki.vg/index.php?title=Plugin_channels&oldid=14089 + //The Notchain client only uses this for command block minecarts and uses MC|AutoCmd for blocks, but the Notchian server still accepts it for either. + //Maybe older versions used this and we need to implement this? The issues is that we would have to save the command block types + wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK); + wrapper.cancel(); + ViaBackwards.getPlatform().getLogger().warning("Client send MC|AdvCmd custom payload to update command block, weird!"); + } else if (type == 1) { + wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK_MINECART); + wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Entity Id + wrapper.passthrough(Type.STRING); //Command + wrapper.passthrough(Type.BOOLEAN); //Track Output + + } else { + wrapper.cancel(); + } + break; + case "MC|AutoCmd": { + wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK); + + int x = wrapper.read(Type.INT); + int y = wrapper.read(Type.INT); + int z = wrapper.read(Type.INT); + + wrapper.write(Type.POSITION, new Position(x, (short) y, z)); + + wrapper.passthrough(Type.STRING); //Command + + byte flags = 0; + if (wrapper.read(Type.BOOLEAN)) flags |= 0x01; //Track Output + + String mode = wrapper.read(Type.STRING); + + int modeId = mode.equals("SEQUENCE") ? 0 : mode.equals("AUTO") ? 1 : 2; + wrapper.write(Type.VAR_INT, modeId); + + if (wrapper.read(Type.BOOLEAN)) flags |= 0x02; //Is conditional + if (wrapper.read(Type.BOOLEAN)) flags |= 0x04; //Automatic + + wrapper.write(Type.BYTE, flags); + break; + } + case "MC|Struct": { + wrapper.setPacketType(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK); + int x = wrapper.read(Type.INT); + int y = wrapper.read(Type.INT); + int z = wrapper.read(Type.INT); + wrapper.write(Type.POSITION, new Position(x, (short) y, z)); + wrapper.write(Type.VAR_INT, wrapper.read(Type.BYTE) - 1); + String mode = wrapper.read(Type.STRING); + int modeId = mode.equals("SAVE") ? 0 : mode.equals("LOAD") ? 1 : mode.equals("CORNER") ? 2 : 3; + wrapper.write(Type.VAR_INT, modeId); + wrapper.passthrough(Type.STRING); //Name + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset X + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset Y + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset Z + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size X + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size Y + + wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size Z + + String mirror = wrapper.read(Type.STRING); + int mirrorId = mode.equals("NONE") ? 0 : mode.equals("LEFT_RIGHT") ? 1 : 2; + String rotation = wrapper.read(Type.STRING); + int rotationId = mode.equals("NONE") ? 0 : mode.equals("CLOCKWISE_90") ? 1 : mode.equals("CLOCKWISE_180") ? 2 : 3; + wrapper.passthrough(Type.STRING); //Metadata + + byte flags = 0; + if (wrapper.read(Type.BOOLEAN)) flags |= 0x01; //Ignore entities + if (wrapper.read(Type.BOOLEAN)) flags |= 0x02; //Show air + if (wrapper.read(Type.BOOLEAN)) flags |= 0x04; //Show bounding box + wrapper.passthrough(Type.FLOAT); //Integrity + + wrapper.passthrough(Type.VAR_LONG); //Seed + + wrapper.write(Type.BYTE, flags); + break; + } + case "MC|Beacon": + wrapper.setPacketType(ServerboundPackets1_13.SET_BEACON_EFFECT); + wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Primary Effect + + wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Secondary Effect + + break; + case "MC|TrSel": + wrapper.setPacketType(ServerboundPackets1_13.SELECT_TRADE); + wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Slot + + break; + case "MC|PickItem": + wrapper.setPacketType(ServerboundPackets1_13.PICK_ITEM); + break; + default: + String newChannel = InventoryPackets.getNewPluginChannelId(channel); + if (newChannel == null) { + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("Ignoring incoming plugin message with channel: " + channel); + } + wrapper.cancel(); return; } + wrapper.write(Type.STRING, newChannel); - if (!assumeCommand && command.startsWith("/")) { - command = command.substring(1); + if (newChannel.equals("minecraft:register") || newChannel.equals("minecraft:unregister")) { + String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); + List rewrittenChannels = new ArrayList<>(); + for (String s : channels) { + String rewritten = InventoryPackets.getNewPluginChannelId(s); + if (rewritten != null) { + rewrittenChannels.add(rewritten); + } else if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("Ignoring plugin channel in incoming REGISTER: " + s); + } + } + if (!rewrittenChannels.isEmpty()) { + wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(rewrittenChannels).getBytes(StandardCharsets.UTF_8)); + } else { + wrapper.cancel(); + return; + } } - - int id = ThreadLocalRandom.current().nextInt(); - wrapper.write(Type.VAR_INT, id); - wrapper.write(Type.STRING, command); - - storage.setLastId(id); - storage.setLastAssumeCommand(assumeCommand); - storage.setLastRequest(command); - }); + break; } }); - protocol.registerServerbound(ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.STATISTICS, new PacketHandlers() { @Override - public void registerMap() { - handler(wrapper -> { - String channel = wrapper.read(Type.STRING); - switch (channel) { - case "MC|BSign": - case "MC|BEdit": - wrapper.setPacketType(ServerboundPackets1_13.EDIT_BOOK); - Item book = wrapper.read(Type.ITEM); - wrapper.write(Type.FLAT_ITEM, protocol.getItemRewriter().handleItemToServer(book)); - boolean signing = channel.equals("MC|BSign"); - wrapper.write(Type.BOOLEAN, signing); - break; - case "MC|ItemName": - wrapper.setPacketType(ServerboundPackets1_13.RENAME_ITEM); - break; - case "MC|AdvCmd": - byte type = wrapper.read(Type.BYTE); - if (type == 0) { - //Information from https://wiki.vg/index.php?title=Plugin_channels&oldid=14089 - //The Notchain client only uses this for command block minecarts and uses MC|AutoCmd for blocks, but the Notchian server still accepts it for either. - //Maybe older versions used this and we need to implement this? The issues is that we would have to save the command block types - wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK); - wrapper.cancel(); - ViaBackwards.getPlatform().getLogger().warning("Client send MC|AdvCmd custom payload to update command block, weird!"); - } else if (type == 1) { - wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK_MINECART); - wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Entity Id - wrapper.passthrough(Type.STRING); //Command - wrapper.passthrough(Type.BOOLEAN); //Track Output - - } else { - wrapper.cancel(); - } - break; - case "MC|AutoCmd": { - wrapper.setPacketType(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK); - - int x = wrapper.read(Type.INT); - int y = wrapper.read(Type.INT); - int z = wrapper.read(Type.INT); - - wrapper.write(Type.POSITION, new Position(x, (short) y, z)); - - wrapper.passthrough(Type.STRING); //Command - - byte flags = 0; - if (wrapper.read(Type.BOOLEAN)) flags |= 0x01; //Track Output - - String mode = wrapper.read(Type.STRING); - - int modeId = mode.equals("SEQUENCE") ? 0 : mode.equals("AUTO") ? 1 : 2; - wrapper.write(Type.VAR_INT, modeId); - - if (wrapper.read(Type.BOOLEAN)) flags |= 0x02; //Is conditional - if (wrapper.read(Type.BOOLEAN)) flags |= 0x04; //Automatic - - wrapper.write(Type.BYTE, flags); - break; - } - case "MC|Struct": { - wrapper.setPacketType(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK); - int x = wrapper.read(Type.INT); - int y = wrapper.read(Type.INT); - int z = wrapper.read(Type.INT); - wrapper.write(Type.POSITION, new Position(x, (short) y, z)); - wrapper.write(Type.VAR_INT, wrapper.read(Type.BYTE) - 1); - String mode = wrapper.read(Type.STRING); - int modeId = mode.equals("SAVE") ? 0 : mode.equals("LOAD") ? 1 : mode.equals("CORNER") ? 2 : 3; - wrapper.write(Type.VAR_INT, modeId); - wrapper.passthrough(Type.STRING); //Name - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset X - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset Y - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Offset Z - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size X - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size Y - - wrapper.write(Type.BYTE, wrapper.read(Type.INT).byteValue()); //Size Z - - String mirror = wrapper.read(Type.STRING); - int mirrorId = mode.equals("NONE") ? 0 : mode.equals("LEFT_RIGHT") ? 1 : 2; - String rotation = wrapper.read(Type.STRING); - int rotationId = mode.equals("NONE") ? 0 : mode.equals("CLOCKWISE_90") ? 1 : mode.equals("CLOCKWISE_180") ? 2 : 3; - wrapper.passthrough(Type.STRING); //Metadata - - byte flags = 0; - if (wrapper.read(Type.BOOLEAN)) flags |= 0x01; //Ignore entities - if (wrapper.read(Type.BOOLEAN)) flags |= 0x02; //Show air - if (wrapper.read(Type.BOOLEAN)) flags |= 0x04; //Show bounding box - wrapper.passthrough(Type.FLOAT); //Integrity - - wrapper.passthrough(Type.VAR_LONG); //Seed - - wrapper.write(Type.BYTE, flags); - break; - } - case "MC|Beacon": - wrapper.setPacketType(ServerboundPackets1_13.SET_BEACON_EFFECT); - wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Primary Effect - - wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Secondary Effect - - break; - case "MC|TrSel": - wrapper.setPacketType(ServerboundPackets1_13.SELECT_TRADE); - wrapper.write(Type.VAR_INT, wrapper.read(Type.INT)); //Slot - - break; - case "MC|PickItem": - wrapper.setPacketType(ServerboundPackets1_13.PICK_ITEM); - break; - default: - String newChannel = InventoryPackets.getNewPluginChannelId(channel); - if (newChannel == null) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("Ignoring incoming plugin message with channel: " + channel); - } - wrapper.cancel(); - return; - } - wrapper.write(Type.STRING, newChannel); - - if (newChannel.equals("minecraft:register") || newChannel.equals("minecraft:unregister")) { - String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); - List rewrittenChannels = new ArrayList<>(); - for (String s : channels) { - String rewritten = InventoryPackets.getNewPluginChannelId(s); - if (rewritten != null) { - rewrittenChannels.add(rewritten); - } else if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("Ignoring plugin channel in incoming REGISTER: " + s); - } - } - if (!rewrittenChannels.isEmpty()) { - wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(rewrittenChannels).getBytes(StandardCharsets.UTF_8)); - } else { - wrapper.cancel(); - return; - } - } - break; - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_13.STATISTICS, new PacketRemapper() { - @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int size = wrapper.get(Type.VAR_INT, 0); - int newSize = size; - for (int i = 0; i < size; i++) { - int categoryId = wrapper.read(Type.VAR_INT); - int statisticId = wrapper.read(Type.VAR_INT); + handler(wrapper -> { + int size = wrapper.get(Type.VAR_INT, 0); + int newSize = size; + for (int i = 0; i < size; i++) { + int categoryId = wrapper.read(Type.VAR_INT); + int statisticId = wrapper.read(Type.VAR_INT); - String name = ""; - // categories 0-7 (items, blocks, entities) - probably not feasible - switch (categoryId) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - wrapper.read(Type.VAR_INT); // remove value + String name = ""; + // categories 0-7 (items, blocks, entities) - probably not feasible + switch (categoryId) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + wrapper.read(Type.VAR_INT); // remove value + newSize--; + continue; + case 8: + name = protocol.getMappingData().getStatisticMappings().get(statisticId); + if (name == null) { + wrapper.read(Type.VAR_INT); newSize--; continue; - case 8: - name = protocol.getMappingData().getStatisticMappings().get(statisticId); - if (name == null) { - wrapper.read(Type.VAR_INT); - newSize--; - continue; - } - break; - } - - wrapper.write(Type.STRING, name); // string id - wrapper.passthrough(Type.VAR_INT); // value + } + break; } - if (newSize != size) { - wrapper.set(Type.VAR_INT, 0, newSize); - } + wrapper.write(Type.STRING, name); // string id + wrapper.passthrough(Type.VAR_INT); // value + } + + if (newSize != size) { + wrapper.set(Type.VAR_INT, 0, newSize); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java index 38d4ec1d..c4840307 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java @@ -19,7 +19,7 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.packets; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.NamedSoundMapping; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.rewriter.RewriterBase; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; @@ -34,55 +34,45 @@ public class SoundPackets1_13 extends RewriterBase { @Override protected void registerPackets() { - protocol.registerClientbound(ClientboundPackets1_13.NAMED_SOUND, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - String sound = wrapper.read(Type.STRING); - String mappedSound = NamedSoundMapping.getOldId(sound); - if (mappedSound != null || (mappedSound = protocol.getMappingData().getMappedNamedSound(sound)) != null) { - wrapper.write(Type.STRING, mappedSound); - } else { - wrapper.write(Type.STRING, sound); - } - }); + protocol.registerClientbound(ClientboundPackets1_13.NAMED_SOUND, wrapper -> { + String sound = wrapper.read(Type.STRING); + String mappedSound = NamedSoundMapping.getOldId(sound); + if (mappedSound != null || (mappedSound = protocol.getMappingData().getMappedNamedSound(sound)) != null) { + wrapper.write(Type.STRING, mappedSound); + } else { + wrapper.write(Type.STRING, sound); } }); // Stop Sound -> Plugin Message - protocol.registerClientbound(ClientboundPackets1_13.STOP_SOUND, ClientboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.write(Type.STRING, "MC|StopSound"); - byte flags = wrapper.read(Type.BYTE); - String source; - if ((flags & 0x01) != 0) { - source = SOUND_SOURCES[wrapper.read(Type.VAR_INT)]; - } else { - source = ""; - } - - String sound; - if ((flags & 0x02) != 0) { - String newSound = wrapper.read(Type.STRING); - sound = protocol.getMappingData().getMappedNamedSound(newSound); - if (sound == null) { - sound = ""; - } - } else { - sound = ""; - } - - wrapper.write(Type.STRING, source); - wrapper.write(Type.STRING, sound); - }); + protocol.registerClientbound(ClientboundPackets1_13.STOP_SOUND, ClientboundPackets1_12_1.PLUGIN_MESSAGE, wrapper -> { + wrapper.write(Type.STRING, "MC|StopSound"); + byte flags = wrapper.read(Type.BYTE); + String source; + if ((flags & 0x01) != 0) { + source = SOUND_SOURCES[wrapper.read(Type.VAR_INT)]; + } else { + source = ""; } + + String sound; + if ((flags & 0x02) != 0) { + String newSound = wrapper.read(Type.STRING); + sound = protocol.getMappingData().getMappedNamedSound(newSound); + if (sound == null) { + sound = ""; + } + } else { + sound = ""; + } + + wrapper.write(Type.STRING, source); + wrapper.write(Type.STRING, sound); }); - protocol.registerClientbound(ClientboundPackets1_13.SOUND, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SOUND, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); handler(wrapper -> { int newSound = wrapper.get(Type.VAR_INT, 0); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java index b461828b..d253f505 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java @@ -22,7 +22,6 @@ import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.libs.fastutil.ints.IntOpenHashSet; import com.viaversion.viaversion.libs.fastutil.ints.IntSet; - import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java index df55ac4d..f42d1d15 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/TabCompleteStorage.java @@ -18,7 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage; import com.viaversion.viaversion.api.connection.StorableObject; - import java.util.HashMap; import java.util.HashSet; import java.util.Map; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java index fe863bb3..0b30d9a9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/Protocol1_13_1To1_13_2.java @@ -22,9 +22,7 @@ import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets.Entit import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets.InventoryPackets1_13_2; import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets.WorldPackets1_13_2; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; @@ -41,52 +39,44 @@ public class Protocol1_13_1To1_13_2 extends BackwardsProtocol { + wrapper.passthrough(Type.BOOLEAN); // Reset/clear + int size = wrapper.passthrough(Type.VAR_INT); // Mapping size - for (int i = 0; i < size; i++) { - wrapper.passthrough(Type.STRING); // Identifier + for (int i = 0; i < size; i++) { + wrapper.passthrough(Type.STRING); // Identifier - // Parent - if (wrapper.passthrough(Type.BOOLEAN)) - wrapper.passthrough(Type.STRING); + // Parent + if (wrapper.passthrough(Type.BOOLEAN)) + wrapper.passthrough(Type.STRING); - // Display data - if (wrapper.passthrough(Type.BOOLEAN)) { - wrapper.passthrough(Type.COMPONENT); // Title - wrapper.passthrough(Type.COMPONENT); // Description - Item icon = wrapper.read(Type.FLAT_VAR_INT_ITEM); - wrapper.write(Type.FLAT_ITEM, icon); - wrapper.passthrough(Type.VAR_INT); // Frame type - int flags = wrapper.passthrough(Type.INT); // Flags - if ((flags & 1) != 0) - wrapper.passthrough(Type.STRING); // Background texture - wrapper.passthrough(Type.FLOAT); // X - wrapper.passthrough(Type.FLOAT); // Y - } + // Display data + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.COMPONENT); // Title + wrapper.passthrough(Type.COMPONENT); // Description + Item icon = wrapper.read(Type.FLAT_VAR_INT_ITEM); + wrapper.write(Type.FLAT_ITEM, icon); + wrapper.passthrough(Type.VAR_INT); // Frame type + int flags = wrapper.passthrough(Type.INT); // Flags + if ((flags & 1) != 0) + wrapper.passthrough(Type.STRING); // Background texture + wrapper.passthrough(Type.FLOAT); // X + wrapper.passthrough(Type.FLOAT); // Y + } - wrapper.passthrough(Type.STRING_ARRAY); // Criteria + wrapper.passthrough(Type.STRING_ARRAY); // Criteria - int arrayLength = wrapper.passthrough(Type.VAR_INT); - for (int array = 0; array < arrayLength; array++) { - wrapper.passthrough(Type.STRING_ARRAY); // String array - } - } - } - }); + int arrayLength = wrapper.passthrough(Type.VAR_INT); + for (int array = 0; array < arrayLength; array++) { + wrapper.passthrough(Type.STRING_ARRAY); // String array + } } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java index bf835f98..47d93bd2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java @@ -19,9 +19,7 @@ package com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets; import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.Protocol1_13_1To1_13_2; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.api.type.types.version.Types1_13_2; @@ -31,9 +29,9 @@ public class EntityPackets1_13_2 { public static void register(Protocol1_13_1To1_13_2 protocol) { - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.UUID); // 1 - Entity UUID map(Type.VAR_INT); // 2 - Entity Type @@ -48,20 +46,17 @@ public class EntityPackets1_13_2 { map(Type.SHORT); // 11 - Velocity Z map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST); // 12 - Metadata - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); - } + handler(wrapper -> { + for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { + metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.UUID); // 1 - Player UUID map(Type.DOUBLE); // 2 - X @@ -71,29 +66,23 @@ public class EntityPackets1_13_2 { map(Type.BYTE); // 6 - Pitch map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST); // 7 - Metadata - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); - } + handler(wrapper -> { + for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { + metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.ENTITY_METADATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); - } + handler(wrapper -> { + for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { + metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId())); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/InventoryPackets1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/InventoryPackets1_13_2.java index 97ac085b..b50d5f8d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/InventoryPackets1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/InventoryPackets1_13_2.java @@ -18,9 +18,7 @@ package com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets; import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.Protocol1_13_1To1_13_2; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; @@ -28,107 +26,96 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPacke public class InventoryPackets1_13_2 { public static void register(Protocol1_13_1To1_13_2 protocol) { - protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.SHORT); // 1 - Slot ID map(Type.FLAT_VAR_INT_ITEM, Type.FLAT_ITEM); // 2 - Slot Value } }); - protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.FLAT_VAR_INT_ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values } }); - protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // Channel - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - String channel = wrapper.get(Type.STRING, 0); - if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) { - wrapper.passthrough(Type.INT); // Passthrough Window ID + handler(wrapper -> { + String channel = wrapper.get(Type.STRING, 0); + if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - // Input Item + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + // Input Item + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); + // Output Item + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); + + boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item + if (secondItem) { wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - // Output Item - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - - boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item - if (secondItem) { - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - } - - wrapper.passthrough(Type.BOOLEAN); // Trade disabled - wrapper.passthrough(Type.INT); // Number of tools uses - wrapper.passthrough(Type.INT); // Maximum number of trade uses } + + wrapper.passthrough(Type.BOOLEAN); // Trade disabled + wrapper.passthrough(Type.INT); // Number of tools uses + wrapper.passthrough(Type.INT); // Maximum number of trade uses } } }); } }); - protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.VAR_INT); // 1 - Slot ID map(Type.FLAT_VAR_INT_ITEM, Type.FLAT_ITEM); // 2 - Item } }); - protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int recipesNo = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < recipesNo; i++) { - wrapper.passthrough(Type.STRING); // Id - String type = wrapper.passthrough(Type.STRING); - if (type.equals("crafting_shapeless")) { - wrapper.passthrough(Type.STRING); // Group - int ingredientsNo = wrapper.passthrough(Type.VAR_INT); - for (int i1 = 0; i1 < ingredientsNo; i1++) { - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); - } - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - } else if (type.equals("crafting_shaped")) { - int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.STRING); // Group - for (int i1 = 0; i1 < ingredientsNo; i1++) { - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); - } - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - } else if (type.equals("smelting")) { - wrapper.passthrough(Type.STRING); // Group - // Ingredient start - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); - // Ingredient end - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - wrapper.passthrough(Type.FLOAT); // EXP - wrapper.passthrough(Type.VAR_INT); // Cooking time - } - } + protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, wrapper -> { + int recipesNo = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < recipesNo; i++) { + wrapper.passthrough(Type.STRING); // Id + String type = wrapper.passthrough(Type.STRING); + if (type.equals("crafting_shapeless")) { + wrapper.passthrough(Type.STRING); // Group + int ingredientsNo = wrapper.passthrough(Type.VAR_INT); + for (int i1 = 0; i1 < ingredientsNo; i1++) { + wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); } - }); + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); + } else if (type.equals("crafting_shaped")) { + int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.STRING); // Group + for (int i1 = 0; i1 < ingredientsNo; i1++) { + wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); + } + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); + } else if (type.equals("smelting")) { + wrapper.passthrough(Type.STRING); // Group + // Ingredient start + wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); + // Ingredient end + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); + wrapper.passthrough(Type.FLOAT); // EXP + wrapper.passthrough(Type.VAR_INT); // Cooking time + } } }); - protocol.registerServerbound(ServerboundPackets1_13.CLICK_WINDOW, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.CLICK_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // 0 - Window ID map(Type.SHORT); // 1 - Slot map(Type.BYTE); // 2 - Button @@ -138,9 +125,9 @@ public class InventoryPackets1_13_2 { } }); - protocol.registerServerbound(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.SHORT); // 0 - Slot map(Type.FLAT_ITEM, Type.FLAT_VAR_INT_ITEM); // 1 - Clicked Item } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/WorldPackets1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/WorldPackets1_13_2.java index 959959e8..c393aee8 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/WorldPackets1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/WorldPackets1_13_2.java @@ -18,18 +18,16 @@ package com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.packets; import com.viaversion.viabackwards.protocol.protocol1_13_1to1_13_2.Protocol1_13_1To1_13_2; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; public class WorldPackets1_13_2 { public static void register(Protocol1_13_1To1_13_2 protocol) { - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // 0 - Particle ID map(Type.BOOLEAN); // 1 - Long Distance map(Type.FLOAT); // 2 - X @@ -41,13 +39,10 @@ public class WorldPackets1_13_2 { map(Type.FLOAT); // 8 - Particle Data map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - if (id == 27) { - wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); - } + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + if (id == 27) { + wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM)); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index 628b9fcf..ac9d8bb4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -29,9 +29,6 @@ import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLi import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; @@ -79,110 +76,94 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { + int blockTagsSize = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < blockTagsSize; i++) { + wrapper.passthrough(Type.STRING); + int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); + for (int j = 0; j < blockIds.length; j++) { + int id = blockIds[j]; + // Ignore new blocktags + int blockId = getMappingData().getNewBlockId(id); + blockIds[j] = blockId; + } + } - int itemTagsSize = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < itemTagsSize; i++) { - wrapper.passthrough(Type.STRING); - int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); - for (int j = 0; j < itemIds.length; j++) { - int itemId = itemIds[j]; - // Ignore new itemtags - int oldId = getMappingData().getItemMappings().get(itemId); - itemIds[j] = oldId; - } - } + int itemTagsSize = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < itemTagsSize; i++) { + wrapper.passthrough(Type.STRING); + int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); + for (int j = 0; j < itemIds.length; j++) { + int itemId = itemIds[j]; + // Ignore new itemtags + int oldId = getMappingData().getItemMappings().get(itemId); + itemIds[j] = oldId; + } + } - int fluidTagsSize = wrapper.passthrough(Type.VAR_INT); // fluid tags - for (int i = 0; i < fluidTagsSize; i++) { - wrapper.passthrough(Type.STRING); - wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); - } + int fluidTagsSize = wrapper.passthrough(Type.VAR_INT); // fluid tags + for (int i = 0; i < fluidTagsSize; i++) { + wrapper.passthrough(Type.STRING); + wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); + } - // Eat entity tags - int entityTagsSize = wrapper.read(Type.VAR_INT); - for (int i = 0; i < entityTagsSize; i++) { - wrapper.read(Type.STRING); - wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); - } - } - }); + // Eat entity tags + int entityTagsSize = wrapper.read(Type.VAR_INT); + for (int i = 0; i < entityTagsSize; i++) { + wrapper.read(Type.STRING); + wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); } }); - registerClientbound(ClientboundPackets1_14.UPDATE_LIGHT, null, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int x = wrapper.read(Type.VAR_INT); - int z = wrapper.read(Type.VAR_INT); - int skyLightMask = wrapper.read(Type.VAR_INT); - int blockLightMask = wrapper.read(Type.VAR_INT); - int emptySkyLightMask = wrapper.read(Type.VAR_INT); - int emptyBlockLightMask = wrapper.read(Type.VAR_INT); + registerClientbound(ClientboundPackets1_14.UPDATE_LIGHT, null, wrapper -> { + int x = wrapper.read(Type.VAR_INT); + int z = wrapper.read(Type.VAR_INT); + int skyLightMask = wrapper.read(Type.VAR_INT); + int blockLightMask = wrapper.read(Type.VAR_INT); + int emptySkyLightMask = wrapper.read(Type.VAR_INT); + int emptyBlockLightMask = wrapper.read(Type.VAR_INT); - byte[][] skyLight = new byte[16][]; - // we don't need void and +256 light - if (isSet(skyLightMask, 0)) { - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } - for (int i = 0; i < 16; i++) { - if (isSet(skyLightMask, i + 1)) { - skyLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } else if (isSet(emptySkyLightMask, i + 1)) { - skyLight[i] = ChunkLightStorage.EMPTY_LIGHT; - } - } - if (isSet(skyLightMask, 17)) { - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } - - byte[][] blockLight = new byte[16][]; - if (isSet(blockLightMask, 0)) { - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } - for (int i = 0; i < 16; i++) { - if (isSet(blockLightMask, i + 1)) { - blockLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } else if (isSet(emptyBlockLightMask, i + 1)) { - blockLight[i] = ChunkLightStorage.EMPTY_LIGHT; - } - } - if (isSet(blockLightMask, 17)) { - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - } - - //TODO Soft memory leak: Don't store light if chunk is already loaded - wrapper.user().get(ChunkLightStorage.class).setStoredLight(skyLight, blockLight, x, z); - wrapper.cancel(); - } - - private boolean isSet(int mask, int i) { - return (mask & (1 << i)) != 0; - } - }); + byte[][] skyLight = new byte[16][]; + // we don't need void and +256 light + if (isSet(skyLightMask, 0)) { + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); } + for (int i = 0; i < 16; i++) { + if (isSet(skyLightMask, i + 1)) { + skyLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + } else if (isSet(emptySkyLightMask, i + 1)) { + skyLight[i] = ChunkLightStorage.EMPTY_LIGHT; + } + } + if (isSet(skyLightMask, 17)) { + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + } + + byte[][] blockLight = new byte[16][]; + if (isSet(blockLightMask, 0)) { + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + } + for (int i = 0; i < 16; i++) { + if (isSet(blockLightMask, i + 1)) { + blockLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + } else if (isSet(emptyBlockLightMask, i + 1)) { + blockLight[i] = ChunkLightStorage.EMPTY_LIGHT; + } + } + if (isSet(blockLightMask, 17)) { + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + } + + //TODO Soft memory leak: Don't store light if chunk is already loaded + wrapper.user().get(ChunkLightStorage.class).setStoredLight(skyLight, blockLight, x, z); + wrapper.cancel(); }); } + private static boolean isSet(int mask, int i) { + return (mask & (1 << i)) != 0; + } + @Override public void init(UserConnection user) { // Register ClientWorld diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java index 9b0d39f6..95c5f3f0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java @@ -34,9 +34,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.api.type.types.version.Types1_13_2; @@ -57,7 +55,6 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14T import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; - import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -72,123 +69,102 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit @Override protected void registerPackets() { - protocol.registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); + protocol.registerServerbound(ServerboundPackets1_13.EDIT_BOOK, wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); + + protocol.registerClientbound(ClientboundPackets1_14.OPEN_WINDOW, wrapper -> { + int windowId = wrapper.read(Type.VAR_INT); + wrapper.write(Type.UNSIGNED_BYTE, (short) windowId); + + int type = wrapper.read(Type.VAR_INT); + String stringType = null; + String containerTitle = null; + int slotSize = 0; + if (type < 6) { + if (type == 2) containerTitle = "Barrel"; + stringType = "minecraft:container"; + slotSize = (type + 1) * 9; + } else { + switch (type) { + case 11: + stringType = "minecraft:crafting_table"; + break; + case 9: //blast furnace + case 20: //smoker + case 13: //furnace + case 14: //grindstone + if (type == 9) containerTitle = "Blast Furnace"; + else if (type == 20) containerTitle = "Smoker"; + else if (type == 14) containerTitle = "Grindstone"; + stringType = "minecraft:furnace"; + slotSize = 3; + break; + case 6: + stringType = "minecraft:dropper"; + slotSize = 9; + break; + case 12: + stringType = "minecraft:enchanting_table"; + break; + case 10: + stringType = "minecraft:brewing_stand"; + slotSize = 5; + break; + case 18: + stringType = "minecraft:villager"; + break; + case 8: + stringType = "minecraft:beacon"; + slotSize = 1; + break; + case 21: //cartography_table + case 7: + if (type == 21) containerTitle = "Cartography Table"; + stringType = "minecraft:anvil"; + break; + case 15: + stringType = "minecraft:hopper"; + slotSize = 5; + break; + case 19: + stringType = "minecraft:shulker_box"; + slotSize = 27; + break; + } } - }); - protocol.registerClientbound(ClientboundPackets1_14.OPEN_WINDOW, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int windowId = wrapper.read(Type.VAR_INT); - wrapper.write(Type.UNSIGNED_BYTE, (short) windowId); + if (stringType == null) { + ViaBackwards.getPlatform().getLogger().warning("Can't open inventory for 1.13 player! Type: " + type); + wrapper.cancel(); + return; + } - int type = wrapper.read(Type.VAR_INT); - String stringType = null; - String containerTitle = null; - int slotSize = 0; - if (type < 6) { - if (type == 2) containerTitle = "Barrel"; - stringType = "minecraft:container"; - slotSize = (type + 1) * 9; - } else { - switch (type) { - case 11: - stringType = "minecraft:crafting_table"; - break; - case 9: //blast furnace - case 20: //smoker - case 13: //furnace - case 14: //grindstone - if (type == 9) containerTitle = "Blast Furnace"; - else if (type == 20) containerTitle = "Smoker"; - else if (type == 14) containerTitle = "Grindstone"; - stringType = "minecraft:furnace"; - slotSize = 3; - break; - case 6: - stringType = "minecraft:dropper"; - slotSize = 9; - break; - case 12: - stringType = "minecraft:enchanting_table"; - break; - case 10: - stringType = "minecraft:brewing_stand"; - slotSize = 5; - break; - case 18: - stringType = "minecraft:villager"; - break; - case 8: - stringType = "minecraft:beacon"; - slotSize = 1; - break; - case 21: //cartography_table - case 7: - if (type == 21) containerTitle = "Cartography Table"; - stringType = "minecraft:anvil"; - break; - case 15: - stringType = "minecraft:hopper"; - slotSize = 5; - break; - case 19: - stringType = "minecraft:shulker_box"; - slotSize = 27; - break; - } - } + wrapper.write(Type.STRING, stringType); - if (stringType == null) { - ViaBackwards.getPlatform().getLogger().warning("Can't open inventory for 1.13 player! Type: " + type); - wrapper.cancel(); - return; - } - - wrapper.write(Type.STRING, stringType); - - JsonElement title = wrapper.read(Type.COMPONENT); - if (containerTitle != null) { - // Don't rewrite renamed, only translatable titles - JsonObject object; - if (title.isJsonObject() && (object = title.getAsJsonObject()).has("translate")) { - // Don't rewrite other 9x3 translatable containers - if (type != 2 || object.getAsJsonPrimitive("translate").getAsString().equals("container.barrel")) { - title = ChatRewriter.legacyTextToJson(containerTitle); - } - } - } - - wrapper.write(Type.COMPONENT, title); - wrapper.write(Type.UNSIGNED_BYTE, (short) slotSize); + JsonElement title = wrapper.read(Type.COMPONENT); + if (containerTitle != null) { + // Don't rewrite renamed, only translatable titles + JsonObject object; + if (title.isJsonObject() && (object = title.getAsJsonObject()).has("translate")) { + // Don't rewrite other 9x3 translatable containers + if (type != 2 || object.getAsJsonPrimitive("translate").getAsString().equals("container.barrel")) { + title = ChatRewriter.legacyTextToJson(containerTitle); } - }); + } } + + wrapper.write(Type.COMPONENT, title); + wrapper.write(Type.UNSIGNED_BYTE, (short) slotSize); }); // Horse window -> Open Window - protocol.registerClientbound(ClientboundPackets1_14.OPEN_HORSE_WINDOW, ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.UNSIGNED_BYTE); // Window id - wrapper.write(Type.STRING, "EntityHorse"); // Type - JsonObject object = new JsonObject(); - object.addProperty("translate", "minecraft.horse"); - wrapper.write(Type.COMPONENT, object); // Title - wrapper.write(Type.UNSIGNED_BYTE, wrapper.read(Type.VAR_INT).shortValue()); // Number of slots - wrapper.passthrough(Type.INT); // Entity id - } - }); - } + protocol.registerClientbound(ClientboundPackets1_14.OPEN_HORSE_WINDOW, ClientboundPackets1_13.OPEN_WINDOW, wrapper -> { + wrapper.passthrough(Type.UNSIGNED_BYTE); // Window id + wrapper.write(Type.STRING, "EntityHorse"); // Type + JsonObject object = new JsonObject(); + object.addProperty("translate", "minecraft.horse"); + wrapper.write(Type.COMPONENT, object); // Title + wrapper.write(Type.UNSIGNED_BYTE, wrapper.read(Type.VAR_INT).shortValue()); // Number of slots + wrapper.passthrough(Type.INT); // Entity id }); BlockRewriter blockRewriter = new BlockRewriter<>(protocol, Type.POSITION); @@ -199,174 +175,146 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); // Trade List -> Plugin Message - protocol.registerClientbound(ClientboundPackets1_14.TRADE_LIST, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.STRING, "minecraft:trader_list"); + protocol.registerClientbound(ClientboundPackets1_14.TRADE_LIST, ClientboundPackets1_13.PLUGIN_MESSAGE, wrapper -> { + wrapper.write(Type.STRING, "minecraft:trader_list"); - int windowId = wrapper.read(Type.VAR_INT); - wrapper.write(Type.INT, windowId); + int windowId = wrapper.read(Type.VAR_INT); + wrapper.write(Type.INT, windowId); - int size = wrapper.passthrough(Type.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - // Input Item - Item input = wrapper.read(Type.FLAT_VAR_INT_ITEM); - input = handleItemToClient(input); - wrapper.write(Type.FLAT_VAR_INT_ITEM, input); + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + // Input Item + Item input = wrapper.read(Type.FLAT_VAR_INT_ITEM); + input = handleItemToClient(input); + wrapper.write(Type.FLAT_VAR_INT_ITEM, input); - // Output Item - Item output = wrapper.read(Type.FLAT_VAR_INT_ITEM); - output = handleItemToClient(output); - wrapper.write(Type.FLAT_VAR_INT_ITEM, output); + // Output Item + Item output = wrapper.read(Type.FLAT_VAR_INT_ITEM); + output = handleItemToClient(output); + wrapper.write(Type.FLAT_VAR_INT_ITEM, output); - boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item - if (secondItem) { - // Second Item - Item second = wrapper.read(Type.FLAT_VAR_INT_ITEM); - second = handleItemToClient(second); - wrapper.write(Type.FLAT_VAR_INT_ITEM, second); - } + boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item + if (secondItem) { + // Second Item + Item second = wrapper.read(Type.FLAT_VAR_INT_ITEM); + second = handleItemToClient(second); + wrapper.write(Type.FLAT_VAR_INT_ITEM, second); + } - wrapper.passthrough(Type.BOOLEAN); // Trade disabled - wrapper.passthrough(Type.INT); // Number of tools uses - wrapper.passthrough(Type.INT); // Maximum number of trade uses + wrapper.passthrough(Type.BOOLEAN); // Trade disabled + wrapper.passthrough(Type.INT); // Number of tools uses + wrapper.passthrough(Type.INT); // Maximum number of trade uses - wrapper.read(Type.INT); - wrapper.read(Type.INT); - wrapper.read(Type.FLOAT); - } - wrapper.read(Type.VAR_INT); - wrapper.read(Type.VAR_INT); - wrapper.read(Type.BOOLEAN); - } - }); + wrapper.read(Type.INT); + wrapper.read(Type.INT); + wrapper.read(Type.FLOAT); } + wrapper.read(Type.VAR_INT); + wrapper.read(Type.VAR_INT); + wrapper.read(Type.BOOLEAN); }); // Open Book -> Plugin Message - protocol.registerClientbound(ClientboundPackets1_14.OPEN_BOOK, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.STRING, "minecraft:book_open"); - wrapper.passthrough(Type.VAR_INT); - } - }); - } + protocol.registerClientbound(ClientboundPackets1_14.OPEN_BOOK, ClientboundPackets1_13.PLUGIN_MESSAGE, wrapper -> { + wrapper.write(Type.STRING, "minecraft:book_open"); + wrapper.passthrough(Type.VAR_INT); }); - protocol.registerClientbound(ClientboundPackets1_14.ENTITY_EQUIPMENT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.ENTITY_EQUIPMENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.VAR_INT); // 1 - Slot ID map(Type.FLAT_VAR_INT_ITEM); // 2 - Item handler(itemToClientHandler(Type.FLAT_VAR_INT_ITEM)); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType entityType = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class).entityType(entityId); - if (entityType == null) return; + handler(wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + EntityType entityType = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class).entityType(entityId); + if (entityType == null) return; - if (entityType.isOrHasParent(Entity1_14Types.ABSTRACT_HORSE)) { - wrapper.setPacketType(ClientboundPackets1_13.ENTITY_METADATA); - wrapper.resetReader(); - wrapper.passthrough(Type.VAR_INT); - wrapper.read(Type.VAR_INT); - Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM); - int armorType = item == null || item.identifier() == 0 ? 0 : item.identifier() - 726; - if (armorType < 0 || armorType > 3) { - wrapper.cancel(); - return; - } - List metadataList = new ArrayList<>(); - metadataList.add(new Metadata(16, Types1_13_2.META_TYPES.varIntType, armorType)); - wrapper.write(Types1_13.METADATA_LIST, metadataList); + if (entityType.isOrHasParent(Entity1_14Types.ABSTRACT_HORSE)) { + wrapper.setPacketType(ClientboundPackets1_13.ENTITY_METADATA); + wrapper.resetReader(); + wrapper.passthrough(Type.VAR_INT); + wrapper.read(Type.VAR_INT); + Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM); + int armorType = item == null || item.identifier() == 0 ? 0 : item.identifier() - 726; + if (armorType < 0 || armorType > 3) { + wrapper.cancel(); + return; } + List metadataList = new ArrayList<>(); + metadataList.add(new Metadata(16, Types1_13_2.META_TYPES.varIntType, armorType)); + wrapper.write(Types1_13.METADATA_LIST, metadataList); } }); } }); RecipeRewriter recipeHandler = new RecipeRewriter1_13_2<>(protocol); - protocol.registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - private final Set removedTypes = ImmutableSet.of("crafting_special_suspiciousstew", "blasting", "smoking", "campfire_cooking", "stonecutting"); - - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int size = wrapper.passthrough(Type.VAR_INT); - int deleted = 0; - for (int i = 0; i < size; i++) { - String type = wrapper.read(Type.STRING); - String id = wrapper.read(Type.STRING); // Recipe Identifier - type = type.replace("minecraft:", ""); - if (removedTypes.contains(type)) { - switch (type) { - case "blasting": - case "smoking": - case "campfire_cooking": - wrapper.read(Type.STRING); // Group - wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - wrapper.read(Type.FLAT_VAR_INT_ITEM); - wrapper.read(Type.FLOAT); // EXP - wrapper.read(Type.VAR_INT); // Cooking time - break; - case "stonecutting": - wrapper.read(Type.STRING); // Group? - wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - wrapper.read(Type.FLAT_VAR_INT_ITEM); // Result - break; - } - deleted++; - continue; - } - wrapper.write(Type.STRING, id); - wrapper.write(Type.STRING, type); - - // Handle the rest of the types - recipeHandler.handle(wrapper, type); - } - wrapper.set(Type.VAR_INT, 0, size - deleted); + final Set removedTypes = ImmutableSet.of("crafting_special_suspiciousstew", "blasting", "smoking", "campfire_cooking", "stonecutting"); + protocol.registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, wrapper -> { + int size = wrapper.passthrough(Type.VAR_INT); + int deleted = 0; + for (int i = 0; i < size; i++) { + String type = wrapper.read(Type.STRING); + String id = wrapper.read(Type.STRING); // Recipe Identifier + type = type.replace("minecraft:", ""); + if (removedTypes.contains(type)) { + switch (type) { + case "blasting": + case "smoking": + case "campfire_cooking": + wrapper.read(Type.STRING); // Group + wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients + wrapper.read(Type.FLAT_VAR_INT_ITEM); + wrapper.read(Type.FLOAT); // EXP + wrapper.read(Type.VAR_INT); // Cooking time + break; + case "stonecutting": + wrapper.read(Type.STRING); // Group? + wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients + wrapper.read(Type.FLAT_VAR_INT_ITEM); // Result + break; } - }); + deleted++; + continue; + } + wrapper.write(Type.STRING, id); + wrapper.write(Type.STRING, type); + + // Handle the rest of the types + recipeHandler.handleRecipeType(wrapper, type); } + wrapper.set(Type.VAR_INT, 0, size - deleted); }); registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); - protocol.registerClientbound(ClientboundPackets1_14.BLOCK_BREAK_ANIMATION, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.BLOCK_BREAK_ANIMATION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.POSITION1_14, Type.POSITION); map(Type.BYTE); } }); - protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ENTITY_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14, Type.POSITION); } }); - protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ACTION, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ACTION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14, Type.POSITION); // Location map(Type.UNSIGNED_BYTE); // Action id map(Type.UNSIGNED_BYTE); // Action param @@ -382,132 +330,107 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_14.BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14, Type.POSITION); map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.VAR_INT, 0); + handler(wrapper -> { + int id = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id)); - } + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id)); }); } }); blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE); - protocol.registerClientbound(ClientboundPackets1_14.EXPLOSION, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.EXPLOSION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.FLOAT); // X map(Type.FLOAT); // Y map(Type.FLOAT); // Z map(Type.FLOAT); // Radius - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (int i = 0; i < 3; i++) { - float coord = wrapper.get(Type.FLOAT, i); + handler(wrapper -> { + for (int i = 0; i < 3; i++) { + float coord = wrapper.get(Type.FLOAT, i); - if (coord < 0f) { - coord = (float) Math.floor(coord); - wrapper.set(Type.FLOAT, i, coord); - } + if (coord < 0f) { + coord = (float) Math.floor(coord); + wrapper.set(Type.FLOAT, i, coord); } } }); } }); - protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - Chunk chunk = wrapper.read(new Chunk1_14Type()); - wrapper.write(new Chunk1_13Type(clientWorld), chunk); + protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, wrapper -> { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + Chunk chunk = wrapper.read(new Chunk1_14Type()); + wrapper.write(new Chunk1_13Type(clientWorld), chunk); - ChunkLightStorage.ChunkLight chunkLight = wrapper.user().get(ChunkLightStorage.class).getStoredLight(chunk.getX(), chunk.getZ()); - for (int i = 0; i < chunk.getSections().length; i++) { - ChunkSection section = chunk.getSections()[i]; - if (section == null) continue; + ChunkLightStorage.ChunkLight chunkLight = wrapper.user().get(ChunkLightStorage.class).getStoredLight(chunk.getX(), chunk.getZ()); + for (int i = 0; i < chunk.getSections().length; i++) { + ChunkSection section = chunk.getSections()[i]; + if (section == null) continue; - ChunkSectionLight sectionLight = new ChunkSectionLightImpl(); - section.setLight(sectionLight); - if (chunkLight == null) { - sectionLight.setBlockLight(ChunkLightStorage.FULL_LIGHT); - if (clientWorld.getEnvironment() == Environment.NORMAL) { - sectionLight.setSkyLight(ChunkLightStorage.FULL_LIGHT); + ChunkSectionLight sectionLight = new ChunkSectionLightImpl(); + section.setLight(sectionLight); + if (chunkLight == null) { + sectionLight.setBlockLight(ChunkLightStorage.FULL_LIGHT); + if (clientWorld.getEnvironment() == Environment.NORMAL) { + sectionLight.setSkyLight(ChunkLightStorage.FULL_LIGHT); + } + } else { + byte[] blockLight = chunkLight.getBlockLight()[i]; + sectionLight.setBlockLight(blockLight != null ? blockLight : ChunkLightStorage.FULL_LIGHT); + if (clientWorld.getEnvironment() == Environment.NORMAL) { + byte[] skyLight = chunkLight.getSkyLight()[i]; + sectionLight.setSkyLight(skyLight != null ? skyLight : ChunkLightStorage.FULL_LIGHT); + } + } + + DataPalette palette = section.palette(PaletteType.BLOCKS); + if (Via.getConfig().isNonFullBlockLightFix() && section.getNonAirBlocksCount() != 0 && sectionLight.hasBlockLight()) { + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + int id = palette.idAt(x, y, z); + if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) { + sectionLight.getBlockLightNibbleArray().set(x, y, z, 0); } - } else { - byte[] blockLight = chunkLight.getBlockLight()[i]; - sectionLight.setBlockLight(blockLight != null ? blockLight : ChunkLightStorage.FULL_LIGHT); - if (clientWorld.getEnvironment() == Environment.NORMAL) { - byte[] skyLight = chunkLight.getSkyLight()[i]; - sectionLight.setSkyLight(skyLight != null ? skyLight : ChunkLightStorage.FULL_LIGHT); - } - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - if (Via.getConfig().isNonFullBlockLightFix() && section.getNonAirBlocksCount() != 0 && sectionLight.hasBlockLight()) { - for (int x = 0; x < 16; x++) { - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - int id = palette.idAt(x, y, z); - if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) { - sectionLight.getBlockLightNibbleArray().set(x, y, z, 0); - } - } - } - } - } - - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); } } } - }); + } + + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } } }); - protocol.registerClientbound(ClientboundPackets1_14.UNLOAD_CHUNK, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int x = wrapper.passthrough(Type.INT); - int z = wrapper.passthrough(Type.INT); - wrapper.user().get(ChunkLightStorage.class).unloadChunk(x, z); - } - }); - } + protocol.registerClientbound(ClientboundPackets1_14.UNLOAD_CHUNK, wrapper -> { + int x = wrapper.passthrough(Type.INT); + int z = wrapper.passthrough(Type.INT); + wrapper.user().get(ChunkLightStorage.class).unloadChunk(x, z); }); - protocol.registerClientbound(ClientboundPackets1_14.EFFECT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.EFFECT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // Effect Id map(Type.POSITION1_14, Type.POSITION); // Location map(Type.INT); // Data - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - int data = wrapper.get(Type.INT, 1); - if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data)); - } else if (id == 2001) { // Block break + block break sound - wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); - } + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + int data = wrapper.get(Type.INT, 1); + if (id == 1010) { // Play record + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data)); + } else if (id == 2001) { // Block break + block break sound + wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); } }); } @@ -515,9 +438,9 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit registerSpawnParticle(ClientboundPackets1_14.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT); - protocol.registerClientbound(ClientboundPackets1_14.MAP_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.MAP_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.BYTE); map(Type.BOOLEAN); @@ -525,9 +448,9 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_14.SPAWN_POSITION, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.SPAWN_POSITION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14, Type.POSITION); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java index 181b7e9e..013d7890 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java @@ -35,8 +35,7 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_13_2; @@ -72,34 +71,29 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { - int entityId = wrapper.passthrough(Type.INT); - byte status = wrapper.passthrough(Type.BYTE); - // Check for death status - if (status != 3) return; + protocol.registerClientbound(ClientboundPackets1_14.ENTITY_STATUS, wrapper -> { + int entityId = wrapper.passthrough(Type.INT); + byte status = wrapper.passthrough(Type.BYTE); + // Check for death status + if (status != 3) return; - EntityTracker tracker = tracker(wrapper.user()); - EntityType entityType = tracker.entityType(entityId); - if (entityType != Entity1_14Types.PLAYER) return; + EntityTracker tracker = tracker(wrapper.user()); + EntityType entityType = tracker.entityType(entityId); + if (entityType != Entity1_14Types.PLAYER) return; - // Remove equipment, else the client will see ghost items - for (int i = 0; i <= 5; i++) { - PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_13.ENTITY_EQUIPMENT); - equipmentPacket.write(Type.VAR_INT, entityId); - equipmentPacket.write(Type.VAR_INT, i); - equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, null); - equipmentPacket.send(Protocol1_13_2To1_14.class); - } - }); + // Remove equipment, else the client will see ghost items + for (int i = 0; i <= 5; i++) { + PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_13.ENTITY_EQUIPMENT); + equipmentPacket.write(Type.VAR_INT, entityId); + equipmentPacket.write(Type.VAR_INT, i); + equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, null); + equipmentPacket.send(Protocol1_13_2To1_14.class); } }); - protocol.registerClientbound(ClientboundPackets1_14.ENTITY_TELEPORT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.ENTITY_TELEPORT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.DOUBLE); map(Type.DOUBLE); @@ -108,30 +102,27 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { + double x = wrapper.get(Type.SHORT, 0) / EntityPositionHandler.RELATIVE_MOVE_FACTOR; + double y = wrapper.get(Type.SHORT, 1) / EntityPositionHandler.RELATIVE_MOVE_FACTOR; + double z = wrapper.get(Type.SHORT, 2) / EntityPositionHandler.RELATIVE_MOVE_FACTOR; + positionHandler.cacheEntityPosition(wrapper, x, y, z, false, true); }); } }; protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION, relativeMoveHandler); protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION_AND_ROTATION, relativeMoveHandler); - protocol.registerClientbound(ClientboundPackets1_14.SPAWN_ENTITY, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.SPAWN_ENTITY, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity id map(Type.UUID); // 1 - UUID map(Type.VAR_INT, Type.BYTE); // 2 - Type @@ -147,62 +138,59 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { + int id = wrapper.get(Type.BYTE, 0); + int mappedId = newEntityId(id); + Entity1_13Types.EntityType entityType = Entity1_13Types.getTypeFromId(mappedId, false); + Entity1_13Types.ObjectType objectType; + if (entityType.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT)) { + objectType = Entity1_13Types.ObjectType.MINECART; + int data = 0; + switch (entityType) { + case CHEST_MINECART: + data = 1; + break; + case FURNACE_MINECART: + data = 2; + break; + case TNT_MINECART: + data = 3; + break; + case SPAWNER_MINECART: + data = 4; + break; + case HOPPER_MINECART: + data = 5; + break; + case COMMAND_BLOCK_MINECART: + data = 6; + break; } + if (data != 0) + wrapper.set(Type.INT, 0, data); + } else { + objectType = Entity1_13Types.ObjectType.fromEntityType(entityType).orElse(null); + } - if (objectType == null) return; + if (objectType == null) return; - wrapper.set(Type.BYTE, 0, (byte) objectType.getId()); + wrapper.set(Type.BYTE, 0, (byte) objectType.getId()); - int data = wrapper.get(Type.INT, 0); - if (objectType == Entity1_13Types.ObjectType.FALLING_BLOCK) { - int blockState = wrapper.get(Type.INT, 0); - int combined = protocol.getMappingData().getNewBlockStateId(blockState); - wrapper.set(Type.INT, 0, combined); - } else if (entityType.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW)) { - wrapper.set(Type.INT, 0, data + 1); - } + int data = wrapper.get(Type.INT, 0); + if (objectType == Entity1_13Types.ObjectType.FALLING_BLOCK) { + int blockState = wrapper.get(Type.INT, 0); + int combined = protocol.getMappingData().getNewBlockStateId(blockState); + wrapper.set(Type.INT, 0, combined); + } else if (entityType.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW)) { + wrapper.set(Type.INT, 0, data + 1); } }); } }); - protocol.registerClientbound(ClientboundPackets1_14.SPAWN_MOB, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.SPAWN_MOB, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.UUID); // 1 - Entity UUID map(Type.VAR_INT); // 2 - Entity Type @@ -217,25 +205,22 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { + int type = wrapper.get(Type.VAR_INT, 1); + EntityType entityType = Entity1_14Types.getTypeFromId(type); + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); - int oldId = newEntityId(type); - if (oldId == -1) { - EntityData entityData = entityDataForType(entityType); - if (entityData == null) { - ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13.2 entity type for 1.14 entity type " + type + "/" + entityType); - wrapper.cancel(); - } else { - wrapper.set(Type.VAR_INT, 1, entityData.replacementId()); - } + int oldId = newEntityId(type); + if (oldId == -1) { + EntityData entityData = entityDataForType(entityType); + if (entityData == null) { + ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13.2 entity type for 1.14 entity type " + type + "/" + entityType); + wrapper.cancel(); } else { - wrapper.set(Type.VAR_INT, 1, oldId); + wrapper.set(Type.VAR_INT, 1, entityData.replacementId()); } + } else { + wrapper.set(Type.VAR_INT, 1, oldId); } }); @@ -244,9 +229,9 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java index 01d608f7..3828a30f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java @@ -20,9 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage; import com.viaversion.viaversion.api.minecraft.Position; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.rewriter.RewriterBase; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; @@ -36,9 +34,9 @@ public class PlayerPackets1_14 extends RewriterBase { @Override protected void registerPackets() { - protocol.registerClientbound(ClientboundPackets1_14.SERVER_DIFFICULTY, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_14.SERVER_DIFFICULTY, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); map(Type.BOOLEAN, Type.NOTHING); // Locked handler(wrapper -> { @@ -48,96 +46,85 @@ public class PlayerPackets1_14 extends RewriterBase { } }); - protocol.registerClientbound(ClientboundPackets1_14.OPEN_SIGN_EDITOR, new PacketRemapper() { // c + protocol.registerClientbound(ClientboundPackets1_14.OPEN_SIGN_EDITOR, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14, Type.POSITION); } }); - protocol.registerServerbound(ServerboundPackets1_13.QUERY_BLOCK_NBT, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.QUERY_BLOCK_NBT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); map(Type.POSITION, Type.POSITION1_14); } }); - protocol.registerServerbound(ServerboundPackets1_13.PLAYER_DIGGING, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.PLAYER_DIGGING, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Action map(Type.POSITION, Type.POSITION1_14); // Position } }); - protocol.registerServerbound(ServerboundPackets1_13.RECIPE_BOOK_DATA, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.RECIPE_BOOK_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int type = wrapper.get(Type.VAR_INT, 0); - if (type == 0) { - wrapper.passthrough(Type.STRING); - } else if (type == 1) { - wrapper.passthrough(Type.BOOLEAN); // Crafting Recipe Book Open - wrapper.passthrough(Type.BOOLEAN); // Crafting Recipe Filter Active - wrapper.passthrough(Type.BOOLEAN); // Smelting Recipe Book Open - wrapper.passthrough(Type.BOOLEAN); // Smelting Recipe Filter Active + handler(wrapper -> { + int type = wrapper.get(Type.VAR_INT, 0); + if (type == 0) { + wrapper.passthrough(Type.STRING); + } else if (type == 1) { + wrapper.passthrough(Type.BOOLEAN); // Crafting Recipe Book Open + wrapper.passthrough(Type.BOOLEAN); // Crafting Recipe Filter Active + wrapper.passthrough(Type.BOOLEAN); // Smelting Recipe Book Open + wrapper.passthrough(Type.BOOLEAN); // Smelting Recipe Filter Active - // Blast furnace/smoker data - wrapper.write(Type.BOOLEAN, false); - wrapper.write(Type.BOOLEAN, false); - wrapper.write(Type.BOOLEAN, false); - wrapper.write(Type.BOOLEAN, false); - } + // Blast furnace/smoker data + wrapper.write(Type.BOOLEAN, false); + wrapper.write(Type.BOOLEAN, false); + wrapper.write(Type.BOOLEAN, false); + wrapper.write(Type.BOOLEAN, false); } }); } }); - protocol.registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION, Type.POSITION1_14); } }); - protocol.registerServerbound(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION, Type.POSITION1_14); } }); - protocol.registerServerbound(ServerboundPackets1_13.UPDATE_SIGN, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_13.UPDATE_SIGN, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION, Type.POSITION1_14); } }); - protocol.registerServerbound(ServerboundPackets1_13.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Position position = wrapper.read(Type.POSITION); - int face = wrapper.read(Type.VAR_INT); - int hand = wrapper.read(Type.VAR_INT); - float x = wrapper.read(Type.FLOAT); - float y = wrapper.read(Type.FLOAT); - float z = wrapper.read(Type.FLOAT); + protocol.registerServerbound(ServerboundPackets1_13.PLAYER_BLOCK_PLACEMENT, wrapper -> { + Position position = wrapper.read(Type.POSITION); + int face = wrapper.read(Type.VAR_INT); + int hand = wrapper.read(Type.VAR_INT); + float x = wrapper.read(Type.FLOAT); + float y = wrapper.read(Type.FLOAT); + float z = wrapper.read(Type.FLOAT); - wrapper.write(Type.VAR_INT, hand); - wrapper.write(Type.POSITION1_14, position); - wrapper.write(Type.VAR_INT, face); - wrapper.write(Type.FLOAT, x); - wrapper.write(Type.FLOAT, y); - wrapper.write(Type.FLOAT, z); - wrapper.write(Type.BOOLEAN, false); // Inside block - } - }); - } + wrapper.write(Type.VAR_INT, hand); + wrapper.write(Type.POSITION1_14, position); + wrapper.write(Type.VAR_INT, face); + wrapper.write(Type.FLOAT, x); + wrapper.write(Type.FLOAT, y); + wrapper.write(Type.FLOAT, z); + wrapper.write(Type.BOOLEAN, false); // Inside block }); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java index 33423865..6e988ced 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java @@ -23,7 +23,6 @@ import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2T import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14; import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.rewriter.RewriterBase; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; @@ -43,43 +42,38 @@ public class SoundPackets1_14 extends RewriterBase { soundRewriter.registerStopSound(ClientboundPackets1_14.STOP_SOUND); // Entity Sound Effect - protocol.registerClientbound(ClientboundPackets1_14.ENTITY_SOUND, null, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.cancel(); + protocol.registerClientbound(ClientboundPackets1_14.ENTITY_SOUND, null, wrapper -> { + wrapper.cancel(); - int soundId = wrapper.read(Type.VAR_INT); - int newId = protocol.getMappingData().getSoundMappings().getNewId(soundId); - if (newId == -1) return; + int soundId = wrapper.read(Type.VAR_INT); + int newId = protocol.getMappingData().getSoundMappings().getNewId(soundId); + if (newId == -1) return; - int category = wrapper.read(Type.VAR_INT); - int entityId = wrapper.read(Type.VAR_INT); + int category = wrapper.read(Type.VAR_INT); + int entityId = wrapper.read(Type.VAR_INT); - StoredEntityData storedEntity = wrapper.user().getEntityTracker(protocol.getClass()).entityData(entityId); - EntityPositionStorage1_14 entityStorage; - if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { - ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); - return; - } - - float volume = wrapper.read(Type.FLOAT); - float pitch = wrapper.read(Type.FLOAT); - int x = (int) (entityStorage.getX() * 8D); - int y = (int) (entityStorage.getY() * 8D); - int z = (int) (entityStorage.getZ() * 8D); - - PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_13.SOUND); - soundPacket.write(Type.VAR_INT, newId); - soundPacket.write(Type.VAR_INT, category); - soundPacket.write(Type.INT, x); - soundPacket.write(Type.INT, y); - soundPacket.write(Type.INT, z); - soundPacket.write(Type.FLOAT, volume); - soundPacket.write(Type.FLOAT, pitch); - soundPacket.send(Protocol1_13_2To1_14.class); - }); + StoredEntityData storedEntity = wrapper.user().getEntityTracker(protocol.getClass()).entityData(entityId); + EntityPositionStorage1_14 entityStorage; + if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { + ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); + return; } + + float volume = wrapper.read(Type.FLOAT); + float pitch = wrapper.read(Type.FLOAT); + int x = (int) (entityStorage.getX() * 8D); + int y = (int) (entityStorage.getY() * 8D); + int z = (int) (entityStorage.getZ() * 8D); + + PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_13.SOUND); + soundPacket.write(Type.VAR_INT, newId); + soundPacket.write(Type.VAR_INT, category); + soundPacket.write(Type.INT, x); + soundPacket.write(Type.INT, y); + soundPacket.write(Type.INT, z); + soundPacket.write(Type.FLOAT, volume); + soundPacket.write(Type.FLOAT, pitch); + soundPacket.send(Protocol1_13_2To1_14.class); }); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/ChunkLightStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/ChunkLightStorage.java index 60bd5bc5..901263a0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/ChunkLightStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/ChunkLightStorage.java @@ -19,7 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; - import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java index ed0f9662..92796ed2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java @@ -30,8 +30,7 @@ import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; @@ -73,9 +72,9 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol(Type.STRING) { @Override @@ -87,24 +86,21 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol { + itemRewriter.handleItemToServer(wrapper.get(Type.FLAT_ITEM, 0)); + wrapper.write(Type.VAR_INT, 0); }); } }); - registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() { + registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // Id map(Type.STRING); // Window Type handler(wrapper -> { @@ -127,96 +123,82 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol { + int start = wrapper.get(Type.VAR_INT, 1); + wrapper.set(Type.VAR_INT, 1, start - 1); // Offset by +1 to take into account / at beginning + // Passthrough suggestions + int count = wrapper.get(Type.VAR_INT, 3); + for (int i = 0; i < count; i++) { + wrapper.passthrough(Type.STRING); + boolean hasTooltip = wrapper.passthrough(Type.BOOLEAN); + if (hasTooltip) { + wrapper.passthrough(Type.STRING); // JSON Tooltip } } }); } }); - registerClientbound(ClientboundPackets1_13.BOSSBAR, new PacketRemapper() { + registerClientbound(ClientboundPackets1_13.BOSSBAR, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UUID); map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int action = wrapper.get(Type.VAR_INT, 0); - if (action == 0 || action == 3) { - translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)); - if (action == 0) { - wrapper.passthrough(Type.FLOAT); - wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.VAR_INT); - short flags = wrapper.read(Type.UNSIGNED_BYTE); - if ((flags & 0x04) != 0) flags |= 0x02; - wrapper.write(Type.UNSIGNED_BYTE, flags); - } + handler(wrapper -> { + int action = wrapper.get(Type.VAR_INT, 0); + if (action == 0 || action == 3) { + translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)); + if (action == 0) { + wrapper.passthrough(Type.FLOAT); + wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.VAR_INT); + short flags = wrapper.read(Type.UNSIGNED_BYTE); + if ((flags & 0x04) != 0) flags |= 0x02; + wrapper.write(Type.UNSIGNED_BYTE, flags); } } }); } }); - registerClientbound(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.BOOLEAN); // Reset/clear - int size = wrapper.passthrough(Type.VAR_INT); // Mapping size + registerClientbound(ClientboundPackets1_13.ADVANCEMENTS, wrapper -> { + wrapper.passthrough(Type.BOOLEAN); // Reset/clear + int size = wrapper.passthrough(Type.VAR_INT); // Mapping size - for (int i = 0; i < size; i++) { - wrapper.passthrough(Type.STRING); // Identifier + for (int i = 0; i < size; i++) { + wrapper.passthrough(Type.STRING); // Identifier - // Parent - if (wrapper.passthrough(Type.BOOLEAN)) - wrapper.passthrough(Type.STRING); + // Parent + if (wrapper.passthrough(Type.BOOLEAN)) + wrapper.passthrough(Type.STRING); - // Display data - if (wrapper.passthrough(Type.BOOLEAN)) { - wrapper.passthrough(Type.COMPONENT); // Title - wrapper.passthrough(Type.COMPONENT); // Description - Item icon = wrapper.passthrough(Type.FLAT_ITEM); - itemRewriter.handleItemToClient(icon); - wrapper.passthrough(Type.VAR_INT); // Frame type - int flags = wrapper.passthrough(Type.INT); // Flags - if ((flags & 1) != 0) - wrapper.passthrough(Type.STRING); // Background texture - wrapper.passthrough(Type.FLOAT); // X - wrapper.passthrough(Type.FLOAT); // Y - } + // Display data + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.COMPONENT); // Title + wrapper.passthrough(Type.COMPONENT); // Description + Item icon = wrapper.passthrough(Type.FLAT_ITEM); + itemRewriter.handleItemToClient(icon); + wrapper.passthrough(Type.VAR_INT); // Frame type + int flags = wrapper.passthrough(Type.INT); // Flags + if ((flags & 1) != 0) + wrapper.passthrough(Type.STRING); // Background texture + wrapper.passthrough(Type.FLOAT); // X + wrapper.passthrough(Type.FLOAT); // Y + } - wrapper.passthrough(Type.STRING_ARRAY); // Criteria + wrapper.passthrough(Type.STRING_ARRAY); // Criteria - int arrayLength = wrapper.passthrough(Type.VAR_INT); - for (int array = 0; array < arrayLength; array++) { - wrapper.passthrough(Type.STRING_ARRAY); // String array - } - } - } - }); + int arrayLength = wrapper.passthrough(Type.VAR_INT); + for (int array = 0; array < arrayLength; array++) { + wrapper.passthrough(Type.STRING_ARRAY); // String array + } } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java index dc6c2a13..d3ac3320 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java @@ -24,14 +24,11 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; - import java.util.List; public class EntityPackets1_13_1 extends LegacyEntityRewriter { @@ -42,9 +39,9 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter { + int entityId = wrapper.get(Type.VAR_INT, 0); + byte type = wrapper.get(Type.BYTE, 0); + Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, true); + if (entType == null) { + ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type); + return; } + + // Rewrite falling block + if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) { + int data = wrapper.get(Type.INT, 0); + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data)); + } + + // Track Entity + tracker(wrapper.user()).addEntity(entityId, entType); }); } }); @@ -82,9 +76,9 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter metadata = wrapper.get(Types1_13.METADATA_LIST, 0); - handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); - } + handler(wrapper -> { + List metadata = wrapper.get(Types1_13.METADATA_LIST, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); }); } }); - protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // 0 - Entity ID map(Type.UUID); // 1 - Player UUID map(Type.DOUBLE); // 2 - X diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/InventoryPackets1_13_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/InventoryPackets1_13_1.java index f538bad7..3f9c9337 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/InventoryPackets1_13_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/InventoryPackets1_13_1.java @@ -19,9 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets; import com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.Protocol1_13To1_13_1; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; @@ -39,39 +36,31 @@ public class InventoryPackets1_13_1 extends ItemRewriter { + String channel = wrapper.passthrough(Type.STRING); + if (channel.equals("minecraft:trader_list")) { + wrapper.passthrough(Type.INT); //Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_BYTE); - for (int i = 0; i < size; i++) { - //Input Item - Item input = wrapper.passthrough(Type.FLAT_ITEM); - handleItemToClient(input); - //Output Item - Item output = wrapper.passthrough(Type.FLAT_ITEM); - handleItemToClient(output); + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + //Input Item + Item input = wrapper.passthrough(Type.FLAT_ITEM); + handleItemToClient(input); + //Output Item + Item output = wrapper.passthrough(Type.FLAT_ITEM); + handleItemToClient(output); - boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item - if (secondItem) { - //Second Item - Item second = wrapper.passthrough(Type.FLAT_ITEM); - handleItemToClient(second); - } - - wrapper.passthrough(Type.BOOLEAN); //Trade disabled - wrapper.passthrough(Type.INT); //Number of tools uses - wrapper.passthrough(Type.INT); //Maximum number of trade uses - } - } + boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item + if (secondItem) { + //Second Item + Item second = wrapper.passthrough(Type.FLAT_ITEM); + handleItemToClient(second); } - }); + + wrapper.passthrough(Type.BOOLEAN); //Trade disabled + wrapper.passthrough(Type.INT); //Number of tools uses + wrapper.passthrough(Type.INT); //Maximum number of trade uses + } } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java index 872645bc..9de4a9aa 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/packets/WorldPackets1_13_1.java @@ -24,10 +24,7 @@ import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; @@ -39,34 +36,29 @@ public class WorldPackets1_13_1 { public static void register(Protocol1_13To1_13_1 protocol) { BlockRewriter blockRewriter = new BlockRewriter<>(protocol, Type.POSITION); - protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld)); + protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, wrapper -> { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld)); - for (ChunkSection section : chunk.getSections()) { - if (section == null) { - continue; - } + for (ChunkSection section : chunk.getSections()) { + if (section == null) { + continue; + } - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } - }); + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int i = 0; i < palette.size(); i++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); + palette.setIdByIndex(i, mappedBlockStateId); + } } }); blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE); blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE); - protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // Effect Id map(Type.POSITION); // Location map(Type.INT); // Data diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java index 810096d3..c851b600 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java @@ -18,9 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_14_2to1_14_3; import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; @@ -35,63 +32,50 @@ public class Protocol1_14_2To1_14_3 extends BackwardsProtocol { + wrapper.passthrough(Type.VAR_INT); + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + } + wrapper.passthrough(Type.BOOLEAN); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.FLOAT); } + wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.VAR_INT); + + wrapper.passthrough(Type.BOOLEAN); + wrapper.read(Type.BOOLEAN); }); RecipeRewriter recipeHandler = new RecipeRewriter1_14<>(this); - registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - int size = wrapper.passthrough(Type.VAR_INT); - int deleted = 0; - for (int i = 0; i < size; i++) { - String fullType = wrapper.read(Type.STRING); - String type = fullType.replace("minecraft:", ""); - String id = wrapper.read(Type.STRING); // id + registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, wrapper -> { + int size = wrapper.passthrough(Type.VAR_INT); + int deleted = 0; + for (int i = 0; i < size; i++) { + String fullType = wrapper.read(Type.STRING); + String type = fullType.replace("minecraft:", ""); + String id = wrapper.read(Type.STRING); // id - if (type.equals("crafting_special_repairitem")) { - deleted++; - continue; - } + if (type.equals("crafting_special_repairitem")) { + deleted++; + continue; + } - wrapper.write(Type.STRING, fullType); - wrapper.write(Type.STRING, id); + wrapper.write(Type.STRING, fullType); + wrapper.write(Type.STRING, id); - recipeHandler.handle(wrapper, type); - } - - wrapper.set(Type.VAR_INT, 0, size - deleted); - }); + recipeHandler.handleRecipeType(wrapper, type); } + + wrapper.set(Type.VAR_INT, 0, size - deleted); }); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_3to1_14_4/Protocol1_14_3To1_14_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_3to1_14_4/Protocol1_14_3To1_14_4.java index 38b4da0d..a2b0d3ea 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_3to1_14_4/Protocol1_14_3To1_14_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_3to1_14_4/Protocol1_14_3To1_14_4.java @@ -18,9 +18,7 @@ package com.viaversion.viabackwards.protocol.protocol1_14_3to1_14_4; import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; @@ -34,9 +32,9 @@ public class Protocol1_14_3To1_14_4 extends BackwardsProtocol { @@ -49,30 +47,22 @@ public class Protocol1_14_3To1_14_4 extends BackwardsProtocol { + wrapper.passthrough(Type.VAR_INT); + int size = wrapper.passthrough(Type.UNSIGNED_BYTE); + for (int i = 0; i < size; i++) { + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); + } + wrapper.passthrough(Type.BOOLEAN); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.INT); + wrapper.passthrough(Type.FLOAT); + wrapper.read(Type.INT); // demand value added in pre-5 } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java index 3c5eca32..bc88d454 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java @@ -28,7 +28,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; @@ -72,9 +72,9 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol(protocol).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES); - protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); - } - }); + protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); registerSetCooldown(ClientboundPackets1_15.COOLDOWN); registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); @@ -67,60 +60,52 @@ public class BlockItemPackets1_15 extends com.viaversion.viabackwards.api.rewrit blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE); blockRewriter.registerMultiBlockChange(ClientboundPackets1_15.MULTI_BLOCK_CHANGE); - protocol.registerClientbound(ClientboundPackets1_15.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Chunk chunk = wrapper.read(new Chunk1_15Type()); - wrapper.write(new Chunk1_14Type(), chunk); + protocol.registerClientbound(ClientboundPackets1_15.CHUNK_DATA, wrapper -> { + Chunk chunk = wrapper.read(new Chunk1_15Type()); + wrapper.write(new Chunk1_14Type(), chunk); - if (chunk.isFullChunk()) { - int[] biomeData = chunk.getBiomeData(); - int[] newBiomeData = new int[256]; - for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 4; ++j) { - int x = j << 2; - int z = i << 2; - int newIndex = z << 4 | x; - int oldIndex = i << 2 | j; + if (chunk.isFullChunk()) { + int[] biomeData = chunk.getBiomeData(); + int[] newBiomeData = new int[256]; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + int x = j << 2; + int z = i << 2; + int newIndex = z << 4 | x; + int oldIndex = i << 2 | j; - int biome = biomeData[oldIndex]; - for (int k = 0; k < 4; k++) { - int offX = newIndex + (k << 4); - for (int l = 0; l < 4; l++) { - newBiomeData[offX + l] = biome; - } - } - } - } - - chunk.setBiomeData(newBiomeData); - } - - for (int i = 0; i < chunk.getSections().length; i++) { - ChunkSection section = chunk.getSections()[i]; - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); + int biome = biomeData[oldIndex]; + for (int k = 0; k < 4; k++) { + int offX = newIndex + (k << 4); + for (int l = 0; l < 4; l++) { + newBiomeData[offX + l] = biome; } } } - }); + } + + chunk.setBiomeData(newBiomeData); + } + + for (int i = 0; i < chunk.getSections().length; i++) { + ChunkSection section = chunk.getSections()[i]; + if (section == null) { + continue; + } + + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } } }); blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001); - protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PARTICLE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PARTICLE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // 0 - Particle ID map(Type.BOOLEAN); // 1 - Long Distance map(Type.DOUBLE, Type.FLOAT); // 2 - X @@ -131,22 +116,19 @@ public class BlockItemPackets1_15 extends com.viaversion.viabackwards.api.rewrit map(Type.FLOAT); // 7 - Offset Z map(Type.FLOAT); // 8 - Particle Data map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - if (id == 3 || id == 23) { - int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); - } else if (id == 32) { - Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM)); - wrapper.write(Type.FLAT_VAR_INT_ITEM, item); - } + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + if (id == 3 || id == 23) { + int data = wrapper.passthrough(Type.VAR_INT); + wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); + } else if (id == 32) { + Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM)); + wrapper.write(Type.FLAT_VAR_INT_ITEM, item); + } - int mappedId = protocol.getMappingData().getNewParticleId(id); - if (id != mappedId) { - wrapper.set(Type.INT, 0, mappedId); - } + int mappedId = protocol.getMappingData().getNewParticleId(id); + if (id != mappedId) { + wrapper.set(Type.INT, 0, mappedId); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java index c2256c05..d82249f6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java @@ -25,12 +25,11 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; - import java.util.ArrayList; public class EntityPackets1_15 extends EntityRewriter { @@ -41,25 +40,20 @@ public class EntityPackets1_15 extends EntityRewriter { - float health = wrapper.passthrough(Type.FLOAT); - if (health > 0) return; - if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn()) return; + protocol.registerClientbound(ClientboundPackets1_15.UPDATE_HEALTH, wrapper -> { + float health = wrapper.passthrough(Type.FLOAT); + if (health > 0) return; + if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn()) return; - // Instantly request respawn when 1.15 gamerule is set - PacketWrapper statusPacket = wrapper.create(ServerboundPackets1_14.CLIENT_STATUS); - statusPacket.write(Type.VAR_INT, 0); - statusPacket.sendToServer(Protocol1_14_4To1_15.class); - }); - } + // Instantly request respawn when 1.15 gamerule is set + PacketWrapper statusPacket = wrapper.create(ServerboundPackets1_14.CLIENT_STATUS); + statusPacket.write(Type.VAR_INT, 0); + statusPacket.sendToServer(Protocol1_14_4To1_15.class); }); - protocol.registerClientbound(ClientboundPackets1_15.GAME_EVENT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_15.GAME_EVENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); map(Type.FLOAT); handler(wrapper -> { @@ -72,9 +66,9 @@ public class EntityPackets1_15 extends EntityRewriter { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java index c1019f27..0662afc9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java @@ -22,13 +22,10 @@ import com.viaversion.viabackwards.protocol.protocol1_14to1_14_1.Protocol1_14To1 import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; - import java.util.List; public class EntityPackets1_14_1 extends LegacyEntityRewriter { @@ -46,9 +43,9 @@ public class EntityPackets1_14_1 extends LegacyEntityRewriter { + int entityId = wrapper.get(Type.VAR_INT, 0); + int type = wrapper.get(Type.VAR_INT, 1); - // Register Type ID - tracker(wrapper.user()).addEntity(entityId, Entity1_14Types.getTypeFromId(type)); + // Register Type ID + tracker(wrapper.user()).addEntity(entityId, Entity1_14Types.getTypeFromId(type)); - List metadata = wrapper.get(Types1_14.METADATA_LIST, 0); - handleMetadata(entityId, metadata, wrapper.user()); - } + List metadata = wrapper.get(Types1_14.METADATA_LIST, 0); + handleMetadata(entityId, metadata, wrapper.user()); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java index 5289ba3b..13c1b11f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java @@ -30,7 +30,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types; import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -44,7 +44,6 @@ import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.Client import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.util.GsonUtil; - import java.util.UUID; public class Protocol1_15_2To1_16 extends BackwardsProtocol { @@ -74,33 +73,28 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { - String original = wrapper.passthrough(Type.STRING); - JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class); - JsonElement description = object.get("description"); - if (description == null) return; + registerClientbound(State.STATUS, 0x00, 0x00, wrapper -> { + String original = wrapper.passthrough(Type.STRING); + JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class); + JsonElement description = object.get("description"); + if (description == null) return; - translatableRewriter.processText(description); - wrapper.set(Type.STRING, 0, object.toString()); - }); - } + translatableRewriter.processText(description); + wrapper.set(Type.STRING, 0, object.toString()); }); - registerClientbound(ClientboundPackets1_16.CHAT_MESSAGE, new PacketRemapper() { + registerClientbound(ClientboundPackets1_16.CHAT_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT))); map(Type.BYTE); map(Type.UUID, Type.NOTHING); // Sender } }); - registerClientbound(ClientboundPackets1_16.OPEN_WINDOW, new PacketRemapper() { + registerClientbound(ClientboundPackets1_16.OPEN_WINDOW, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Window Id map(Type.VAR_INT); // Window Type handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT))); @@ -122,71 +116,51 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { - // Transform uuid to plain string - UUID uuid = wrapper.read(Type.UUID); - wrapper.write(Type.STRING, uuid.toString()); - }); - } + registerClientbound(State.LOGIN, 0x02, 0x02, wrapper -> { + // Transform uuid to plain string + UUID uuid = wrapper.read(Type.UUID); + wrapper.write(Type.STRING, uuid.toString()); }); new TagRewriter<>(this).register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY); new StatisticsRewriter<>(this).register(ClientboundPackets1_16.STATISTICS); - registerServerbound(ServerboundPackets1_14.ENTITY_ACTION, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.passthrough(Type.VAR_INT); // player id - int action = wrapper.passthrough(Type.VAR_INT); - if (action == 0) { - wrapper.user().get(PlayerSneakStorage.class).setSneaking(true); - } else if (action == 1) { - wrapper.user().get(PlayerSneakStorage.class).setSneaking(false); - } - }); + registerServerbound(ServerboundPackets1_14.ENTITY_ACTION, wrapper -> { + wrapper.passthrough(Type.VAR_INT); // player id + int action = wrapper.passthrough(Type.VAR_INT); + if (action == 0) { + wrapper.user().get(PlayerSneakStorage.class).setSneaking(true); + } else if (action == 1) { + wrapper.user().get(PlayerSneakStorage.class).setSneaking(false); } }); - registerServerbound(ServerboundPackets1_14.INTERACT_ENTITY, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.passthrough(Type.VAR_INT); // Entity Id - int action = wrapper.passthrough(Type.VAR_INT); - if (action == 0 || action == 2) { - if (action == 2) { - // Location - wrapper.passthrough(Type.FLOAT); - wrapper.passthrough(Type.FLOAT); - wrapper.passthrough(Type.FLOAT); - } + registerServerbound(ServerboundPackets1_14.INTERACT_ENTITY, wrapper -> { + wrapper.passthrough(Type.VAR_INT); // Entity Id + int action = wrapper.passthrough(Type.VAR_INT); + if (action == 0 || action == 2) { + if (action == 2) { + // Location + wrapper.passthrough(Type.FLOAT); + wrapper.passthrough(Type.FLOAT); + wrapper.passthrough(Type.FLOAT); + } - wrapper.passthrough(Type.VAR_INT); // Hand - } - - // New boolean: Whether the client is sneaking - wrapper.write(Type.BOOLEAN, wrapper.user().get(PlayerSneakStorage.class).isSneaking()); - }); + wrapper.passthrough(Type.VAR_INT); // Hand } + + // New boolean: Whether the client is sneaking + wrapper.write(Type.BOOLEAN, wrapper.user().get(PlayerSneakStorage.class).isSneaking()); }); - registerServerbound(ServerboundPackets1_14.PLAYER_ABILITIES, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - byte flags = wrapper.read(Type.BYTE); - flags &= 2; // Only take the isFlying value (everything else has been removed and wasn't used anyways) - wrapper.write(Type.BYTE, flags); + registerServerbound(ServerboundPackets1_14.PLAYER_ABILITIES, wrapper -> { + byte flags = wrapper.read(Type.BYTE); + flags &= 2; // Only take the isFlying value (everything else has been removed and wasn't used anyways) + wrapper.write(Type.BYTE, flags); - wrapper.read(Type.FLOAT); - wrapper.read(Type.FLOAT); - }); - } + wrapper.read(Type.FLOAT); + wrapper.read(Type.FLOAT); }); cancelServerbound(ServerboundPackets1_14.UPDATE_JIGSAW_BLOCK); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java index 4eecb2ad..adcd92a3 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java @@ -25,7 +25,6 @@ import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; - import java.util.Map; import java.util.regex.Pattern; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java index 9226969b..9d71d507 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java @@ -19,7 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; - import java.util.HashMap; import java.util.Map; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index 40637f09..11300167 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -23,14 +23,13 @@ import com.viaversion.viabackwards.api.rewriters.MapColorRewriter; import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewrites; import com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.storage.BiomeStorage; -import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.UUIDIntArrayType; @@ -48,7 +47,6 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.Inventor import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.util.CompactArrayUtil; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -68,33 +66,28 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit RecipeRewriter1_14 recipeRewriter = new RecipeRewriter1_14<>(protocol); // Remove new smithing type, only in this handler - protocol.registerClientbound(ClientboundPackets1_16.DECLARE_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - int size = wrapper.passthrough(Type.VAR_INT); - int newSize = size; - for (int i = 0; i < size; i++) { - String originalType = wrapper.read(Type.STRING); - String type = originalType.replace("minecraft:", ""); - if (type.equals("smithing")) { - newSize--; + protocol.registerClientbound(ClientboundPackets1_16.DECLARE_RECIPES, wrapper -> { + int size = wrapper.passthrough(Type.VAR_INT); + int newSize = size; + for (int i = 0; i < size; i++) { + String originalType = wrapper.read(Type.STRING); + String type = originalType.replace("minecraft:", ""); + if (type.equals("smithing")) { + newSize--; - wrapper.read(Type.STRING); - wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); - wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); - wrapper.read(Type.FLAT_VAR_INT_ITEM); - continue; - } + wrapper.read(Type.STRING); + wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); + wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); + wrapper.read(Type.FLAT_VAR_INT_ITEM); + continue; + } - wrapper.write(Type.STRING, originalType); - wrapper.passthrough(Type.STRING); // Recipe Identifier - recipeRewriter.handle(wrapper, type); - } - - wrapper.set(Type.VAR_INT, 0, newSize); - }); + wrapper.write(Type.STRING, originalType); + wrapper.passthrough(Type.STRING); // Recipe Identifier + recipeRewriter.handleRecipeType(wrapper, type); } + + wrapper.set(Type.VAR_INT, 0, newSize); }); registerSetCooldown(ClientboundPackets1_16.COOLDOWN); @@ -108,108 +101,98 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE); blockRewriter.registerMultiBlockChange(ClientboundPackets1_16.MULTI_BLOCK_CHANGE); - protocol.registerClientbound(ClientboundPackets1_16.ENTITY_EQUIPMENT, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - int entityId = wrapper.passthrough(Type.VAR_INT); + protocol.registerClientbound(ClientboundPackets1_16.ENTITY_EQUIPMENT, wrapper -> { + int entityId = wrapper.passthrough(Type.VAR_INT); - List equipmentData = new ArrayList<>(); - byte slot; - do { - slot = wrapper.read(Type.BYTE); - Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM)); - int rawSlot = slot & 0x7F; - equipmentData.add(new EquipmentData(rawSlot, item)); - } while ((slot & 0xFFFFFF80) != 0); + List equipmentData = new ArrayList<>(); + byte slot; + do { + slot = wrapper.read(Type.BYTE); + Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM)); + int rawSlot = slot & 0x7F; + equipmentData.add(new EquipmentData(rawSlot, item)); + } while ((slot & 0xFFFFFF80) != 0); - // Send first data in the current packet - EquipmentData firstData = equipmentData.get(0); - wrapper.write(Type.VAR_INT, firstData.slot); - wrapper.write(Type.FLAT_VAR_INT_ITEM, firstData.item); + // Send first data in the current packet + EquipmentData firstData = equipmentData.get(0); + wrapper.write(Type.VAR_INT, firstData.slot); + wrapper.write(Type.FLAT_VAR_INT_ITEM, firstData.item); - // If there are more items, send new packets for them - for (int i = 1; i < equipmentData.size(); i++) { - PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_15.ENTITY_EQUIPMENT); - EquipmentData data = equipmentData.get(i); - equipmentPacket.write(Type.VAR_INT, entityId); - equipmentPacket.write(Type.VAR_INT, data.slot); - equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, data.item); - equipmentPacket.send(Protocol1_15_2To1_16.class); - } - }); + // If there are more items, send new packets for them + for (int i = 1; i < equipmentData.size(); i++) { + PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_15.ENTITY_EQUIPMENT); + EquipmentData data = equipmentData.get(i); + equipmentPacket.write(Type.VAR_INT, entityId); + equipmentPacket.write(Type.VAR_INT, data.slot); + equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, data.item); + equipmentPacket.send(Protocol1_15_2To1_16.class); } }); - protocol.registerClientbound(ClientboundPackets1_16.UPDATE_LIGHT, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_16.UPDATE_LIGHT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // x map(Type.VAR_INT); // y map(Type.BOOLEAN, Type.NOTHING); } }); - protocol.registerClientbound(ClientboundPackets1_16.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - Chunk chunk = wrapper.read(new Chunk1_16Type()); - wrapper.write(new Chunk1_15Type(), chunk); + protocol.registerClientbound(ClientboundPackets1_16.CHUNK_DATA, wrapper -> { + Chunk chunk = wrapper.read(new Chunk1_16Type()); + wrapper.write(new Chunk1_15Type(), chunk); - for (int i = 0; i < chunk.getSections().length; i++) { - ChunkSection section = chunk.getSections()[i]; - if (section == null) { - continue; + for (int i = 0; i < chunk.getSections().length; i++) { + ChunkSection section = chunk.getSections()[i]; + if (section == null) { + continue; + } + + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } + } + + CompoundTag heightMaps = chunk.getHeightMap(); + for (Tag heightMapTag : heightMaps.values()) { + LongArrayTag heightMap = (LongArrayTag) heightMapTag; + int[] heightMapData = new int[256]; + CompactArrayUtil.iterateCompactArrayWithPadding(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v); + heightMap.setValue(CompactArrayUtil.createCompactArray(9, heightMapData.length, i -> heightMapData[i])); + } + + if (chunk.isBiomeData()) { + if (wrapper.user().getProtocolInfo().getServerProtocolVersion() >= ProtocolVersion.v1_16_2.getVersion()) { + BiomeStorage biomeStorage = wrapper.user().get(BiomeStorage.class); + for (int i = 0; i < 1024; i++) { + int biome = chunk.getBiomeData()[i]; + int legacyBiome = biomeStorage.legacyBiome(biome); + if (legacyBiome == -1) { + ViaBackwards.getPlatform().getLogger().warning("Biome sent that does not exist in the biome registry: " + biome); + legacyBiome = 1; } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); + chunk.getBiomeData()[i] = legacyBiome; + } + } else { + for (int i = 0; i < 1024; i++) { + int biome = chunk.getBiomeData()[i]; + switch (biome) { + case 170: // new nether biomes + case 171: + case 172: + case 173: + chunk.getBiomeData()[i] = 8; + break; } } + } + } - CompoundTag heightMaps = chunk.getHeightMap(); - for (Tag heightMapTag : heightMaps.values()) { - LongArrayTag heightMap = (LongArrayTag) heightMapTag; - int[] heightMapData = new int[256]; - CompactArrayUtil.iterateCompactArrayWithPadding(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v); - heightMap.setValue(CompactArrayUtil.createCompactArray(9, heightMapData.length, i -> heightMapData[i])); - } - - if (chunk.isBiomeData()) { - if (wrapper.user().getProtocolInfo().getServerProtocolVersion() >= ProtocolVersion.v1_16_2.getVersion()) { - BiomeStorage biomeStorage = wrapper.user().get(BiomeStorage.class); - for (int i = 0; i < 1024; i++) { - int biome = chunk.getBiomeData()[i]; - int legacyBiome = biomeStorage.legacyBiome(biome); - if (legacyBiome == -1) { - ViaBackwards.getPlatform().getLogger().warning("Biome sent that does not exist in the biome registry: " + biome); - legacyBiome = 1; - } - chunk.getBiomeData()[i] = legacyBiome; - } - } else { - for (int i = 0; i < 1024; i++) { - int biome = chunk.getBiomeData()[i]; - switch (biome) { - case 170: // new nether biomes - case 171: - case 172: - case 173: - chunk.getBiomeData()[i] = 8; - break; - } - } - } - } - - if (chunk.getBlockEntities() == null) return; - for (CompoundTag blockEntity : chunk.getBlockEntities()) { - handleBlockEntity(blockEntity); - } - }); + if (chunk.getBlockEntities() == null) return; + for (CompoundTag blockEntity : chunk.getBlockEntities()) { + handleBlockEntity(blockEntity); } }); @@ -217,9 +200,9 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); - protocol.registerClientbound(ClientboundPackets1_16.WINDOW_PROPERTY, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_16.WINDOW_PROPERTY, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.UNSIGNED_BYTE); // Window id map(Type.SHORT); // Property map(Type.SHORT); // Value @@ -237,9 +220,9 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_16.MAP_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_16.MAP_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Map ID map(Type.BYTE); // Scale map(Type.BOOLEAN); // Tracking Position @@ -248,27 +231,17 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit } }); - protocol.registerClientbound(ClientboundPackets1_16.BLOCK_ENTITY_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - Position position = wrapper.passthrough(Type.POSITION1_14); - short action = wrapper.passthrough(Type.UNSIGNED_BYTE); - CompoundTag tag = wrapper.passthrough(Type.NBT); - handleBlockEntity(tag); - }); - } + protocol.registerClientbound(ClientboundPackets1_16.BLOCK_ENTITY_DATA, wrapper -> { + wrapper.passthrough(Type.POSITION1_14); // Position + wrapper.passthrough(Type.UNSIGNED_BYTE); // Action + CompoundTag tag = wrapper.passthrough(Type.NBT); + handleBlockEntity(tag); }); registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); - protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); - } - }); + protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); } private void handleBlockEntity(CompoundTag tag) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index 40888ed4..e3eb81e4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -30,7 +30,7 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; @@ -65,9 +65,9 @@ public class EntityPackets1_16 extends EntityRewriter { // Grab the tracker for world names @@ -143,9 +143,9 @@ public class EntityPackets1_16 extends EntityRewriter { - wrapper.passthrough(Type.VAR_INT); - int size = wrapper.passthrough(Type.INT); - for (int i = 0; i < size; i++) { - String attributeIdentifier = wrapper.read(Type.STRING); - String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier); - wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", "")); + protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, wrapper -> { + wrapper.passthrough(Type.VAR_INT); + int size = wrapper.passthrough(Type.INT); + for (int i = 0; i < size; i++) { + String attributeIdentifier = wrapper.read(Type.STRING); + String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier); + wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", "")); - wrapper.passthrough(Type.DOUBLE); - int modifierSize = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < modifierSize; j++) { - wrapper.passthrough(Type.UUID); - wrapper.passthrough(Type.DOUBLE); - wrapper.passthrough(Type.BYTE); - } - } - }); + wrapper.passthrough(Type.DOUBLE); + int modifierSize = wrapper.passthrough(Type.VAR_INT); + for (int j = 0; j < modifierSize; j++) { + wrapper.passthrough(Type.UUID); + wrapper.passthrough(Type.DOUBLE); + wrapper.passthrough(Type.BYTE); + } } }); - protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketHandlers() { @Override - public void registerMap() { + public void register() { handler(packetWrapper -> { int action = packetWrapper.passthrough(Type.VAR_INT); int playerCount = packetWrapper.passthrough(Type.VAR_INT); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java index 2f12b0b0..eb9be8e7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java @@ -29,8 +29,6 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -76,56 +74,32 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol { - JsonElement message = wrapper.passthrough(Type.COMPONENT); - translatableRewriter.processText(message); - byte position = wrapper.passthrough(Type.BYTE); - if (position == 2) { // https://bugs.mojang.com/browse/MC-119145 - wrapper.clearPacket(); - wrapper.setPacketType(ClientboundPackets1_16.TITLE); - wrapper.write(Type.VAR_INT, 2); - wrapper.write(Type.COMPONENT, message); - } - }); + registerClientbound(ClientboundPackets1_16_2.CHAT_MESSAGE, wrapper -> { + JsonElement message = wrapper.passthrough(Type.COMPONENT); + translatableRewriter.processText(message); + byte position = wrapper.passthrough(Type.BYTE); + if (position == 2) { // https://bugs.mojang.com/browse/MC-119145 + wrapper.clearPacket(); + wrapper.setPacketType(ClientboundPackets1_16.TITLE); + wrapper.write(Type.VAR_INT, 2); + wrapper.write(Type.COMPONENT, message); } }); // Recipe book data has been split into 2 separate packets - registerServerbound(ServerboundPackets1_16.RECIPE_BOOK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int type = wrapper.read(Type.VAR_INT); - if (type == 0) { - // Shown, change to its own packet - wrapper.passthrough(Type.STRING); // Recipe - wrapper.setPacketType(ServerboundPackets1_16_2.SEEN_RECIPE); - } else { - wrapper.cancel(); + registerServerbound(ServerboundPackets1_16.RECIPE_BOOK_DATA, wrapper -> { + int type = wrapper.read(Type.VAR_INT); + if (type == 0) { + // Shown, change to its own packet + wrapper.passthrough(Type.STRING); // Recipe + wrapper.setPacketType(ServerboundPackets1_16_2.SEEN_RECIPE); + } else { + wrapper.cancel(); - // Settings - for (int i = 0; i < 3; i++) { - sendSeenRecipePacket(i, wrapper); - } - } - } - - private void sendSeenRecipePacket(int recipeType, PacketWrapper wrapper) throws Exception { - boolean open = wrapper.read(Type.BOOLEAN); - boolean filter = wrapper.read(Type.BOOLEAN); - - PacketWrapper newPacket = wrapper.create(ServerboundPackets1_16_2.RECIPE_BOOK_DATA); - newPacket.write(Type.VAR_INT, recipeType); - newPacket.write(Type.BOOLEAN, open); - newPacket.write(Type.BOOLEAN, filter); - newPacket.sendToServer(Protocol1_16_1To1_16_2.class); - } - }); + // Settings + for (int i = 0; i < 3; i++) { + sendSeenRecipePacket(i, wrapper); + } } }); @@ -134,6 +108,17 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol(this).register(ClientboundPackets1_16_2.STATISTICS); } + private static void sendSeenRecipePacket(int recipeType, PacketWrapper wrapper) throws Exception { + boolean open = wrapper.read(Type.BOOLEAN); + boolean filter = wrapper.read(Type.BOOLEAN); + + PacketWrapper newPacket = wrapper.create(ServerboundPackets1_16_2.RECIPE_BOOK_DATA); + newPacket.write(Type.VAR_INT, recipeType); + newPacket.write(Type.BOOLEAN, open); + newPacket.write(Type.BOOLEAN, filter); + newPacket.sendToServer(Protocol1_16_1To1_16_2.class); + } + @Override public void init(UserConnection user) { user.put(new BiomeStorage()); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java index 37cc9023..f822bc68 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java @@ -24,7 +24,6 @@ import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap; import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; - import java.util.Map; public final class BiomeMappings { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java index ba183292..62417f4e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/BlockItemPackets1_16_2.java @@ -24,7 +24,7 @@ import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag; @@ -57,61 +57,51 @@ public class BlockItemPackets1_16_2 extends com.viaversion.viabackwards.api.rewr registerTradeList(ClientboundPackets1_16_2.TRADE_LIST); registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); - protocol.registerClientbound(ClientboundPackets1_16_2.UNLOCK_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.BOOLEAN); // Open - wrapper.passthrough(Type.BOOLEAN); // Filter - wrapper.passthrough(Type.BOOLEAN); // Furnace Open - wrapper.passthrough(Type.BOOLEAN); // Filter furnace - // Blast furnace / smoker - wrapper.read(Type.BOOLEAN); - wrapper.read(Type.BOOLEAN); - wrapper.read(Type.BOOLEAN); - wrapper.read(Type.BOOLEAN); - }); - } + protocol.registerClientbound(ClientboundPackets1_16_2.UNLOCK_RECIPES, wrapper -> { + wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.BOOLEAN); // Open + wrapper.passthrough(Type.BOOLEAN); // Filter + wrapper.passthrough(Type.BOOLEAN); // Furnace Open + wrapper.passthrough(Type.BOOLEAN); // Filter furnace + // Blast furnace / smoker + wrapper.read(Type.BOOLEAN); + wrapper.read(Type.BOOLEAN); + wrapper.read(Type.BOOLEAN); + wrapper.read(Type.BOOLEAN); }); blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING); blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION); blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE); - protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - Chunk chunk = wrapper.read(new Chunk1_16_2Type()); - wrapper.write(new Chunk1_16Type(), chunk); + protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, wrapper -> { + Chunk chunk = wrapper.read(new Chunk1_16_2Type()); + wrapper.write(new Chunk1_16Type(), chunk); - chunk.setIgnoreOldLightData(true); - for (int i = 0; i < chunk.getSections().length; i++) { - ChunkSection section = chunk.getSections()[i]; - if (section == null) { - continue; - } + chunk.setIgnoreOldLightData(true); + for (int i = 0; i < chunk.getSections().length; i++) { + ChunkSection section = chunk.getSections()[i]; + if (section == null) { + continue; + } - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); - } - } + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } + } - for (CompoundTag blockEntity : chunk.getBlockEntities()) { - if (blockEntity != null) { - handleBlockEntity(blockEntity); - } - } - }); + for (CompoundTag blockEntity : chunk.getBlockEntities()) { + if (blockEntity != null) { + handleBlockEntity(blockEntity); + } } }); - protocol.registerClientbound(ClientboundPackets1_16_2.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_16_2.BLOCK_ENTITY_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.POSITION1_14); map(Type.UNSIGNED_BYTE); handler(wrapper -> { @@ -120,28 +110,23 @@ public class BlockItemPackets1_16_2 extends com.viaversion.viabackwards.api.rewr } }); - protocol.registerClientbound(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - long chunkPosition = wrapper.read(Type.LONG); - wrapper.read(Type.BOOLEAN); // Ignore old light data + protocol.registerClientbound(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE, wrapper -> { + long chunkPosition = wrapper.read(Type.LONG); + wrapper.read(Type.BOOLEAN); // Ignore old light data - int chunkX = (int) (chunkPosition >> 42); - int chunkY = (int) (chunkPosition << 44 >> 44); - int chunkZ = (int) (chunkPosition << 22 >> 42); - wrapper.write(Type.INT, chunkX); - wrapper.write(Type.INT, chunkZ); + int chunkX = (int) (chunkPosition >> 42); + int chunkY = (int) (chunkPosition << 44 >> 44); + int chunkZ = (int) (chunkPosition << 22 >> 42); + wrapper.write(Type.INT, chunkX); + wrapper.write(Type.INT, chunkZ); - BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY); - wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecord); - for (int i = 0; i < blockChangeRecord.length; i++) { - BlockChangeRecord record = blockChangeRecord[i]; - int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId()); - // Relative y -> absolute y - blockChangeRecord[i] = new BlockChangeRecord1_8(record.getSectionX(), record.getY(chunkY), record.getSectionZ(), blockId); - } - }); + BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY); + wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecord); + for (int i = 0; i < blockChangeRecord.length; i++) { + BlockChangeRecord record = blockChangeRecord[i]; + int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId()); + // Relative y -> absolute y + blockChangeRecord[i] = new BlockChangeRecord1_8(record.getSectionX(), record.getY(chunkY), record.getSectionZ(), blockId); } }); @@ -151,12 +136,7 @@ public class BlockItemPackets1_16_2 extends com.viaversion.viabackwards.api.rewr registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); - protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); - } - }); + protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); } private void handleBlockEntity(CompoundTag tag) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java index a71b163f..36c3152f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/packets/EntityPackets1_16_2.java @@ -25,7 +25,7 @@ import com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.storage.Biome import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_16; @@ -36,7 +36,6 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets; - import java.util.Set; public class EntityPackets1_16_2 extends EntityRewriter { @@ -58,9 +57,9 @@ public class EntityPackets1_16_2 extends EntityRewriter { boolean hardcore = wrapper.read(Type.BOOLEAN); @@ -108,14 +107,9 @@ public class EntityPackets1_16_2 extends EntityRewriter { - CompoundTag dimensionData = wrapper.read(Type.NBT); - wrapper.write(Type.STRING, getDimensionFromData(dimensionData)); - }); - } + protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, wrapper -> { + CompoundTag dimensionData = wrapper.read(Type.NBT); + wrapper.write(Type.STRING, getDimensionFromData(dimensionData)); }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_3to1_16_4/Protocol1_16_3To1_16_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_3to1_16_4/Protocol1_16_3To1_16_4.java index 2e34ad1c..bc99a603 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_3to1_16_4/Protocol1_16_3To1_16_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_3to1_16_4/Protocol1_16_3To1_16_4.java @@ -20,7 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_16_3to1_16_4; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.protocol.protocol1_16_3to1_16_4.storage.PlayerHandStorage; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; @@ -33,9 +33,9 @@ public class Protocol1_16_3To1_16_4 extends BackwardsProtocol { @@ -49,14 +49,9 @@ public class Protocol1_16_3To1_16_4 extends BackwardsProtocol { - short slot = wrapper.passthrough(Type.SHORT); - wrapper.user().get(PlayerHandStorage.class).setCurrentHand(slot); - }); - } + registerServerbound(ServerboundPackets1_16_2.HELD_ITEM_CHANGE, wrapper -> { + short slot = wrapper.passthrough(Type.SHORT); + wrapper.user().get(PlayerHandStorage.class).setCurrentHand(slot); }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java index a5832b73..f5729f0d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java @@ -31,7 +31,7 @@ import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.TagData; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.fastutil.ints.IntArrayList; @@ -84,80 +84,70 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol tagRewriter = new TagRewriter<>(this); - registerClientbound(ClientboundPackets1_17.TAGS, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - Map> tags = new HashMap<>(); + registerClientbound(ClientboundPackets1_17.TAGS, wrapper -> { + Map> tags = new HashMap<>(); - int length = wrapper.read(Type.VAR_INT); - for (int i = 0; i < length; i++) { - String resourceKey = wrapper.read(Type.STRING); - if (resourceKey.startsWith("minecraft:")) { - resourceKey = resourceKey.substring(10); - } + int length = wrapper.read(Type.VAR_INT); + for (int i = 0; i < length; i++) { + String resourceKey = wrapper.read(Type.STRING); + if (resourceKey.startsWith("minecraft:")) { + resourceKey = resourceKey.substring(10); + } - List tagList = new ArrayList<>(); - tags.put(resourceKey, tagList); + List tagList = new ArrayList<>(); + tags.put(resourceKey, tagList); - int tagLength = wrapper.read(Type.VAR_INT); - for (int j = 0; j < tagLength; j++) { - String identifier = wrapper.read(Type.STRING); - int[] entries = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); - tagList.add(new TagData(identifier, entries)); - } - } + int tagLength = wrapper.read(Type.VAR_INT); + for (int j = 0; j < tagLength; j++) { + String identifier = wrapper.read(Type.STRING); + int[] entries = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); + tagList.add(new TagData(identifier, entries)); + } + } - // Put them into the hardcoded order of Vanilla tags (and only those), rewrite ids - for (RegistryType type : RegistryType.getValues()) { - List tagList = tags.get(type.resourceLocation()); - IdRewriteFunction rewriter = tagRewriter.getRewriter(type); + // Put them into the hardcoded order of Vanilla tags (and only those), rewrite ids + for (RegistryType type : RegistryType.getValues()) { + List tagList = tags.get(type.resourceLocation()); + IdRewriteFunction rewriter = tagRewriter.getRewriter(type); - wrapper.write(Type.VAR_INT, tagList.size()); - for (TagData tagData : tagList) { - int[] entries = tagData.entries(); - if (rewriter != null) { - // Handle id rewriting now - IntList idList = new IntArrayList(entries.length); - for (int id : entries) { - int mappedId = rewriter.rewrite(id); - if (mappedId != -1) { - idList.add(mappedId); - } - } - entries = idList.toArray(EMPTY_ARRAY); + wrapper.write(Type.VAR_INT, tagList.size()); + for (TagData tagData : tagList) { + int[] entries = tagData.entries(); + if (rewriter != null) { + // Handle id rewriting now + IntList idList = new IntArrayList(entries.length); + for (int id : entries) { + int mappedId = rewriter.rewrite(id); + if (mappedId != -1) { + idList.add(mappedId); } - - wrapper.write(Type.STRING, tagData.identifier()); - wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, entries); - } - - // Stop after the entity types - if (type == RegistryType.ENTITY) { - break; } + entries = idList.toArray(EMPTY_ARRAY); } - }); + + wrapper.write(Type.STRING, tagData.identifier()); + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, entries); + } + + // Stop after the entity types + if (type == RegistryType.ENTITY) { + break; + } } }); new StatisticsRewriter<>(this).register(ClientboundPackets1_17.STATISTICS); - registerClientbound(ClientboundPackets1_17.RESOURCE_PACK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - wrapper.passthrough(Type.STRING); - wrapper.passthrough(Type.STRING); - wrapper.read(Type.BOOLEAN); // Required - wrapper.read(Type.OPTIONAL_COMPONENT); // Prompt message - }); - } + registerClientbound(ClientboundPackets1_17.RESOURCE_PACK, wrapper -> { + wrapper.passthrough(Type.STRING); + wrapper.passthrough(Type.STRING); + wrapper.read(Type.BOOLEAN); // Required + wrapper.read(Type.OPTIONAL_COMPONENT); // Prompt message }); - registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketRemapper() { + registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.FLOAT); // X map(Type.FLOAT); // Y map(Type.FLOAT); // Z @@ -168,9 +158,9 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { // Angle (which Mojang just forgot to write to the buffer, lol) @@ -179,37 +169,32 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { - wrapper.cancel(); + registerClientbound(ClientboundPackets1_17.PING, null, wrapper -> { + wrapper.cancel(); - int id = wrapper.read(Type.INT); - short shortId = (short) id; - if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { - wrapper.user().get(PingRequests.class).addId(shortId); + int id = wrapper.read(Type.INT); + short shortId = (short) id; + if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { + wrapper.user().get(PingRequests.class).addId(shortId); - // Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range - PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION); - acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0); // Inventory id - acknowledgementPacket.write(Type.SHORT, shortId); // Confirmation id - acknowledgementPacket.write(Type.BOOLEAN, false); // Accepted - acknowledgementPacket.send(Protocol1_16_4To1_17.class); - return; - } - - // Plugins expecting a real response will have to handle this accordingly themselves - PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); - pongPacket.write(Type.INT, id); - pongPacket.sendToServer(Protocol1_16_4To1_17.class); - }); + // Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range + PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION); + acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0); // Inventory id + acknowledgementPacket.write(Type.SHORT, shortId); // Confirmation id + acknowledgementPacket.write(Type.BOOLEAN, false); // Accepted + acknowledgementPacket.send(Protocol1_16_4To1_17.class); + return; } + + // Plugins expecting a real response will have to handle this accordingly themselves + PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); + pongPacket.write(Type.INT, id); + pongPacket.sendToServer(Protocol1_16_4To1_17.class); }); - registerServerbound(ServerboundPackets1_16_2.CLIENT_SETTINGS, new PacketRemapper() { + registerServerbound(ServerboundPackets1_16_2.CLIENT_SETTINGS, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // Locale map(Type.BYTE); // View distance map(Type.VAR_INT); // Chat mode @@ -227,16 +212,11 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { - if (wrapper.read(Type.BOOLEAN)) { - wrapper.write(Type.VAR_INT, 5); // Reset times - } else { - wrapper.write(Type.VAR_INT, 4); // Simple clear - } - }); + registerClientbound(ClientboundPackets1_17.CLEAR_TITLES, ClientboundPackets1_16_2.TITLE, wrapper -> { + if (wrapper.read(Type.BOOLEAN)) { + wrapper.write(Type.VAR_INT, 5); // Reset times + } else { + wrapper.write(Type.VAR_INT, 4); // Simple clear } }); @@ -262,14 +242,7 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { - wrapper.write(Type.VAR_INT, type); - }); - } - }); + registerClientbound(newPacketType, oldPacketType, wrapper -> wrapper.write(Type.VAR_INT, type)); } @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java index 49c2af66..c9fcdc5e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java @@ -32,7 +32,7 @@ import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag; @@ -48,7 +48,6 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17T import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.util.CompactArrayUtil; import com.viaversion.viaversion.util.MathUtil; - import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; @@ -78,12 +77,7 @@ public final class BlockItemPackets1_17 extends ItemRewriter handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); - } - }); + protocol.registerServerbound(ServerboundPackets1_16_2.EDIT_BOOK, wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); // TODO Since the carried and modified items are typically set incorrectly, the server sends unnecessary // set slot packets after practically every window click, since it thinks the client and server @@ -92,9 +86,9 @@ public final class BlockItemPackets1_17 extends ItemRewriter { short slot = wrapper.passthrough(Type.SHORT); // Slot @@ -142,55 +136,45 @@ public final class BlockItemPackets1_17 extends ItemRewriter { - short windowId = wrapper.passthrough(Type.UNSIGNED_BYTE); - short slot = wrapper.passthrough(Type.SHORT); + protocol.registerClientbound(ClientboundPackets1_17.SET_SLOT, wrapper -> { + short windowId = wrapper.passthrough(Type.UNSIGNED_BYTE); + short slot = wrapper.passthrough(Type.SHORT); - Item carried = wrapper.read(Type.FLAT_VAR_INT_ITEM); - if (carried != null && windowId == -1 && slot == -1) { - // This is related to the hack to fix click and drag ghost items above. - // After a completed drag, we have no idea how many items remain on the cursor, - // and vanilla logic replication would be required to calculate the value. - // When the click drag complete packet is sent, we will send an incorrect - // carried item, and the server will helpfully send this packet allowing us - // to update the internal state. This is necessary for fixing multiple sequential - // click drag actions without intermittent pickup actions. - wrapper.user().get(PlayerLastCursorItem.class).setLastCursorItem(carried); - } + Item carried = wrapper.read(Type.FLAT_VAR_INT_ITEM); + if (carried != null && windowId == -1 && slot == -1) { + // This is related to the hack to fix click and drag ghost items above. + // After a completed drag, we have no idea how many items remain on the cursor, + // and vanilla logic replication would be required to calculate the value. + // When the click drag complete packet is sent, we will send an incorrect + // carried item, and the server will helpfully send this packet allowing us + // to update the internal state. This is necessary for fixing multiple sequential + // click drag actions without intermittent pickup actions. + wrapper.user().get(PlayerLastCursorItem.class).setLastCursorItem(carried); + } - wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(carried)); - }); + wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(carried)); + }); + + protocol.registerServerbound(ServerboundPackets1_16_2.WINDOW_CONFIRMATION, null, wrapper -> { + wrapper.cancel(); + if (!ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { + return; + } + + // Handle ping packet replacement + short inventoryId = wrapper.read(Type.UNSIGNED_BYTE); + short confirmationId = wrapper.read(Type.SHORT); + boolean accepted = wrapper.read(Type.BOOLEAN); + if (inventoryId == 0 && accepted && wrapper.user().get(PingRequests.class).removeId(confirmationId)) { + PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); + pongPacket.write(Type.INT, (int) confirmationId); + pongPacket.sendToServer(Protocol1_16_4To1_17.class); } }); - protocol.registerServerbound(ServerboundPackets1_16_2.WINDOW_CONFIRMATION, null, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_17.SPAWN_PARTICLE, new PacketHandlers() { @Override - public void registerMap() { - handler(wrapper -> { - wrapper.cancel(); - if (!ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { - return; - } - - // Handle ping packet replacement - short inventoryId = wrapper.read(Type.UNSIGNED_BYTE); - short confirmationId = wrapper.read(Type.SHORT); - boolean accepted = wrapper.read(Type.BOOLEAN); - if (inventoryId == 0 && accepted && wrapper.user().get(PingRequests.class).removeId(confirmationId)) { - PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); - pongPacket.write(Type.INT, (int) confirmationId); - pongPacket.sendToServer(Protocol1_16_4To1_17.class); - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_17.SPAWN_PARTICLE, new PacketRemapper() { - @Override - public void registerMap() { + public void register() { map(Type.INT); // Particle id map(Type.BOOLEAN); // Long distance map(Type.DOUBLE); // X @@ -232,9 +216,9 @@ public final class BlockItemPackets1_17 extends ItemRewriter { @@ -318,8 +303,9 @@ public final class BlockItemPackets1_17 extends ItemRewriter { @@ -334,72 +320,62 @@ public final class BlockItemPackets1_17 extends ItemRewriter { - EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class); - int currentWorldSectionHeight = tracker.currentWorldSectionHeight(); + protocol.registerClientbound(ClientboundPackets1_17.CHUNK_DATA, wrapper -> { + EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class); + int currentWorldSectionHeight = tracker.currentWorldSectionHeight(); - Chunk chunk = wrapper.read(new Chunk1_17Type(currentWorldSectionHeight)); - wrapper.write(new Chunk1_16_2Type(), chunk); + Chunk chunk = wrapper.read(new Chunk1_17Type(currentWorldSectionHeight)); + wrapper.write(new Chunk1_16_2Type(), chunk); - // Cut sections - int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4)); - chunk.setBiomeData(Arrays.copyOfRange(chunk.getBiomeData(), startFromSection * 64, (startFromSection * 64) + 1024)); + // Cut sections + int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4)); + chunk.setBiomeData(Arrays.copyOfRange(chunk.getBiomeData(), startFromSection * 64, (startFromSection * 64) + 1024)); - chunk.setBitmask(cutMask(chunk.getChunkMask(), startFromSection, false)); - chunk.setChunkMask(null); + chunk.setBitmask(cutMask(chunk.getChunkMask(), startFromSection, false)); + chunk.setChunkMask(null); - ChunkSection[] sections = Arrays.copyOfRange(chunk.getSections(), startFromSection, startFromSection + 16); - chunk.setSections(sections); + ChunkSection[] sections = Arrays.copyOfRange(chunk.getSections(), startFromSection, startFromSection + 16); + chunk.setSections(sections); - CompoundTag heightMaps = chunk.getHeightMap(); - for (Tag heightMapTag : heightMaps.values()) { - LongArrayTag heightMap = (LongArrayTag) heightMapTag; - int[] heightMapData = new int[256]; - int bitsPerEntry = MathUtil.ceilLog2((currentWorldSectionHeight << 4) + 1); - // Shift back to 0 based and clamp to normal height with 9 bits - CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerEntry, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = MathUtil.clamp(v + tracker.currentMinY(), 0, 255)); - heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i])); - } + CompoundTag heightMaps = chunk.getHeightMap(); + for (Tag heightMapTag : heightMaps.values()) { + LongArrayTag heightMap = (LongArrayTag) heightMapTag; + int[] heightMapData = new int[256]; + int bitsPerEntry = MathUtil.ceilLog2((currentWorldSectionHeight << 4) + 1); + // Shift back to 0 based and clamp to normal height with 9 bits + CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerEntry, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = MathUtil.clamp(v + tracker.currentMinY(), 0, 255)); + heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i])); + } - for (int i = 0; i < 16; i++) { - ChunkSection section = sections[i]; - if (section == null) { - continue; - } + for (int i = 0; i < 16; i++) { + ChunkSection section = sections[i]; + if (section == null) { + continue; + } - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int j = 0; j < palette.size(); j++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); - palette.setIdByIndex(j, mappedBlockStateId); - } - } + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int j = 0; j < palette.size(); j++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(j)); + palette.setIdByIndex(j, mappedBlockStateId); + } + } - chunk.getBlockEntities().removeIf(compound -> { - NumberTag tag = compound.get("y"); - return tag != null && (tag.asInt() < 0 || tag.asInt() > 255); - }); - }); + chunk.getBlockEntities().removeIf(compound -> { + NumberTag tag = compound.get("y"); + return tag != null && (tag.asInt() < 0 || tag.asInt() > 255); + }); + }); + + protocol.registerClientbound(ClientboundPackets1_17.BLOCK_ENTITY_DATA, wrapper -> { + int y = wrapper.passthrough(Type.POSITION1_14).y(); + if (y < 0 || y > 255) { + wrapper.cancel(); } }); - protocol.registerClientbound(ClientboundPackets1_17.BLOCK_ENTITY_DATA, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_17.BLOCK_BREAK_ANIMATION, new PacketHandlers() { @Override - public void registerMap() { - handler(wrapper -> { - int y = wrapper.passthrough(Type.POSITION1_14).y(); - if (y < 0 || y > 255) { - wrapper.cancel(); - } - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_17.BLOCK_BREAK_ANIMATION, new PacketRemapper() { - @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); handler(wrapper -> { int y = wrapper.passthrough(Type.POSITION1_14).y(); @@ -410,9 +386,9 @@ public final class BlockItemPackets1_17 extends ItemRewriter wrapper.write(Type.BOOLEAN, true)); // Tracking position diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java index 4d556edb..39eed683 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java @@ -24,7 +24,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_16; @@ -54,23 +54,18 @@ public final class EntityPackets1_17 extends EntityRewriter { - int entityId = wrapper.read(Type.VAR_INT); - tracker(wrapper.user()).removeEntity(entityId); + protocol.registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_16_2.DESTROY_ENTITIES, wrapper -> { + int entityId = wrapper.read(Type.VAR_INT); + tracker(wrapper.user()).removeEntity(entityId); - // Write into single value array - int[] array = {entityId}; - wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, array); - }); - } + // Write into single value array + int[] array = {entityId}; + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, array); }); - protocol.registerClientbound(ClientboundPackets1_17.JOIN_GAME, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_17.JOIN_GAME, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.INT); // Entity ID map(Type.BOOLEAN); // Hardcore map(Type.UNSIGNED_BYTE); // Gamemode @@ -111,9 +106,9 @@ public final class EntityPackets1_17 extends EntityRewriter { wrapper.write(Type.INT, wrapper.read(Type.VAR_INT)); // Collection length diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/Protocol1_17_1To1_18.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/Protocol1_17_1To1_18.java index 1bdcaf47..e64a12cb 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/Protocol1_17_1To1_18.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/Protocol1_17_1To1_18.java @@ -27,7 +27,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1; @@ -76,9 +76,9 @@ public final class Protocol1_17_1To1_18 extends BackwardsProtocol { final int id = wrapper.read(Type.VAR_INT); @@ -166,87 +166,82 @@ public final class BlockItemPackets1_18 extends ItemRewriter { - final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); - final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), - MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), - MathUtil.ceilLog2(tracker.biomesSent())); - final Chunk oldChunk = wrapper.read(chunkType); - final ChunkSection[] sections = oldChunk.getSections(); - final BitSet mask = new BitSet(oldChunk.getSections().length); - final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE]; - int biomeIndex = 0; - for (int j = 0; j < sections.length; j++) { - final ChunkSection section = sections[j]; - // Write biome palette into biome array - final DataPalette biomePalette = section.palette(PaletteType.BIOMES); - for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) { - biomeData[biomeIndex++] = biomePalette.idAt(i); - } + protocol.registerClientbound(ClientboundPackets1_18.CHUNK_DATA, wrapper -> { + final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); + final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), + MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), + MathUtil.ceilLog2(tracker.biomesSent())); + final Chunk oldChunk = wrapper.read(chunkType); + final ChunkSection[] sections = oldChunk.getSections(); + final BitSet mask = new BitSet(oldChunk.getSections().length); + final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE]; + int biomeIndex = 0; + for (int j = 0; j < sections.length; j++) { + final ChunkSection section = sections[j]; + // Write biome palette into biome array + final DataPalette biomePalette = section.palette(PaletteType.BIOMES); + for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) { + biomeData[biomeIndex++] = biomePalette.idAt(i); + } - // Rewrite to empty section - if (section.getNonAirBlocksCount() == 0) { - sections[j] = null; - } else { - mask.set(j); - } - } - - final List blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size()); - for (final BlockEntity blockEntity : oldChunk.blockEntities()) { - final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId()); - if (id == null) { - // Shrug - continue; - } - - final CompoundTag tag; - if (blockEntity.tag() != null) { - tag = blockEntity.tag(); - handleSpawner(blockEntity.typeId(), tag); - } else { - tag = new CompoundTag(); - } - - blockEntityTags.add(tag); - tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX())); - tag.put("y", new IntTag(blockEntity.y())); - tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ())); - tag.put("id", new StringTag("minecraft:" + id)); - } - - final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, - oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags); - wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk); - - // Create and send light packet first - final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT); - lightPacket.write(Type.VAR_INT, chunk.getX()); - lightPacket.write(Type.VAR_INT, chunk.getZ()); - lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN)); // Trust edges - lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Sky light mask - lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Block light mask - lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Empty sky light mask - lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Empty block light mask - - final int skyLightLength = wrapper.read(Type.VAR_INT); - lightPacket.write(Type.VAR_INT, skyLightLength); - for (int i = 0; i < skyLightLength; i++) { - lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE)); - } - - final int blockLightLength = wrapper.read(Type.VAR_INT); - lightPacket.write(Type.VAR_INT, blockLightLength); - for (int i = 0; i < blockLightLength; i++) { - lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE)); - } - - lightPacket.send(Protocol1_17_1To1_18.class); - }); + // Rewrite to empty section + if (section.getNonAirBlocksCount() == 0) { + sections[j] = null; + } else { + mask.set(j); + } } + + final List blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size()); + for (final BlockEntity blockEntity : oldChunk.blockEntities()) { + final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId()); + if (id == null) { + // Shrug + continue; + } + + final CompoundTag tag; + if (blockEntity.tag() != null) { + tag = blockEntity.tag(); + handleSpawner(blockEntity.typeId(), tag); + } else { + tag = new CompoundTag(); + } + + blockEntityTags.add(tag); + tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX())); + tag.put("y", new IntTag(blockEntity.y())); + tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ())); + tag.put("id", new StringTag("minecraft:" + id)); + } + + final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, + oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags); + wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk); + + // Create and send light packet first + final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT); + lightPacket.write(Type.VAR_INT, chunk.getX()); + lightPacket.write(Type.VAR_INT, chunk.getZ()); + lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN)); // Trust edges + lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Sky light mask + lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Block light mask + lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Empty sky light mask + lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE)); // Empty block light mask + + final int skyLightLength = wrapper.read(Type.VAR_INT); + lightPacket.write(Type.VAR_INT, skyLightLength); + for (int i = 0; i < skyLightLength; i++) { + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE)); + } + + final int blockLightLength = wrapper.read(Type.VAR_INT); + lightPacket.write(Type.VAR_INT, blockLightLength); + for (int i = 0; i < blockLightLength; i++) { + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE)); + } + + lightPacket.send(Protocol1_17_1To1_18.class); }); protocol.cancelClientbound(ClientboundPackets1_18.SET_SIMULATION_DISTANCE); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/EntityPackets1_18.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/EntityPackets1_18.java index 53b449ec..59cdb48e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/EntityPackets1_18.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/EntityPackets1_18.java @@ -22,7 +22,7 @@ import com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1T import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_17; @@ -44,9 +44,9 @@ public final class EntityPackets1_18 extends EntityRewriter { - int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); - wrapper.cancel(); - for (int entityId : entityIds) { - // Send individual remove packets - PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY); - newPacket.write(Type.VAR_INT, entityId); - newPacket.send(Protocol1_17To1_17_1.class); - } - }); + registerClientbound(ClientboundPackets1_17_1.REMOVE_ENTITIES, null, wrapper -> { + int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); + wrapper.cancel(); + for (int entityId : entityIds) { + // Send individual remove packets + PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY); + newPacket.write(Type.VAR_INT, entityId); + newPacket.send(Protocol1_17To1_17_1.class); } }); - registerClientbound(ClientboundPackets1_17_1.CLOSE_WINDOW, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); - wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); - }); - } + registerClientbound(ClientboundPackets1_17_1.CLOSE_WINDOW, wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); }); - registerClientbound(ClientboundPackets1_17_1.SET_SLOT, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); - int stateId = wrapper.read(Type.VAR_INT); - wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); - }); - } + registerClientbound(ClientboundPackets1_17_1.SET_SLOT, wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.read(Type.VAR_INT); + wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); }); - registerClientbound(ClientboundPackets1_17_1.WINDOW_ITEMS, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); - int stateId = wrapper.read(Type.VAR_INT); - wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); + registerClientbound(ClientboundPackets1_17_1.WINDOW_ITEMS, wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.read(Type.VAR_INT); + wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); - // Length is encoded as a var int in 1.17.1 - wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); + // Length is encoded as a var int in 1.17.1 + wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); - // Carried item - should work without adding it to the array above - Item carried = wrapper.read(Type.FLAT_VAR_INT_ITEM); + // Carried item - should work without adding it to the array above + Item carried = wrapper.read(Type.FLAT_VAR_INT_ITEM); - PlayerLastCursorItem lastCursorItem = wrapper.user().get(PlayerLastCursorItem.class); - if (lastCursorItem != null) { - // For click drag ghost item fix -- since the state ID is always wrong, - // the server always resends the entire window contents after a drag action, - // which is useful since we need to update the carried item in preparation - // for a subsequent drag + PlayerLastCursorItem lastCursorItem = wrapper.user().get(PlayerLastCursorItem.class); + if (lastCursorItem != null) { + // For click drag ghost item fix -- since the state ID is always wrong, + // the server always resends the entire window contents after a drag action, + // which is useful since we need to update the carried item in preparation + // for a subsequent drag - lastCursorItem.setLastCursorItem(carried); - } - }); + lastCursorItem.setLastCursorItem(carried); } }); - registerServerbound(ServerboundPackets1_17.CLOSE_WINDOW, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); - wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); - }); - } + registerServerbound(ServerboundPackets1_17.CLOSE_WINDOW, wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); }); - registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); - int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); - wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId); - }); - } + registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); + wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId); }); - registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM); - boolean signing = wrapper.read(Type.BOOLEAN); - wrapper.passthrough(Type.VAR_INT); // Slot comes first + registerServerbound(ServerboundPackets1_17.EDIT_BOOK, wrapper -> { + Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM); + boolean signing = wrapper.read(Type.BOOLEAN); + wrapper.passthrough(Type.VAR_INT); // Slot comes first - CompoundTag tag = item.tag(); - ListTag pagesTag; - StringTag titleTag = null; - // Sanity checks - if (tag == null || (pagesTag = tag.get("pages")) == null - || (signing && (titleTag = tag.get("title")) == null)) { - wrapper.write(Type.VAR_INT, 0); // Pages length - wrapper.write(Type.BOOLEAN, false); // Optional title - return; - } + CompoundTag tag = item.tag(); + ListTag pagesTag; + StringTag titleTag = null; + // Sanity checks + if (tag == null || (pagesTag = tag.get("pages")) == null + || (signing && (titleTag = tag.get("title")) == null)) { + wrapper.write(Type.VAR_INT, 0); // Pages length + wrapper.write(Type.BOOLEAN, false); // Optional title + return; + } - // Write pages - limit them first - if (pagesTag.size() > MAX_PAGES) { - pagesTag = new ListTag(pagesTag.getValue().subList(0, MAX_PAGES)); - } + // Write pages - limit them first + if (pagesTag.size() > MAX_PAGES) { + pagesTag = new ListTag(pagesTag.getValue().subList(0, MAX_PAGES)); + } - wrapper.write(Type.VAR_INT, pagesTag.size()); - for (Tag pageTag : pagesTag) { - String page = ((StringTag) pageTag).getValue(); - // Limit page length - if (page.length() > MAX_PAGE_LENGTH) { - page = page.substring(0, MAX_PAGE_LENGTH); - } + wrapper.write(Type.VAR_INT, pagesTag.size()); + for (Tag pageTag : pagesTag) { + String page = ((StringTag) pageTag).getValue(); + // Limit page length + if (page.length() > MAX_PAGE_LENGTH) { + page = page.substring(0, MAX_PAGE_LENGTH); + } - wrapper.write(Type.STRING, page); - } + wrapper.write(Type.STRING, page); + } - // Write optional title - wrapper.write(Type.BOOLEAN, signing); - if (signing) { - if (titleTag == null) { - titleTag = tag.get("title"); - } + // Write optional title + wrapper.write(Type.BOOLEAN, signing); + if (signing) { + if (titleTag == null) { + titleTag = tag.get("title"); + } - // Limit title length - String title = titleTag.getValue(); - if (title.length() > MAX_TITLE_LENGTH) { - title = title.substring(0, MAX_TITLE_LENGTH); - } + // Limit title length + String title = titleTag.getValue(); + if (title.length() > MAX_TITLE_LENGTH) { + title = title.substring(0, MAX_TITLE_LENGTH); + } - wrapper.write(Type.STRING, title); - } - }); + wrapper.write(Type.STRING, title); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java index 68c97a9d..75850b5d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java @@ -31,7 +31,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types; import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; @@ -86,9 +86,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol soundRewriter = new SoundRewriter<>(this); soundRewriter.registerStopSound(ClientboundPackets1_19.STOP_SOUND); - registerClientbound(ClientboundPackets1_19.SOUND, new PacketRemapper() { + registerClientbound(ClientboundPackets1_19.SOUND, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Sound id map(Type.VAR_INT); // Source map(Type.INT); // X @@ -100,9 +100,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol(this).register(ClientboundPackets1_19.STATISTICS); final CommandRewriter commandRewriter = new CommandRewriter1_19(this); - registerClientbound(ClientboundPackets1_19.DECLARE_COMMANDS, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final int size = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < size; i++) { - final byte flags = wrapper.passthrough(Type.BYTE); - wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices - if ((flags & 0x08) != 0) { - wrapper.passthrough(Type.VAR_INT); // Redirect node index - } + registerClientbound(ClientboundPackets1_19.DECLARE_COMMANDS, wrapper -> { + final int size = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < size; i++) { + final byte flags = wrapper.passthrough(Type.BYTE); + wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices + if ((flags & 0x08) != 0) { + wrapper.passthrough(Type.VAR_INT); // Redirect node index + } - final int nodeType = flags & 0x03; - if (nodeType == 1 || nodeType == 2) { // Literal/argument node - wrapper.passthrough(Type.STRING); // Name - } + final int nodeType = flags & 0x03; + if (nodeType == 1 || nodeType == 2) { // Literal/argument node + wrapper.passthrough(Type.STRING); // Name + } - if (nodeType == 2) { // Argument node - final int argumentTypeId = wrapper.read(Type.VAR_INT); - String argumentType = MAPPINGS.argumentType(argumentTypeId); - if (argumentType == null) { - ViaBackwards.getPlatform().getLogger().warning("Unknown command argument type id: " + argumentTypeId); - argumentType = "minecraft:no"; - } - - wrapper.write(Type.STRING, commandRewriter.handleArgumentType(argumentType)); - commandRewriter.handleArgument(wrapper, argumentType); - - if ((flags & 0x10) != 0) { - wrapper.passthrough(Type.STRING); // Suggestion type - } - } + if (nodeType == 2) { // Argument node + final int argumentTypeId = wrapper.read(Type.VAR_INT); + String argumentType = MAPPINGS.argumentType(argumentTypeId); + if (argumentType == null) { + ViaBackwards.getPlatform().getLogger().warning("Unknown command argument type id: " + argumentTypeId); + argumentType = "minecraft:no"; } - wrapper.passthrough(Type.VAR_INT); // Root node index - }); + wrapper.write(Type.STRING, commandRewriter.handleArgumentType(argumentType)); + commandRewriter.handleArgument(wrapper, argumentType); + + if ((flags & 0x10) != 0) { + wrapper.passthrough(Type.STRING); // Suggestion type + } + } } + + wrapper.passthrough(Type.VAR_INT); // Root node index }); cancelClientbound(ClientboundPackets1_19.SERVER_DATA); cancelClientbound(ClientboundPackets1_19.CHAT_PREVIEW); cancelClientbound(ClientboundPackets1_19.SET_DISPLAY_CHAT_PREVIEW); - registerClientbound(ClientboundPackets1_19.PLAYER_CHAT, ClientboundPackets1_18.CHAT_MESSAGE, new PacketRemapper() { + registerClientbound(ClientboundPackets1_19.PLAYER_CHAT, ClientboundPackets1_18.CHAT_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { handler(wrapper -> { final JsonElement signedContent = wrapper.read(Type.COMPONENT); final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT); @@ -212,9 +207,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol { final JsonElement content = wrapper.passthrough(Type.COMPONENT); translatableRewriter.processText(content); @@ -227,9 +222,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol wrapper.write(Type.LONG, Instant.now().toEpochMilli())); // Timestamp create(Type.LONG, 0L); // Salt @@ -248,9 +243,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol { @@ -266,18 +261,18 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol { final int size = wrapper.read(Type.VAR_INT); @@ -96,17 +96,17 @@ public final class BlockItemPackets1_19 extends ItemRewriter { - final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); - final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), - MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), - MathUtil.ceilLog2(tracker.biomesSent())); - final Chunk chunk = wrapper.passthrough(chunkType); - for (final ChunkSection section : chunk.getSections()) { - final DataPalette blockPalette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < blockPalette.size(); i++) { - final int id = blockPalette.idByIndex(i); - blockPalette.setIdByIndex(i, protocol.getMappingData().getNewBlockStateId(id)); - } - } - }); + protocol.registerClientbound(ClientboundPackets1_19.CHUNK_DATA, wrapper -> { + final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); + final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), + MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), + MathUtil.ceilLog2(tracker.biomesSent())); + final Chunk chunk = wrapper.passthrough(chunkType); + for (final ChunkSection section : chunk.getSections()) { + final DataPalette blockPalette = section.palette(PaletteType.BLOCKS); + for (int i = 0; i < blockPalette.size(); i++) { + final int id = blockPalette.idByIndex(i); + blockPalette.setIdByIndex(i, protocol.getMappingData().getNewBlockStateId(id)); + } } }); // The server does nothing but track the sequence, so we can just set it as 0 - protocol.registerServerbound(ServerboundPackets1_17.PLAYER_DIGGING, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_17.PLAYER_DIGGING, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Action map(Type.POSITION1_14); // Block position map(Type.UNSIGNED_BYTE); // Direction create(Type.VAR_INT, 0); // Sequence } }); - protocol.registerServerbound(ServerboundPackets1_17.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() { + protocol.registerServerbound(ServerboundPackets1_17.PLAYER_BLOCK_PLACEMENT, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.VAR_INT); // Hand map(Type.POSITION1_14); // Block position map(Type.VAR_INT); // Direction @@ -182,34 +177,29 @@ public final class BlockItemPackets1_19 extends ItemRewriter { - final int primaryEffect = wrapper.read(Type.VAR_INT); - if (primaryEffect != -1) { - wrapper.write(Type.BOOLEAN, true); - wrapper.write(Type.VAR_INT, primaryEffect); - } else { - wrapper.write(Type.BOOLEAN, false); - } + protocol.registerServerbound(ServerboundPackets1_17.SET_BEACON_EFFECT, wrapper -> { + final int primaryEffect = wrapper.read(Type.VAR_INT); + if (primaryEffect != -1) { + wrapper.write(Type.BOOLEAN, true); + wrapper.write(Type.VAR_INT, primaryEffect); + } else { + wrapper.write(Type.BOOLEAN, false); + } - final int secondaryEffect = wrapper.read(Type.VAR_INT); - if (secondaryEffect != -1) { - wrapper.write(Type.BOOLEAN, true); - wrapper.write(Type.VAR_INT, secondaryEffect); - } else { - wrapper.write(Type.BOOLEAN, false); - } - }); + final int secondaryEffect = wrapper.read(Type.VAR_INT); + if (secondaryEffect != -1) { + wrapper.write(Type.BOOLEAN, true); + wrapper.write(Type.VAR_INT, secondaryEffect); + } else { + wrapper.write(Type.BOOLEAN, false); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java index 73d0f8d1..e3b6459d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/packets/EntityPackets1_19.java @@ -28,7 +28,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.Particle; import com.viaversion.viaversion.api.type.types.version.Types1_18; @@ -54,9 +54,9 @@ public final class EntityPackets1_19 extends EntityRewriter { final String dimensionKey = wrapper.read(Type.STRING); final CompoundTag dimension = wrapper.user().get(DimensionRegistryStorage.class).dimension(dimensionKey); @@ -204,37 +204,32 @@ public final class EntityPackets1_19 extends EntityRewriter { - final int action = wrapper.passthrough(Type.VAR_INT); - final int entries = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < entries; i++) { - wrapper.passthrough(Type.UUID); // UUID - if (action == 0) { // Add player - wrapper.passthrough(Type.STRING); // Player Name + protocol.registerClientbound(ClientboundPackets1_19.PLAYER_INFO, wrapper -> { + final int action = wrapper.passthrough(Type.VAR_INT); + final int entries = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < entries; i++) { + wrapper.passthrough(Type.UUID); // UUID + if (action == 0) { // Add player + wrapper.passthrough(Type.STRING); // Player Name - final int properties = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < properties; j++) { - wrapper.passthrough(Type.STRING); // Name - wrapper.passthrough(Type.STRING); // Value - wrapper.passthrough(Type.OPTIONAL_STRING); // Signature - } - - wrapper.passthrough(Type.VAR_INT); // Gamemode - wrapper.passthrough(Type.VAR_INT); // Ping - wrapper.passthrough(Type.OPTIONAL_COMPONENT); // Display name - - // Remove public profile signature - wrapper.read(Type.OPTIONAL_PROFILE_KEY); - } else if (action == 1 || action == 2) { // Update gamemode/update latency - wrapper.passthrough(Type.VAR_INT); - } else if (action == 3) { // Update display name - wrapper.passthrough(Type.OPTIONAL_COMPONENT); - } + final int properties = wrapper.passthrough(Type.VAR_INT); + for (int j = 0; j < properties; j++) { + wrapper.passthrough(Type.STRING); // Name + wrapper.passthrough(Type.STRING); // Value + wrapper.passthrough(Type.OPTIONAL_STRING); // Signature } - }); + + wrapper.passthrough(Type.VAR_INT); // Gamemode + wrapper.passthrough(Type.VAR_INT); // Ping + wrapper.passthrough(Type.OPTIONAL_COMPONENT); // Display name + + // Remove public profile signature + wrapper.read(Type.OPTIONAL_PROFILE_KEY); + } else if (action == 1 || action == 2) { // Update gamemode/update latency + wrapper.passthrough(Type.VAR_INT); + } else if (action == 3) { // Update display name + wrapper.passthrough(Type.OPTIONAL_COMPONENT); + } } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/DimensionRegistryStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/DimensionRegistryStorage.java index 35cf455c..5ba4cc0e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/DimensionRegistryStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/DimensionRegistryStorage.java @@ -22,10 +22,9 @@ import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.HashMap; import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; public final class DimensionRegistryStorage implements StorableObject { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/StoredPainting.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/StoredPainting.java index a685eb6c..83845697 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/StoredPainting.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/storage/StoredPainting.java @@ -19,7 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage; import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.minecraft.Position; - import java.util.UUID; public final class StoredPainting implements StorableObject { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18to1_18_2/Protocol1_18To1_18_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18to1_18_2/Protocol1_18To1_18_2.java index b0cdd5bf..1d9d05f7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18to1_18_2/Protocol1_18To1_18_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18to1_18_2/Protocol1_18To1_18_2.java @@ -22,7 +22,7 @@ import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.protocol.protocol1_18to1_18_2.data.CommandRewriter1_18_2; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; @@ -53,25 +53,25 @@ public final class Protocol1_18To1_18_2 extends BackwardsProtocol removeTagPrefix(wrapper.passthrough(Type.NBT))); - } - }); + registerClientbound(ClientboundPackets1_18.RESPAWN, wrapper -> removeTagPrefix(wrapper.passthrough(Type.NBT))); } private void removeTagPrefix(CompoundTag tag) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java index 759c41e7..d1293200 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java @@ -33,7 +33,7 @@ import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_3Types; import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.BitSetType; import com.viaversion.viaversion.api.type.types.ByteArrayType; @@ -90,76 +90,66 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol soundRewriter = new SoundRewriter<>(this); soundRewriter.registerStopSound(ClientboundPackets1_19_3.STOP_SOUND); - registerClientbound(ClientboundPackets1_19_3.SOUND, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id - if (soundId != -1) { - final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); - if (mappedId == -1) { - wrapper.cancel(); - return; - } + registerClientbound(ClientboundPackets1_19_3.SOUND, wrapper -> { + final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id + if (soundId != -1) { + final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); + if (mappedId == -1) { + wrapper.cancel(); + return; + } - wrapper.write(Type.VAR_INT, mappedId); - return; - } - - String soundIdentifier = wrapper.read(Type.STRING); - wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range - final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); - if (mappedIdentifier != null) { - if (mappedIdentifier.isEmpty()) { - wrapper.cancel(); - return; - } - - soundIdentifier = mappedIdentifier; - } - - wrapper.write(Type.STRING, soundIdentifier); - wrapper.setPacketType(ClientboundPackets1_19_1.NAMED_SOUND); - }); + wrapper.write(Type.VAR_INT, mappedId); + return; } + + String soundIdentifier = wrapper.read(Type.STRING); + wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range + final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); + if (mappedIdentifier != null) { + if (mappedIdentifier.isEmpty()) { + wrapper.cancel(); + return; + } + + soundIdentifier = mappedIdentifier; + } + + wrapper.write(Type.STRING, soundIdentifier); + wrapper.setPacketType(ClientboundPackets1_19_1.NAMED_SOUND); }); - registerClientbound(ClientboundPackets1_19_3.ENTITY_SOUND, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id - if (soundId != -1) { - final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); - if (mappedId == -1) { - wrapper.cancel(); - return; - } + registerClientbound(ClientboundPackets1_19_3.ENTITY_SOUND, wrapper -> { + final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id + if (soundId != -1) { + final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); + if (mappedId == -1) { + wrapper.cancel(); + return; + } - wrapper.write(Type.VAR_INT, mappedId); - } - - // Convert the resource location to the corresponding integer id - String soundIdentifier = wrapper.read(Type.STRING); - wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range - final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); - if (mappedIdentifier != null) { - if (mappedIdentifier.isEmpty()) { - wrapper.cancel(); - return; - } - - soundIdentifier = mappedIdentifier; - } - - final int mappedId = MAPPINGS.mappedSound(soundIdentifier); - if (mappedId == -1) { - wrapper.cancel(); - return; - } - - wrapper.write(Type.VAR_INT, mappedId); - }); + wrapper.write(Type.VAR_INT, mappedId); } + + // Convert the resource location to the corresponding integer id + String soundIdentifier = wrapper.read(Type.STRING); + wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range + final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); + if (mappedIdentifier != null) { + if (mappedIdentifier.isEmpty()) { + wrapper.cancel(); + return; + } + + soundIdentifier = mappedIdentifier; + } + + final int mappedId = MAPPINGS.mappedSound(soundIdentifier); + if (mappedId == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.VAR_INT, mappedId); }); final TagRewriter tagRewriter = new TagRewriter<>(this); @@ -170,49 +160,44 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol(this).register(ClientboundPackets1_19_3.STATISTICS); final CommandRewriter commandRewriter = new CommandRewriter<>(this); - registerClientbound(ClientboundPackets1_19_3.DECLARE_COMMANDS, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final int size = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < size; i++) { - final byte flags = wrapper.passthrough(Type.BYTE); - wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices - if ((flags & 0x08) != 0) { - wrapper.passthrough(Type.VAR_INT); // Redirect node index - } + registerClientbound(ClientboundPackets1_19_3.DECLARE_COMMANDS, wrapper -> { + final int size = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < size; i++) { + final byte flags = wrapper.passthrough(Type.BYTE); + wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); // Children indices + if ((flags & 0x08) != 0) { + wrapper.passthrough(Type.VAR_INT); // Redirect node index + } - final int nodeType = flags & 0x03; - if (nodeType == 1 || nodeType == 2) { // Literal/argument node - wrapper.passthrough(Type.STRING); // Name - } + final int nodeType = flags & 0x03; + if (nodeType == 1 || nodeType == 2) { // Literal/argument node + wrapper.passthrough(Type.STRING); // Name + } - if (nodeType == 2) { // Argument node - final int argumentTypeId = wrapper.read(Type.VAR_INT); - final int mappedArgumentTypeId = MAPPINGS.getArgumentTypeMappings().mappings().getNewId(argumentTypeId); - Preconditions.checkArgument(mappedArgumentTypeId != -1, "Unknown command argument type id: " + argumentTypeId); - wrapper.write(Type.VAR_INT, mappedArgumentTypeId); + if (nodeType == 2) { // Argument node + final int argumentTypeId = wrapper.read(Type.VAR_INT); + final int mappedArgumentTypeId = MAPPINGS.getArgumentTypeMappings().mappings().getNewId(argumentTypeId); + Preconditions.checkArgument(mappedArgumentTypeId != -1, "Unknown command argument type id: " + argumentTypeId); + wrapper.write(Type.VAR_INT, mappedArgumentTypeId); - final String identifier = MAPPINGS.getArgumentTypeMappings().identifier(argumentTypeId); - commandRewriter.handleArgument(wrapper, identifier); - if (identifier.equals("minecraft:gamemode")) { - wrapper.write(Type.VAR_INT, 0); // Word - } - - if ((flags & 0x10) != 0) { - wrapper.passthrough(Type.STRING); // Suggestion type - } - } + final String identifier = MAPPINGS.getArgumentTypeMappings().identifier(argumentTypeId); + commandRewriter.handleArgument(wrapper, identifier); + if (identifier.equals("minecraft:gamemode")) { + wrapper.write(Type.VAR_INT, 0); // Word } - wrapper.passthrough(Type.VAR_INT); // Root node index - }); + if ((flags & 0x10) != 0) { + wrapper.passthrough(Type.STRING); // Suggestion type + } + } } + + wrapper.passthrough(Type.VAR_INT); // Root node index }); - registerClientbound(ClientboundPackets1_19_3.SERVER_DATA, new PacketRemapper() { + registerClientbound(ClientboundPackets1_19_3.SERVER_DATA, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.OPTIONAL_COMPONENT); // Motd map(Type.OPTIONAL_STRING); // Encoded icon create(Type.BOOLEAN, false); // Previews chat @@ -220,9 +205,9 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { final ProfileKey profileKey = wrapper.read(Type.OPTIONAL_PROFILE_KEY); @@ -232,9 +217,9 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { if (wrapper.user().has(NonceStorage.class)) { @@ -246,9 +231,9 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { final NonceStorage nonceStorage = wrapper.user().remove(NonceStorage.class); @@ -262,9 +247,9 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { - final JsonElement content = wrapper.read(Type.COMPONENT); - translatableRewriter.processText(content); - final int chatTypeId = wrapper.read(Type.VAR_INT); - final JsonElement senderName = wrapper.read(Type.COMPONENT); - final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT); - final JsonElement result = Protocol1_19To1_19_1.decorateChatMessage(wrapper.user().get(ChatTypeStorage1_19_3.class), chatTypeId, senderName, targetName, content); - if (result == null) { - wrapper.cancel(); - return; - } - - wrapper.write(Type.COMPONENT, result); - wrapper.write(Type.BOOLEAN, false); - }); + registerClientbound(ClientboundPackets1_19_3.DISGUISED_CHAT, ClientboundPackets1_19_1.SYSTEM_CHAT, wrapper -> { + final JsonElement content = wrapper.read(Type.COMPONENT); + translatableRewriter.processText(content); + final int chatTypeId = wrapper.read(Type.VAR_INT); + final JsonElement senderName = wrapper.read(Type.COMPONENT); + final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT); + final JsonElement result = Protocol1_19To1_19_1.decorateChatMessage(wrapper.user().get(ChatTypeStorage1_19_3.class), chatTypeId, senderName, targetName, content); + if (result == null) { + wrapper.cancel(); + return; } + + wrapper.write(Type.COMPONENT, result); + wrapper.write(Type.BOOLEAN, false); }); cancelClientbound(ClientboundPackets1_19_3.UPDATE_ENABLED_FEATURES); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/BlockItemPackets1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/BlockItemPackets1_19_3.java index 293646d8..1d64e72c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/BlockItemPackets1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/BlockItemPackets1_19_3.java @@ -20,7 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets; import com.viaversion.viabackwards.api.rewriters.ItemRewriter; import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.Protocol1_19_1To1_19_3; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; @@ -55,9 +55,9 @@ public final class BlockItemPackets1_19_3 extends ItemRewriter recipeRewriter = new RecipeRewriter1_16<>(protocol); - protocol.registerClientbound(ClientboundPackets1_19_3.DECLARE_RECIPES, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final int size = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < size; i++) { - final String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); - wrapper.passthrough(Type.STRING); // Recipe Identifier - switch (type) { - case "crafting_shapeless": { - wrapper.passthrough(Type.STRING); // Group - wrapper.read(Type.VAR_INT); // Crafting book category - final int ingredients = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < ingredients; j++) { - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - handleItemToClient(item); - } - } - handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - break; + protocol.registerClientbound(ClientboundPackets1_19_3.DECLARE_RECIPES, wrapper -> { + final int size = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < size; i++) { + final String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); + wrapper.passthrough(Type.STRING); // Recipe Identifier + switch (type) { + case "crafting_shapeless": { + wrapper.passthrough(Type.STRING); // Group + wrapper.read(Type.VAR_INT); // Crafting book category + final int ingredients = wrapper.passthrough(Type.VAR_INT); + for (int j = 0; j < ingredients; j++) { + final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients + for (final Item item : items) { + handleItemToClient(item); } - case "crafting_shaped": { - final int ingredients = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.STRING); // Group - wrapper.read(Type.VAR_INT); // Crafting book category - for (int j = 0; j < ingredients; j++) { - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - handleItemToClient(item); - } - } - handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - break; - } - case "smelting": - case "campfire_cooking": - case "blasting": - case "smoking": - wrapper.passthrough(Type.STRING); // Group - wrapper.read(Type.VAR_INT); // Crafting book category - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - handleItemToClient(item); - } - handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - wrapper.passthrough(Type.FLOAT); // EXP - wrapper.passthrough(Type.VAR_INT); // Cooking time - break; - case "crafting_special_armordye": - case "crafting_special_bookcloning": - case "crafting_special_mapcloning": - case "crafting_special_mapextending": - case "crafting_special_firework_rocket": - case "crafting_special_firework_star": - case "crafting_special_firework_star_fade": - case "crafting_special_tippedarrow": - case "crafting_special_bannerduplicate": - case "crafting_special_shielddecoration": - case "crafting_special_shulkerboxcoloring": - case "crafting_special_suspiciousstew": - case "crafting_special_repairitem": - wrapper.read(Type.VAR_INT); // Crafting book category - break; - default: - recipeRewriter.handle(wrapper, type); - break; } + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + break; } - }); + case "crafting_shaped": { + final int ingredients = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.STRING); // Group + wrapper.read(Type.VAR_INT); // Crafting book category + for (int j = 0; j < ingredients; j++) { + final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients + for (final Item item : items) { + handleItemToClient(item); + } + } + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + break; + } + case "smelting": + case "campfire_cooking": + case "blasting": + case "smoking": + wrapper.passthrough(Type.STRING); // Group + wrapper.read(Type.VAR_INT); // Crafting book category + final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients + for (final Item item : items) { + handleItemToClient(item); + } + handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + wrapper.passthrough(Type.FLOAT); // EXP + wrapper.passthrough(Type.VAR_INT); // Cooking time + break; + case "crafting_special_armordye": + case "crafting_special_bookcloning": + case "crafting_special_mapcloning": + case "crafting_special_mapextending": + case "crafting_special_firework_rocket": + case "crafting_special_firework_star": + case "crafting_special_firework_star_fade": + case "crafting_special_tippedarrow": + case "crafting_special_bannerduplicate": + case "crafting_special_shielddecoration": + case "crafting_special_shulkerboxcoloring": + case "crafting_special_suspiciousstew": + case "crafting_special_repairitem": + wrapper.read(Type.VAR_INT); // Crafting book category + break; + default: + recipeRewriter.handleRecipeType(wrapper, type); + break; + } } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java index 6b3464e8..98b6c903 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/packets/EntityPackets1_19_3.java @@ -25,7 +25,7 @@ import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_3Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.BitSetType; import com.viaversion.viaversion.api.type.types.version.Types1_19; @@ -37,10 +37,9 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3; -import org.checkerframework.checker.nullness.qual.Nullable; - import java.util.BitSet; import java.util.UUID; +import org.checkerframework.checker.nullness.qual.Nullable; public final class EntityPackets1_19_3 extends EntityRewriter { @@ -63,9 +62,9 @@ public final class EntityPackets1_19_3 extends EntityRewriter { - wrapper.cancel(); - final BitSet actions = wrapper.read(PROFILE_ACTIONS_ENUM_TYPE); - final int entries = wrapper.read(Type.VAR_INT); - if (actions.get(ADD_PLAYER)) { - // Special case, as we need to write everything into one action - final PacketWrapper playerInfoPacket = wrapper.create(ClientboundPackets1_19_1.PLAYER_INFO); - playerInfoPacket.write(Type.VAR_INT, 0); - playerInfoPacket.write(Type.VAR_INT, entries); - for (int i = 0; i < entries; i++) { - playerInfoPacket.write(Type.UUID, wrapper.read(Type.UUID)); - playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Player Name + protocol.registerClientbound(ClientboundPackets1_19_3.PLAYER_INFO_UPDATE, ClientboundPackets1_19_1.PLAYER_INFO, wrapper -> { + wrapper.cancel(); + final BitSet actions = wrapper.read(PROFILE_ACTIONS_ENUM_TYPE); + final int entries = wrapper.read(Type.VAR_INT); + if (actions.get(ADD_PLAYER)) { + // Special case, as we need to write everything into one action + final PacketWrapper playerInfoPacket = wrapper.create(ClientboundPackets1_19_1.PLAYER_INFO); + playerInfoPacket.write(Type.VAR_INT, 0); + playerInfoPacket.write(Type.VAR_INT, entries); + for (int i = 0; i < entries; i++) { + playerInfoPacket.write(Type.UUID, wrapper.read(Type.UUID)); + playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Player Name - final int properties = wrapper.read(Type.VAR_INT); - playerInfoPacket.write(Type.VAR_INT, properties); - for (int j = 0; j < properties; j++) { - playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Name - playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Value - playerInfoPacket.write(Type.OPTIONAL_STRING, wrapper.read(Type.OPTIONAL_STRING)); // Signature - } - - // Now check for the other parts individually and add dummy values if not present - final ProfileKey profileKey; - if (actions.get(INITIALIZE_CHAT) && wrapper.read(Type.BOOLEAN)) { - wrapper.read(Type.UUID); // Session UUID - profileKey = wrapper.read(Type.PROFILE_KEY); - } else { - profileKey = null; - } - - final int gamemode = actions.get(UPDATE_GAMEMODE) ? wrapper.read(Type.VAR_INT) : 0; - - if (actions.get(UPDATE_LISTED)) { - wrapper.read(Type.BOOLEAN); // Listed - throw away - } - - final int latency = actions.get(UPDATE_LATENCY) ? wrapper.read(Type.VAR_INT) : 0; - - final JsonElement displayName = actions.get(UPDATE_DISPLAYNAME) ? wrapper.read(Type.OPTIONAL_COMPONENT) : null; - playerInfoPacket.write(Type.VAR_INT, gamemode); - playerInfoPacket.write(Type.VAR_INT, latency); - playerInfoPacket.write(Type.OPTIONAL_COMPONENT, displayName); - playerInfoPacket.write(Type.OPTIONAL_PROFILE_KEY, profileKey); - } - playerInfoPacket.send(Protocol1_19_1To1_19_3.class); - return; + final int properties = wrapper.read(Type.VAR_INT); + playerInfoPacket.write(Type.VAR_INT, properties); + for (int j = 0; j < properties; j++) { + playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Name + playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Value + playerInfoPacket.write(Type.OPTIONAL_STRING, wrapper.read(Type.OPTIONAL_STRING)); // Signature } - final PlayerProfileUpdate[] updates = new PlayerProfileUpdate[entries]; - for (int i = 0; i < entries; i++) { - final UUID uuid = wrapper.read(Type.UUID); - int gamemode = 0; - int latency = 0; - JsonElement displayName = null; - for (final int action : PROFILE_ACTIONS) { - if (!actions.get(action)) { - continue; - } - switch (action) { - case UPDATE_GAMEMODE: - gamemode = wrapper.read(Type.VAR_INT); - break; - case UPDATE_LATENCY: - latency = wrapper.read(Type.VAR_INT); - break; - case UPDATE_DISPLAYNAME: - displayName = wrapper.read(Type.OPTIONAL_COMPONENT); - break; - } - } - - updates[i] = new PlayerProfileUpdate(uuid, gamemode, latency, displayName); + // Now check for the other parts individually and add dummy values if not present + final ProfileKey profileKey; + if (actions.get(INITIALIZE_CHAT) && wrapper.read(Type.BOOLEAN)) { + wrapper.read(Type.UUID); // Session UUID + profileKey = wrapper.read(Type.PROFILE_KEY); + } else { + profileKey = null; } - if (actions.get(UPDATE_GAMEMODE)) { - sendPlayerProfileUpdate(wrapper.user(), 1, updates); - } else if (actions.get(UPDATE_LATENCY)) { - sendPlayerProfileUpdate(wrapper.user(), 2, updates); - } else if (actions.get(UPDATE_DISPLAYNAME)) { - sendPlayerProfileUpdate(wrapper.user(), 3, updates); + final int gamemode = actions.get(UPDATE_GAMEMODE) ? wrapper.read(Type.VAR_INT) : 0; + + if (actions.get(UPDATE_LISTED)) { + wrapper.read(Type.BOOLEAN); // Listed - throw away } - }); + + final int latency = actions.get(UPDATE_LATENCY) ? wrapper.read(Type.VAR_INT) : 0; + + final JsonElement displayName = actions.get(UPDATE_DISPLAYNAME) ? wrapper.read(Type.OPTIONAL_COMPONENT) : null; + playerInfoPacket.write(Type.VAR_INT, gamemode); + playerInfoPacket.write(Type.VAR_INT, latency); + playerInfoPacket.write(Type.OPTIONAL_COMPONENT, displayName); + playerInfoPacket.write(Type.OPTIONAL_PROFILE_KEY, profileKey); + } + playerInfoPacket.send(Protocol1_19_1To1_19_3.class); + return; + } + + final PlayerProfileUpdate[] updates = new PlayerProfileUpdate[entries]; + for (int i = 0; i < entries; i++) { + final UUID uuid = wrapper.read(Type.UUID); + int gamemode = 0; + int latency = 0; + JsonElement displayName = null; + for (final int action : PROFILE_ACTIONS) { + if (!actions.get(action)) { + continue; + } + switch (action) { + case UPDATE_GAMEMODE: + gamemode = wrapper.read(Type.VAR_INT); + break; + case UPDATE_LATENCY: + latency = wrapper.read(Type.VAR_INT); + break; + case UPDATE_DISPLAYNAME: + displayName = wrapper.read(Type.OPTIONAL_COMPONENT); + break; + } + } + + updates[i] = new PlayerProfileUpdate(uuid, gamemode, latency, displayName); + } + + if (actions.get(UPDATE_GAMEMODE)) { + sendPlayerProfileUpdate(wrapper.user(), 1, updates); + } else if (actions.get(UPDATE_LATENCY)) { + sendPlayerProfileUpdate(wrapper.user(), 2, updates); + } else if (actions.get(UPDATE_DISPLAYNAME)) { + sendPlayerProfileUpdate(wrapper.user(), 3, updates); } }); - protocol.registerClientbound(ClientboundPackets1_19_3.PLAYER_INFO_REMOVE, ClientboundPackets1_19_1.PLAYER_INFO, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final UUID[] uuids = wrapper.read(Type.UUID_ARRAY); - wrapper.write(Type.VAR_INT, 4); // Remove player - wrapper.write(Type.VAR_INT, uuids.length); - for (final UUID uuid : uuids) { - wrapper.write(Type.UUID, uuid); - } - }); + protocol.registerClientbound(ClientboundPackets1_19_3.PLAYER_INFO_REMOVE, ClientboundPackets1_19_1.PLAYER_INFO, wrapper -> { + final UUID[] uuids = wrapper.read(Type.UUID_ARRAY); + wrapper.write(Type.VAR_INT, 4); // Remove player + wrapper.write(Type.VAR_INT, uuids.length); + for (final UUID uuid : uuids) { + wrapper.write(Type.UUID, uuid); } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/ChatSessionStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/ChatSessionStorage.java index e9cc66ed..340421ec 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/ChatSessionStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/ChatSessionStorage.java @@ -18,7 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage; import com.viaversion.viaversion.api.connection.StorableObject; - import java.util.UUID; public final class ChatSessionStorage implements StorableObject { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/NonceStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/NonceStorage.java index 4869a031..91f68314 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/NonceStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/storage/NonceStorage.java @@ -24,7 +24,7 @@ public final class NonceStorage implements StorableObject { private final byte[] nonce; - public NonceStorage(final byte @Nullable[] nonce) { + public NonceStorage(final byte @Nullable [] nonce) { this.nonce = nonce; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/Protocol1_19_3To1_19_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/Protocol1_19_3To1_19_4.java index 7532dcf2..2b1bf760 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/Protocol1_19_3To1_19_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/Protocol1_19_3To1_19_4.java @@ -24,7 +24,7 @@ import com.viaversion.viabackwards.protocol.protocol1_19_3to1_19_4.packets.Entit import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3; @@ -77,9 +77,9 @@ public final class Protocol1_19_3To1_19_4 extends BackwardsProtocol { - wrapper.read(Type.OPTIONAL_BYTE_ARRAY_PRIMITIVE); // Previous signature + registerClientbound(ClientboundPackets1_19_1.PLAYER_CHAT, ClientboundPackets1_19.SYSTEM_CHAT, wrapper -> { + wrapper.read(Type.OPTIONAL_BYTE_ARRAY_PRIMITIVE); // Previous signature - final PlayerMessageSignature signature = wrapper.read(Type.PLAYER_MESSAGE_SIGNATURE); + final PlayerMessageSignature signature = wrapper.read(Type.PLAYER_MESSAGE_SIGNATURE); - // Store message signature for last seen - if (!signature.uuid().equals(ZERO_UUID) && signature.signatureBytes().length != 0) { - final ReceivedMessagesStorage messagesStorage = wrapper.user().get(ReceivedMessagesStorage.class); - messagesStorage.add(signature); - if (messagesStorage.tickUnacknowledged() > 64) { - messagesStorage.resetUnacknowledgedCount(); + // Store message signature for last seen + if (!signature.uuid().equals(ZERO_UUID) && signature.signatureBytes().length != 0) { + final ReceivedMessagesStorage messagesStorage = wrapper.user().get(ReceivedMessagesStorage.class); + messagesStorage.add(signature); + if (messagesStorage.tickUnacknowledged() > 64) { + messagesStorage.resetUnacknowledgedCount(); - // Send chat acknowledgement - final PacketWrapper chatAckPacket = wrapper.create(ServerboundPackets1_19_1.CHAT_ACK); - chatAckPacket.write(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY, messagesStorage.lastSignatures()); - chatAckPacket.write(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE, null); - chatAckPacket.sendToServer(Protocol1_19To1_19_1.class); - } - } - - // Send the unsigned message if present, otherwise the signed message - final String plainMessage = wrapper.read(Type.STRING); // Plain message - JsonElement message = null; - JsonElement decoratedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); - if (decoratedMessage != null) { - message = decoratedMessage; - } - - wrapper.read(Type.LONG); // Timestamp - wrapper.read(Type.LONG); // Salt - wrapper.read(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY); // Last seen - - final JsonElement unsignedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); - if (unsignedMessage != null) { - message = unsignedMessage; - } - if (message == null) { - // If no decorated or unsigned message is given, use the plain one - message = GsonComponentSerializer.gson().serializeToTree(Component.text(plainMessage)); - } - - final int filterMaskType = wrapper.read(Type.VAR_INT); - if (filterMaskType == 2) { // Partially filtered - wrapper.read(Type.LONG_ARRAY_PRIMITIVE); // Mask - } - - final int chatTypeId = wrapper.read(Type.VAR_INT); - final JsonElement senderName = wrapper.read(Type.COMPONENT); - final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT); - decoratedMessage = decorateChatMessage(wrapper.user().get(ChatRegistryStorage1_19_1.class), chatTypeId, senderName, targetName, message); - if (decoratedMessage == null) { - wrapper.cancel(); - return; - } - - translatableRewriter.processText(decoratedMessage); - wrapper.write(Type.COMPONENT, decoratedMessage); - wrapper.write(Type.VAR_INT, SYSTEM_CHAT_ID); - }); + // Send chat acknowledgement + final PacketWrapper chatAckPacket = wrapper.create(ServerboundPackets1_19_1.CHAT_ACK); + chatAckPacket.write(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY, messagesStorage.lastSignatures()); + chatAckPacket.write(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE, null); + chatAckPacket.sendToServer(Protocol1_19To1_19_1.class); + } } + + // Send the unsigned message if present, otherwise the signed message + final String plainMessage = wrapper.read(Type.STRING); // Plain message + JsonElement message = null; + JsonElement decoratedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); + if (decoratedMessage != null) { + message = decoratedMessage; + } + + wrapper.read(Type.LONG); // Timestamp + wrapper.read(Type.LONG); // Salt + wrapper.read(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY); // Last seen + + final JsonElement unsignedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); + if (unsignedMessage != null) { + message = unsignedMessage; + } + if (message == null) { + // If no decorated or unsigned message is given, use the plain one + message = GsonComponentSerializer.gson().serializeToTree(Component.text(plainMessage)); + } + + final int filterMaskType = wrapper.read(Type.VAR_INT); + if (filterMaskType == 2) { // Partially filtered + wrapper.read(Type.LONG_ARRAY_PRIMITIVE); // Mask + } + + final int chatTypeId = wrapper.read(Type.VAR_INT); + final JsonElement senderName = wrapper.read(Type.COMPONENT); + final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT); + decoratedMessage = decorateChatMessage(wrapper.user().get(ChatRegistryStorage1_19_1.class), chatTypeId, senderName, targetName, message); + if (decoratedMessage == null) { + wrapper.cancel(); + return; + } + + translatableRewriter.processText(decoratedMessage); + wrapper.write(Type.COMPONENT, decoratedMessage); + wrapper.write(Type.VAR_INT, SYSTEM_CHAT_ID); }); - registerClientbound(ClientboundPackets1_19_1.SYSTEM_CHAT, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - final JsonElement content = wrapper.passthrough(Type.COMPONENT); - translatableRewriter.processText(content); + registerClientbound(ClientboundPackets1_19_1.SYSTEM_CHAT, wrapper -> { + final JsonElement content = wrapper.passthrough(Type.COMPONENT); + translatableRewriter.processText(content); - final boolean overlay = wrapper.read(Type.BOOLEAN); - wrapper.write(Type.VAR_INT, overlay ? GAME_INFO_ID : SYSTEM_CHAT_ID); - }); - } + final boolean overlay = wrapper.read(Type.BOOLEAN); + wrapper.write(Type.VAR_INT, overlay ? GAME_INFO_ID : SYSTEM_CHAT_ID); }); - registerServerbound(ServerboundPackets1_19.CHAT_MESSAGE, new PacketRemapper() { + registerServerbound(ServerboundPackets1_19.CHAT_MESSAGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // Message map(Type.LONG); // Timestamp map(Type.LONG); // Salt @@ -216,9 +206,9 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { final ProfileKey profileKey = wrapper.read(Type.OPTIONAL_PROFILE_KEY); @@ -268,9 +258,9 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { if (wrapper.user().get(NonceStorage.class) != null) { @@ -285,9 +275,9 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { final NonceStorage nonceStorage = wrapper.user().remove(NonceStorage.class); @@ -310,9 +300,9 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/NonceStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/NonceStorage.java index cbf08d7f..1801d5ee 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/NonceStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/NonceStorage.java @@ -24,7 +24,7 @@ public final class NonceStorage implements StorableObject { private final byte[] nonce; - public NonceStorage(final byte @Nullable[] nonce) { + public NonceStorage(final byte @Nullable [] nonce) { this.nonce = nonce; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/ReceivedMessagesStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/ReceivedMessagesStorage.java index 36b72534..60e6690e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/ReceivedMessagesStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/storage/ReceivedMessagesStorage.java @@ -19,7 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage; import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; - import java.util.Arrays; public final class ReceivedMessagesStorage implements StorableObject { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java index 41176838..903131e8 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java @@ -26,7 +26,7 @@ import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.packets.EntityPa import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; @@ -54,9 +54,9 @@ public class Protocol1_9_4To1_10 extends BackwardsProtocol soundRewriter = new SoundRewriter<>(this); - registerClientbound(ClientboundPackets1_9_3.NAMED_SOUND, new PacketRemapper() { + registerClientbound(ClientboundPackets1_9_3.NAMED_SOUND, new PacketHandlers() { @Override - public void registerMap() { + public void register() { map(Type.STRING); // 0 - Sound name map(Type.VAR_INT); // 1 - Sound Category map(Type.INT); // 2 - x @@ -67,9 +67,9 @@ public class Protocol1_9_4To1_10 extends BackwardsProtocol { + if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) { + wrapper.passthrough(Type.INT); // Passthrough Window ID - int size = wrapper.passthrough(Type.UNSIGNED_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 + int size = wrapper.passthrough(Type.UNSIGNED_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 + 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 } } }); @@ -80,54 +75,40 @@ public class BlockItemPackets1_10 extends LegacyBlockItemRewriter { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); - Chunk chunk = wrapper.passthrough(type); + Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); + Chunk chunk = wrapper.passthrough(type); - handleChunk(chunk); - } - }); - } + handleChunk(chunk); }); // Block Change Packet - protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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)); - } + handler(wrapper -> { + int idx = wrapper.get(Type.VAR_INT, 0); + wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); }); } }); // Multi Block Change Packet - protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketRemapper() { + protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketHandlers() { @Override - public void registerMap() { + public void register() { 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 { - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - record.setBlockId(handleBlockID(record.getBlockId())); - } + handler(wrapper -> { + for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { + record.setBlockId(handleBlockID(record.getBlockId())); } }); } @@ -140,9 +121,9 @@ public class BlockItemPackets1_10 extends LegacyBlockItemRewriter block_dust - } + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + if (id == 46) { // new falling_dust + wrapper.set(Type.INT, 0, 38); // -> block_dust } }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java index e2c9bb7e..93f12ffe 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java @@ -29,13 +29,10 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; - import java.util.List; import java.util.Optional; @@ -47,9 +44,9 @@ public class EntityPackets1_10 extends LegacyEntityRewriter Entity1_11Types.ObjectType.findById(id).orElse(null))); // Handle FallingBlock blocks - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); - if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { - int objectData = wrapper.get(Type.INT, 0); - int objType = objectData & 4095; - int data = objectData >> 12 & 15; + handler(wrapper -> { + Optional type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) { + int objectData = wrapper.get(Type.INT, 0); + int objType = objectData & 4095; + int data = objectData >> 12 & 15; - Block block = protocol.getItemRewriter().handleBlock(objType, data); - if (block == null) - return; + Block block = protocol.getItemRewriter().handleBlock(objType, data); + if (block == null) + return; - wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); - } + wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); } }); } @@ -88,9 +82,9 @@ public class EntityPackets1_10 extends LegacyEntityRewriter { + int entityId = wrapper.get(Type.VAR_INT, 0); + EntityType type = tracker(wrapper.user()).entityType(entityId); - List metadata = wrapper.get(Types1_9.METADATA_LIST, 0); - handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); + List metadata = wrapper.get(Types1_9.METADATA_LIST, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); - EntityData entityData = entityDataForType(type); - if (entityData != null) { - WrappedMetadata storage = new WrappedMetadata(metadata); - wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId()); - if (entityData.hasBaseMeta()) - entityData.defaultMeta().createMeta(storage); - } + EntityData entityData = entityDataForType(type); + if (entityData != null) { + WrappedMetadata storage = new WrappedMetadata(metadata); + wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId()); + if (entityData.hasBaseMeta()) + entityData.defaultMeta().createMeta(storage); } }); @@ -135,9 +126,9 @@ public class EntityPackets1_10 extends LegacyEntityRewriter