Add Forge mod list support

This commit is contained in:
Alex Thomson
2018-10-10 19:52:22 +13:00
parent 7578aa27a9
commit c5a27bb135
8 changed files with 140 additions and 70 deletions

View File

@@ -1,10 +1,9 @@
package com.velocitypowered.proxy.connection.client;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.player.TabList;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.VelocityConstants;
@@ -13,7 +12,6 @@ import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.packet.*;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import com.velocitypowered.proxy.tablist.VelocityTabList;
import com.velocitypowered.proxy.util.ThrowableUtils;
import io.netty.buffer.ByteBuf;
import net.kyori.text.TextComponent;
@@ -147,6 +145,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
player.getConnectedServer().getConnection().write(PluginMessageUtil.rewriteMCBrand(packet));
} else if (player.getConnectedServer().isLegacyForge() && !player.getConnectedServer().hasCompletedJoin()) {
if (packet.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) {
List<ModInfo.Mod> mods = PluginMessageUtil.readModList(packet);
if (!mods.isEmpty()) {
player.setModInfo(new ModInfo("FML", mods));
}
// Always forward the FML handshake to the remote server.
player.getConnectedServer().getConnection().write(packet);
} else {

View File

@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.gson.JsonObject;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.player.KickedFromServerEvent;
import com.velocitypowered.api.event.player.PlayerModInfoEvent;
import com.velocitypowered.api.event.player.PlayerSettingsChangedEvent;
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.permission.PermissionFunction;
@@ -16,8 +17,8 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.player.PlayerSettings;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.api.proxy.player.TabList;
import com.velocitypowered.api.util.MessagePosition;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.api.util.title.TextTitle;
import com.velocitypowered.api.util.title.Title;
import com.velocitypowered.api.util.title.Titles;
@@ -31,9 +32,6 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults;
import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.packet.*;
import com.velocitypowered.proxy.server.VelocityRegisteredServer;
import com.velocitypowered.proxy.protocol.packet.Chat;
import com.velocitypowered.proxy.protocol.packet.ClientSettings;
import com.velocitypowered.proxy.protocol.packet.PluginMessage;
import com.velocitypowered.proxy.tablist.VelocityTabList;
import com.velocitypowered.proxy.util.ThrowableUtils;
import net.kyori.text.Component;
@@ -69,6 +67,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
private VelocityServerConnection connectedServer;
private VelocityServerConnection connectionInFlight;
private PlayerSettings settings;
private ModInfo modInfo;
private final VelocityTabList tabList;
private final VelocityServer server;
@@ -120,7 +119,16 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
this.settings = new ClientSettingsWrapper(settings);
server.getEventManager().fireAndForget(new PlayerSettingsChangedEvent(this, this.settings));
}
public Optional<ModInfo> getModInfo() {
return Optional.ofNullable(modInfo);
}
void setModInfo(ModInfo modInfo) {
this.modInfo = modInfo;
server.getEventManager().fireAndForget(new PlayerModInfoEvent(this, this.modInfo));
}
@Override
public InetSocketAddress getRemoteAddress() {
return (InetSocketAddress) connection.getRemoteAddress();

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.event.proxy.ProxyPingEvent;
import com.velocitypowered.api.proxy.InboundConnection;
import com.velocitypowered.api.proxy.server.ServerPing;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.config.VelocityConfiguration;
import com.velocitypowered.proxy.connection.MinecraftConnection;
@@ -42,7 +43,7 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
new ServerPing.Players(server.getPlayerCount(), configuration.getShowMaxPlayers(), ImmutableList.of()),
configuration.getMotdComponent(),
configuration.getFavicon().orElse(null),
configuration.isAnnounceForge() ? ServerPing.ModInfo.DEFAULT : null
configuration.isAnnounceForge() ? ModInfo.DEFAULT : null
);
ProxyPingEvent event = new ProxyPingEvent(inboundWrapper, initialPing);

View File

@@ -2,6 +2,8 @@ package com.velocitypowered.proxy.protocol.util;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.PluginMessage;
@@ -76,4 +78,23 @@ public class PluginMessageUtil {
newMsg.setData(rewrittenData);
return newMsg;
}
public static List<ModInfo.Mod> readModList(PluginMessage message) {
List<ModInfo.Mod> mods = Lists.newArrayList();
ByteBuf byteBuf = Unpooled.wrappedBuffer(message.getData());
byte discriminator = byteBuf.readByte();
if (discriminator == 2) {
int modCount = ProtocolUtils.readVarInt(byteBuf);
for (int index = 0; index < modCount; index++) {
String id = ProtocolUtils.readString(byteBuf);
String version = ProtocolUtils.readString(byteBuf);
mods.add(new ModInfo.Mod(id, version));
}
}
return ImmutableList.copyOf(mods);
}
}