From fcfea24b36e61142a5752e3d0ac6b0ea6bce3c48 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 12 May 2024 15:05:51 +0200 Subject: [PATCH] Rework logging inside protocols, add Protocol#getLogger() (#3843) --- .../api/protocol/AbstractProtocol.java | 15 ++++- .../viaversion/api/protocol/Protocol.java | 8 +++ .../api/protocol/remapper/PacketHandlers.java | 2 +- .../viaversion/util/ProtocolLogger.java | 66 +++++++++++++++++++ .../viaversion/util/ProtocolUtil.java | 14 ++++ .../configuration/AbstractViaConfig.java | 3 +- .../v1_10to1_11/Protocol1_10To1_11.java | 5 +- .../rewriter/EntityPacketRewriter1_11.java | 12 ++-- .../v1_11_1to1_12/data/TranslateRewriter.java | 5 +- .../v1_12_2to1_13/Protocol1_12_2To1_13.java | 11 +++- .../v1_12_2to1_13/data/BlockIdData.java | 6 +- .../data/ComponentRewriter1_13.java | 9 +-- .../v1_12_2to1_13/data/MappingData1_13.java | 2 +- .../v1_12_2to1_13/data/ParticleRewriter.java | 3 +- .../provider/BlockEntityProvider.java | 3 +- .../provider/blockentities/BannerHandler.java | 6 +- .../provider/blockentities/BedHandler.java | 4 +- .../provider/blockentities/SkullHandler.java | 6 +- .../rewriter/ItemPacketRewriter1_13.java | 32 ++++----- .../rewriter/WorldPacketRewriter1_13.java | 18 +++-- .../rewriter/ItemPacketRewriter1_14.java | 5 +- .../v1_15_2to1_16/Protocol1_15_2To1_16.java | 6 +- .../rewriter/EntityPacketRewriter1_16.java | 7 +- .../rewriter/EntityPacketRewriter1_16_2.java | 3 +- .../v1_17_1to1_18/Protocol1_17_1To1_18.java | 8 ++- .../data/BlockEntityIds1_18.java | 4 +- .../rewriter/WorldPacketRewriter1_18.java | 4 +- .../v1_18_2to1_19/Protocol1_18_2To1_19.java | 4 +- .../rewriter/EntityPacketRewriter1_19.java | 13 ++-- .../v1_19to1_19_1/Protocol1_19To1_19_1.java | 14 +++- .../Protocol1_20_3To1_20_5.java | 8 ++- .../BlockItemPacketRewriter1_20_5.java | 5 +- .../rewriter/ComponentRewriter1_20_5.java | 8 +-- .../rewriter/SpawnPacketRewriter1_9.java | 5 +- .../rewriter/ComponentRewriter.java | 3 +- .../viaversion/rewriter/EntityRewriter.java | 16 ++--- 36 files changed, 230 insertions(+), 113 deletions(-) create mode 100644 api/src/main/java/com/viaversion/viaversion/util/ProtocolLogger.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index 41c7dba09..463f8bf63 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -42,6 +42,7 @@ import com.viaversion.viaversion.api.rewriter.MappingDataListener; import com.viaversion.viaversion.api.rewriter.Rewriter; import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.InformativeException; +import com.viaversion.viaversion.util.ProtocolLogger; import com.viaversion.viaversion.util.ProtocolUtil; import java.util.HashMap; import java.util.Map; @@ -71,6 +72,7 @@ public abstract class AbstractProtocol, Object> storedObjects = new HashMap<>(); private boolean initialized; + private ProtocolLogger logger; @Deprecated protected AbstractProtocol() { @@ -97,6 +99,10 @@ public abstract class AbstractProtocol protocol) { + this(Via.getPlatform().getLogger(), protocol); + } + + public ProtocolLogger(final Logger logger, final Class protocol) { + this.logger = logger; + this.name = ProtocolUtil.toNiceName(protocol); + } + + public void log(final Level level, final String msg) { + logger.log(level, formatMessage(msg)); + } + + public void log(final Level level, final String msg, final Throwable thrown) { + logger.log(level, formatMessage(msg), thrown); + } + + public void warning(final String msg) { + logger.warning(formatMessage(msg)); + } + + public void severe(final String msg) { + logger.severe(formatMessage(msg)); + } + + private String formatMessage(final String msg) { + return "(" + name + ") " + msg; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java b/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java index 63d8049d6..0ada5b8b5 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java +++ b/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java @@ -23,6 +23,7 @@ package com.viaversion.viaversion.util; import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.PacketType; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypeMap; @@ -70,4 +71,17 @@ public final class ProtocolUtil { final String hex = Integer.toHexString(id).toUpperCase(Locale.ROOT); return (hex.length() == 1 ? "0x0" : "0x") + hex; } + + /** + * Returns a readable name of a protocol. For example, "Protocol1_12_2To1_13" becomes "1.12.2->1.13". + * + * @param protocol protocol class + * @return readable name of the protocol + */ + public static String toNiceName(Class protocol) { + return protocol.getSimpleName(). + replace("Protocol", ""). + replace("To", "->"). + replace("_", "."); + } } diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index 20767abf8..2c3bcf9d8 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -18,6 +18,7 @@ package com.viaversion.viaversion.configuration; import com.google.gson.JsonElement; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.configuration.ViaVersionConfig; import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions; @@ -412,7 +413,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf @Override public boolean isSuppressConversionWarnings() { - return suppressConversionWarnings; + return suppressConversionWarnings && !Via.getManager().isDebug(); // Debug mode overrules config } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java index 2eb156926..13521f9b1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java @@ -23,14 +23,12 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.ClientWorld; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; -import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_11; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_9_3; -import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.v1_10to1_11.data.PotionColors1_11; import com.viaversion.viaversion.protocols.v1_10to1_11.rewriter.EntityPacketRewriter1_11; import com.viaversion.viaversion.protocols.v1_10to1_11.data.BlockEntityNames1_11; @@ -43,6 +41,7 @@ import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.util.Pair; public class Protocol1_10To1_11 extends AbstractProtocol { + private static final ValueTransformer toOldByte = new ValueTransformer<>(Types.UNSIGNED_BYTE) { @Override public Short transform(PacketWrapper wrapper, Float inputValue) { @@ -160,7 +159,7 @@ public class Protocol1_10To1_11 extends AbstractProtocol newData = PotionColors1_11.getNewData(data); if (newData == null) { - Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 1.10.2 potion data (" + data + ")"); + getLogger().warning("Received unknown potion data: " + data); data = 0; } else { data = newData.key(); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/EntityPacketRewriter1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/EntityPacketRewriter1_11.java index 3a2964866..c5223675c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/EntityPacketRewriter1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/rewriter/EntityPacketRewriter1_11.java @@ -81,7 +81,7 @@ public class EntityPacketRewriter1_11 extends EntityRewriter metadata) { + public EntityType rewriteEntityType(int numType, List metadata) { Optional optType = EntityType.findById(numType); if (optType.isEmpty()) { Via.getManager().getPlatform().getLogger().severe("Error: could not find Entity type " + numType + " with metadata: " + metadata); @@ -326,16 +326,16 @@ public class EntityPacketRewriter1_11 extends EntityRewriter getById(List metadatas, int id) { + public Optional getById(List metadatas, int id) { for (Metadata metadata : metadatas) { if (metadata.id() == id) return Optional.of(metadata); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/data/TranslateRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/data/TranslateRewriter.java index e38a92697..d6066be93 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/data/TranslateRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/data/TranslateRewriter.java @@ -22,6 +22,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.protocols.v1_11_1to1_12.Protocol1_11_1To1_12; import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ClientboundPackets1_9_3; import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.util.SerializerVersion; @@ -86,8 +87,8 @@ public class TranslateRewriter { hoverEvent.addProperty("action", "show_text"); hoverEvent.add("value", baseArray); } catch (Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error rewriting show_achievement: " + hoverEvent, e); + if (!Via.getConfig().isSuppressConversionWarnings()) { + protocol.getLogger().log(Level.WARNING, "Error rewriting show_achievement: " + hoverEvent, e); } JsonObject invalidText = new JsonObject(); invalidText.addProperty("text", "Invalid statistic/achievement!"); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/Protocol1_12_2To1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/Protocol1_12_2To1_13.java index 72b8685a1..dbbc54a42 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/Protocol1_12_2To1_13.java @@ -69,6 +69,7 @@ import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.IdAndData; +import com.viaversion.viaversion.util.ProtocolLogger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -79,6 +80,7 @@ import java.util.logging.Level; public class Protocol1_12_2To1_13 extends AbstractProtocol { public static final MappingData1_13 MAPPINGS = new MappingData1_13(); + public static final ProtocolLogger LOGGER = new ProtocolLogger(Protocol1_12_2To1_13.class); // These are arbitrary rewrite values, it just needs an invalid color code character. private static final Map SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>(); private static final Set FORMATTING_CODES = Sets.newHashSet('k', 'l', 'm', 'n', 'o', 'r'); @@ -189,7 +191,7 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol 1.12.2 statistic mapping for " + name); + LOGGER.warning("Could not find statistic mapping for " + name); } } else if (split.length > 2) { String category = split[1]; @@ -900,6 +902,11 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol 1.13)", e); + Protocol1_12_2To1_13.LOGGER.log(Level.SEVERE, "Failed to load block id mappings", e); } InputStream blockS = MappingData1_13.class.getClassLoader() @@ -67,7 +67,7 @@ public class BlockIdData { ); numberIdToString.putAll(map); } catch (IOException e) { - Via.getPlatform().getLogger().log(Level.SEVERE, "Failed to load block number to string mappings (1.12.2)", e); + Protocol1_12_2To1_13.LOGGER.log(Level.SEVERE, "Failed to load block number to string mappings", e); } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ComponentRewriter1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ComponentRewriter1_13.java index a4a462fea..6e4206462 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ComponentRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ComponentRewriter1_13.java @@ -32,6 +32,7 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.util.ComponentUtil; +import com.viaversion.viaversion.util.ProtocolLogger; import com.viaversion.viaversion.util.SerializerVersion; import java.util.logging.Level; @@ -54,8 +55,8 @@ public class ComponentRewriter1_13 extends Comp try { tag = ComponentUtil.deserializeLegacyShowItem(value, SerializerVersion.V1_12); } catch (Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error reading 1.12.2 NBT in show_item: " + value, e); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.log(Level.WARNING, "Error reading NBT in show_item: " + value, e); } return; } @@ -86,8 +87,8 @@ public class ComponentRewriter1_13 extends Comp showItem.addProperty("text", SerializerVersion.V1_13.toSNBT(tag)); hoverEvent.add("value", newValue); } catch (Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + value, e); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.log(Level.WARNING, "Error writing NBT in show_item: " + value, e); } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/MappingData1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/MappingData1_13.java index 3bf978dae..84bae7f74 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/MappingData1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/MappingData1_13.java @@ -87,7 +87,7 @@ public class MappingData1_13 extends MappingDataBase { String oldChannel = entry.getKey(); String newChannel = entry.getValue().getAsString(); if (!Key.isValid(newChannel)) { - Via.getPlatform().getLogger().warning("Channel '" + newChannel + "' is not a valid 1.13 plugin channel, please check your configuration!"); + getLogger().warning("Channel '" + newChannel + "' is not a valid 1.13 plugin channel, please check your configuration!"); continue; } channelMappings.put(oldChannel, newChannel); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ParticleRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ParticleRewriter.java index 4f9ed4d64..f3beaa5e8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ParticleRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/data/ParticleRewriter.java @@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.v1_12_2to1_13.data; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.rewriter.WorldPacketRewriter1_13; @@ -96,7 +95,7 @@ public class ParticleRewriter { public static Particle rewriteParticle(int particleId, Integer[] data) { if (particleId >= particles.size()) { - Via.getPlatform().getLogger().severe("Failed to transform particles with id " + particleId + " and data " + Arrays.toString(data)); + Protocol1_12_2To1_13.LOGGER.severe("Failed to transform particles with id " + particleId + " and data " + Arrays.toString(data)); return null; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/BlockEntityProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/BlockEntityProvider.java index 84f4fdc80..689f12c11 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/BlockEntityProvider.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/BlockEntityProvider.java @@ -24,7 +24,6 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.platform.providers.Provider; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.packet.ClientboundPackets1_13; @@ -65,7 +64,7 @@ public class BlockEntityProvider implements Provider { BlockEntityHandler handler = handlers.get(idTag.getValue()); if (handler == null) { if (Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + idTag.getValue() + " full tag: " + tag); + Protocol1_12_2To1_13.LOGGER.warning("Unhandled BlockEntity " + idTag.getValue() + " full tag: " + tag); } return -1; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BannerHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BannerHandler.java index 2ca8f364d..fa3855d99 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BannerHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BannerHandler.java @@ -21,9 +21,9 @@ import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.NumberTag; import com.viaversion.nbt.tag.StringTag; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider; import com.viaversion.viaversion.protocols.v1_12_2to1_13.storage.BlockStorage; import com.viaversion.viaversion.util.ComponentUtil; @@ -41,7 +41,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler { Position position = new Position(tag.getNumberTag("x").asInt(), tag.getNumberTag("y").asShort(), tag.getNumberTag("z").asInt()); if (!storage.contains(position)) { - Via.getPlatform().getLogger().warning("Received an banner color update packet, but there is no banner! O_o " + tag); + Protocol1_12_2To1_13.LOGGER.warning("Received an banner color update packet, but there is no banner! O_o " + tag); return -1; } @@ -56,7 +56,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler { } else if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) { blockId += ((15 - color) * 4); } else { - Via.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag); + Protocol1_12_2To1_13.LOGGER.warning("Why does this block have the banner block entity? :(" + tag); } ListTag patterns = tag.getListTag("Patterns", CompoundTag.class); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BedHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BedHandler.java index b995587e4..e60ba7a11 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BedHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/BedHandler.java @@ -19,9 +19,9 @@ package com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.blockentities import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.NumberTag; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider; import com.viaversion.viaversion.protocols.v1_12_2to1_13.storage.BlockStorage; @@ -33,7 +33,7 @@ public class BedHandler implements BlockEntityProvider.BlockEntityHandler { Position position = new Position(tag.getNumberTag("x").asInt(), tag.getNumberTag("y").asShort(), tag.getNumberTag("z").asInt()); if (!storage.contains(position)) { - Via.getPlatform().getLogger().warning("Received an bed color update packet, but there is no bed! O_o " + tag); + Protocol1_12_2To1_13.LOGGER.warning("Received an bed color update packet, but there is no bed! O_o " + tag); return -1; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/SkullHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/SkullHandler.java index 9aa94dd86..24a1448a1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/SkullHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/SkullHandler.java @@ -19,9 +19,9 @@ package com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.blockentities import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.NumberTag; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider; import com.viaversion.viaversion.protocols.v1_12_2to1_13.storage.BlockStorage; @@ -35,7 +35,7 @@ public class SkullHandler implements BlockEntityProvider.BlockEntityHandler { Position position = new Position(tag.getNumberTag("x").asInt(), tag.getNumberTag("y").asShort(), tag.getNumberTag("z").asInt()); if (!storage.contains(position)) { - Via.getPlatform().getLogger().warning("Received an head update packet, but there is no head! O_o " + tag); + Protocol1_12_2To1_13.LOGGER.warning("Received an head update packet, but there is no head! O_o " + tag); return -1; } @@ -51,7 +51,7 @@ public class SkullHandler implements BlockEntityProvider.BlockEntityHandler { id += rot.asInt(); } } else { - Via.getPlatform().getLogger().warning("Why does this block have the skull block entity? " + tag); + Protocol1_12_2To1_13.LOGGER.warning("Why does this block have the skull block entity? " + tag); return -1; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java index e07e71299..f0cb58aa2 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java @@ -29,8 +29,8 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; +import com.viaversion.viaversion.protocols.v1_11_1to1_12.Protocol1_11_1To1_12; import com.viaversion.viaversion.protocols.v1_12_2to1_13.packet.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.packet.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.v1_12to1_12_1.packet.ClientboundPackets1_12_1; @@ -43,6 +43,7 @@ import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.IdAndData; import com.viaversion.viaversion.util.Key; + import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -120,11 +121,10 @@ public class ItemPacketRewriter1_13 extends ItemRewriter finalSource = SoundSource.findBySource(originalSource); if (finalSource.isEmpty()) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().info("Could not handle unknown sound source " + originalSource + " falling back to default: master"); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.warning("Could not handle unknown sound source " + originalSource + " falling back to default: master"); } finalSource = Optional.of(SoundSource.MASTER); - } wrapper.write(Types.VAR_INT, finalSource.get().getId()); @@ -167,8 +167,8 @@ public class ItemPacketRewriter1_13 extends ItemRewriter id = provider.getIntByIdentifier(motive); - - if (id.isEmpty() && (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug())) { - Via.getPlatform().getLogger().warning("Could not find painting motive: " + motive + " falling back to default (0)"); + if (id.isEmpty() && !Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.warning("Could not find painting motive: " + motive + " falling back to default (0)"); } wrapper.write(Types.VAR_INT, id.orElse(0)); }); @@ -410,8 +408,8 @@ public class WorldPacketRewriter1_13 { if (!VALID_BIOMES.contains(biome)) { if (biome != 255 // is it generated naturally? *shrug* && latestBiomeWarn != biome) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("Received invalid biome id " + biome); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.warning("Received invalid biome id: " + biome); } latestBiomeWarn = biome; } @@ -573,13 +571,13 @@ public class WorldPacketRewriter1_13 { } newId = Protocol1_12_2To1_13.MAPPINGS.getBlockMappings().getNewId(IdAndData.removeData(oldId)); // Remove data if (newId != -1) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("Missing block " + oldId); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.warning("Missing block " + oldId); } return newId; } - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("Missing block completely " + oldId); + if (!Via.getConfig().isSuppressConversionWarnings()) { + Protocol1_12_2To1_13.LOGGER.warning("Missing block completely " + oldId); } // Default air return 0; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java index ca54d12ce..1d43d0db4 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java @@ -25,13 +25,11 @@ import com.viaversion.nbt.tag.Tag; import com.google.common.collect.Sets; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.v1_12_2to1_13.packet.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.v1_12_2to1_13.packet.ServerboundPackets1_13; @@ -44,6 +42,7 @@ import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.Key; + import java.util.Set; import java.util.concurrent.ThreadLocalRandom; @@ -127,7 +126,7 @@ public class ItemPacketRewriter1_14 extends ItemRewriter 32) { if (!Via.getConfig().isSuppressConversionWarnings()) { - Via.getPlatform().getLogger().warning("Ignoring serverbound plugin channel, as it is longer than 32 characters: " + channel); + getLogger().warning("Ignoring serverbound plugin channel, as it is longer than 32 characters: " + channel); } wrapper.cancel(); } else if (namespacedChannel.equals("minecraft:register") || namespacedChannel.equals("minecraft:unregister")) { @@ -179,8 +180,7 @@ public class Protocol1_15_2To1_16 extends AbstractProtocol 32) { if (!Via.getConfig().isSuppressConversionWarnings()) { - Via.getPlatform().getLogger().warning("Ignoring serverbound plugin channel register of '" - + registeredChannel + "', as it is longer than 32 characters"); + getLogger().warning("Ignoring serverbound plugin channel register of '" + registeredChannel + "', as it is longer than 32 characters"); } continue; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/EntityPacketRewriter1_16.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/EntityPacketRewriter1_16.java index c7990192b..d0cfa0755 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/EntityPacketRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/EntityPacketRewriter1_16.java @@ -36,11 +36,12 @@ import com.viaversion.viaversion.protocols.v1_15_2to1_16.packet.ServerboundPacke import com.viaversion.viaversion.protocols.v1_15_2to1_16.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.util.Key; + import java.util.UUID; public class EntityPacketRewriter1_16 extends EntityRewriter { - private static final PacketHandler DIMENSION_HANDLER = wrapper -> { + private final PacketHandler DIMENSION_HANDLER = wrapper -> { WorldIdentifiers map = Via.getConfig().get1_16WorldNamesMap(); WorldIdentifiers userMap = wrapper.user().get(WorldIdentifiers.class); if (userMap != null) { @@ -63,7 +64,7 @@ public class EntityPacketRewriter1_16 extends EntityRewriter { public static final MappingData MAPPINGS = new MappingDataBase("1.17", "1.18"); + public static final ProtocolLogger LOGGER = new ProtocolLogger(Protocol1_17_1To1_18.class); private final EntityPacketRewriter1_18 entityRewriter = new EntityPacketRewriter1_18(this); private final ItemPacketRewriter1_18 itemRewriter = new ItemPacketRewriter1_18(this); private final TagRewriter tagRewriter = new TagRewriter<>(this); @@ -104,6 +105,11 @@ public final class Protocol1_17_1To1_18 extends AbstractProtocol IDS.length || (newId = IDS[id]) == -1) { - Via.getPlatform().getLogger().warning("Received out of bounds block entity id: " + id); + Protocol1_17_1To1_18.LOGGER.warning("Received out of bounds block entity id: " + id); return -1; } return newId; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/rewriter/WorldPacketRewriter1_18.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/rewriter/WorldPacketRewriter1_18.java index 838689d1a..f876a557a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/rewriter/WorldPacketRewriter1_18.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/rewriter/WorldPacketRewriter1_18.java @@ -119,7 +119,7 @@ public final class WorldPacketRewriter1_18 { final String id = idTag.getValue(); final int typeId = BlockEntities1_18.blockEntityIds().getInt(Key.stripMinecraftNamespace(id)); if (typeId == -1) { - Via.getPlatform().getLogger().warning("Unknown block entity: " + id); + protocol.getLogger().warning("Unknown block entity: " + id); } handleSpawners(typeId, tag); @@ -167,7 +167,7 @@ public final class WorldPacketRewriter1_18 { final ChunkLightStorage.ChunkLight light = Via.getConfig().cache1_17Light() ? lightStorage.getLight(chunk.getX(), chunk.getZ()) : lightStorage.removeLight(chunk.getX(), chunk.getZ()); if (light == null) { - Via.getPlatform().getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded); + protocol.getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded); final BitSet emptyLightMask = new BitSet(); emptyLightMask.set(0, tracker.currentWorldSectionHeight() + 2); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/Protocol1_18_2To1_19.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/Protocol1_18_2To1_19.java index 25221d1d8..0e0b414bf 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/Protocol1_18_2To1_19.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/Protocol1_18_2To1_19.java @@ -18,7 +18,6 @@ package com.viaversion.viaversion.protocols.v1_18_2to1_19; import com.google.gson.JsonElement; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19; import com.viaversion.viaversion.api.platform.providers.ViaProviders; @@ -50,6 +49,7 @@ import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.util.CipherUtil; import com.viaversion.viaversion.util.ComponentUtil; + import java.util.concurrent.ThreadLocalRandom; public final class Protocol1_18_2To1_19 extends AbstractProtocol { @@ -172,7 +172,7 @@ public final class Protocol1_18_2To1_19 extends AbstractProtocol 1; // Up/down -> Up case 2 -> 2; // North @@ -309,7 +308,7 @@ public final class EntityPacketRewriter1_19 extends EntityRewriter { + public static final ProtocolLogger LOGGER = new ProtocolLogger(Protocol1_19To1_19_1.class); + public Protocol1_19To1_19_1() { super(ClientboundPackets1_19.class, ClientboundPackets1_19_1.class, ServerboundPackets1_19.class, ServerboundPackets1_19_1.class); } @@ -297,7 +300,7 @@ public final class Protocol1_19To1_19_1 extends AbstractProtocol { public static final MappingData1_20_5 MAPPINGS = new MappingData1_20_5(); + public static final ProtocolLogger LOGGER = new ProtocolLogger(Protocol1_20_3To1_20_5.class); // Mojang will remove this in the next release, so if we were to set this to false, // people would miss the changes and not fix their plugins before forcefully running into the errors then public static boolean strictErrorHandling = System.getProperty("viaversion.strict-error-handling1_20_5", "true").equalsIgnoreCase("true"); @@ -294,6 +295,11 @@ public final class Protocol1_20_3To1_20_5 extends AbstractProtocol extends Co try { tagTag = tag != null ? (CompoundTag) SerializerVersion.V1_20_3.toTag(tag.getValue()) : null; } catch (final Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error reading 1.20.3 NBT in show_item: " + contentsTag, e); + if (!Via.getConfig().isSuppressConversionWarnings()) { + protocol.getLogger().log(Level.WARNING, "Error reading NBT in show_item: " + contentsTag, e); } return; } @@ -249,8 +249,8 @@ public class ComponentRewriter1_20_5 extends Co try { components = toTag(data, false); } catch (final Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.20.5 components in show_item!", e); + if (!Via.getConfig().isSuppressConversionWarnings()) { + protocol.getLogger().log(Level.WARNING, "Error writing components in show_item!", e); } return; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/SpawnPacketRewriter1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/SpawnPacketRewriter1_9.java index 119297235..041de33ee 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/SpawnPacketRewriter1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/SpawnPacketRewriter1_9.java @@ -17,7 +17,6 @@ */ package com.viaversion.viaversion.protocols.v1_8to1_9.rewriter; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10; import com.viaversion.viaversion.api.minecraft.item.DataItem; @@ -199,7 +198,7 @@ public class SpawnPacketRewriter1_9 { if (tracker.hasEntity(entityId)) { protocol.getEntityRewriter().handleMetadata(entityId, metadataList, wrapper.user()); } else { - Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); + protocol.getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); } }); @@ -277,7 +276,7 @@ public class SpawnPacketRewriter1_9 { if (tracker.hasEntity(entityId)) { protocol.getEntityRewriter().handleMetadata(entityId, metadataList, wrapper.user()); } else { - Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); + protocol.getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java index 21d2a3465..dd873e766 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -34,7 +34,6 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import org.checkerframework.checker.nullness.qual.Nullable; @@ -176,7 +175,7 @@ public class ComponentRewriter { return root; } catch (final JsonSyntaxException e) { if (Via.getManager().isDebug()) { - Via.getPlatform().getLogger().severe("Error when trying to parse json: " + value); + protocol.getLogger().severe("Error when trying to parse json: " + value); throw e; } // Yay to malformed json being accepted diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java index 0d057ce67..9ebbd1d15 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -56,7 +56,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; -import java.util.logging.Logger; import java.util.stream.Collectors; import org.checkerframework.checker.nullness.qual.Nullable; @@ -395,14 +394,14 @@ public abstract class EntityRewriter> 4); } else { - Via.getPlatform().getLogger().warning("Height missing in dimension data: " + registryData); + protocol.getLogger().warning("Height missing in dimension data: " + registryData); } NumberTag minY = registryData.getNumberTag("min_y"); if (minY != null) { tracker.setCurrentMinY(minY.asInt()); } else { - Via.getPlatform().getLogger().warning("Min Y missing in dimension data: " + registryData); + protocol.getLogger().warning("Min Y missing in dimension data: " + registryData); } String world = wrapper.get(Types.STRING, 0); @@ -420,7 +419,7 @@ public abstract class EntityRewriter metadataList, Metadata metadata) { if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Logger logger = Via.getPlatform().getLogger(); - logger.severe("An error occurred in metadata handler " + this.getClass().getSimpleName() + protocol.getLogger().severe("An error occurred in metadata handler " + this.getClass().getSimpleName() + " for " + (type != null ? type.name() : "untracked") + " entity type: " + metadata); - logger.severe(metadataList.stream().sorted(Comparator.comparingInt(Metadata::id)) + protocol.getLogger().severe(metadataList.stream().sorted(Comparator.comparingInt(Metadata::id)) .map(Metadata::toString).collect(Collectors.joining("\n", "Full metadata: ", ""))); - logger.log(Level.SEVERE, "Error: ", e); + protocol.getLogger().log(Level.SEVERE, "Error: ", e); } } }