feat: mixin for server-side custom arg types on Fabric

dependabot/gradle/org.projectlombok-lombok-1.18.34
William 5 months ago
parent b411cc9c57
commit 984a111a09
No known key found for this signature in database

@ -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<ServerCommandSource> {
});
}
@Override
@NotNull
@SuppressWarnings("unchecked")
public List<CommandSyntax<ServerCommandSource>> 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<ServerCommandSource>) e)
.toList()
)
).toList();
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command));

@ -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 <will27528@gmail.com>
* 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 <https://www.gnu.org/licenses/>.
*/
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 = "<init>(Lcom/mojang/brigadier/tree/ArgumentCommandNode;)V", at = @At("RETURN"))
private <A> void onConstruct(ArgumentCommandNode<CommandSource, A> 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());
}
}
}

@ -24,5 +24,9 @@
},
"depends": {
"fabric-api": "*"
}
},
"mixins": [
"uniform.mixins.json"
],
"accessWidener": "uniform.accesswidener"
}

@ -0,0 +1,3 @@
accessWidener v2 named
accessible class net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode

@ -0,0 +1,6 @@
{
"package": "net.william278.uniform.fabric.mixins",
"mixins": [
"ArgumentNodeMixin"
]
}

@ -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<ServerCommandSource> {
});
}
@Override
@NotNull
@SuppressWarnings("unchecked")
public List<CommandSyntax<ServerCommandSource>> 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<ServerCommandSource>) e)
.toList()
)
).toList();
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new FabricCommand(command));

@ -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 <will27528@gmail.com>
* 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 <https://www.gnu.org/licenses/>.
*/
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 = "<init>(Lcom/mojang/brigadier/tree/ArgumentCommandNode;)V", at = @At("RETURN"))
private <A> void onConstruct(ArgumentCommandNode<CommandSource, A> 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());
}
}
}

@ -24,5 +24,9 @@
},
"depends": {
"fabric-api": "*"
}
},
"mixins": [
"uniform.mixins.json"
],
"accessWidener": "uniform.accesswidener"
}

@ -0,0 +1,3 @@
accessWidener v2 named
accessible class net/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$ArgumentNode

@ -0,0 +1,6 @@
{
"package": "net.william278.uniform.fabric.mixins",
"mixins": [
"ArgumentNodeMixin"
]
}
Loading…
Cancel
Save