From bdfe47c1c29d8e46dd5d5f794892ec8d71114780 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Thu, 11 Apr 2019 19:34:34 +0200 Subject: [PATCH] use ClientSideReference --- .../client/MixinCustomPayloadPacket.java | 96 ------------------- .../protocol/ClientSideReference.java | 52 ++++++++++ src/main/resources/mixins.viafabric.json | 3 +- 3 files changed, 53 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinCustomPayloadPacket.java diff --git a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinCustomPayloadPacket.java b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinCustomPayloadPacket.java deleted file mode 100644 index ffb5825..0000000 --- a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinCustomPayloadPacket.java +++ /dev/null @@ -1,96 +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.mixin.client; - -import net.fabricmc.fabric.impl.network.PacketTypes; -import net.minecraft.client.network.packet.CustomPayloadS2CPacket; -import net.minecraft.util.Identifier; -import net.minecraft.util.InvalidIdentifierException; -import net.minecraft.util.PacketByteBuf; -import org.spongepowered.asm.mixin.Mixin; -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.Via; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.HashSet; -import java.util.Set; - -@Mixin(CustomPayloadS2CPacket.class) -public class MixinCustomPayloadPacket { - - @Inject(method = "read", at = @At(value = "HEAD", target = "Lnet/minecraft/network/Packet;read(Lnet/minecraft/util/PacketByteBuf;)V")) - private void onRead(PacketByteBuf buf, CallbackInfo callbackInfo) throws IOException { - try { - int readerIndex = buf.readerIndex(); - - Identifier identifier = buf.readIdentifier(); - - buf.readerIndex(readerIndex); - - if (identifier.equals(PacketTypes.REGISTER) || identifier.equals(PacketTypes.UNREGISTER)) { - Set identifiers = new HashSet(); - StringBuilder builder = new StringBuilder(); - - while (buf.readerIndex() < buf.writerIndex()) { - char c = (char) buf.readByte(); - if (c == 0 || buf.readableBytes() == 0) { - String s = builder.toString(); - if (!s.isEmpty()) { - try { - identifiers.add(new Identifier(s)); - } catch (InvalidIdentifierException ex) { - Via.getPlatform().getLogger().info("Ignoring invalid custom payload identifier in (un)register:\n" + ex.getMessage()); - } - } - - builder = new StringBuilder(); - } else { - builder.append(c); - } - } - - buf.clear(); - buf.writeIdentifier(identifier); - - boolean first = true; - for (Identifier id : identifiers) { - if (!first) { - buf.writeByte(0); - } else { - first = false; - } - buf.writeBytes(id.toString().getBytes(StandardCharsets.US_ASCII)); - } - } - } catch (InvalidIdentifierException ex) { - Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier:\n" + ex.getMessage()); - buf.clear(); - buf.writeString("viafabric:invalid"); - } - } -} diff --git a/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java b/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java index 84fff86..efe2bbd 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java +++ b/src/main/java/com/github/creeper123123321/viafabric/protocol/ClientSideReference.java @@ -24,12 +24,64 @@ 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() { + // Plugin Message + registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { + @Override + public void registerMap() { + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + String channel = wrapper.passthrough(Type.STRING); + + try { + new Identifier(channel); + } catch (InvalidIdentifierException ex) { + Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier: " + ex.getMessage()); + wrapper.set(Type.STRING, 0, "viafabric:invalid"); + return; + } + + if (channel.equals("minecraft:register") || channel.equals("minecraft:unregister")) { + String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); + + List filteredChannels = new LinkedList<>(); + + for (String c : channels) { + try { + new Identifier(c); + } catch (InvalidIdentifierException ex) { + Via.getPlatform().getLogger().warning("Ignoring invalid custom payload identifier in " + channel + ": " + ex.getMessage()); + continue; + } + filteredChannels.add(c); + } + + wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(filteredChannels).getBytes(StandardCharsets.UTF_8)); + } + } + }); + } + }); } @Override diff --git a/src/main/resources/mixins.viafabric.json b/src/main/resources/mixins.viafabric.json index f4b9fed..6c13530 100644 --- a/src/main/resources/mixins.viafabric.json +++ b/src/main/resources/mixins.viafabric.json @@ -8,8 +8,7 @@ ], "client": [ "client.MixinClientConnectionChInit", - "client.MixinMultiplayerScreen", - "client.MixinCustomPayloadPacket" + "client.MixinMultiplayerScreen" ], "injectors": { "defaultRequire": 1