diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java index d9c790eed..2672f3632 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java @@ -1,7 +1,7 @@ package com.velocitypowered.api.event.player; import com.google.common.base.Preconditions; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import java.util.List; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index cd6c200f2..84d2c4857 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -71,8 +71,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { if (server.getConfiguration().isBungeePluginChannelEnabled()) { MinecraftConnection serverMc = serverConn.ensureConnected(); serverMc.write(PluginMessageUtil.constructChannelsPacket(serverMc.getProtocolVersion(), - ImmutableList.of(getBungeeCordChannel(serverMc.getProtocolVersion())) - )); + ImmutableList.of(getBungeeCordChannel(serverMc.getProtocolVersion())), + ServerboundPluginMessagePacket.FACTORY)); } } @@ -88,7 +88,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public boolean handle(ClientboundKeepAlivePacket packet) { - serverConn.setLastPingId(packet.getRandomId()); + serverConn.getPendingPings().put(packet.getRandomId(), System.currentTimeMillis()); return false; // forwards on } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 534b356a8..cfae1dd68 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -113,7 +113,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, return getHandshakeRemoteAddress(); } StringBuilder data = new StringBuilder() - .append(registeredServer.getServerInfo().getAddress().getHostString()) + .append(getHandshakeRemoteAddress()) .append('\0') .append(((InetSocketAddress) proxyPlayer.getRemoteAddress()).getHostString()) .append('\0') diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index e199e01a3..d0d14a4ee 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -42,6 +42,7 @@ import com.velocitypowered.proxy.network.StateRegistry; import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundChatPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundHeaderAndFooterPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundResourcePackRequestPacket; @@ -268,16 +269,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } } - @Override - public Component getPlayerListHeader() { - return this.playerListHeader; - } - - @Override - public Component getPlayerListFooter() { - return this.playerListFooter; - } - @Override public void sendPlayerListHeader(@NonNull final Component header) { this.sendPlayerListHeaderAndFooter(header, this.playerListFooter); @@ -292,9 +283,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { public void sendPlayerListHeaderAndFooter(final Component header, final Component footer) { this.playerListHeader = Objects.requireNonNull(header, "header"); this.playerListFooter = Objects.requireNonNull(footer, "footer"); - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { - this.connection.write(HeaderAndFooter.create(header, footer, this.getProtocolVersion())); - } + this.tabList.setHeaderAndFooter(header, footer); } @Override @@ -360,11 +349,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { this.profile = profile.withProperties(properties); } - @Override - public void clearHeaderAndFooter() { - tabList.clearHeaderAndFooter(); - } - @Override public VelocityTabList getTabList() { return tabList; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java index fb45c86eb..eb5b60659 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java @@ -15,4 +15,12 @@ public interface Packet { boolean handle(PacketHandler handler); + // TODO: Move this into decoder + default int expectedMinLength(ByteBuf buf, PacketDirection direction, ProtocolVersion version) { + return 0; + } + + default int expectedMaxLength(ByteBuf buf, PacketDirection direction, ProtocolVersion version) { + return -1; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java index 165cb8c07..f5ceb835f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java @@ -24,6 +24,7 @@ import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.network.serialization.brigadier.ArgumentPropertyRegistry; +import com.velocitypowered.proxy.util.collect.IdentityHashStrategy; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenCustomHashMap; import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java index a3801223c..0906452aa 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java @@ -314,26 +314,4 @@ public class ClientboundJoinGamePacket implements Packet { public boolean handle(PacketHandler handler) { return handler.handle(this); } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("entityId", this.entityId) - .add("gamemode", this.gamemode) - .add("dimension", this.dimension) - .add("partialHashedSeed", this.partialHashedSeed) - .add("difficulty", this.difficulty) - .add("isHardcore", this.isHardcore) - .add("maxPlayers", this.maxPlayers) - .add("levelType", this.levelType) - .add("viewDistance", this.viewDistance) - .add("reducedDebugInfo", this.reducedDebugInfo) - .add("showRespawnScreen", this.showRespawnScreen) - .add("dimensionRegistry", this.dimensionRegistry) - .add("dimensionInfo", this.dimensionInfo) - .add("currentDimensionData", this.currentDimensionData) - .add("previousGamemode", this.previousGamemode) - .add("biomeRegistry", this.biomeRegistry) - .toString(); - } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java index c24f1e741..3e05677eb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java @@ -7,6 +7,7 @@ import com.velocitypowered.proxy.network.StateRegistry; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.util.except.QuietDecoderException; +import com.velocitypowered.proxy.util.except.QuietRuntimeException; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java deleted file mode 100644 index e7baaecb1..000000000 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.velocitypowered.proxy.protocol; - -import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import io.netty.buffer.ByteBuf; - -public interface MinecraftPacket { - - void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion); - - void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion); - - boolean handle(MinecraftSessionHandler handler); - - default int expectedMaxLength(ByteBuf buf, ProtocolUtils.Direction direction, - ProtocolVersion version) { - return -1; - } - - default int expectedMinLength(ByteBuf buf, ProtocolUtils.Direction direction, - ProtocolVersion version) { - return 0; - } -} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java deleted file mode 100644 index ecd610629..000000000 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.velocitypowered.proxy.protocol.packet; - -import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.transformLegacyToModernChannel; - -import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolUtils; -import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; -import io.netty.buffer.ByteBuf; -import org.checkerframework.checker.nullness.qual.MonotonicNonNull; -import org.checkerframework.checker.nullness.qual.Nullable; - -public class PluginMessage extends DeferredByteBufHolder implements MinecraftPacket { - - private @Nullable String channel; - - public PluginMessage() { - super(null); - } - - public PluginMessage(String channel, - @MonotonicNonNull ByteBuf backing) { - super(backing); - this.channel = channel; - } - - public String getChannel() { - if (channel == null) { - throw new IllegalStateException("Channel is not specified."); - } - return channel; - } - - public void setChannel(String channel) { - this.channel = channel; - } - - @Override - public String toString() { - return "PluginMessage{" - + "channel='" + channel + '\'' - + ", data=" + super.toString() - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - this.channel = ProtocolUtils.readString(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { - this.channel = transformLegacyToModernChannel(this.channel); - } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { - this.replace(buf.readRetainedSlice(buf.readableBytes())); - } else { - this.replace(ProtocolUtils.readRetainedByteBufSlice17(buf)); - } - - } - - @Override - public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (channel == null) { - throw new IllegalStateException("Channel is not specified."); - } - - if (refCnt() == 0) { - throw new IllegalStateException("Plugin message contents for " + this.channel - + " freed too many times."); - } - - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { - ProtocolUtils.writeString(buf, transformLegacyToModernChannel(this.channel)); - } else { - ProtocolUtils.writeString(buf, this.channel); - } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { - buf.writeBytes(content()); - } else { - ProtocolUtils.writeByteBuf17(content(), buf, true); // True for Forge support - } - - } - - @Override - public boolean handle(MinecraftSessionHandler handler) { - return handler.handle(this); - } - - @Override - public PluginMessage copy() { - return (PluginMessage) super.copy(); - } - - @Override - public PluginMessage duplicate() { - return (PluginMessage) super.duplicate(); - } - - @Override - public PluginMessage retainedDuplicate() { - return (PluginMessage) super.retainedDuplicate(); - } - - @Override - public PluginMessage replace(ByteBuf content) { - return (PluginMessage) super.replace(content); - } - - @Override - public PluginMessage retain() { - return (PluginMessage) super.retain(); - } - - @Override - public PluginMessage retain(int increment) { - return (PluginMessage) super.retain(increment); - } - - @Override - public PluginMessage touch() { - return (PluginMessage) super.touch(); - } - - @Override - public PluginMessage touch(Object hint) { - return (PluginMessage) super.touch(hint); - } -} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index 728ec5ad2..361aa1a98 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -5,6 +5,7 @@ import com.velocitypowered.api.proxy.player.TabList; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.proxy.connection.MinecraftConnection; +import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundHeaderAndFooterPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket; @@ -16,6 +17,7 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.checkerframework.checker.nullness.qual.Nullable; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java index edd753daf..c438ca50f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java @@ -3,7 +3,7 @@ package com.velocitypowered.proxy.tablist; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.util.GameProfile; -import com.velocitypowered.proxy.connection.MinecraftConnection; +import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket.Item; import java.util.Collections; @@ -18,8 +18,8 @@ public class VelocityTabListLegacy extends VelocityTabList { private final Map nameMapping = new ConcurrentHashMap<>(); - public VelocityTabListLegacy(MinecraftConnection connection) { - super(connection); + public VelocityTabListLegacy(ConnectedPlayer player) { + super(player); } @Override diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java index fe77b362c..634555600 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import org.checkerframework.checker.nullness.qual.NonNull; import org.junit.jupiter.api.Test; public class CommandManagerTests { @@ -444,29 +445,29 @@ public class CommandManagerTests { } }; - manager.register(rawCommand, "foo"); + manager.register("foo", rawCommand); assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo").get().isEmpty()); assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "foo bar").get().isEmpty()); - Command oldCommand = new Command() { + SimpleCommand oldCommand = new SimpleCommand() { @Override - public void execute(CommandSource source, String @NonNull [] args) { + public void execute(Invocation invocation) { fail("The Command should not be executed while testing suggestions"); } @Override - public boolean hasPermission(CommandSource source, String @NonNull [] args) { - return args.length > 0; + public boolean hasPermission(Invocation invocation) { + return invocation.arguments().length > 0; } @Override - public List suggest(CommandSource source, String @NonNull [] currentArgs) { + public List suggest(Invocation invocation) { return ImmutableList.of("suggestion"); } }; - manager.register(oldCommand, "bar"); + manager.register("bar", oldCommand); assertTrue(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar").get().isEmpty()); assertFalse(manager.offerSuggestions(MockCommandSource.INSTANCE, "bar foo").get().isEmpty());