2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
|
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.bungee.handlers;
|
2016-11-02 17:17:41 +01:00
|
|
|
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.api.connection.ProtocolInfo;
|
2021-04-26 21:35:29 +02:00
|
|
|
import com.viaversion.viaversion.api.connection.StoredObject;
|
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
2021-04-27 13:25:18 +02:00
|
|
|
import com.viaversion.viaversion.api.platform.ExternalJoinGameListener;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolPipeline;
|
2021-04-27 18:21:51 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
|
|
|
import com.viaversion.viaversion.bungee.service.ProtocolDetectorService;
|
|
|
|
import com.viaversion.viaversion.bungee.storage.BungeeStorage;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.EntityIdProvider;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
|
2021-04-26 22:54:43 +02:00
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
|
|
|
import net.md_5.bungee.api.event.ServerConnectEvent;
|
|
|
|
import net.md_5.bungee.api.event.ServerConnectedEvent;
|
|
|
|
import net.md_5.bungee.api.event.ServerSwitchEvent;
|
|
|
|
import net.md_5.bungee.api.plugin.Listener;
|
|
|
|
import net.md_5.bungee.api.score.Team;
|
|
|
|
import net.md_5.bungee.event.EventHandler;
|
|
|
|
import net.md_5.bungee.protocol.packet.PluginMessage;
|
2016-11-02 17:17:41 +01:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2018-08-04 12:37:33 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-04-21 11:58:19 +02:00
|
|
|
import java.util.ArrayList;
|
2019-03-18 19:11:35 +01:00
|
|
|
import java.util.Arrays;
|
2016-11-02 17:17:41 +01:00
|
|
|
import java.util.List;
|
2019-03-18 19:11:35 +01:00
|
|
|
import java.util.Objects;
|
2019-01-18 11:05:23 +01:00
|
|
|
import java.util.UUID;
|
2019-03-18 19:11:35 +01:00
|
|
|
import java.util.stream.Collectors;
|
2016-11-02 17:17:41 +01:00
|
|
|
|
|
|
|
public class BungeeServerHandler implements Listener {
|
|
|
|
private static Method getHandshake;
|
2018-08-04 12:37:33 +02:00
|
|
|
private static Method getRelayMessages;
|
2016-11-02 17:17:41 +01:00
|
|
|
private static Method setProtocol;
|
|
|
|
private static Method getEntityMap = null;
|
|
|
|
private static Method setVersion = null;
|
|
|
|
private static Field entityRewrite = null;
|
|
|
|
private static Field channelWrapper = null;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
getHandshake = Class.forName("net.md_5.bungee.connection.InitialHandler").getDeclaredMethod("getHandshake");
|
2018-08-04 12:37:33 +02:00
|
|
|
getRelayMessages = Class.forName("net.md_5.bungee.connection.InitialHandler").getDeclaredMethod("getRelayMessages");
|
2016-11-02 17:17:41 +01:00
|
|
|
setProtocol = Class.forName("net.md_5.bungee.protocol.packet.Handshake").getDeclaredMethod("setProtocolVersion", int.class);
|
|
|
|
getEntityMap = Class.forName("net.md_5.bungee.entitymap.EntityMap").getDeclaredMethod("getEntityMap", int.class);
|
|
|
|
setVersion = Class.forName("net.md_5.bungee.netty.ChannelWrapper").getDeclaredMethod("setVersion", int.class);
|
|
|
|
channelWrapper = Class.forName("net.md_5.bungee.UserConnection").getDeclaredField("ch");
|
|
|
|
channelWrapper.setAccessible(true);
|
|
|
|
entityRewrite = Class.forName("net.md_5.bungee.UserConnection").getDeclaredField("entityRewrite");
|
|
|
|
entityRewrite.setAccessible(true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the handshake version every time someone connects to any server
|
2020-06-30 13:51:06 +02:00
|
|
|
@EventHandler(priority = 120)
|
2016-11-14 20:59:06 +01:00
|
|
|
public void onServerConnect(ServerConnectEvent e) {
|
2019-09-19 11:22:06 +02:00
|
|
|
if (e.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId());
|
2016-11-15 18:01:22 +01:00
|
|
|
if (user == null) return;
|
2016-11-02 17:17:41 +01:00
|
|
|
if (!user.has(BungeeStorage.class)) {
|
|
|
|
user.put(new BungeeStorage(user, e.getPlayer()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName());
|
2021-03-25 22:34:30 +01:00
|
|
|
List<ProtocolPathEntry> protocols = Via.getManager().getProtocolManager().getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId);
|
2016-11-02 17:17:41 +01:00
|
|
|
|
|
|
|
// Check if ViaVersion can support that version
|
|
|
|
try {
|
2018-08-04 12:37:33 +02:00
|
|
|
//Object pendingConnection = getPendingConnection.invoke(e.getPlayer());
|
|
|
|
Object handshake = getHandshake.invoke(e.getPlayer().getPendingConnection());
|
2020-06-07 12:19:36 +02:00
|
|
|
setProtocol.invoke(handshake, protocols == null ? user.getProtocolInfo().getProtocolVersion() : protocolId);
|
2016-11-14 20:59:06 +01:00
|
|
|
} catch (InvocationTargetException | IllegalAccessException e1) {
|
2016-11-02 17:17:41 +01:00
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:51:06 +02:00
|
|
|
@EventHandler(priority = -120)
|
2016-11-02 17:17:41 +01:00
|
|
|
public void onServerConnected(ServerConnectedEvent e) {
|
|
|
|
try {
|
2021-03-24 15:21:18 +01:00
|
|
|
checkServerChange(e, Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()));
|
2016-11-02 17:17:41 +01:00
|
|
|
} catch (Exception e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:51:06 +02:00
|
|
|
@EventHandler(priority = -120)
|
2019-04-23 22:29:51 +02:00
|
|
|
public void onServerSwitch(ServerSwitchEvent e) {
|
|
|
|
// Update entity id
|
2021-03-24 15:21:18 +01:00
|
|
|
UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId());
|
2019-04-23 22:29:51 +02:00
|
|
|
if (userConnection == null) return;
|
|
|
|
int playerId;
|
|
|
|
try {
|
|
|
|
playerId = Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(userConnection);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
return; // Ignored
|
|
|
|
}
|
|
|
|
for (StoredObject storedObject : userConnection.getStoredObjects().values()) {
|
|
|
|
if (storedObject instanceof ExternalJoinGameListener) {
|
|
|
|
((ExternalJoinGameListener) storedObject).onExternalJoinGame(playerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 17:17:41 +01:00
|
|
|
public void checkServerChange(ServerConnectedEvent e, UserConnection user) throws Exception {
|
2016-11-15 09:17:15 +01:00
|
|
|
if (user == null) return;
|
2019-02-24 19:02:09 +01:00
|
|
|
// Auto-team handling
|
2017-05-25 18:14:56 +02:00
|
|
|
// Handle server/version change
|
2016-11-02 17:17:41 +01:00
|
|
|
if (user.has(BungeeStorage.class)) {
|
|
|
|
BungeeStorage storage = user.get(BungeeStorage.class);
|
|
|
|
ProxiedPlayer player = storage.getPlayer();
|
|
|
|
|
|
|
|
if (e.getServer() != null) {
|
|
|
|
if (!e.getServer().getInfo().getName().equals(storage.getCurrentServer())) {
|
2019-02-24 19:02:09 +01:00
|
|
|
// Clear auto-team
|
2019-05-08 12:14:41 +02:00
|
|
|
EntityTracker1_9 oldEntityTracker = user.get(EntityTracker1_9.class);
|
2019-02-24 19:02:09 +01:00
|
|
|
if (oldEntityTracker != null) {
|
|
|
|
if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) {
|
|
|
|
oldEntityTracker.sendTeamPacket(false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 17:17:41 +01:00
|
|
|
String serverName = e.getServer().getInfo().getName();
|
|
|
|
|
|
|
|
storage.setCurrentServer(serverName);
|
|
|
|
|
|
|
|
int protocolId = ProtocolDetectorService.getProtocolId(serverName);
|
|
|
|
|
2020-10-16 18:21:45 +02:00
|
|
|
if (protocolId <= ProtocolVersion.v1_8.getVersion()) { // 1.8 doesn't have BossBar packet
|
2019-01-18 11:05:23 +01:00
|
|
|
if (storage.getBossbar() != null) {
|
2020-05-30 22:31:28 +02:00
|
|
|
// TODO: Verify whether this packet needs to be sent when 1.8 -> 1.9 protocol isn't present in the pipeline
|
|
|
|
// This ensures we can encode it properly as only the 1.9 protocol is currently implemented.
|
2020-06-07 12:19:36 +02:00
|
|
|
if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
|
2020-05-30 22:31:28 +02:00
|
|
|
for (UUID uuid : storage.getBossbar()) {
|
2021-04-26 22:54:43 +02:00
|
|
|
PacketWrapper wrapper = PacketWrapper.create(0x0C, null, user);
|
2020-05-30 22:31:28 +02:00
|
|
|
wrapper.write(Type.UUID, uuid);
|
|
|
|
wrapper.write(Type.VAR_INT, 1); // remove
|
|
|
|
wrapper.send(Protocol1_9To1_8.class, true, true);
|
|
|
|
}
|
2019-01-18 11:05:23 +01:00
|
|
|
}
|
|
|
|
storage.getBossbar().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 12:19:36 +02:00
|
|
|
ProtocolInfo info = user.getProtocolInfo();
|
2018-08-04 12:37:33 +02:00
|
|
|
int previousServerProtocol = info.getServerProtocolVersion();
|
|
|
|
|
2016-11-02 17:17:41 +01:00
|
|
|
// Refresh the pipes
|
2021-04-21 11:58:19 +02:00
|
|
|
List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId);
|
2020-06-07 12:19:36 +02:00
|
|
|
ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline();
|
2016-11-15 09:17:15 +01:00
|
|
|
user.clearStoredObjects();
|
2016-11-02 17:17:41 +01:00
|
|
|
pipeline.cleanPipes();
|
2021-04-21 11:58:19 +02:00
|
|
|
if (protocolPath == null) {
|
2016-11-02 17:17:41 +01:00
|
|
|
// TODO Check Bungee Supported Protocols? *shrugs*
|
|
|
|
protocolId = info.getProtocolVersion();
|
|
|
|
} else {
|
2021-04-21 11:58:19 +02:00
|
|
|
List<Protocol> protocols = new ArrayList<>(protocolPath.size());
|
|
|
|
for (ProtocolPathEntry entry : protocolPath) {
|
|
|
|
protocols.add(entry.getProtocol());
|
2016-11-02 17:17:41 +01:00
|
|
|
}
|
2021-04-21 11:58:19 +02:00
|
|
|
pipeline.add(protocols);
|
2016-11-02 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2018-07-14 14:11:17 +02:00
|
|
|
info.setServerProtocolVersion(protocolId);
|
|
|
|
// Add version-specific base Protocol
|
2021-03-24 15:21:18 +01:00
|
|
|
pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId));
|
2018-07-14 14:11:17 +02:00
|
|
|
|
2018-08-04 12:37:33 +02:00
|
|
|
// Workaround 1.13 server change
|
|
|
|
Object relayMessages = getRelayMessages.invoke(e.getPlayer().getPendingConnection());
|
2019-01-18 11:05:23 +01:00
|
|
|
for (Object message : (List) relayMessages) {
|
|
|
|
PluginMessage plMsg = (PluginMessage) message;
|
|
|
|
String channel = plMsg.getTag();
|
2020-10-16 18:21:45 +02:00
|
|
|
int id1_13 = ProtocolVersion.v1_13.getVersion();
|
2019-01-18 11:05:23 +01:00
|
|
|
if (previousServerProtocol != -1) {
|
2019-03-18 19:11:35 +01:00
|
|
|
String oldChannel = channel;
|
2019-01-18 11:05:23 +01:00
|
|
|
if (previousServerProtocol < id1_13 && protocolId >= id1_13) {
|
|
|
|
channel = InventoryPackets.getNewPluginChannelId(channel);
|
2019-03-18 19:11:35 +01:00
|
|
|
if (channel == null) {
|
|
|
|
throw new RuntimeException(oldChannel + " found in relayMessages");
|
|
|
|
}
|
2019-01-18 11:05:23 +01:00
|
|
|
if (channel.equals("minecraft:register")) {
|
2019-03-18 19:11:35 +01:00
|
|
|
plMsg.setData(Arrays.stream(new String(plMsg.getData(), StandardCharsets.UTF_8).split("\0"))
|
|
|
|
.map(InventoryPackets::getNewPluginChannelId)
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.collect(Collectors.joining("\0")).getBytes(StandardCharsets.UTF_8));
|
2019-01-18 11:05:23 +01:00
|
|
|
}
|
|
|
|
} else if (previousServerProtocol >= id1_13 && protocolId < id1_13) {
|
|
|
|
channel = InventoryPackets.getOldPluginChannelId(channel);
|
2019-03-18 19:11:35 +01:00
|
|
|
if (channel == null) {
|
|
|
|
throw new RuntimeException(oldChannel + " found in relayMessages");
|
|
|
|
}
|
2019-01-18 11:05:23 +01:00
|
|
|
if (channel.equals("REGISTER")) {
|
2019-03-18 19:11:35 +01:00
|
|
|
plMsg.setData(Arrays.stream(new String(plMsg.getData(), StandardCharsets.UTF_8).split("\0"))
|
|
|
|
.map(InventoryPackets::getOldPluginChannelId)
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.collect(Collectors.joining("\0")).getBytes(StandardCharsets.UTF_8));
|
2018-08-04 12:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 11:05:23 +01:00
|
|
|
plMsg.setTag(channel);
|
2018-08-04 12:37:33 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 09:17:15 +01:00
|
|
|
user.put(storage);
|
2016-11-02 17:17:41 +01:00
|
|
|
|
2021-04-21 11:58:19 +02:00
|
|
|
user.setActive(protocolPath != null);
|
2016-11-02 17:17:41 +01:00
|
|
|
|
|
|
|
// 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()) {
|
2016-11-15 09:17:15 +01:00
|
|
|
protocol.init(user);
|
2016-11-02 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:14:41 +02:00
|
|
|
EntityTracker1_9 newTracker = user.get(EntityTracker1_9.class);
|
2019-02-24 19:02:09 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 17:44:41 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:17:41 +01:00
|
|
|
Object wrapper = channelWrapper.get(player);
|
|
|
|
setVersion.invoke(wrapper, protocolId);
|
|
|
|
|
|
|
|
Object entityMap = getEntityMap.invoke(null, protocolId);
|
|
|
|
entityRewrite.set(player, entityMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|