From d7d7940d0b12edcfed574eb904e60f5d21d0e589 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Mon, 16 Oct 2023 22:30:12 +1000 Subject: [PATCH] Move protocol templates from wiki to extra module --- .../viaversion/api/protocol/Protocol.java | 11 ++ .../storage/ConfigurationState.java | 2 +- .../viaversion/rewriter/ItemRewriter.java | 12 +- settings.gradle.kts | 1 + template/build.gradle.kts | 3 + .../template/protocols/Protocol1_99To_98.java | 138 ++++++++++++++++++ .../rewriter/BlockItemPacketRewriter1_99.java | 69 +++++++++ .../rewriter/EntityPacketRewriter1_99.java | 120 +++++++++++++++ 8 files changed, 353 insertions(+), 3 deletions(-) create mode 100644 template/build.gradle.kts create mode 100644 template/src/main/java/com/viaversion/viaversion/template/protocols/Protocol1_99To_98.java create mode 100644 template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java create mode 100644 template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java index 18de97e1d..bd0eeaa57 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java @@ -23,6 +23,7 @@ package com.viaversion.viaversion.api.protocol; import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.platform.providers.ViaProviders; @@ -58,6 +59,16 @@ public interface Protocol componentType) { protocol.registerClientbound(packetType, wrapper -> { wrapper.passthrough(Type.BOOLEAN); // Reset/clear int size = wrapper.passthrough(Type.VAR_INT); // Mapping size @@ -325,8 +333,8 @@ public abstract class ItemRewriter. + */ +package com.viaversion.viaversion.template.protocols; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; +import com.viaversion.viaversion.api.protocol.AbstractProtocol; +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2; +import com.viaversion.viaversion.rewriter.SoundRewriter; +import com.viaversion.viaversion.rewriter.StatisticsRewriter; +import com.viaversion.viaversion.rewriter.TagRewriter; +import com.viaversion.viaversion.template.protocols.rewriter.EntityPacketRewriter1_99; +import com.viaversion.viaversion.template.protocols.rewriter.BlockItemPacketRewriter1_99; + +// Placeholders to replace (in the entire package): +// Protocol1_99To_98, EntityPacketRewriter1_99, BlockItemPacketRewriter1_99 +// ClientboundPackets1_20_2 +// ServerboundPackets1_20_2 +// ClientboundConfigurationPackets1_20_2 +// ServerboundConfigurationPackets1_20_2 +// Entity1_19_4Types (MAPPED type) +// 1.99, 1.98 +public final class Protocol1_99To_98 extends AbstractProtocol { + + public static final MappingData MAPPINGS = new MappingDataBase("1.99", "1.98"); + private final EntityPacketRewriter1_99 entityRewriter = new EntityPacketRewriter1_99(this); + private final BlockItemPacketRewriter1_99 itemRewriter = new BlockItemPacketRewriter1_99(this); + + public Protocol1_99To_98() { + // Passing the class types into the super constructor is needed for automatic packet type id remapping, but can otherwise be omitted + super(ClientboundPackets1_20_2.class, ClientboundPackets1_20_2.class, ServerboundPackets1_20_2.class, ServerboundPackets1_20_2.class); + } + + @Override + protected void registerPackets() { + super.registerPackets(); + + // Registers renames etc. as well as registry type id changes + final TagRewriter tagRewriter = new TagRewriter<>(this); + tagRewriter.registerGeneric(ClientboundPackets1_20_2.TAGS); + + // Registers sound id changes + final SoundRewriter soundRewriter = new SoundRewriter<>(this); + soundRewriter.register1_19_3Sound(ClientboundPackets1_20_2.SOUND); + soundRewriter.registerSound(ClientboundPackets1_20_2.ENTITY_SOUND); + + // Registers registry type id changes as well as stat id changes if also included in the json mappings + new StatisticsRewriter<>(this).register(ClientboundPackets1_20_2.STATISTICS); + + // Uncomment if an existing type changed serialization format. Mappings for argument type keys can also be defined in mapping files + /*final CommandRewriter1_z commandRewriter = new CommandRewriter1_z(this) { + @Override + public void handleArgument(final PacketWrapper wrapper, final String argumentType) throws Exception { + if (argumentType.equals("minecraft:abc")) { + // New argument + wrapper.write(Type.INT, 0); + } else { + super.handleArgument(wrapper, argumentType); + } + } + }.registerDeclareCommands1_19(ClientboundPackets1_20_2.DECLARE_COMMANDS);*/ + } + + @Override + protected void onMappingDataLoaded() { + super.onMappingDataLoaded(); // Calls load methods on rewriters + + // Uncomment this if the entity types enum has been newly added specificly for this Protocol + // Entity1_19_4Types.initialize(this); + + // Uncomment if a new particle was added = ids shifted; requires a new Types_ class copied from the last + /*Types1_19_4.PARTICLE.filler(this) + .reader("block", ParticleType.Readers.BLOCK) + .reader("block_marker", ParticleType.Readers.BLOCK) + .reader("dust", ParticleType.Readers.DUST) + .reader("falling_dust", ParticleType.Readers.BLOCK) + .reader("dust_color_transition", ParticleType.Readers.DUST_TRANSITION) + .reader("item", ParticleType.Readers.VAR_INT_ITEM) + .reader("vibration", ParticleType.Readers.VIBRATION) + .reader("sculk_charge", ParticleType.Readers.SCULK_CHARGE) + .reader("shriek", ParticleType.Readers.SHRIEK);*/ + } + + @Override + public void init(final UserConnection user) { + // Register the entity tracker - used for entity id/metadata rewriting AND for tracking world data sent to the client (then used for chunk data rewriting) + addEntityTracker(user, new EntityTrackerBase(user, Entity1_19_4Types.PLAYER)); + } + + // Overriding these three methods is important as they are relied on various rewriter classes + @Override + public MappingData getMappingData() { + return MAPPINGS; + } + + @Override + public EntityPacketRewriter1_99 getEntityRewriter() { + return entityRewriter; + } + + @Override + public BlockItemPacketRewriter1_99 getItemRewriter() { + return itemRewriter; + } + + @Override + protected ClientboundPacketType clientboundFinishConfigurationPacket() { + return ClientboundConfigurationPackets1_20_2.FINISH_CONFIGURATION; + } + + @Override + protected ServerboundPacketType serverboundFinishConfigurationPacket() { + return ServerboundConfigurationPackets1_20_2.FINISH_CONFIGURATION; + } +} \ No newline at end of file diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java new file mode 100644 index 000000000..377539fc9 --- /dev/null +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java @@ -0,0 +1,69 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2023 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.template.protocols.rewriter; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.RecipeRewriter1_20_2; +import com.viaversion.viaversion.rewriter.BlockRewriter; +import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.template.protocols.Protocol1_99To_98; + +// To replace if needed: +// Chunk1_18Type +// RecipeRewriter1_20_2 +public final class BlockItemPacketRewriter1_99 extends ItemRewriter { + + public BlockItemPacketRewriter1_99(final Protocol1_99To_98 protocol) { + super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_VAR_INT_ARRAY); + } + + @Override + public void registerPackets() { + // Register block and block state id changes + // Other places using block state id mappings: Spawn particle, entity metadata, entity spawn (falling blocks) + // Tags and statistics use block (!) ids + final BlockRewriter blockRewriter = new BlockRewriter<>(protocol, Type.POSITION1_14); + blockRewriter.registerBlockAction(ClientboundPackets1_20_2.BLOCK_ACTION); + blockRewriter.registerBlockChange(ClientboundPackets1_20_2.BLOCK_CHANGE); + blockRewriter.registerVarLongMultiBlockChange1_20(ClientboundPackets1_20_2.MULTI_BLOCK_CHANGE); + blockRewriter.registerEffect(ClientboundPackets1_20_2.EFFECT, 1010, 2001); + blockRewriter.registerChunkData1_19(ClientboundPackets1_20_2.CHUNK_DATA, Chunk1_18Type::new); + blockRewriter.registerBlockEntityData(ClientboundPackets1_20_2.BLOCK_ENTITY_DATA); + + // Registers item id changes + // Other places using item ids are: Entity metadata, tags, statistics, effect + registerSetCooldown(ClientboundPackets1_20_2.COOLDOWN); + registerWindowItems1_17_1(ClientboundPackets1_20_2.WINDOW_ITEMS); + registerSetSlot1_17_1(ClientboundPackets1_20_2.SET_SLOT); + registerAdvancements1_20_3(ClientboundPackets1_20_2.ADVANCEMENTS); + registerEntityEquipmentArray(ClientboundPackets1_20_2.ENTITY_EQUIPMENT); + registerClickWindow1_17_1(ServerboundPackets1_20_2.CLICK_WINDOW); + registerTradeList1_19(ClientboundPackets1_20_2.TRADE_LIST); + registerCreativeInvAction(ServerboundPackets1_20_2.CREATIVE_INVENTORY_ACTION, Type.ITEM1_20_2); + registerWindowPropertyEnchantmentHandler(ClientboundPackets1_20_2.WINDOW_PROPERTY); + registerSpawnParticle1_19(ClientboundPackets1_20_2.SPAWN_PARTICLE); + + new RecipeRewriter1_20_2<>(protocol).register(ClientboundPackets1_20_2.DECLARE_RECIPES); + // OR do this if serialization of recipes changed and override the relevant method + // Add new serializers to RecipeRewriter, or extend the last one for changes + // new RecipeRewriter1_20_2(this) {}.register(ClientboundPackets1_20_2.DECLARE_RECIPES); + } +} \ No newline at end of file diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java new file mode 100644 index 000000000..44b846edd --- /dev/null +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java @@ -0,0 +1,120 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2023 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.template.protocols.rewriter; + +import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.version.Types1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.rewriter.EntityRewriter; +import com.viaversion.viaversion.template.protocols.Protocol1_99To_98; + +// Replace if needed +// Types1_OLD +// Types1_20_3 +public final class EntityPacketRewriter1_99 extends EntityRewriter { + + public EntityPacketRewriter1_99(final Protocol1_99To_98 protocol) { + super(protocol); + } + + @Override + public void registerPackets() { + // Tracks entities, applies metadata rewrites registered below, untracks entities + registerTrackerWithData1_19(ClientboundPackets1_20_2.SPAWN_ENTITY, Entity1_19_4Types.FALLING_BLOCK); + registerMetadataRewriter(ClientboundPackets1_20_2.ENTITY_METADATA, /*Types1_OLD.METADATA_LIST, */Types1_20_3.METADATA_LIST); // Specify old and new metadata list if changed + registerRemoveEntities(ClientboundPackets1_20_2.REMOVE_ENTITIES); + + protocol.registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.REGISTRY_DATA, new PacketHandlers() { + @Override + protected void register() { + map(Type.NAMED_COMPOUND_TAG); // Registry data + handler(dimensionDataHandler()); // Caches dimensions to access data like height later + handler(biomeSizeTracker()); // Tracks the amount of biomes sent for chunk data + } + }); + + protocol.registerClientbound(ClientboundPackets1_20_2.JOIN_GAME, new PacketHandlers() { + @Override + public void register() { + map(Type.INT); // Entity id + map(Type.BOOLEAN); // Hardcore + map(Type.STRING_ARRAY); // World List + map(Type.VAR_INT); // Max players + map(Type.VAR_INT); // View distance + map(Type.VAR_INT); // Simulation distance + map(Type.BOOLEAN); // Reduced debug info + map(Type.BOOLEAN); // Show death screen + map(Type.BOOLEAN); // Limited crafting + map(Type.STRING); // Dimension key + map(Type.STRING); // World + handler(worldDataTrackerHandlerByKey()); // Tracks world height and name for chunk data and entity (un)tracking + } + }); + + protocol.registerClientbound(ClientboundPackets1_20_2.RESPAWN, new PacketHandlers() { + @Override + public void register() { + map(Type.STRING); // Dimension + map(Type.STRING); // World + handler(worldDataTrackerHandlerByKey()); // Tracks world height and name for chunk data and entity (un)tracking + } + }); + } + + @Override + protected void registerRewrites() { + /* Uncomment if metatype classes changed + filter().handler((event, meta) -> { + int id = meta.metaType().typeId(); + if (id >= SomeAddedIndex) { + id++; + } + meta.setMetaType(Types1_20_3.META_TYPES.byId(id)); + });*/ + + // Registers registry type id changes + registerMetaTypeHandler( + Types1_20_3.META_TYPES.itemType, + Types1_20_3.META_TYPES.blockStateType, + Types1_20_3.META_TYPES.optionalBlockStateType, + Types1_20_3.META_TYPES.particleType + ); + + // Minecarts are special + filter().filterFamily(Entity1_19_4Types.MINECART_ABSTRACT).index(11).handler((event, meta) -> { + final int blockState = meta.value(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(blockState)); + }); + } + + @Override + public void onMappingDataLoaded() { + // IF ENTITY TYPES CHANGED: Automatically map entity id changes AFTER entity ids have been loaded + // mapTypes(); + } + + @Override + public EntityType typeFromId(final int type) { + return Entity1_19_4Types.getTypeFromId(type); + } +} \ No newline at end of file