ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java

302 lines
15 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_16to1_15_2;
2020-02-05 20:09:06 +01:00
import com.google.common.base.Joiner;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
2021-04-26 21:35:29 +02:00
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.RegistryType;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_16;
2023-05-03 09:17:51 +02:00
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
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_16;
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
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets;
2023-05-03 09:17:51 +02:00
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.provider.PlayerAbilitiesProvider;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.GsonUtil;
import com.viaversion.viaversion.util.Key;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
2020-03-18 19:15:58 +01:00
import java.util.UUID;
public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_15, ClientboundPackets1_16, ServerboundPackets1_14, ServerboundPackets1_16> {
2020-02-05 20:09:06 +01:00
private static final UUID ZERO_UUID = new UUID(0, 0);
public static final MappingData MAPPINGS = new MappingDataBase("1.15", "1.16");
private final MetadataRewriter1_16To1_15_2 metadataRewriter = new MetadataRewriter1_16To1_15_2(this);
private final InventoryPackets itemRewriter = new InventoryPackets(this);
private final TranslationMappings componentRewriter = new TranslationMappings(this);
private final TagRewriter<ClientboundPackets1_15> tagRewriter = new TagRewriter<>(this);
2020-04-03 19:57:07 +02:00
public Protocol1_16To1_15_2() {
2020-08-16 16:24:06 +02:00
super(ClientboundPackets1_15.class, ClientboundPackets1_16.class, ServerboundPackets1_14.class, ServerboundPackets1_16.class);
}
2020-02-05 20:09:06 +01:00
@Override
protected void registerPackets() {
super.registerPackets();
2020-02-05 20:09:06 +01:00
EntityPackets.register(this);
WorldPackets.register(this);
tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
new StatisticsRewriter<>(this).register(ClientboundPackets1_15.STATISTICS);
2020-03-18 19:15:58 +01:00
// Login Success
registerClientbound(State.LOGIN, 0x02, 0x02, wrapper -> {
// Transform string to a uuid
UUID uuid = UUID.fromString(wrapper.read(Type.STRING));
wrapper.write(Type.UUID, uuid);
2020-03-18 19:15:58 +01:00
});
// Motd Status - line breaks are no longer allowed for player samples
registerClientbound(State.STATUS, 0x00, 0x00, wrapper -> {
String original = wrapper.passthrough(Type.STRING);
JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class);
JsonObject players = object.getAsJsonObject("players");
if (players == null) return;
JsonArray sample = players.getAsJsonArray("sample");
if (sample == null) return;
JsonArray splitSamples = new JsonArray();
for (JsonElement element : sample) {
JsonObject playerInfo = element.getAsJsonObject();
String name = playerInfo.getAsJsonPrimitive("name").getAsString();
if (name.indexOf('\n') == -1) {
splitSamples.add(playerInfo);
continue;
}
String id = playerInfo.getAsJsonPrimitive("id").getAsString();
for (String s : name.split("\n")) {
JsonObject newSample = new JsonObject();
newSample.addProperty("name", s);
newSample.addProperty("id", id);
splitSamples.add(newSample);
}
}
// Replace data if changed
if (splitSamples.size() != sample.size()) {
players.add("sample", splitSamples);
wrapper.set(Type.STRING, 0, object.toString());
}
});
// Handle (relevant) component cases for translatable and score changes
registerClientbound(ClientboundPackets1_15.CHAT_MESSAGE, new PacketHandlers() {
2020-05-20 16:44:12 +02:00
@Override
public void register() {
map(Type.COMPONENT);
2020-05-20 16:44:12 +02:00
map(Type.BYTE);
handler(wrapper -> {
componentRewriter.processText(wrapper.get(Type.COMPONENT, 0));
wrapper.write(Type.UUID, ZERO_UUID); // Sender uuid - always send as 'system'
});
2020-05-20 16:44:12 +02:00
}
});
componentRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
componentRewriter.registerTitle(ClientboundPackets1_15.TITLE);
componentRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
2020-05-20 16:44:12 +02:00
SoundRewriter<ClientboundPackets1_15> soundRewriter = new SoundRewriter<>(this);
soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
2020-02-05 20:09:06 +01:00
registerServerbound(ServerboundPackets1_16.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);
}
2020-06-12 17:18:06 +02:00
wrapper.passthrough(Type.VAR_INT); // Hand
2020-06-10 19:34:22 +02:00
}
// New boolean: Whether the client is sneaking/pressing shift
wrapper.read(Type.BOOLEAN);
2020-06-10 19:34:22 +02:00
});
if (Via.getConfig().isIgnoreLong1_16ChannelNames()) {
registerServerbound(ServerboundPackets1_16.PLUGIN_MESSAGE, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING); // Channel
handlerSoftFail(wrapper -> {
final String channel = wrapper.get(Type.STRING, 0);
final String namespacedChannel = Key.namespaced(channel);
if (channel.length() > 32) {
2020-06-30 13:51:06 +02:00
if (!Via.getConfig().isSuppressConversionWarnings()) {
Via.getPlatform().getLogger().warning("Ignoring serverbound plugin channel, as it is longer than 32 characters: " + channel);
2020-06-30 13:51:06 +02:00
}
wrapper.cancel();
} else if (namespacedChannel.equals("minecraft:register") || namespacedChannel.equals("minecraft:unregister")) {
String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0");
List<String> checkedChannels = new ArrayList<>(channels.length);
for (String registeredChannel : channels) {
if (registeredChannel.length() > 32) {
if (!Via.getConfig().isSuppressConversionWarnings()) {
Via.getPlatform().getLogger().warning("Ignoring serverbound plugin channel register of '"
+ registeredChannel + "', as it is longer than 32 characters");
}
continue;
}
checkedChannels.add(registeredChannel);
}
if (checkedChannels.isEmpty()) {
wrapper.cancel();
return;
}
wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(checkedChannels).getBytes(StandardCharsets.UTF_8));
}
});
}
});
}
registerServerbound(ServerboundPackets1_16.PLAYER_ABILITIES, wrapper -> {
2023-05-03 09:17:51 +02:00
wrapper.passthrough(Type.BYTE); // Flags
final PlayerAbilitiesProvider playerAbilities = Via.getManager().getProviders().get(PlayerAbilitiesProvider.class);
2023-05-03 15:21:24 +02:00
wrapper.write(Type.FLOAT, playerAbilities.getFlyingSpeed(wrapper.user()));
wrapper.write(Type.FLOAT, playerAbilities.getWalkingSpeed(wrapper.user()));
2020-06-11 19:30:52 +02:00
});
cancelServerbound(ServerboundPackets1_16.GENERATE_JIGSAW);
cancelServerbound(ServerboundPackets1_16.UPDATE_JIGSAW_BLOCK);
2020-02-05 20:09:06 +01:00
}
@Override
2020-08-16 16:24:06 +02:00
protected void onMappingDataLoaded() {
super.onMappingDataLoaded();
2020-05-12 22:51:02 +02:00
int[] wallPostOverrideTag = new int[47];
int arrayIndex = 0;
wallPostOverrideTag[arrayIndex++] = 140;
wallPostOverrideTag[arrayIndex++] = 179;
wallPostOverrideTag[arrayIndex++] = 264;
for (int i = 153; i <= 158; i++) {
wallPostOverrideTag[arrayIndex++] = i;
}
for (int i = 163; i <= 168; i++) {
wallPostOverrideTag[arrayIndex++] = i;
}
for (int i = 408; i <= 439; i++) {
wallPostOverrideTag[arrayIndex++] = i;
}
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:wall_post_override", wallPostOverrideTag);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:beacon_base_blocks", 133, 134, 148, 265);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:climbable", 160, 241, 658);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:fire", 142);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:campfires", 679);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:fence_gates", 242, 467, 468, 469, 470, 471);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:unstable_bottom_center", 242, 467, 468, 469, 470, 471);
tagRewriter.addTag(RegistryType.BLOCK, "minecraft:wooden_trapdoors", 193, 194, 195, 196, 197, 198);
tagRewriter.addTag(RegistryType.ITEM, "minecraft:wooden_trapdoors", 215, 216, 217, 218, 219, 220);
tagRewriter.addTag(RegistryType.ITEM, "minecraft:beacon_payment_items", 529, 530, 531, 760);
tagRewriter.addTag(RegistryType.ENTITY, "minecraft:impact_projectiles", 2, 72, 71, 37, 69, 79, 83, 15, 93);
2020-06-23 17:30:37 +02:00
2020-04-03 19:57:07 +02:00
// The client crashes if we don't send all tags it may use
tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:guarded_by_piglins");
tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:soul_speed_blocks");
tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:soul_fire_base_blocks");
tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:non_flammable_wood");
tagRewriter.addEmptyTag(RegistryType.ITEM, "minecraft:non_flammable_wood");
// The rest of not accessed tags added in older versions; #1830
tagRewriter.addEmptyTags(RegistryType.BLOCK, "minecraft:bamboo_plantable_on", "minecraft:beds", "minecraft:bee_growables",
"minecraft:beehives", "minecraft:coral_plants", "minecraft:crops", "minecraft:dragon_immune", "minecraft:flowers",
"minecraft:portals", "minecraft:shulker_boxes", "minecraft:small_flowers", "minecraft:tall_flowers", "minecraft:trapdoors",
"minecraft:underwater_bonemeals", "minecraft:wither_immune", "minecraft:wooden_fences", "minecraft:wooden_trapdoors");
tagRewriter.addEmptyTags(RegistryType.ENTITY, "minecraft:arrows", "minecraft:beehive_inhabitors", "minecraft:raiders", "minecraft:skeletons");
tagRewriter.addEmptyTags(RegistryType.ITEM, "minecraft:beds", "minecraft:coals", "minecraft:fences", "minecraft:flowers",
"minecraft:lectern_books", "minecraft:music_discs", "minecraft:small_flowers", "minecraft:tall_flowers", "minecraft:trapdoors", "minecraft:walls", "minecraft:wooden_fences");
Types1_16.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);
}
2023-05-03 09:17:51 +02:00
@Override
public void register(ViaProviders providers) {
providers.register(PlayerAbilitiesProvider.class, new PlayerAbilitiesProvider());
}
2020-02-05 20:09:06 +01:00
@Override
public void init(UserConnection userConnection) {
2023-10-19 01:28:12 +02:00
userConnection.addEntityTracker(this.getClass(), new EntityTrackerBase(userConnection, EntityTypes1_16.PLAYER));
userConnection.put(new InventoryTracker1_16());
2020-02-05 20:09:06 +01:00
}
2020-08-16 16:24:06 +02:00
@Override
public MappingData getMappingData() {
return MAPPINGS;
}
@Override
public MetadataRewriter1_16To1_15_2 getEntityRewriter() {
return metadataRewriter;
}
@Override
public InventoryPackets getItemRewriter() {
return itemRewriter;
}
public TranslationMappings getComponentRewriter() {
return componentRewriter;
}
@Override
public TagRewriter<ClientboundPackets1_15> getTagRewriter() {
return tagRewriter;
}
2020-02-05 20:09:06 +01:00
}