ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java

182 lines
8.8 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
2024-01-01 12:39:45 +01:00
* Copyright (C) 2016-2024 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.protocols.protocol1_14to1_13_2;
2018-10-24 19:05:14 +02:00
2021-04-26 21:35:29 +02:00
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.type.Type;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_13_2;
import com.viaversion.viaversion.api.type.types.version.Types1_14;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.ComponentRewriter1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.MappingData;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.PlayerPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.WorldPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
2023-10-19 13:03:00 +02:00
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.rewriter.CommandRewriter;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import org.checkerframework.checker.nullness.qual.Nullable;
2018-10-24 19:05:14 +02:00
public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_13, ClientboundPackets1_14, ServerboundPackets1_13, ServerboundPackets1_14> {
2018-11-08 13:43:38 +01:00
2020-08-16 16:24:06 +02:00
public static final MappingData MAPPINGS = new MappingData();
private final MetadataRewriter1_14To1_13_2 metadataRewriter = new MetadataRewriter1_14To1_13_2(this);
private final InventoryPackets itemRewriter = new InventoryPackets(this);
2020-08-16 16:24:06 +02:00
public Protocol1_14To1_13_2() {
2020-08-16 16:24:06 +02:00
super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class);
2018-11-08 13:43:38 +01:00
}
2018-10-27 13:25:42 +02:00
@Override
protected void registerPackets() {
super.registerPackets();
2019-05-09 14:43:48 +02:00
2018-10-27 13:25:42 +02:00
EntityPackets.register(this);
WorldPackets.register(this);
PlayerPackets.register(this);
2018-10-24 19:05:14 +02:00
new SoundRewriter<>(this).registerSound(ClientboundPackets1_13.SOUND);
new StatisticsRewriter<>(this).register(ClientboundPackets1_13.STATISTICS);
2019-04-05 18:48:38 +02:00
ComponentRewriter<ClientboundPackets1_13> componentRewriter = new ComponentRewriter1_14<>(this);
2021-11-21 11:12:13 +01:00
componentRewriter.registerComponentPacket(ClientboundPackets1_13.CHAT_MESSAGE);
CommandRewriter<ClientboundPackets1_13> commandRewriter = new CommandRewriter<ClientboundPackets1_13>(this) {
@Override
public @Nullable String handleArgumentType(String argumentType) {
if (argumentType.equals("minecraft:nbt")) {
return "minecraft:nbt_compound_tag";
}
return super.handleArgumentType(argumentType);
}
};
commandRewriter.registerDeclareCommands(ClientboundPackets1_13.DECLARE_COMMANDS);
registerClientbound(ClientboundPackets1_13.TAGS, wrapper -> {
int blockTagsSize = wrapper.read(Type.VAR_INT);
wrapper.write(Type.VAR_INT, blockTagsSize + 6); // block tags
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++) {
2023-03-03 22:00:00 +01:00
blockIds[j] = MAPPINGS.getNewBlockId(blockIds[j]);
}
}
// Minecraft crashes if we not send signs tags
wrapper.write(Type.STRING, "minecraft:signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{
2023-03-03 22:00:00 +01:00
MAPPINGS.getNewBlockId(150), MAPPINGS.getNewBlockId(155)
});
wrapper.write(Type.STRING, "minecraft:wall_signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{
2023-03-03 22:00:00 +01:00
MAPPINGS.getNewBlockId(155)
});
wrapper.write(Type.STRING, "minecraft:standing_signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{
2023-03-03 22:00:00 +01:00
MAPPINGS.getNewBlockId(150)
});
// Fences and walls tags - used for block connections
wrapper.write(Type.STRING, "minecraft:fences");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{189, 248, 472, 473, 474, 475});
wrapper.write(Type.STRING, "minecraft:walls");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{271, 272});
wrapper.write(Type.STRING, "minecraft:wooden_fences");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{189, 472, 473, 474, 475});
int itemTagsSize = wrapper.read(Type.VAR_INT);
wrapper.write(Type.VAR_INT, itemTagsSize + 2); // item tags
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++) {
2023-03-03 22:00:00 +01:00
itemIds[j] = MAPPINGS.getNewItemId(itemIds[j]);
}
}
// Should fix fuel shift clicking
wrapper.write(Type.STRING, "minecraft:signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{
2023-03-03 22:00:00 +01:00
MAPPINGS.getNewItemId(541)
});
// Arrows tag (used by bow)
wrapper.write(Type.STRING, "minecraft:arrows");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{526, 825, 826});
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);
2018-10-27 13:25:42 +02:00
}
wrapper.write(Type.VAR_INT, 0); // new entity tags - do we need to send this?
2018-10-27 13:25:42 +02:00
});
2019-03-13 17:53:17 +01:00
// Set Difficulty packet added in 19w11a
cancelServerbound(ServerboundPackets1_14.SET_DIFFICULTY);
// Lock Difficulty packet added in 19w11a
cancelServerbound(ServerboundPackets1_14.LOCK_DIFFICULTY);
// Unknown packet added in 19w13a
cancelServerbound(ServerboundPackets1_14.UPDATE_JIGSAW_BLOCK);
2018-10-27 13:25:42 +02:00
}
2018-10-24 19:05:14 +02:00
@Override
2020-08-16 16:24:06 +02:00
protected void onMappingDataLoaded() {
2023-03-03 22:00:00 +01:00
WorldPackets.air = MAPPINGS.getBlockStateMappings().getNewId(0);
WorldPackets.voidAir = MAPPINGS.getBlockStateMappings().getNewId(8591);
WorldPackets.caveAir = MAPPINGS.getBlockStateMappings().getNewId(8592);
Types1_13_2.PARTICLE.filler(this, false)
.reader("block", ParticleType.Readers.BLOCK)
.reader("dust", ParticleType.Readers.DUST)
.reader("falling_dust", ParticleType.Readers.BLOCK)
2023-11-09 06:35:07 +01:00
.reader("item", ParticleType.Readers.ITEM1_13_2);
Types1_14.PARTICLE.filler(this)
.reader("block", ParticleType.Readers.BLOCK)
.reader("dust", ParticleType.Readers.DUST)
.reader("falling_dust", ParticleType.Readers.BLOCK)
2023-11-09 06:35:07 +01:00
.reader("item", ParticleType.Readers.ITEM1_13_2);
2018-10-27 13:25:42 +02:00
}
2018-10-24 19:05:14 +02:00
2018-10-27 13:25:42 +02:00
@Override
public void init(UserConnection userConnection) {
Refactor entity tracking and meta handling This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs. ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes. ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries. This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers). All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
userConnection.addEntityTracker(this.getClass(), new EntityTracker1_14(userConnection));
if (!userConnection.has(ClientWorld.class)) {
userConnection.put(new ClientWorld());
}
2018-10-27 13:25:42 +02:00
}
2020-08-16 16:24:06 +02:00
@Override
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public MetadataRewriter1_14To1_13_2 getEntityRewriter() {
return metadataRewriter;
}
@Override
public InventoryPackets getItemRewriter() {
return itemRewriter;
}
2018-10-24 19:05:14 +02:00
}