From 7920a6add1aa9e2779dd0f74d89bea1eec3fc65f Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Mon, 27 Aug 2018 15:00:04 -0300 Subject: [PATCH] Make commands less hacky, implement kickPlayer --- .../creeper123123321/viarift/ViaRift.java | 35 ++-------- .../MixinNetworkManagerClientChInit.java | 4 +- .../viarift/platform/VRPlatform.java | 35 +++++++--- .../viarift/protocol/Interceptor.java | 68 +++++++++++++++++++ .../viarift/protocol/VRProtocolPipeline.java | 40 +++++++++++ 5 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/github/creeper123123321/viarift/protocol/Interceptor.java create mode 100644 src/main/java/com/github/creeper123123321/viarift/protocol/VRProtocolPipeline.java diff --git a/src/main/java/com/github/creeper123123321/viarift/ViaRift.java b/src/main/java/com/github/creeper123123321/viarift/ViaRift.java index ae0fd15..83292d3 100644 --- a/src/main/java/com/github/creeper123123321/viarift/ViaRift.java +++ b/src/main/java/com/github/creeper123123321/viarift/ViaRift.java @@ -24,7 +24,10 @@ package com.github.creeper123123321.viarift; -import com.github.creeper123123321.viarift.platform.*; +import com.github.creeper123123321.viarift.platform.VRCommandHandler; +import com.github.creeper123123321.viarift.platform.VRInjector; +import com.github.creeper123123321.viarift.platform.VRLoader; +import com.github.creeper123123321.viarift.platform.VRPlatform; import com.github.creeper123123321.viarift.util.JLoggerToLog4j; import io.netty.channel.DefaultEventLoop; import io.netty.channel.EventLoop; @@ -35,15 +38,7 @@ import org.dimdev.riftloader.listener.InitializationListener; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.mixin.Mixins; import us.myles.ViaVersion.ViaManager; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; -import us.myles.ViaVersion.api.protocol.ProtocolVersion; -import us.myles.ViaVersion.api.remapper.PacketHandler; -import us.myles.ViaVersion.api.remapper.PacketRemapper; -import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.concurrent.ThreadFactory; @@ -63,27 +58,5 @@ public class ViaRift implements InitializationListener { .commandHandler(new VRCommandHandler()) .platform(new VRPlatform()).build()); Via.getManager().init(); - ProtocolRegistry.getProtocolPath(ProtocolVersion.v1_13.getId(), ProtocolVersion.v1_12_2.getId()) // XGH to intercept /viarift commands - .get(0).getValue().registerIncoming(State.PLAY, 0x02, 0x02, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.STRING); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper packetWrapper) throws Exception { - String msg = packetWrapper.get(Type.STRING, 0); - ProtocolInfo info = packetWrapper.user().get(ProtocolInfo.class); - if (msg.startsWith("/viarift")) { - Via.getManager().getCommandHandler().onCommand( - new VRCommandSender(info.getUuid(), info.getUsername()), - (msg.length() == 8 ? "" : msg.substring(9)).split(" ", -1) - ); - packetWrapper.cancel(); - } - } - }); - } - }); - //Via.getManager().setDebug(true); } } diff --git a/src/main/java/com/github/creeper123123321/viarift/mixin/MixinNetworkManagerClientChInit.java b/src/main/java/com/github/creeper123123321/viarift/mixin/MixinNetworkManagerClientChInit.java index de66b90..83b1d64 100644 --- a/src/main/java/com/github/creeper123123321/viarift/mixin/MixinNetworkManagerClientChInit.java +++ b/src/main/java/com/github/creeper123123321/viarift/mixin/MixinNetworkManagerClientChInit.java @@ -27,6 +27,7 @@ package com.github.creeper123123321.viarift.mixin; import com.github.creeper123123321.viarift.handler.VRInHandler; import com.github.creeper123123321.viarift.handler.VROutHandler; import com.github.creeper123123321.viarift.platform.VRUserConnection; +import com.github.creeper123123321.viarift.protocol.VRProtocolPipeline; import io.netty.channel.Channel; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; @@ -36,7 +37,6 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.protocol.ProtocolPipeline; @Mixin(targets = "net.minecraft.network.NetworkManager$1") public class MixinNetworkManagerClientChInit { @@ -44,7 +44,7 @@ public class MixinNetworkManagerClientChInit { private void onInitChannel(Channel channel, CallbackInfo ci) { if (channel instanceof SocketChannel) { UserConnection user = new VRUserConnection((SocketChannel) channel); - new ProtocolPipeline(user); + new VRProtocolPipeline(user); MessageToByteEncoder oldEncoder = (MessageToByteEncoder) channel.pipeline().get("encoder"); ByteToMessageDecoder oldDecoder = (ByteToMessageDecoder) channel.pipeline().get("decoder"); diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java index d75fe07..abc1091 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java @@ -25,18 +25,22 @@ package com.github.creeper123123321.viarift.platform; import com.github.creeper123123321.viarift.ViaRift; +import com.github.creeper123123321.viarift.protocol.Interceptor; import com.github.creeper123123321.viarift.util.FutureTaskId; import com.github.creeper123123321.viarift.util.ThreadTaskId; -import net.minecraft.client.Minecraft; -import net.minecraft.util.text.TextComponentString; +import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; +import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatform; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.sponge.VersionInfo; import us.myles.viaversion.libs.gson.JsonObject; @@ -46,7 +50,7 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Logger; public class VRPlatform implements ViaPlatform { - private VRViaConfig config = new VRViaConfig(new File("config/ViaRift/config.yml")); + private VRViaConfig config = new VRViaConfig(new File("config/ViaRift")); @Override public Logger getLogger() { @@ -121,16 +125,31 @@ public class VRPlatform implements ViaPlatform { @Override public void sendMessage(UUID uuid, String s) { - if (uuid.equals(Minecraft.getMinecraft().player.getUniqueID())) { - Minecraft.getMinecraft().addScheduledTask(() -> - Minecraft.getMinecraft().player.sendMessage(new TextComponentString(s)) - ); + UserConnection user = Via.getManager().getPortedPlayers().get(uuid); + PacketWrapper chat = new PacketWrapper(0x0E, null, user); + chat.write(Type.STRING, ChatRewriter.legacyTextToJson(s)); + chat.write(Type.BYTE, (byte) 0); // Position chat box + try { + chat.send(Interceptor.class); + } catch (CancelException e) { + // Ignore + } catch (Exception e) { + e.printStackTrace(); } } @Override public boolean kickPlayer(UUID uuid, String s) { - throw new UnsupportedOperationException(); + UserConnection user = Via.getManager().getPortedPlayers().get(uuid); + PacketWrapper chat = new PacketWrapper(0x1B, null, user); + chat.write(Type.STRING, ChatRewriter.legacyTextToJson(s)); + try { + chat.sendFuture(Interceptor.class).addListener(future -> user.getChannel().close()); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } } @Override diff --git a/src/main/java/com/github/creeper123123321/viarift/protocol/Interceptor.java b/src/main/java/com/github/creeper123123321/viarift/protocol/Interceptor.java new file mode 100644 index 0000000..5c07db5 --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viarift/protocol/Interceptor.java @@ -0,0 +1,68 @@ +/* + * MIT License + * + * Copyright (c) 2018 creeper123123321 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viarift.protocol; + +import com.github.creeper123123321.viarift.platform.VRCommandSender; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; + +public class Interceptor extends Protocol { + @Override + protected void registerPackets() { + // Chat message + registerIncoming(State.PLAY, 0x02, 0x02, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper packetWrapper) throws Exception { + String msg = packetWrapper.get(Type.STRING, 0); + ProtocolInfo info = packetWrapper.user().get(ProtocolInfo.class); + if (msg.startsWith("/viarift")) { + Via.getManager().getCommandHandler().onCommand( + new VRCommandSender(info.getUuid(), info.getUsername()), + msg.length() == 8 ? new String[0] : msg.substring(9).split(" ", -1) + ); + packetWrapper.cancel(); + } + } + }); + } + }); + } + + @Override + public void init(UserConnection userConnection) { + + } +} diff --git a/src/main/java/com/github/creeper123123321/viarift/protocol/VRProtocolPipeline.java b/src/main/java/com/github/creeper123123321/viarift/protocol/VRProtocolPipeline.java new file mode 100644 index 0000000..8fff8ee --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viarift/protocol/VRProtocolPipeline.java @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2018 creeper123123321 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viarift.protocol; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.ProtocolPipeline; + +public class VRProtocolPipeline extends ProtocolPipeline { + public VRProtocolPipeline(UserConnection userConnection) { + super(userConnection); + } + + @Override + protected void registerPackets() { + super.registerPackets(); + add(new Interceptor()); + } +}