Move protocol templates from wiki to extra module

This commit is contained in:
Nassim Jahnke 2023-10-16 22:30:12 +10:00
parent 667278f9e6
commit d7d7940d0b
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
8 changed files with 353 additions and 3 deletions

View File

@ -23,6 +23,7 @@
package com.viaversion.viaversion.api.protocol; package com.viaversion.viaversion.api.protocol;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.platform.providers.ViaProviders;
@ -58,6 +59,16 @@ public interface Protocol<CU extends ClientboundPacketType, CM extends Clientbou
registerServerbound(state, unmappedPacketId, mappedPacketId, handler, false); registerServerbound(state, unmappedPacketId, mappedPacketId, handler, false);
} }
default void registerClientbound(State state, ClientboundPacketType packetType, PacketHandler handler) {
Preconditions.checkArgument(packetType.state() == state);
registerClientbound(state, packetType.getId(), packetType.getId(), handler, false);
}
default void registerServerbound(State state, ServerboundPacketType packetType, PacketHandler handler) {
Preconditions.checkArgument(packetType.state() == state);
registerServerbound(state, packetType.getId(), packetType.getId(), handler, false);
}
/** /**
* Registers a serverbound packet, with id transformation and remapper. * Registers a serverbound packet, with id transformation and remapper.
* *

View File

@ -92,7 +92,7 @@ public class ConfigurationState implements StorableObject {
@Override @Override
public boolean clearOnServerSwitch() { public boolean clearOnServerSwitch() {
return false; // This might be bad return false;
} }
@Override @Override

View File

@ -312,6 +312,14 @@ public abstract class ItemRewriter<C extends ClientboundPacketType, S extends Se
} }
public void registerAdvancements1_20_2(C packetType) { public void registerAdvancements1_20_2(C packetType) {
registerAdvancements1_20_2(packetType, Type.COMPONENT);
}
public void registerAdvancements1_20_3(C packetType) {
registerAdvancements1_20_2(packetType, Type.TAG);
}
private void registerAdvancements1_20_2(C packetType, Type<?> componentType) {
protocol.registerClientbound(packetType, wrapper -> { protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Type.BOOLEAN); // Reset/clear wrapper.passthrough(Type.BOOLEAN); // Reset/clear
int size = wrapper.passthrough(Type.VAR_INT); // Mapping size int size = wrapper.passthrough(Type.VAR_INT); // Mapping size
@ -325,8 +333,8 @@ public abstract class ItemRewriter<C extends ClientboundPacketType, S extends Se
// Display data // Display data
if (wrapper.passthrough(Type.BOOLEAN)) { if (wrapper.passthrough(Type.BOOLEAN)) {
wrapper.passthrough(Type.COMPONENT); // Title wrapper.passthrough(componentType); // Title
wrapper.passthrough(Type.COMPONENT); // Description wrapper.passthrough(componentType); // Description
handleItemToClient(wrapper.passthrough(itemType)); // Icon handleItemToClient(wrapper.passthrough(itemType)); // Icon
wrapper.passthrough(Type.VAR_INT); // Frame type wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags int flags = wrapper.passthrough(Type.INT); // Flags

View File

@ -39,6 +39,7 @@ setupViaSubproject("bungee")
setupViaSubproject("velocity") setupViaSubproject("velocity")
setupViaSubproject("sponge") setupViaSubproject("sponge")
setupViaSubproject("fabric") setupViaSubproject("fabric")
setupViaSubproject("template")
setupSubproject("viaversion") { setupSubproject("viaversion") {
projectDir = file("universal") projectDir = file("universal")

View File

@ -0,0 +1,3 @@
dependencies {
implementation(projects.viaversionCommon)
}

View File

@ -0,0 +1,138 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
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<ClientboundPackets1_20_2, ClientboundPackets1_20_2, ServerboundPackets1_20_2, ServerboundPackets1_20_2> {
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<ClientboundPackets1_20_2> tagRewriter = new TagRewriter<>(this);
tagRewriter.registerGeneric(ClientboundPackets1_20_2.TAGS);
// Registers sound id changes
final SoundRewriter<ClientboundPackets1_20_2> 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<ClientboundPackets1_20_2> commandRewriter = new CommandRewriter1_z<ClientboundPackets1_20_2>(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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<ClientboundPackets1_20_2, ServerboundPackets1_20_2, Protocol1_99To_98> {
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<ClientboundPackets1_20_2> 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<ClientboundPackets1_20_2>(this) {}.register(ClientboundPackets1_20_2.DECLARE_RECIPES);
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<ClientboundPackets1_20_2, Protocol1_99To_98> {
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);
}
}