From 984a111a09b84be5534622c623053f8f8bf08d03 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 17 Jun 2024 15:07:16 +0100 Subject: [PATCH] feat: mixin for server-side custom arg types on Fabric --- .../uniform/fabric/FabricCommand.java | 25 ------- .../fabric/mixins/ArgumentNodeMixin.java | 68 +++++++++++++++++++ .../src/main/resources/fabric.mod.json | 6 +- .../src/main/resources/uniform.accesswidener | 3 + .../src/main/resources/uniform.mixins.json | 6 ++ .../uniform/fabric/FabricCommand.java | 25 ------- .../fabric/mixins/ArgumentNodeMixin.java | 68 +++++++++++++++++++ .../src/main/resources/fabric.mod.json | 6 +- .../src/main/resources/uniform.accesswidener | 3 + .../src/main/resources/uniform.mixins.json | 6 ++ 10 files changed, 164 insertions(+), 52 deletions(-) create mode 100644 fabric-1.20.1/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java create mode 100644 fabric-1.20.1/src/main/resources/uniform.accesswidener create mode 100644 fabric-1.20.1/src/main/resources/uniform.mixins.json create mode 100644 fabric-1.20.6/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java create mode 100644 fabric-1.20.6/src/main/resources/uniform.accesswidener create mode 100644 fabric-1.20.6/src/main/resources/uniform.mixins.json diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java index a043651..86982b9 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -21,7 +21,6 @@ package net.william278.uniform.fabric; -import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -32,10 +31,8 @@ import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandSyntax; import net.william278.uniform.Uniform; import net.william278.uniform.element.ArgumentElement; -import net.william278.uniform.element.CommandElement; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -83,28 +80,6 @@ public class FabricCommand extends BaseCommand { }); } - @Override - @NotNull - @SuppressWarnings("unchecked") - public List> getSyntaxes() { - return super.getSyntaxes().stream().map( - syntax -> new CommandSyntax<>( - syntax.condition(), - syntax.executor(), - syntax.elements().stream() - .filter(e -> e instanceof ArgumentElement) - .map(e -> (ArgumentElement) e) - .map(e -> e.custom() ? new ArgumentElement<>( - e.name(), - StringArgumentType.string(), - e.suggestionProvider() - ) : e) - .map(e -> (CommandElement) e) - .toList() - ) - ).toList(); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new FabricCommand(command)); diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java new file mode 100644 index 0000000..2e06334 --- /dev/null +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java @@ -0,0 +1,68 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.fabric.mixins; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.tree.ArgumentCommandNode; +import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.ArgumentTypes; +import net.minecraft.command.argument.serialize.ArgumentSerializer; +import net.minecraft.command.suggestion.SuggestionProviders; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(targets = "net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode") +public class ArgumentNodeMixin { + + @Mutable + @Final + @Shadow + private String name; + @Mutable + @Final + @Shadow + private ArgumentSerializer.ArgumentTypeProperties properties; + @Mutable + @Final + @Nullable + @Shadow + private Identifier id; // Actually, the suggestion provider. Possibly the worst field name in the entire yarn mappings? + + @Inject(method = "(Lcom/mojang/brigadier/tree/ArgumentCommandNode;)V", at = @At("RETURN")) + private void onConstruct(ArgumentCommandNode node, CallbackInfo ci) { + this.name = node.getName(); + this.id = node.getCustomSuggestions() != null ? SuggestionProviders.computeId(node.getCustomSuggestions()) : null; + try { + this.properties = ArgumentTypes.get(node.getType()).getArgumentTypeProperties(node.getType()); + } catch (IllegalArgumentException e) { + this.properties = ArgumentTypes.get(StringArgumentType.string()).getArgumentTypeProperties(StringArgumentType.string()); + } + } + +} diff --git a/fabric-1.20.1/src/main/resources/fabric.mod.json b/fabric-1.20.1/src/main/resources/fabric.mod.json index 3beba1b..2892d9e 100644 --- a/fabric-1.20.1/src/main/resources/fabric.mod.json +++ b/fabric-1.20.1/src/main/resources/fabric.mod.json @@ -24,5 +24,9 @@ }, "depends": { "fabric-api": "*" - } + }, + "mixins": [ + "uniform.mixins.json" + ], + "accessWidener": "uniform.accesswidener" } \ No newline at end of file diff --git a/fabric-1.20.1/src/main/resources/uniform.accesswidener b/fabric-1.20.1/src/main/resources/uniform.accesswidener new file mode 100644 index 0000000..75137e2 --- /dev/null +++ b/fabric-1.20.1/src/main/resources/uniform.accesswidener @@ -0,0 +1,3 @@ +accessWidener v2 named + +accessible class net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode \ No newline at end of file diff --git a/fabric-1.20.1/src/main/resources/uniform.mixins.json b/fabric-1.20.1/src/main/resources/uniform.mixins.json new file mode 100644 index 0000000..b5c2588 --- /dev/null +++ b/fabric-1.20.1/src/main/resources/uniform.mixins.json @@ -0,0 +1,6 @@ +{ + "package": "net.william278.uniform.fabric.mixins", + "mixins": [ + "ArgumentNodeMixin" + ] +} \ No newline at end of file diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java index a043651..86982b9 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -21,7 +21,6 @@ package net.william278.uniform.fabric; -import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -32,10 +31,8 @@ import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; -import net.william278.uniform.CommandSyntax; import net.william278.uniform.Uniform; import net.william278.uniform.element.ArgumentElement; -import net.william278.uniform.element.CommandElement; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -83,28 +80,6 @@ public class FabricCommand extends BaseCommand { }); } - @Override - @NotNull - @SuppressWarnings("unchecked") - public List> getSyntaxes() { - return super.getSyntaxes().stream().map( - syntax -> new CommandSyntax<>( - syntax.condition(), - syntax.executor(), - syntax.elements().stream() - .filter(e -> e instanceof ArgumentElement) - .map(e -> (ArgumentElement) e) - .map(e -> e.custom() ? new ArgumentElement<>( - e.name(), - StringArgumentType.string(), - e.suggestionProvider() - ) : e) - .map(e -> (CommandElement) e) - .toList() - ) - ).toList(); - } - @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new FabricCommand(command)); diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java new file mode 100644 index 0000000..2e06334 --- /dev/null +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/mixins/ArgumentNodeMixin.java @@ -0,0 +1,68 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.fabric.mixins; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.tree.ArgumentCommandNode; +import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.ArgumentTypes; +import net.minecraft.command.argument.serialize.ArgumentSerializer; +import net.minecraft.command.suggestion.SuggestionProviders; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(targets = "net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode") +public class ArgumentNodeMixin { + + @Mutable + @Final + @Shadow + private String name; + @Mutable + @Final + @Shadow + private ArgumentSerializer.ArgumentTypeProperties properties; + @Mutable + @Final + @Nullable + @Shadow + private Identifier id; // Actually, the suggestion provider. Possibly the worst field name in the entire yarn mappings? + + @Inject(method = "(Lcom/mojang/brigadier/tree/ArgumentCommandNode;)V", at = @At("RETURN")) + private void onConstruct(ArgumentCommandNode node, CallbackInfo ci) { + this.name = node.getName(); + this.id = node.getCustomSuggestions() != null ? SuggestionProviders.computeId(node.getCustomSuggestions()) : null; + try { + this.properties = ArgumentTypes.get(node.getType()).getArgumentTypeProperties(node.getType()); + } catch (IllegalArgumentException e) { + this.properties = ArgumentTypes.get(StringArgumentType.string()).getArgumentTypeProperties(StringArgumentType.string()); + } + } + +} diff --git a/fabric-1.20.6/src/main/resources/fabric.mod.json b/fabric-1.20.6/src/main/resources/fabric.mod.json index 3beba1b..2892d9e 100644 --- a/fabric-1.20.6/src/main/resources/fabric.mod.json +++ b/fabric-1.20.6/src/main/resources/fabric.mod.json @@ -24,5 +24,9 @@ }, "depends": { "fabric-api": "*" - } + }, + "mixins": [ + "uniform.mixins.json" + ], + "accessWidener": "uniform.accesswidener" } \ No newline at end of file diff --git a/fabric-1.20.6/src/main/resources/uniform.accesswidener b/fabric-1.20.6/src/main/resources/uniform.accesswidener new file mode 100644 index 0000000..75137e2 --- /dev/null +++ b/fabric-1.20.6/src/main/resources/uniform.accesswidener @@ -0,0 +1,3 @@ +accessWidener v2 named + +accessible class net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode \ No newline at end of file diff --git a/fabric-1.20.6/src/main/resources/uniform.mixins.json b/fabric-1.20.6/src/main/resources/uniform.mixins.json new file mode 100644 index 0000000..b5c2588 --- /dev/null +++ b/fabric-1.20.6/src/main/resources/uniform.mixins.json @@ -0,0 +1,6 @@ +{ + "package": "net.william278.uniform.fabric.mixins", + "mixins": [ + "ArgumentNodeMixin" + ] +} \ No newline at end of file