Skip to content

Commit 584cdeb

Browse files
committed
Wrap mod command argument types in proxy environment
1 parent 7d3050f commit 584cdeb

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/mixins/resources/mixins.sponge.ipforward.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"plugin": "org.spongepowered.common.mixin.plugin.IpForwardPlugin",
55
"mixins": [
66
"network.ConnectionMixin_IpForward",
7+
"network.protocol.game.ClientboundCommandsPacket_ArgumentNodeStub_IpForward",
78
"network.protocol.handshake.ClientIntentionPacketMixin_IpForward",
89
"server.MinecraftServerMixin_IpForward",
910
"server.network.ServerHandshakePacketListenerImplMixin_IpForward",

0 commit comments

Comments
 (0)