Compare commits

...
6 Commits
14 changed files with 291 additions and 40 deletions

No files matched your search

@@ -64,7 +64,7 @@ public interface ViaAPI<T> {
* @return API version incremented with meaningful API changes
*/
default int apiVersion() {
return 13;
return 14;
}
/**
@@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.WorldIdentifiers;
import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface ViaVersionConfig {
@@ -455,4 +456,6 @@ public interface ViaVersionConfig {
WorldIdentifiers get1_16WorldNamesMap();
boolean cache1_17Light();
@Nullable String chatTypeFormat(String translationKey);
}
@@ -302,7 +302,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
*
* @param userConnection The user to initialise
*/
default void init(UserConnection userConnection) {
default void init(UserConnection connection) {
}
/**
@@ -177,35 +177,49 @@ public interface ProtocolManager {
@Nullable Class<S> serverboundPacketsClass);
/**
* Returns whether protocol path calculation expects the path to come closer to the expected version with each entry, true by default.
* Sets the max delta the path calculation allows the distance to the target protocol version to increase.
* <p>
* In practice, this means a path will never go to a protocol version that puts it farther from the desired
* If set to 0, protocol paths will have to come closer to the target protocol version with every entry,
* never going farther away from it (1→5→10 and 1→11→10 are ok, 1→20→10 is not ok).
* If set to -1, no distance checks will be applied (1→20→10 is ok).
*
* @param maxPathDeltaIncrease the max delta the path calculation allows the distance to the target protocol version to increase
* @see #onlyCheckLoweringPathEntries()
*/
void setMaxPathDeltaIncrease(int maxPathDeltaIncrease);
/**
* Returns the max delta the path calculation allows the distance to the target protocol version to increase. 0 by default.
* <p>
* In practice, a value of 0 means a path will never go to a protocol version that puts it farther from the desired
* server protocol version, even if a path existed.
* If this is set to false, *all* possible paths will be checked until a fitting one is found.
* If this is set to -1, *all* possible paths will be checked until a fitting one is found.
* <p>
* Negative examples if this returns true:
* Negative examples if this returns 0:
* <ul>
* A possible path from 3 to 5 in order of 3→10→5 will be dismissed.
* A possible path from 5 to 3 in order of 5→0→3 will be dismissed.
* </ul>
* <p>
* Negative examples if this returns false:
* Negative examples if this returns -1:
* <ul>
* While searching for a path from 3 to 5, 3→2→1 could be checked first before 3→4→5 is found.
* While searching for a path from 5 to 3, 5→6→7 could be checked first before 5→4→3 is found.
* </ul>
*
* @return whether protocol path calculation expects the path to come closer to the expected version with each entry
* @return max delta the path calculation allows the distance to the target protocol version to increase
*/
boolean onlyCheckLoweringPathEntries();
int getMaxPathDeltaIncrease();
/**
* Sets whether protocol path calculation expects the path to come closer to the expected version with each entry.
*
* @param onlyCheckLoweringPathEntries whether protocol path calculation expects the path to come closer to the expected version with each entry
* @see #onlyCheckLoweringPathEntries()
*/
void setOnlyCheckLoweringPathEntries(boolean onlyCheckLoweringPathEntries);
@Deprecated/*(forRemoval = true)*/
default void setOnlyCheckLoweringPathEntries(final boolean onlyCheckLoweringPathEntries) {
setMaxPathDeltaIncrease(onlyCheckLoweringPathEntries ? 0 : -1);
}
@Deprecated/*(forRemoval = true)*/
default boolean onlyCheckLoweringPathEntries() {
return getMaxPathDeltaIncrease() != -1;
}
/**
* Returns the maximum protocol path size applied to {@link #getProtocolPath(int, int)}.
@@ -90,6 +90,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
private JsonElement resourcePack1_17PromptMessage;
private WorldIdentifiers map1_16WorldNames;
private boolean cache1_17Light;
private Map<String, String> chatTypeFormats;
protected AbstractViaConfig(File configFile) {
super(configFile);
@@ -158,6 +159,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
worlds.getOrDefault("nether", WorldIdentifiers.NETHER_DEFAULT),
worlds.getOrDefault("end", WorldIdentifiers.END_DEFAULT));
cache1_17Light = getBoolean("cache-1_17-light", true);
chatTypeFormats = get("chat-types-1_19", Map.class, new HashMap<String, String>());
}
private BlockedProtocolVersions loadBlockedProtocolVersions() {
@@ -526,4 +528,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
public boolean cache1_17Light() {
return cache1_17Light;
}
@Override
public @Nullable String chatTypeFormat(final String translationKey) {
return chatTypeFormats.get(translationKey);
}
}
@@ -119,7 +119,7 @@ public class ProtocolManagerImpl implements ProtocolManager {
private boolean mappingsLoaded;
private ServerProtocolVersion serverProtocolVersion = new ServerProtocolVersionSingleton(-1);
private boolean onlyCheckLoweringPathEntries = true;
private int maxPathDeltaIncrease; // Only allow lowering path entries by default
private int maxProtocolPathSize = 50;
public ProtocolManagerImpl() {
@@ -317,7 +317,7 @@ public class ProtocolManagerImpl implements ProtocolManager {
if (current.containsKey(translatedToVersion)) continue;
// Check if the new version is farther away than the current client version
if (onlyCheckLoweringPathEntries && Math.abs(serverVersion - translatedToVersion) > Math.abs(serverVersion - clientVersion)) {
if (maxPathDeltaIncrease != -1 && Math.abs(serverVersion - translatedToVersion) - Math.abs(serverVersion - clientVersion) > maxPathDeltaIncrease) {
continue;
}
@@ -385,13 +385,13 @@ public class ProtocolManagerImpl implements ProtocolManager {
}
@Override
public void setOnlyCheckLoweringPathEntries(boolean onlyCheckLoweringPathEntries) {
this.onlyCheckLoweringPathEntries = onlyCheckLoweringPathEntries;
public void setMaxPathDeltaIncrease(final int maxPathDeltaIncrease) {
this.maxPathDeltaIncrease = Math.max(-1, maxPathDeltaIncrease);
}
@Override
public boolean onlyCheckLoweringPathEntries() {
return onlyCheckLoweringPathEntries;
public int getMaxPathDeltaIncrease() {
return maxPathDeltaIncrease;
}
@Override
@@ -17,22 +17,41 @@
*/
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
import com.viaversion.viaversion.libs.kyori.adventure.text.TranslatableComponent;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.NamedTextColor;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.Style;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.TextDecoration;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.ChatTypeStorage;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.NonceStorage;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19;
import com.viaversion.viaversion.util.CipherUtil;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> {
@@ -94,18 +113,18 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
@Override
public void registerMap() {
handler(wrapper -> {
// Back to system chat - bye bye chat formats for 1.19.0 servers
// ... not that big of a deal since the majority of modded servers only has Vanilla /say command and the alike sent as proper player chat
// Back to system chat
final JsonElement signedContent = wrapper.read(Type.COMPONENT);
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
wrapper.write(Type.COMPONENT, unsignedContent != null ? unsignedContent : signedContent);
final int chatType = wrapper.read(Type.VAR_INT);
final int type = wrapper.read(Type.VAR_INT);
wrapper.write(Type.BOOLEAN, type == 2); // Overlay, going by the default 1.19 chat type registry
wrapper.read(Type.UUID); // Sender UUID
final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement teamName = wrapper.read(Type.OPTIONAL_COMPONENT);
if (!decorateChatMessage(wrapper, chatType, senderName, teamName, unsignedContent != null ? unsignedContent : signedContent)) {
wrapper.cancel();
}
});
read(Type.UUID); // Sender uuid
read(Type.COMPONENT); // Sender display name
read(Type.OPTIONAL_COMPONENT); // Target display name
read(Type.LONG); // Timestamp
read(Type.LONG); // Salt
read(Type.BYTE_ARRAY_PRIMITIVE); // Signature
@@ -151,11 +170,20 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
map(Type.UNSIGNED_BYTE); // Gamemode
map(Type.BYTE); // Previous Gamemode
map(Type.STRING_ARRAY); // World List
map(Type.NBT); // Registry
handler(wrapper -> {
// Replace chat types - not worth the effort of handling them properly
final CompoundTag tag = wrapper.get(Type.NBT, 0);
tag.put("minecraft:chat_type", CHAT_REGISTRY.clone());
final ChatTypeStorage chatTypeStorage = wrapper.user().get(ChatTypeStorage.class);
chatTypeStorage.clear();
final CompoundTag registry = wrapper.passthrough(Type.NBT);
final ListTag chatTypes = ((CompoundTag) registry.get("minecraft:chat_type")).get("value");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
chatTypeStorage.addChatType(idTag.asInt(), chatTypeCompound);
}
// Replace chat types - they won't actually be used
registry.put("minecraft:chat_type", CHAT_REGISTRY.clone());
});
}
});
@@ -222,4 +250,90 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
}
});
}
@Override
public void init(final UserConnection connection) {
connection.put(new ChatTypeStorage());
}
private boolean decorateChatMessage(final PacketWrapper wrapper, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement teamName, final JsonElement message) {
final CompoundTag chatType = wrapper.user().get(ChatTypeStorage.class).chatType(chatTypeId);
if (chatType == null) {
Via.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
return false;
}
CompoundTag chatData = chatType.<CompoundTag>get("element").get("chat");
boolean overlay = false;
if (chatData == null) {
chatData = chatType.<CompoundTag>get("element").get("overlay");
if (chatData == null) {
// Either narration or something we don't know
return false;
}
overlay = true;
}
final CompoundTag decoaration = chatData.get("decoration");
if (decoaration == null) {
wrapper.write(Type.COMPONENT, message);
wrapper.write(Type.BOOLEAN, overlay);
return true;
}
final String translationKey = (String) decoaration.get("translation_key").getValue();
final TranslatableComponent.Builder componentBuilder = Component.translatable().key(translationKey);
// Add the style
final CompoundTag style = decoaration.get("style");
if (style != null) {
final Style.Builder styleBuilder = Style.style();
final StringTag color = style.get("color");
if (color != null) {
final NamedTextColor textColor = NamedTextColor.NAMES.value(color.getValue());
if (textColor != null) {
styleBuilder.color(NamedTextColor.NAMES.value(color.getValue()));
}
}
for (final String key : TextDecoration.NAMES.keys()) {
if (style.contains(key)) {
styleBuilder.decoration(TextDecoration.NAMES.value(key), style.<ByteTag>get(key).asByte() == 1);
}
}
componentBuilder.style(styleBuilder.build());
}
// Add the replacements
final ListTag parameters = decoaration.get("parameters");
if (parameters != null) {
final List<Component> arguments = new ArrayList<>();
for (final Tag element : parameters) {
JsonElement argument = null;
switch ((String) element.getValue()) {
case "sender":
argument = senderName;
break;
case "content":
argument = message;
break;
case "team_name":
Preconditions.checkNotNull(teamName, "Team name is null");
argument = teamName;
break;
default:
Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
}
if (argument != null) {
arguments.add(GsonComponentSerializer.gson().deserializeFromTree(argument));
}
}
componentBuilder.args(arguments);
}
wrapper.write(Type.COMPONENT, GsonComponentSerializer.gson().serializeToTree(componentBuilder.build()));
wrapper.write(Type.BOOLEAN, overlay);
return true;
}
}
@@ -0,0 +1,47 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2022 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_19_1to1_19.storage;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ChatTypeStorage implements StorableObject {
private final Int2ObjectMap<CompoundTag> chatTypes = new Int2ObjectOpenHashMap<>();
public @Nullable CompoundTag chatType(final int id) {
return chatTypes.isEmpty() ? Protocol1_19To1_18_2.MAPPINGS.chatType(id) : chatTypes.get(id);
}
public void addChatType(final int id, final CompoundTag chatType) {
chatTypes.put(id, chatType);
}
public void clear() {
chatTypes.clear();
}
@Override
public boolean clearOnServerSwitch() {
return false;
}
}
@@ -20,8 +20,6 @@ package com.viaversion.viaversion.protocols.protocol1_19to1_18_2;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
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_19Types;
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
@@ -40,6 +38,7 @@ import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data.MappingData;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.WorldPackets;
@@ -57,7 +56,7 @@ import java.util.concurrent.ThreadLocalRandom;
public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPackets1_18, ClientboundPackets1_19, ServerboundPackets1_17, ServerboundPackets1_19> {
public static final MappingData MAPPINGS = new MappingDataBase("1.18", "1.19", true);
public static final MappingData MAPPINGS = new MappingData();
private final EntityPackets entityRewriter = new EntityPackets(this);
private final InventoryPackets itemRewriter = new InventoryPackets(this);
@@ -0,0 +1,59 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2022 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_19to1_18_2.data;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.IOException;
public final class MappingData extends MappingDataBase {
private final Int2ObjectMap<CompoundTag> defaultChatTypes = new Int2ObjectOpenHashMap<>();
public MappingData() {
super("1.18", "1.19", true);
}
@Override
protected void loadExtras(final JsonObject oldMappings, final JsonObject newMappings, @Nullable final JsonObject diffMappings) {
try {
final ListTag chatTypes = BinaryTagIO.readCompressedInputStream(MappingDataLoader.getResource("chat-types-1.19.nbt")).get("values");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
defaultChatTypes.put(idTag.asInt(), chatTypeCompound);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
public @Nullable CompoundTag chatType(final int id) {
return defaultChatTypes.get(id);
}
}
@@ -218,10 +218,9 @@ public final class EntityPackets extends EntityRewriter<Protocol1_19To1_18_2> {
final CompoundTag dimensionCompound = (CompoundTag) dimension;
final CompoundTag element = dimensionCompound.get("element");
final String name = (String) dimensionCompound.get("name").getValue();
addMonsterSpawnData(element);
dimensionDataMap.put(name, new DimensionDataImpl(element));
dimensionsMap.put(element.clone(), name);
addMonsterSpawnData(element);
}
tracker(wrapper.user()).setDimensions(dimensionDataMap);
@@ -310,6 +309,7 @@ public final class EntityPackets extends EntityRewriter<Protocol1_19To1_18_2> {
private static void writeDimensionKey(final PacketWrapper wrapper, final DimensionRegistryStorage registryStorage) throws Exception {
// Find dimension key by data
final CompoundTag currentDimension = wrapper.read(Type.NBT);
addMonsterSpawnData(currentDimension);
String dimensionKey = registryStorage.dimensionKey(currentDimension);
if (dimensionKey == null) {
if (!Via.getConfig().isSuppressConversionWarnings()) {
@@ -164,6 +164,14 @@ resource-pack-1_17-prompt: ''
# Only disable this if you know what you are doing.
cache-1_17-light: true
#
# 1.19 chat type formats used for 1.19.1+ clients.
chat-types-1_19:
"chat.type.text": "<%s> %s"
"chat.type.announcement": "[%s] %s"
"commands.message.display.incoming": "%s whispers to you: %s"
"chat.type.team.text": "%s <%s> %s"
"chat.type.emote": "* %s %s"
#
#----------------------------------------------------------#
# 1.9+ CLIENTS ON 1.8 SERVERS OPTIONS #
#----------------------------------------------------------#
+1 -1
View File
@@ -1,5 +1,5 @@
# Project properties - we put these here so they can be modified without causing a recompile of the build scripts
projectVersion=4.4.0
projectVersion=4.4.1
# Gradle properties
org.gradle.daemon=true