diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index a18665dc1..5e3402f96 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -71,7 +71,7 @@ body: attributes: label: Additional Server Info description: | - Do you use a proxy (eg. BungeeCord)? What software do you use and what plugins? + Do you use a proxy (e.g. BungeeCord)? What software do you use and what plugins? placeholder: | Example: "I also use BungeeCord with the following plugins: x, y, z" validations: diff --git a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java index 1cfd79f69..0bb12ee15 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java +++ b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java @@ -117,7 +117,7 @@ public interface ViaAPI { * Sends a raw packet to the player. * * @param player the platform's player object, e.g. for Bukkit this is Player - * @param packet the packet; you need a VarInt Id, then the packet contents + * @param packet the packet; you need a VarInt id, then the packet contents * @throws IllegalArgumentException if the player is not injected by Via */ void sendRawPacket(T player, ByteBuf packet); @@ -126,7 +126,7 @@ public interface ViaAPI { * Sends a raw packet to the player. * * @param uuid the uuid from the player to send packet - * @param packet the packet; you need a VarInt Id, then the packet contents + * @param packet the packet; you need a VarInt id, then the packet contents * @throws IllegalArgumentException if the player is not injected by Via */ void sendRawPacket(UUID uuid, ByteBuf packet); diff --git a/api/src/main/java/com/viaversion/viaversion/api/command/ViaCommandSender.java b/api/src/main/java/com/viaversion/viaversion/api/command/ViaCommandSender.java index f90deb5f2..2f0fdf8b8 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/command/ViaCommandSender.java +++ b/api/src/main/java/com/viaversion/viaversion/api/command/ViaCommandSender.java @@ -28,7 +28,7 @@ public interface ViaCommandSender { /** * Check if the sender has a permission. * - * @param permission Permission string eg. viaversion.admin + * @param permission Permission string e.g. viaversion.admin * @return True if the sender has the permission */ boolean hasPermission(String permission); diff --git a/api/src/main/java/com/viaversion/viaversion/api/connection/ConnectionManager.java b/api/src/main/java/com/viaversion/viaversion/api/connection/ConnectionManager.java index 6a252c82b..0605ab451 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/connection/ConnectionManager.java +++ b/api/src/main/java/com/viaversion/viaversion/api/connection/ConnectionManager.java @@ -65,7 +65,7 @@ public interface ConnectionManager { /** * Returns the UUID from the frontend connection to this proxy server - * Returns null when there isn't a server or this connection isn't frontend or it doesn't have an id + * Returns null when there isn't a server or this connection isn't frontend, or it doesn't have an id * When ViaVersion is reloaded, this method may not return some players. * May not return ProtocolSupport players. *

diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaInjector.java b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaInjector.java index 33954f041..d341d7173 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaInjector.java +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaInjector.java @@ -31,14 +31,14 @@ public interface ViaInjector { /** * Inject into the current Platform * - * @throws Exception If there is an error with injecting + * @throws Exception if there is an error with injecting */ void inject() throws Exception; /** * Uninject into the current Platform * - * @throws Exception If there is an error with uninjecting + * @throws Exception if there is an error with uninjecting */ void uninject() throws Exception; @@ -57,7 +57,7 @@ public interface ViaInjector { * For proxies, this returns the lowest supported protocol version. * * @return server protocol version - * @throws Exception if there is an error with getting this info, eg. not binded + * @throws Exception if there is an error with getting this info, e.g. not binded * @see ViaPlatform#isProxy() */ int getServerProtocolVersion() throws Exception; @@ -66,7 +66,7 @@ public interface ViaInjector { * Returns the supported server protocol versions. * * @return server protocol versions - * @throws Exception if there is an error with getting this info, eg. not binded + * @throws Exception if there is an error with getting this info, e.g. not binded * @see ViaPlatform#isProxy() */ default IntSortedSet getServerProtocolVersions() throws Exception { diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatformLoader.java b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatformLoader.java index 187438750..38b090238 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatformLoader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatformLoader.java @@ -25,7 +25,7 @@ package com.viaversion.viaversion.api.platform; public interface ViaPlatformLoader { /** - * Initialise the loading for a platform, eg. registering listeners / providers / events etc. + * Initialise the loading for a platform, e.g. registering listeners / providers / events etc. */ void load(); diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/SimpleProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/SimpleProtocol.java index 9c3457492..91c946506 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/SimpleProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/SimpleProtocol.java @@ -27,7 +27,7 @@ import com.viaversion.viaversion.api.protocol.packet.Direction; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; /** - * Dummy protocol class when there is no need of any of the + * Dummy protocol class when there is no need of the * existing packet type enums or automated channel mappings. * * @see Protocol diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ClientboundPacketType.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ClientboundPacketType.java index b83e16b1f..76547e016 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ClientboundPacketType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ClientboundPacketType.java @@ -23,8 +23,7 @@ package com.viaversion.viaversion.api.protocol.packet; /** - * Interface to be implemented by server outgoing packet type enums, - * representing PLAY state packets, ordered by their packet id. + * Interface to be implemented by server outgoing packet types. */ public interface ClientboundPacketType extends PacketType { diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketTracker.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketTracker.java index c17038452..9a042c0bd 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketTracker.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketTracker.java @@ -81,11 +81,9 @@ public class PacketTracker { if (connection.isClientSide()) return false; // Don't apply PPS limiting for client-side ViaVersionConfig conf = Via.getConfig(); // Max PPS Checker - if (conf.getMaxPPS() > 0) { - if (packetsPerSecond >= conf.getMaxPPS()) { - connection.disconnect(conf.getMaxPPSKickMessage().replace("%pps", Long.toString(packetsPerSecond))); - return true; // don't send current packet - } + if (conf.getMaxPPS() > 0 && packetsPerSecond >= conf.getMaxPPS()) { + connection.disconnect(conf.getMaxPPSKickMessage().replace("%pps", Long.toString(packetsPerSecond))); + return true; // don't send current packet } // Tracking PPS Checker diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ServerboundPacketType.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ServerboundPacketType.java index a86678942..dd93ad735 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ServerboundPacketType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/ServerboundPacketType.java @@ -23,8 +23,7 @@ package com.viaversion.viaversion.api.protocol.packet; /** - * Interface to be implemented by server incoming packet type enums, - * representing PLAY state packets, ordered by their packet id. + * Interface to be implemented by server incoming packet types. */ public interface ServerboundPacketType extends PacketType { diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java index 10f6c5a17..0c7bf63e6 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java @@ -107,7 +107,7 @@ public class ProtocolVersion { * @param snapshotVersion snapshot protocol version, or -1 if not a snapshot * @param name version name * @param versionRange range of versions that are supported by this protocol version, null if not a range - * @return registered {@link ProtocolVersion} + * @return registered ProtocolVersion */ public static ProtocolVersion register(int version, int snapshotVersion, String name, @Nullable VersionRange versionRange) { ProtocolVersion protocol = new ProtocolVersion(version, snapshotVersion, name, versionRange); @@ -130,11 +130,11 @@ public class ProtocolVersion { } /** - * Returns a {@link ProtocolVersion} instance, even if this protocol version + * Returns a ProtocolVersion instance, even if this protocol version * has not been registered. See {@link #isRegistered(int)} berorehand or {@link #isKnown()}. * * @param version protocol version - * @return registered or unknown {@link ProtocolVersion} + * @return registered or unknown ProtocolVersion */ public static @NonNull ProtocolVersion getProtocol(int version) { ProtocolVersion protocolVersion = VERSIONS.get(version); @@ -185,10 +185,8 @@ public class ProtocolVersion { if (majorVersion.equals(protocol) || (protocol.startsWith(name.substring(0, name.length() - 1)))) { return version; } - } else if (version.isRange()) { - if (version.getIncludedVersions().contains(protocol)) { - return version; - } + } else if (version.isRange() && version.getIncludedVersions().contains(protocol)) { + return version; } } return null; @@ -248,7 +246,7 @@ public class ProtocolVersion { * Returns the snapshot protocol version without the snapshot indicator bit if this is a snapshot protocol version. * * @return snapshot protocol version without the snapshot indicator bit - * @throws IllegalArgumentException if the version if not a snapshot version + * @throws IllegalArgumentException if the version is not a snapshot version * @see #isSnapshot() */ public int getSnapshotVersion() { @@ -260,7 +258,7 @@ public class ProtocolVersion { * Returns the snapshot protocol version with the snapshot indicator bit if this is a snapshot protocol version. * * @return snapshot protocol version with the snapshot indicator bit - * @throws IllegalArgumentException if the version if not a snapshot version + * @throws IllegalArgumentException if the version is not a snapshot version * @see #isSnapshot() */ public int getFullSnapshotVersion() { diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ServerProtocolVersion.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ServerProtocolVersion.java index 1f24f25dc..5d087308f 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ServerProtocolVersion.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ServerProtocolVersion.java @@ -52,7 +52,7 @@ public interface ServerProtocolVersion { /** * Returns true if the actual protocol version has not yet been identified. - * In that case, all methods above will returns -1. + * In that case, all methods above will return -1. * * @return true if set, false if unknown (yet) */ diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/VersionProvider.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/VersionProvider.java index debd9bf91..2dabad850 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/VersionProvider.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/VersionProvider.java @@ -25,6 +25,7 @@ package com.viaversion.viaversion.api.protocol.version; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.platform.providers.Provider; +@FunctionalInterface public interface VersionProvider extends Provider { /** diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java index 2f92b5993..903512e9c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java @@ -30,7 +30,7 @@ import java.nio.charset.StandardCharsets; public class StringType extends Type { // String#length() (used to limit the string in Minecraft source code) uses char[]#length - private static final int maxJavaCharUtf8Length = Character.toString(Character.MAX_VALUE) + private static final int MAX_CHAR_UTF_8_LENGTH = Character.toString(Character.MAX_VALUE) .getBytes(StandardCharsets.UTF_8).length; private final int maxLength; @@ -47,8 +47,8 @@ public class StringType extends Type { public String read(ByteBuf buffer) throws Exception { int len = Type.VAR_INT.readPrimitive(buffer); - Preconditions.checkArgument(len <= maxLength * maxJavaCharUtf8Length, - "Cannot receive string longer than Short.MAX_VALUE * " + maxJavaCharUtf8Length + " bytes (got %s bytes)", len); + Preconditions.checkArgument(len <= maxLength * MAX_CHAR_UTF_8_LENGTH, + "Cannot receive string longer than Short.MAX_VALUE * " + MAX_CHAR_UTF_8_LENGTH + " bytes (got %s bytes)", len); String string = buffer.toString(buffer.readerIndex(), len, StandardCharsets.UTF_8); buffer.skipBytes(len); diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/PaperViaInjector.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/PaperViaInjector.java index f8496d5c2..f1e4bc9e0 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/PaperViaInjector.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/PaperViaInjector.java @@ -58,7 +58,7 @@ public final class PaperViaInjector { try { Class.forName("org.bukkit.UnsafeValues").getDeclaredMethod("getProtocolVersion"); return true; - } catch (ReflectiveOperationException e) { + } catch (final ClassNotFoundException | NoSuchMethodException e) { return false; } } @@ -75,7 +75,7 @@ public final class PaperViaInjector { try { Class.forName(className); return true; - } catch (ReflectiveOperationException e) { + } catch (final ClassNotFoundException e) { return false; } } diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeEncodeHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeEncodeHandler.java index ed35e30bf..eaa5f2e11 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeEncodeHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeEncodeHandler.java @@ -65,30 +65,28 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buf) { boolean needsCompress = false; - if (!handledCompression) { - if (ctx.pipeline().names().indexOf("compress") > ctx.pipeline().names().indexOf("via-encoder")) { - // Need to decompress this packet due to bad order - ByteBuf decompressed = BungeePipelineUtil.decompress(ctx, buf); + if (!handledCompression && ctx.pipeline().names().indexOf("compress") > ctx.pipeline().names().indexOf("via-encoder")) { + // Need to decompress this packet due to bad order + ByteBuf decompressed = BungeePipelineUtil.decompress(ctx, buf); - // Ensure the buffer wasn't reused - if (buf != decompressed) { - try { - buf.clear().writeBytes(decompressed); - } finally { - decompressed.release(); - } + // Ensure the buffer wasn't reused + if (buf != decompressed) { + try { + buf.clear().writeBytes(decompressed); + } finally { + decompressed.release(); } - - // Reorder the pipeline - ChannelHandler dec = ctx.pipeline().get("via-decoder"); - ChannelHandler enc = ctx.pipeline().get("via-encoder"); - ctx.pipeline().remove(dec); - ctx.pipeline().remove(enc); - ctx.pipeline().addAfter("decompress", "via-decoder", dec); - ctx.pipeline().addAfter("compress", "via-encoder", enc); - needsCompress = true; - handledCompression = true; } + + // Reorder the pipeline + ChannelHandler decoder = ctx.pipeline().get("via-decoder"); + ChannelHandler encoder = ctx.pipeline().get("via-encoder"); + ctx.pipeline().remove(decoder); + ctx.pipeline().remove(encoder); + ctx.pipeline().addAfter("decompress", "via-decoder", decoder); + ctx.pipeline().addAfter("compress", "via-encoder", encoder); + needsCompress = true; + handledCompression = true; } return needsCompress; } diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java index 2bad00b69..0e13154c1 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java @@ -45,6 +45,7 @@ import java.util.Iterator; import java.util.List; import java.util.UUID; import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectedEvent; import net.md_5.bungee.api.event.ServerSwitchEvent; @@ -140,150 +141,147 @@ public class BungeeServerHandler implements Listener { } } - public void checkServerChange(ServerConnectedEvent e, UserConnection user) throws Exception { - if (user == null) return; + public void checkServerChange(ServerConnectedEvent event, UserConnection user) throws Exception { + if (user == null || !user.has(BungeeStorage.class)) { + return; + } + // Auto-team handling // Handle server/version change - if (user.has(BungeeStorage.class)) { - BungeeStorage storage = user.get(BungeeStorage.class); - ProxiedPlayer player = storage.getPlayer(); + BungeeStorage storage = user.get(BungeeStorage.class); + Server server = event.getServer(); + if (server == null || server.getInfo().getName().equals(storage.getCurrentServer())) { + return; + } - if (e.getServer() != null) { - if (!e.getServer().getInfo().getName().equals(storage.getCurrentServer())) { - // Clear auto-team - EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class); - if (oldEntityTracker != null) { - if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { - oldEntityTracker.sendTeamPacket(false, true); - } - } + // Clear auto-team + EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class); + if (oldEntityTracker != null) { + if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { + oldEntityTracker.sendTeamPacket(false, true); + } + } - String serverName = e.getServer().getInfo().getName(); + String serverName = event.getServer().getInfo().getName(); - storage.setCurrentServer(serverName); + storage.setCurrentServer(serverName); - int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); + int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); - if (protocolId <= ProtocolVersion.v1_8.getVersion()) { // 1.8 doesn't have BossBar packet - if (storage.getBossbar() != null) { - // This ensures we can encode it properly as only the 1.9 protocol is currently implemented. - if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { - for (UUID uuid : storage.getBossbar()) { - PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user); - wrapper.write(Type.UUID, uuid); - wrapper.write(Type.VAR_INT, 1); // remove - wrapper.send(Protocol1_9To1_8.class); - } - } - storage.getBossbar().clear(); - } - } + if (protocolId <= ProtocolVersion.v1_8.getVersion() && storage.getBossbar() != null) { // 1.8 doesn't have BossBar packet + // This ensures we can encode it properly as only the 1.9 protocol is currently implemented. + if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { + for (UUID uuid : storage.getBossbar()) { + PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user); + wrapper.write(Type.UUID, uuid); + wrapper.write(Type.VAR_INT, 1); // remove + wrapper.send(Protocol1_9To1_8.class); + } + } + storage.getBossbar().clear(); + } - ProtocolInfo info = user.getProtocolInfo(); - int previousServerProtocol = info.getServerProtocolVersion(); + ProtocolInfo info = user.getProtocolInfo(); + int previousServerProtocol = info.getServerProtocolVersion(); - // Refresh the pipes - List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); - ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); - user.clearStoredObjects(true); - pipeline.cleanPipes(); - if (protocolPath == null) { - // TODO Check Bungee Supported Protocols? *shrugs* - protocolId = info.getProtocolVersion(); + // Refresh the pipes + List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); + ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); + user.clearStoredObjects(true); + pipeline.cleanPipes(); + if (protocolPath == null) { + // TODO Check Bungee Supported Protocols? *shrugs* + protocolId = info.getProtocolVersion(); + } else { + List protocols = new ArrayList<>(protocolPath.size()); + for (ProtocolPathEntry entry : protocolPath) { + protocols.add(entry.protocol()); + } + pipeline.add(protocols); + } + + info.setServerProtocolVersion(protocolId); + // Add version-specific base Protocol + pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId)); + + // Workaround 1.13 server change + int id1_13 = ProtocolVersion.v1_13.getVersion(); + boolean toNewId = previousServerProtocol < id1_13 && protocolId >= id1_13; + boolean toOldId = previousServerProtocol >= id1_13 && protocolId < id1_13; + if (previousServerProtocol != -1 && (toNewId || toOldId)) { + Collection registeredChannels = (Collection) getRegisteredChannels.invoke(event.getPlayer().getPendingConnection()); + if (!registeredChannels.isEmpty()) { + Collection newChannels = new HashSet<>(); + for (Iterator iterator = registeredChannels.iterator(); iterator.hasNext(); ) { + String channel = iterator.next(); + String oldChannel = channel; + if (toNewId) { + channel = InventoryPackets.getNewPluginChannelId(channel); } else { - List protocols = new ArrayList<>(protocolPath.size()); - for (ProtocolPathEntry entry : protocolPath) { - protocols.add(entry.protocol()); - } - pipeline.add(protocols); + channel = InventoryPackets.getOldPluginChannelId(channel); } - - info.setServerProtocolVersion(protocolId); - // Add version-specific base Protocol - pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId)); - - // Workaround 1.13 server change - int id1_13 = ProtocolVersion.v1_13.getVersion(); - boolean toNewId = previousServerProtocol < id1_13 && protocolId >= id1_13; - boolean toOldId = previousServerProtocol >= id1_13 && protocolId < id1_13; - if (previousServerProtocol != -1 && (toNewId || toOldId)) { - Collection registeredChannels = (Collection) getRegisteredChannels.invoke(e.getPlayer().getPendingConnection()); - if (!registeredChannels.isEmpty()) { - Collection newChannels = new HashSet<>(); - for (Iterator iterator = registeredChannels.iterator(); iterator.hasNext(); ) { - String channel = iterator.next(); - String oldChannel = channel; - if (toNewId) { - channel = InventoryPackets.getNewPluginChannelId(channel); - } else { - channel = InventoryPackets.getOldPluginChannelId(channel); - } - if (channel == null) { - iterator.remove(); - continue; - } - if (!oldChannel.equals(channel)) { - iterator.remove(); - newChannels.add(channel); - } - } - registeredChannels.addAll(newChannels); - } - PluginMessage brandMessage = (PluginMessage) getBrandMessage.invoke(e.getPlayer().getPendingConnection()); - if (brandMessage != null) { - String channel = brandMessage.getTag(); - if (toNewId) { - channel = InventoryPackets.getNewPluginChannelId(channel); - } else { - channel = InventoryPackets.getOldPluginChannelId(channel); - } - if (channel != null) { - brandMessage.setTag(channel); - } - } + if (channel == null) { + iterator.remove(); + continue; } - - user.put(storage); - - user.setActive(protocolPath != null); - - // Init all protocols TODO check if this can get moved up to the previous for loop, and doesn't require the pipeline to already exist. - for (Protocol protocol : pipeline.pipes()) { - protocol.init(user); + if (!oldChannel.equals(channel)) { + iterator.remove(); + newChannels.add(channel); } + } + registeredChannels.addAll(newChannels); + } - EntityTracker1_9 newTracker = user.getEntityTracker(Protocol1_9To1_8.class); - if (newTracker != null) { - if (Via.getConfig().isAutoTeam()) { - String currentTeam = null; - for (Team team : player.getScoreboard().getTeams()) { - if (team.getPlayers().contains(info.getUsername())) { - currentTeam = team.getName(); - - } - } - - // Reinitialize auto-team - newTracker.setAutoTeam(true); - if (currentTeam == null) { - // Send auto-team as it was cleared above - newTracker.sendTeamPacket(true, true); - newTracker.setCurrentTeam("viaversion"); - } else { - // Auto-team will be sent when bungee send remove packet - newTracker.setAutoTeam(Via.getConfig().isAutoTeam()); - newTracker.setCurrentTeam(currentTeam); - } - } - } - - Object wrapper = channelWrapper.get(player); - setVersion.invoke(wrapper, protocolId); - - Object entityMap = getEntityMap.invoke(null, protocolId); - entityRewrite.set(player, entityMap); + PluginMessage brandMessage = (PluginMessage) getBrandMessage.invoke(event.getPlayer().getPendingConnection()); + if (brandMessage != null) { + String channel = brandMessage.getTag(); + if (toNewId) { + channel = InventoryPackets.getNewPluginChannelId(channel); + } else { + channel = InventoryPackets.getOldPluginChannelId(channel); + } + if (channel != null) { + brandMessage.setTag(channel); } } } + + user.put(storage); + + user.setActive(protocolPath != null); + + // Init all protocols TODO check if this can get moved up to the previous for loop, and doesn't require the pipeline to already exist. + for (Protocol protocol : pipeline.pipes()) { + protocol.init(user); + } + + ProxiedPlayer player = storage.getPlayer(); + EntityTracker1_9 newTracker = user.getEntityTracker(Protocol1_9To1_8.class); + if (newTracker != null && Via.getConfig().isAutoTeam()) { + String currentTeam = null; + for (Team team : player.getScoreboard().getTeams()) { + if (team.getPlayers().contains(info.getUsername())) { + currentTeam = team.getName(); + } + } + + // Reinitialize auto-team + newTracker.setAutoTeam(true); + if (currentTeam == null) { + // Send auto-team as it was cleared above + newTracker.sendTeamPacket(true, true); + newTracker.setCurrentTeam("viaversion"); + } else { + // Auto-team will be sent when bungee send remove packet + newTracker.setAutoTeam(Via.getConfig().isAutoTeam()); + newTracker.setCurrentTeam(currentTeam); + } + } + + Object wrapper = channelWrapper.get(player); + setVersion.invoke(wrapper, protocolId); + + Object entityMap = getEntityMap.invoke(null, protocolId); + entityRewrite.set(player, entityMap); } } diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/util/BungeePipelineUtil.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/util/BungeePipelineUtil.java index c35a64250..1e8b7127e 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/util/BungeePipelineUtil.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/util/BungeePipelineUtil.java @@ -27,21 +27,17 @@ import java.util.ArrayList; import java.util.List; public class BungeePipelineUtil { - private static Method DECODE_METHOD; - private static Method ENCODE_METHOD; + private static final Method DECODE_METHOD; + private static final Method ENCODE_METHOD; static { try { DECODE_METHOD = MessageToMessageDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, Object.class, List.class); DECODE_METHOD.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - try { ENCODE_METHOD = MessageToByteEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, ByteBuf.class); ENCODE_METHOD.setAccessible(true); } catch (NoSuchMethodException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } diff --git a/common/src/main/java/com/viaversion/viaversion/ViaListener.java b/common/src/main/java/com/viaversion/viaversion/ViaListener.java index e35ed3d7b..e63513f9d 100644 --- a/common/src/main/java/com/viaversion/viaversion/ViaListener.java +++ b/common/src/main/java/com/viaversion/viaversion/ViaListener.java @@ -32,7 +32,7 @@ public abstract class ViaListener { } /** - * Get the UserConnection from an UUID + * Get the UserConnection from a UUID * * @param uuid UUID object * @return The UserConnection diff --git a/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java index 508ac6387..8dc3b447a 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java @@ -47,7 +47,7 @@ public class ProtocolInfoImpl implements ProtocolInfo { @Override public void setClientState(final State clientState) { if (Via.getManager().debugHandler().enabled()) { - Via.getPlatform().getLogger().info("Client state changed from " + this.clientState + " to " + clientState + " for " + connection.getProtocolInfo().getUuid()); + Via.getPlatform().getLogger().info("Client state changed from " + this.clientState + " to " + clientState + " for " + uuid); } this.clientState = clientState; } @@ -60,7 +60,7 @@ public class ProtocolInfoImpl implements ProtocolInfo { @Override public void setServerState(final State serverState) { if (Via.getManager().debugHandler().enabled()) { - Via.getPlatform().getLogger().info("Server state changed from " + this.serverState + " to " + serverState + " for " + connection.getProtocolInfo().getUuid()); + Via.getPlatform().getLogger().info("Server state changed from " + this.serverState + " to " + serverState + " for " + uuid); } this.serverState = serverState; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 6437b3fa4..e6d22fa59 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -372,7 +372,9 @@ public class PacketWrapperImpl implements PacketWrapper { } private void sendRaw(boolean currentThread) throws Exception { - if (isCancelled()) return; + if (isCancelled()) { + return; + } ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); try { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/packets/InventoryPackets.java index 2aad39989..f4bf131e3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/packets/InventoryPackets.java @@ -40,7 +40,7 @@ public class InventoryPackets extends ItemRewriter= 213 && item.identifier() <= 217; if (newItem) { // Replace server-side unknown items - item.setIdentifier((short) 1); + item.setIdentifier(1); item.setData((short) 0); } return item; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java index ed863e89b..598ce03d7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java @@ -40,7 +40,7 @@ public class InventoryPackets extends ItemRewriter= 218 && item.identifier() <= 234; newItem |= item.identifier() == 449 || item.identifier() == 450; if (newItem) { // Replace server-side unknown items - item.setIdentifier((short) 1); + item.setIdentifier(1); item.setData((short) 0); } return item; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java index 57f4b03b0..d8bded3a6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java @@ -122,7 +122,7 @@ public class InventoryPackets extends ItemRewriter= 235 && item.identifier() <= 252; newItem |= item.identifier() == 453; if (newItem) { // Replace server-side unknown items - item.setIdentifier((short) 1); + item.setIdentifier(1); item.setData((short) 0); } return item; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java index d1491044d..69138ce77 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java @@ -18,17 +18,17 @@ package com.viaversion.viaversion.protocols.protocol1_13_2to1_13_1.packets; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.api.type.types.version.Types1_13_2; +import com.viaversion.viaversion.protocols.protocol1_13_2to1_13_1.Protocol1_13_2To1_13_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; public class EntityPackets { - public static void register(Protocol protocol) { + public static void register(Protocol1_13_2To1_13_1 protocol) { final PacketHandler metaTypeHandler = wrapper -> { for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) { metadata.setMetaType(Types1_13_2.META_TYPES.byId(metadata.metaType().typeId())); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/TabCompleteThread.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/TabCompleteThread.java index 808c61332..cbf7c6b3f 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/TabCompleteThread.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/TabCompleteThread.java @@ -26,10 +26,8 @@ public class TabCompleteThread implements Runnable { public void run() { for (UserConnection info : Via.getManager().getConnectionManager().getConnections()) { if (info.getProtocolInfo() == null) continue; - if (info.getProtocolInfo().getPipeline().contains(Protocol1_13To1_12_2.class)) { - if (info.getChannel().isOpen()) { - info.get(TabCompleteTracker.class).sendPacketToServer(info); - } + if (info.getProtocolInfo().getPipeline().contains(Protocol1_13To1_12_2.class) && info.getChannel().isOpen()) { + info.get(TabCompleteTracker.class).sendPacketToServer(info); } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java index aaf9afd26..28a0ee10c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java @@ -77,7 +77,7 @@ public class SpawnEggRewriter { spawnEggs.put(key, spawnEggs.size()); } - // Make it a non existing block id + // Make it a non-existing block id public static int getSpawnEggId(String entityIdentifier) { // Fallback to bat if (!spawnEggs.containsKey(entityIdentifier)) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java index 829003e01..6cf34bdf3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java @@ -88,7 +88,9 @@ public class BlockEntityProvider implements Provider { wrapper.send(Protocol1_13To1_12_2.class); } + @FunctionalInterface public interface BlockEntityHandler { + int transform(UserConnection user, CompoundTag tag); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index 17d50d50a..950fc83de 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -30,7 +30,8 @@ public class EntityTracker1_14 extends EntityTrackerBase { private final Int2ObjectMap playerEntityFlags = Int2ObjectSyncMap.hashmap(); private int latestTradeWindowId; private boolean forceSendCenterChunk = true; - private int chunkCenterX, chunkCenterZ; + private int chunkCenterX; + private int chunkCenterZ; public EntityTracker1_14(UserConnection user) { super(user, Entity1_14Types.PLAYER); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java index 4dbaec973..b16711085 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java @@ -207,7 +207,7 @@ public class ItemRewriter { newItem |= item.identifier() == 397 && item.data() == 5; newItem |= item.identifier() >= 432 && item.identifier() <= 448; if (newItem) { // Replace server-side unknown items - item.setIdentifier((short) 1); + item.setIdentifier(1); item.setData((short) 0); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 892a8c2db..6e0d5020e 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -93,9 +93,9 @@ public class EntityPackets { @Override public void register() { map(Type.VAR_INT); // 0 - Entity ID - map(Type.INT, SpawnPackets.toNewDouble); // 1 - X - Needs to be divide by 32 - map(Type.INT, SpawnPackets.toNewDouble); // 2 - Y - Needs to be divide by 32 - map(Type.INT, SpawnPackets.toNewDouble); // 3 - Z - Needs to be divide by 32 + map(Type.INT, SpawnPackets.toNewDouble); // 1 - X - Needs to be divided by 32 + map(Type.INT, SpawnPackets.toNewDouble); // 2 - Y - Needs to be divided by 32 + map(Type.INT, SpawnPackets.toNewDouble); // 3 - Z - Needs to be divided by 32 map(Type.BYTE); // 4 - Pitch map(Type.BYTE); // 5 - Yaw @@ -176,12 +176,11 @@ public class EntityPackets { int entityID = wrapper.get(Type.VAR_INT, 0); Item stack = wrapper.get(Type.ITEM, 0); - if (stack != null) { - if (Protocol1_9To1_8.isSword(stack.identifier())) { - entityTracker.getValidBlocking().add(entityID); - return; - } + if (stack != null && Protocol1_9To1_8.isSword(stack.identifier())) { + entityTracker.getValidBlocking().add(entityID); + return; } + entityTracker.getValidBlocking().remove(entityID); }); } @@ -234,7 +233,7 @@ public class EntityPackets { handler(wrapper -> { boolean showParticles = wrapper.read(Type.BOOLEAN); //In 1.8 = true->Show particles : false->Hide particles boolean newEffect = Via.getConfig().isNewEffectIndicator(); - //0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator but we don't use it. + //0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator, but we don't use it. wrapper.write(Type.BYTE, (byte) (showParticles ? newEffect ? 2 : 1 : 0)); }); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java index 320c192c8..095c8e91d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java @@ -47,21 +47,19 @@ public class InventoryPackets { final short property = wrapper.get(Type.SHORT, 0); short value = wrapper.get(Type.SHORT, 1); InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - if (inventoryTracker.getInventory() != null) { - if (inventoryTracker.getInventory().equalsIgnoreCase("minecraft:enchanting_table")) { - if (property > 3 && property < 7) { - // Send 2 properties, splitting it into enchantID & level - final short level = (short) (value >> 8); - final short enchantID = (short) (value & 0xFF); - wrapper.create(wrapper.getId(), propertyPacket -> { - propertyPacket.write(Type.UNSIGNED_BYTE, windowId); - propertyPacket.write(Type.SHORT, property); - propertyPacket.write(Type.SHORT, enchantID); - }).scheduleSend(Protocol1_9To1_8.class); + if (inventoryTracker.getInventory() != null && inventoryTracker.getInventory().equalsIgnoreCase("minecraft:enchanting_table")) { + if (property > 3 && property < 7) { + // Send 2 properties, splitting it into enchantID & level + final short level = (short) (value >> 8); + final short enchantID = (short) (value & 0xFF); + wrapper.create(wrapper.getId(), propertyPacket -> { + propertyPacket.write(Type.UNSIGNED_BYTE, windowId); + propertyPacket.write(Type.SHORT, property); + propertyPacket.write(Type.SHORT, enchantID); + }).scheduleSend(Protocol1_9To1_8.class); - wrapper.set(Type.SHORT, 0, (short) (property + 3)); - wrapper.set(Type.SHORT, 1, level); - } + wrapper.set(Type.SHORT, 0, (short) (property + 3)); + wrapper.set(Type.SHORT, 1, level); } } }); @@ -125,11 +123,9 @@ public class InventoryPackets { InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); short slotID = wrapper.get(Type.SHORT, 0); - if (inventoryTracker.getInventory() != null) { - if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { - if (slotID >= 4) { - wrapper.set(Type.SHORT, 0, (short) (slotID + 1)); - } + if (inventoryTracker.getInventory() != null && inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { + if (slotID >= 4) { + wrapper.set(Type.SHORT, 0, (short) (slotID + 1)); } } }); @@ -171,21 +167,19 @@ public class InventoryPackets { // Brewing Patch handler(wrapper -> { InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - if (inventoryTracker.getInventory() != null) { - if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { - Item[] oldStack = wrapper.get(Type.ITEM_ARRAY, 0); - Item[] newStack = new Item[oldStack.length + 1]; - for (int i = 0; i < newStack.length; i++) { - if (i > 4) { - newStack[i] = oldStack[i - 1]; - } else { - if (i != 4) { // Leave index 3 blank - newStack[i] = oldStack[i]; - } + if (inventoryTracker.getInventory() != null && inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { + Item[] oldStack = wrapper.get(Type.ITEM_ARRAY, 0); + Item[] newStack = new Item[oldStack.length + 1]; + for (int i = 0; i < newStack.length; i++) { + if (i > 4) { + newStack[i] = oldStack[i - 1]; + } else { + if (i != 4) { // Leave index 3 blank + newStack[i] = oldStack[i]; } } - wrapper.set(Type.ITEM_ARRAY, 0, newStack); } + wrapper.set(Type.ITEM_ARRAY, 0, newStack); } }); } @@ -298,14 +292,12 @@ public class InventoryPackets { final short slot = wrapper.get(Type.SHORT, 0); boolean throwItem = (slot == 45 && windowID == 0); InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - if (inventoryTracker.getInventory() != null) { - if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { - if (slot == 4) { - throwItem = true; - } - if (slot > 4) { - wrapper.set(Type.SHORT, 0, (short) (slot - 1)); - } + if (inventoryTracker.getInventory() != null && inventoryTracker.getInventory().equals("minecraft:brewing_stand")) { + if (slot == 4) { + throwItem = true; + } + if (slot > 4) { + wrapper.set(Type.SHORT, 0, (short) (slot - 1)); } } @@ -328,49 +320,53 @@ public class InventoryPackets { } }); - protocol.registerServerbound(ServerboundPackets1_9.CLOSE_WINDOW, new PacketHandlers() { + protocol.registerServerbound(ServerboundPackets1_9.CLOSE_WINDOW, new - @Override - public void register() { - map(Type.UNSIGNED_BYTE); // 0 - Window ID + PacketHandlers() { - // Inventory tracking - handler(wrapper -> { - InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - inventoryTracker.setInventory(null); - inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0)); - }); - } - }); + @Override + public void register() { + map(Type.UNSIGNED_BYTE); // 0 - Window ID - protocol.registerServerbound(ServerboundPackets1_9.HELD_ITEM_CHANGE, new PacketHandlers() { - @Override - public void register() { - map(Type.SHORT); // 0 - Slot id - - // Blocking patch - handler(wrapper -> { - boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() - && Via.getConfig().isShieldBlocking(); - - EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); - if (entityTracker.isBlocking()) { - entityTracker.setBlocking(false); - - if (!showShieldWhenSwordInHand) { - entityTracker.setSecondHand(null); - } - } - - if (showShieldWhenSwordInHand) { - // Update current held item slot index - entityTracker.setHeldItemSlot(wrapper.get(Type.SHORT, 0)); - - // Sync shield item in offhand with main hand - entityTracker.syncShieldWithSword(); + // Inventory tracking + handler(wrapper -> { + InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); + inventoryTracker.setInventory(null); + inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0)); + }); + } + }); + + protocol.registerServerbound(ServerboundPackets1_9.HELD_ITEM_CHANGE, new + + PacketHandlers() { + @Override + public void register() { + map(Type.SHORT); // 0 - Slot id + + // Blocking patch + handler(wrapper -> { + boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() + && Via.getConfig().isShieldBlocking(); + + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); + if (entityTracker.isBlocking()) { + entityTracker.setBlocking(false); + + if (!showShieldWhenSwordInHand) { + entityTracker.setSecondHand(null); + } + } + + if (showShieldWhenSwordInHand) { + // Update current held item slot index + entityTracker.setHeldItemSlot(wrapper.get(Type.SHORT, 0)); + + // Sync shield item in offhand with main hand + entityTracker.syncShieldWithSword(); + } + }); } }); - } - }); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java index f3bddb61a..a9c1f84c7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java @@ -195,7 +195,7 @@ public class PlayerPackets { clientWorld.setEnvironment(dimensionId); }); - // Gotta fake their op + // Fake their op status handler(wrapper -> { CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class); provider.sendPermission(wrapper.user()); @@ -381,11 +381,9 @@ public class PlayerPackets { handler(wrapper -> { int hand = wrapper.read(Type.VAR_INT); - if (Via.getConfig().isLeftHandedHandling()) { - // Add 0x80 if left handed - if (hand == 0) wrapper.set(Type.UNSIGNED_BYTE, 0, - (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80) - ); + // Add 0x80 if left-handed + if (Via.getConfig().isLeftHandedHandling() && hand == 0) { + wrapper.set(Type.UNSIGNED_BYTE, 0, (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80)); } wrapper.sendToServer(Protocol1_9To1_8.class); wrapper.cancel(); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 8da2a41db..64318e88d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -69,9 +69,9 @@ public class SpawnPackets { tracker.sendMetadataBuffer(entityID); }); - map(Type.INT, toNewDouble); // 3 - X - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 4 - Y - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 5 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - X - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 4 - Y - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 5 - Z - Needs to be divided by 32 map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 7 - Yaw @@ -82,7 +82,9 @@ public class SpawnPackets { handler(wrapper -> { int data = wrapper.get(Type.INT, 0); // Data (1st Integer) - short vX = 0, vY = 0, vZ = 0; + short vX = 0; + short vY = 0; + short vZ = 0; if (data > 0) { vX = wrapper.read(Type.SHORT); vY = wrapper.read(Type.SHORT); @@ -134,9 +136,9 @@ public class SpawnPackets { tracker.sendMetadataBuffer(entityID); }); - map(Type.INT, toNewDouble); // 1 - X - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 2 - Y - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 3 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 1 - X - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 2 - Y - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 3 - Z - Needs to be divided by 32 map(Type.SHORT); // 4 - Amount to spawn } @@ -156,9 +158,9 @@ public class SpawnPackets { tracker.sendMetadataBuffer(entityID); }); - map(Type.INT, toNewDouble); // 2 - X - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 3 - Y - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 4 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 2 - X - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 3 - Y - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 4 - Z - Needs to be divided by 32 } }); @@ -183,9 +185,9 @@ public class SpawnPackets { tracker.sendMetadataBuffer(entityID); }); - map(Type.INT, toNewDouble); // 3 - X - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 4 - Y - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 5 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - X - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 4 - Y - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 5 - Z - Needs to be divided by 32 map(Type.BYTE); // 6 - Yaw map(Type.BYTE); // 7 - Pitch @@ -255,9 +257,9 @@ public class SpawnPackets { tracker.sendMetadataBuffer(entityID); }); - map(Type.INT, toNewDouble); // 2 - X - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 3 - Y - Needs to be divide by 32 - map(Type.INT, toNewDouble); // 4 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 2 - X - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 3 - Y - Needs to be divided by 32 + map(Type.INT, toNewDouble); // 4 - Z - Needs to be divided by 32 map(Type.BYTE); // 5 - Yaw map(Type.BYTE); // 6 - Pitch diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java index fc1d731b1..5f6ff1da1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -32,8 +32,8 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8; import com.viaversion.viaversion.protocols.protocol1_8.ServerboundPackets1_8; -import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.protocols.protocol1_9_1to1_9.types.Chunk1_9_1_2Type; +import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9; import com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; @@ -92,7 +92,7 @@ public class WorldPackets { public void register() { map(Type.STRING); // 0 - Sound Name // 1 - Sound Category ID - // Everything else get's written through + // Everything else gets written through handler(wrapper -> { String name = wrapper.get(Type.STRING, 0); @@ -298,17 +298,15 @@ public class WorldPackets { : item != null && Protocol1_9To1_8.isSword(item.identifier()); if (isSword) { - if (hand == 0) { - if (!tracker.isBlocking()) { - tracker.setBlocking(true); + if (hand == 0 && !tracker.isBlocking()) { + tracker.setBlocking(true); - // Check if the shield is already in the offhand - if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) { + // Check if the shield is already in the offhand + if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) { - // Set shield in offhand when interacting with main hand - Item shield = new DataItem(442, (byte) 1, (short) 0, null); - tracker.setSecondHand(shield); - } + // Set shield in offhand when interacting with main hand + Item shield = new DataItem(442, (byte) 1, (short) 0, null); + tracker.setSecondHand(shield); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/CommandBlockStorage.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/CommandBlockStorage.java index 851b91dfe..f55c2a8ff 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/CommandBlockStorage.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/CommandBlockStorage.java @@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap; public class CommandBlockStorage implements StorableObject { private final Map, Map> storedCommandBlocks = new ConcurrentHashMap<>(); - private boolean permissions = false; + private boolean permissions; public void unloadChunk(int x, int z) { Pair chunkPos = new Pair<>(x, z); @@ -38,14 +38,15 @@ public class CommandBlockStorage implements StorableObject { public void addOrUpdateBlock(Position position, CompoundTag tag) { Pair chunkPos = getChunkCoords(position); - if (!storedCommandBlocks.containsKey(chunkPos)) + if (!storedCommandBlocks.containsKey(chunkPos)) { storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<>()); + } Map blocks = storedCommandBlocks.get(chunkPos); - if (blocks.containsKey(position)) - if (blocks.get(position).equals(tag)) - return; + if (blocks.containsKey(position) && blocks.get(position).equals(tag)) { + return; + } blocks.put(position, tag); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java index cfbc304d1..68f345d8c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java @@ -117,7 +117,7 @@ public class EntityTracker1_9 extends EntityTrackerBase { // Update if there is no sword in the main hand or if the player has no shield in the second hand but a sword in the main hand if (!swordInHand || this.itemInSecondHand == null) { - // Update shield in off hand depending if a sword is in the main hand + // Update shield in off-hand depending on whether a sword is in the main hand setSecondHand(swordInHand ? new DataItem(442, (byte) 1, (short) 0, null) : null); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/MovementTracker.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/MovementTracker.java index 7b3944c11..a7e87d87c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/MovementTracker.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/MovementTracker.java @@ -22,8 +22,8 @@ import com.viaversion.viaversion.api.connection.StorableObject; public class MovementTracker implements StorableObject { private static final long IDLE_PACKET_DELAY = 50L; // Update every 50ms (20tps) private static final long IDLE_PACKET_LIMIT = 20; // Max 20 ticks behind - private long nextIdlePacket = 0L; - private boolean ground = false; + private long nextIdlePacket; + private boolean ground; public void incrementIdlePacket() { // Notify of next update diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java index 0deb21d87..a3185d79f 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java @@ -41,7 +41,7 @@ public class SoundRewriter { protocol.registerClientbound(packetType, new PacketHandlers() { @Override public void register() { - map(Type.VAR_INT); // Sound Id + map(Type.VAR_INT); // Sound id handler(getSoundHandler()); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/update/Version.java b/common/src/main/java/com/viaversion/viaversion/update/Version.java index d43ee5c1c..58c677685 100644 --- a/common/src/main/java/com/viaversion/viaversion/update/Version.java +++ b/common/src/main/java/com/viaversion/viaversion/update/Version.java @@ -111,7 +111,7 @@ public class Version implements Comparable { } /** - * Get the tag, eg. -ALPHA + * Get the tag, e.g. -ALPHA * * @return The version tag */ diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index 2eee91f78..9e2f599a4 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -26,7 +26,7 @@ block-disconnect-msg: "You are using an unsupported Minecraft version!" # (We don't suggest using reload either, use a plugin manager) # You can customise the message we kick people with if you use ProtocolLib here. reload-disconnect-msg: "Server reload, please rejoin!" -# We warn when there's a error converting item and block data over versions, should we suppress these? (Only suggested if spamming) +# We warn when there's an error converting item and block data over versions, should we suppress these? (Only suggested if spamming) suppress-conversion-warnings: false # #----------------------------------------------------------# @@ -41,7 +41,7 @@ suppress-conversion-warnings: false bungee-ping-interval: 60 # If the above is enabled, should we save the info to the config (in the section below) bungee-ping-save: true -# To get a servers protocol, ViaVersion will do the following: +# To get a server's protocol, ViaVersion will do the following: # Look for the server in the following section, then look for the last ping if bungee-ping is enabled # otherwise use default. # @@ -63,14 +63,14 @@ bungee-servers: {} velocity-ping-interval: 60 # If the above is enabled, should we save the info to the config (in the section below) velocity-ping-save: true -# To get a servers protocol, ViaVersion will do the following: +# To get a server's protocol, ViaVersion will do the following: # Look for the server in the following section, then look for the last ping if velocity-ping is enabled # otherwise use default. # # The format for the following is: # servername: protocolversion # You can find protocol ids on https://wiki.vg/Protocol_version_numbers -# It will fallback to the default option if none found. +# It will fall back to the default option if none found. velocity-servers: {} # #----------------------------------------------------------# @@ -135,14 +135,14 @@ change-1_9-hitbox: false # WARNING: This gives 1.14+ players the ability to sneak under blocks, that players under that version cannot (sneaking in places that are only 1.5 blocks high)! # Another thing to remember is that those players might be missed by projectiles and other hits directed at the very top of their head whilst sneaking. change-1_14-hitbox: false -# Fixes 1.14+ clients on sub 1.14 servers having a light value of 0 for non full blocks. +# Fixes 1.14+ clients on sub 1.14 servers having a light value of 0 for non-full blocks. fix-non-full-blocklight: true # Fixes walk animation not shown when health is set to Float.NaN fix-1_14-health-nan: true # Should 1.15+ clients respawn instantly / without showing a death screen? use-1_15-instant-respawn: false # -# Enable serverside block-connections for 1.13+ clients - all of the options in this section are built around this option +# Enable serverside block-connections for 1.13+ clients - all the options in this section are built around this option serverside-blockconnections: true # Sets the method for the block connections (world for highly experimental (USE AT OWN RISK) world-level or packet for packet-level) blockconnection-method: packet @@ -191,7 +191,7 @@ auto-team: true suppress-metadata-errors: false # When enabled 1.9+ will be able to block by using shields shield-blocking: true -# If this setting is active, the main hand is used instead of the off hand to trigger the blocking of the player. +# If this setting is active, the main hand is used instead of the off-hand to trigger the blocking of the player. # With the main hand the blocking starts way faster. # (Requires "show-shield-when-sword-in-hand" to be disabled) no-delay-shield-blocking: false diff --git a/common/src/main/resources/assets/viaversion/data/blockIds1.12to1.13.json b/common/src/main/resources/assets/viaversion/data/blockIds1.12to1.13.json index e75dd61a0..661e8398a 100644 --- a/common/src/main/resources/assets/viaversion/data/blockIds1.12to1.13.json +++ b/common/src/main/resources/assets/viaversion/data/blockIds1.12to1.13.json @@ -1,4 +1,3 @@ -// Data from https://minecraft.gamepedia.com/1.13/Flattening { "wooden_slab": [ "oak_slab", diff --git a/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java b/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java index 6c554535a..868728a75 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java +++ b/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java @@ -221,7 +221,7 @@ public class VelocityPlugin implements ViaServerProxyPlatform { true, p.getDescription().getName().orElse(p.getDescription().getId()), p.getDescription().getVersion().orElse("Unknown Version"), - p.getInstance().isPresent() ? p.getInstance().get().getClass().getCanonicalName() : "Unknown", + p.getInstance().map(instance -> instance.getClass().getCanonicalName()).orElse("Unknown"), p.getDescription().getAuthors() )); } diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java index c680912ca..0ceab9468 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java @@ -70,12 +70,10 @@ public class VelocityDecodeHandler extends MessageToMessageDecoder { // Move Via codec handlers back to right position ChannelPipeline pipeline = ctx.pipeline(); - ChannelHandler encoder = pipeline.get(VelocityChannelInitializer.VIA_ENCODER); - pipeline.remove(encoder); + ChannelHandler encoder = pipeline.remove(VelocityChannelInitializer.VIA_ENCODER); pipeline.addBefore(VelocityChannelInitializer.MINECRAFT_ENCODER, VelocityChannelInitializer.VIA_ENCODER, encoder); - ChannelHandler decoder = pipeline.get(VelocityChannelInitializer.VIA_DECODER); - pipeline.remove(decoder); + ChannelHandler decoder = pipeline.remove(VelocityChannelInitializer.VIA_DECODER); pipeline.addBefore(VelocityChannelInitializer.MINECRAFT_DECODER, VelocityChannelInitializer.VIA_DECODER, decoder); super.userEventTriggered(ctx, event);