From ccb52007fc46fbbf7f3be7a4bc01acb762bf4101 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sun, 21 Jul 2019 19:52:20 -0500 Subject: [PATCH] Allow connected sessions to run Geyser commands --- .../network/UpstreamPacketHandler.java | 16 +++++----- .../network/session/GeyserSession.java | 30 +++++++++++++++++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java index 757fc714e..44d69aaea 100644 --- a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java @@ -176,8 +176,13 @@ public class UpstreamPacketHandler implements BedrockPacketHandler { public boolean handle(CommandRequestPacket packet) { connector.getLogger().debug("Handled packet: " + packet.getClass().getSimpleName()); - ClientChatPacket chatPacket = new ClientChatPacket(packet.getCommand()); - session.getDownstream().getSession().send(chatPacket); + String command = packet.getCommand().replace("/", ""); + if (connector.getCommandMap().getCommands().containsKey(command)) { + connector.getCommandMap().runCommand(session, command); + } else { + ClientChatPacket chatPacket = new ClientChatPacket(packet.getCommand()); + session.getDownstream().getSession().send(chatPacket); + } return true; } @@ -408,18 +413,13 @@ public class UpstreamPacketHandler implements BedrockPacketHandler { public boolean handle(TextPacket packet) { connector.getLogger().debug("Handled packet: " + packet.getClass().getSimpleName()); - if(packet.getMessage().charAt(0) == '.') { - + if (packet.getMessage().charAt(0) == '.') { ClientChatPacket chatPacket = new ClientChatPacket(packet.getMessage().replace(".", "/")); - session.getDownstream().getSession().send(chatPacket); - return true; - } ClientChatPacket chatPacket = new ClientChatPacket(packet.getMessage()); - session.getDownstream().getSession().send(chatPacket); return true; diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 42f965479..07a40f4fb 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -31,20 +31,21 @@ import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; -import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.tcp.TcpSessionFactory; import com.nukkitx.network.util.DisconnectReason; import com.nukkitx.protocol.PlayerSession; import com.nukkitx.protocol.bedrock.BedrockServerSession; +import com.nukkitx.protocol.bedrock.packet.TextPacket; import lombok.AllArgsConstructor; import lombok.Getter; +import org.geysermc.api.command.CommandSender; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.remote.RemoteJavaServer; import org.geysermc.connector.network.translators.Registry; import java.util.UUID; -public class GeyserSession implements PlayerSession { +public class GeyserSession implements PlayerSession, CommandSender { private GeyserConnector connector; @@ -126,6 +127,31 @@ public class GeyserSession implements PlayerSession { authenticationData = new AuthenticationData(name, uuid, xboxUUID); } + @Override + public String getName() { + return authenticationData.getName(); + } + + @Override + public void sendMessage(String message) { + TextPacket textPacket = new TextPacket(); + textPacket.setPlatformChatId(""); + textPacket.setSourceName(""); + textPacket.setXuid(""); + textPacket.setType(TextPacket.Type.CHAT); + textPacket.setNeedsTranslation(false); + textPacket.setMessage(message); + + upstream.sendPacket(textPacket); + } + + @Override + public void sendMessage(String[] messages) { + for (String message : messages) { + sendMessage(message); + } + } + @Getter @AllArgsConstructor public class AuthenticationData {