|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.common.mixin.ipforward.network.protocol.game; |
| 26 | + |
| 27 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 28 | +import io.netty.buffer.Unpooled; |
| 29 | +import net.minecraft.commands.synchronization.ArgumentTypeInfo; |
| 30 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 31 | +import net.minecraft.network.FriendlyByteBuf; |
| 32 | +import net.minecraft.resources.ResourceLocation; |
| 33 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 34 | +import org.spongepowered.asm.mixin.Mixin; |
| 35 | +import org.spongepowered.asm.mixin.injection.At; |
| 36 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 37 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 38 | + |
| 39 | +/** |
| 40 | + * If a proxy server tries to inspect the commands sent |
| 41 | + * to the client, it is unable to parse the elements |
| 42 | + * that contain mod specific information due to being |
| 43 | + * unaware of their structure. |
| 44 | + * |
| 45 | + * To fix this, the proxy server expects that non-vanilla |
| 46 | + * elements are wrapped in a magic argument that it can |
| 47 | + * detect to skip its content. |
| 48 | + * |
| 49 | + * The proxy server is expected to unwrap these arguments |
| 50 | + * afterward to allow the client to parse the commands |
| 51 | + * as normal. |
| 52 | + */ |
| 53 | +@Mixin(targets = "net/minecraft/network/protocol/game/ClientboundCommandsPacket$ArgumentNodeStub") |
| 54 | +public final class ClientboundCommandsPacket_ArgumentNodeStub_IpForward { |
| 55 | + |
| 56 | + //Magic value that was chosen by the proxy implementation. |
| 57 | + private static final int ipForward$MOD_ARGUMENT_ID = -256; |
| 58 | + |
| 59 | + @Inject(method = "serializeCap(Lnet/minecraft/network/FriendlyByteBuf;Lnet/minecraft/commands/synchronization/ArgumentTypeInfo;Lnet/minecraft/commands/synchronization/ArgumentTypeInfo$Template;)V", at = @At("HEAD"), cancellable = true) |
| 60 | + private static <A extends ArgumentType<?>, T extends ArgumentTypeInfo.Template<A>> void ipForward$onSerializeCap( |
| 61 | + final FriendlyByteBuf buf, final ArgumentTypeInfo<A, T> $$1, final ArgumentTypeInfo.Template<A> $$2, final CallbackInfo ci) { |
| 62 | + final @Nullable ResourceLocation key = BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getKey($$1); |
| 63 | + if (key == null || ClientboundCommandsPacket_ArgumentNodeStub_IpForward.ipForward$isVanillaArgument(key)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + ci.cancel(); |
| 68 | + |
| 69 | + final FriendlyByteBuf commandData = new FriendlyByteBuf(Unpooled.buffer()); |
| 70 | + $$1.serializeToNetwork((T) $$2, commandData); |
| 71 | + |
| 72 | + buf.writeVarInt(ClientboundCommandsPacket_ArgumentNodeStub_IpForward.ipForward$MOD_ARGUMENT_ID); |
| 73 | + buf.writeVarInt(BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getId($$1)); |
| 74 | + buf.writeVarInt(commandData.readableBytes()); |
| 75 | + buf.writeBytes(commandData); |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean ipForward$isVanillaArgument(final ResourceLocation key) { |
| 79 | + return switch (key.getNamespace()) { |
| 80 | + case "brigadier" -> true; |
| 81 | + case "minecraft" -> switch (key.getPath()) { |
| 82 | + case "test_argument", "test_class" -> false; //These are normally under IS_RUNNING_IN_IDE, proxies cant handle them. |
| 83 | + default -> true; |
| 84 | + }; |
| 85 | + default -> false; |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
0 commit comments