From b954b04800fc102fd6cf35004c4782581659a8a1 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Wed, 29 May 2019 17:35:25 -0300 Subject: [PATCH] Throw away ClientSideReference and add entrypoint - not tested --- .../creeper123123321/viafabric/ViaFabric.java | 2 + .../client/MixinClientConnectionChInit.java | 3 - .../viafabric/platform/VRPlatform.java | 56 +++++++++++-------- .../protocol/ClientSideReference.java | 52 ----------------- 4 files changed, 36 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java diff --git a/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java b/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java index b86bfa5..66d3d1a 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java +++ b/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java @@ -91,6 +91,8 @@ public class ViaFabric implements ModInitializer { new VRRewindPlatform().init(); new VRBackwardsPlatform().init(); + FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); + CommandRegistry.INSTANCE.register(false, c -> c.register(command("viaversion"))); CommandRegistry.INSTANCE.register(false, c -> c.register(command("viaver"))); CommandRegistry.INSTANCE.register(false, c -> c.register(command("vvfabric"))); diff --git a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java index 74e38e3..3422b0b 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java +++ b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java @@ -27,7 +27,6 @@ package com.github.creeper123123321.viafabric.mixin.client; import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler; import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler; import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection; -import com.github.creeper123123321.viafabric.protocol.ClientSideReference; import io.netty.channel.Channel; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; @@ -37,7 +36,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.ClientConnection$1") public class MixinClientConnectionChInit { @@ -45,7 +43,6 @@ public class MixinClientConnectionChInit { private void onInitChannel(Channel channel, CallbackInfo ci) { if (channel instanceof SocketChannel) { UserConnection user = new VRClientSideUserConnection(channel); - new ProtocolPipeline(user).add(new ClientSideReference()); MessageToByteEncoder oldEncoder = (MessageToByteEncoder) channel.pipeline().get("encoder"); ByteToMessageDecoder oldDecoder = (ByteToMessageDecoder) channel.pipeline().get("decoder"); diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java index d51cd11..d1ca005 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java @@ -27,7 +27,6 @@ package com.github.creeper123123321.viafabric.platform; import com.github.creeper123123321.viafabric.ViaFabric; import com.github.creeper123123321.viafabric.commands.NMSCommandSender; import com.github.creeper123123321.viafabric.commands.UserCommandSender; -import com.github.creeper123123321.viafabric.protocol.ClientSideReference; import com.github.creeper123123321.viafabric.providers.VRVersionProvider; import com.github.creeper123123321.viafabric.util.FutureTaskId; import net.fabricmc.api.EnvType; @@ -35,12 +34,15 @@ import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.client.network.packet.ChatMessageS2CPacket; +import net.minecraft.client.network.packet.DisconnectS2CPacket; import net.minecraft.entity.Entity; +import net.minecraft.network.OffThreadException; import net.minecraft.network.chat.ChatMessageType; import net.minecraft.network.chat.TextComponent; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; @@ -49,9 +51,7 @@ 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.dump.PluginInfo; -import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.sponge.VersionInfo; @@ -198,16 +198,7 @@ public class VRPlatform implements ViaPlatform { public void sendMessage(UUID uuid, String s) { UserConnection user = Via.getManager().getPortedPlayers().get(uuid); if (user instanceof VRClientSideUserConnection) { - 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(ClientSideReference.class); - } catch (CancelException e) { - // Ignore - } catch (Exception e) { - e.printStackTrace(); - } + sendMessageClient(s); } else { runSync(() -> { MinecraftServer server = getServer(); @@ -219,18 +210,24 @@ public class VRPlatform implements ViaPlatform { } } + @Environment(EnvType.CLIENT) + private void sendMessageClient(String s) { + ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler(); + if (handler != null) { + try { + handler.onChatMessage(new ChatMessageS2CPacket( + TextComponent.Serializer.fromJsonString(ChatRewriter.legacyTextToJson(s)) + )); + } catch (OffThreadException ignored) { + } + } + } + @Override public boolean kickPlayer(UUID uuid, String s) { UserConnection user = Via.getManager().getPortedPlayers().get(uuid); if (user instanceof VRClientSideUserConnection) { - PacketWrapper chat = new PacketWrapper(0x1B, null, user); - chat.write(Type.STRING, ChatRewriter.legacyTextToJson(s)); - try { - chat.sendFuture(ClientSideReference.class).addListener(future -> user.getChannel().close()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + return kickClient(s); } else { MinecraftServer server = getServer(); if (server != null && server.isOnThread()) { @@ -242,6 +239,21 @@ public class VRPlatform implements ViaPlatform { return false; } + @Environment(EnvType.CLIENT) + private boolean kickClient(String msg) { + ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler(); + if (handler != null) { + try { + handler.onDisconnect(new DisconnectS2CPacket( + TextComponent.Serializer.fromJsonString(ChatRewriter.legacyTextToJson(msg)) + )); + } catch (OffThreadException ignored) { + } + return true; + } + return false; + } + @Override public boolean isPluginEnabled() { return true; diff --git a/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java b/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java deleted file mode 100644 index fb2ca78..0000000 --- a/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.viafabric.protocol; - -import com.google.common.base.Joiner; -import net.minecraft.util.Identifier; -import net.minecraft.util.InvalidIdentifierException; -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 java.nio.charset.StandardCharsets; -import java.util.LinkedList; -import java.util.List; - -public class ClientSideReference extends Protocol { - - @Override - protected void registerPackets() { - } - - @Override - public void init(UserConnection userConnection) { - } -}