feat: register aliases of Fabric commands

dependabot/gradle/org.projectlombok-lombok-1.18.34 1.1.4
William 5 months ago
parent 2a869008cb
commit 1e7297c0b3
No known key found for this signature in database

@ -22,6 +22,7 @@
package net.william278.uniform.fabric; package net.william278.uniform.fabric;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
@ -33,6 +34,7 @@ import net.william278.uniform.Uniform;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
@ -47,14 +49,29 @@ public final class FabricUniform implements Uniform {
static FabricUniform INSTANCE; static FabricUniform INSTANCE;
private final Set<FabricCommand> commands = Sets.newHashSet(); private final Set<FabricCommand> commands = Sets.newHashSet();
@Getter
private final String modId;
@Getter @Getter
@Setter @Setter
Function<Object, CommandUser> commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user); Function<Object, CommandUser> commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user);
private FabricUniform() { private FabricUniform(@NotNull String modId) {
this.modId = modId.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_-]", "");
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
commands.forEach(command -> dispatcher.register(command.createBuilder())) commands.forEach(command -> {
final LiteralArgumentBuilder<ServerCommandSource> builder = command.createBuilder();
dispatcher.register(builder);
final Set<String> aliases = Sets.newHashSet(command.getAliases());
command.getAliases().forEach(a -> aliases.add(modId + ":" + a));
aliases.add(modId + ":" + command.getName());
aliases.forEach(alias -> dispatcher.register(
LiteralArgumentBuilder.<ServerCommandSource>literal(alias)
.requires(builder.getRequirement()).executes(builder.getCommand())
.redirect(builder.build())
));
})
); );
} }
@ -65,8 +82,8 @@ public final class FabricUniform implements Uniform {
* @since 1.0 * @since 1.0
*/ */
@NotNull @NotNull
public static FabricUniform getInstance() { public static FabricUniform getInstance(@NotNull String modId) {
return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform()); return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform(modId));
} }
/** /**

@ -22,6 +22,7 @@
package net.william278.uniform.fabric; package net.william278.uniform.fabric;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
@ -33,6 +34,7 @@ import net.william278.uniform.Uniform;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
@ -47,14 +49,29 @@ public final class FabricUniform implements Uniform {
static FabricUniform INSTANCE; static FabricUniform INSTANCE;
private final Set<FabricCommand> commands = Sets.newHashSet(); private final Set<FabricCommand> commands = Sets.newHashSet();
@Getter
private final String modId;
@Getter @Getter
@Setter @Setter
Function<Object, CommandUser> commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user); Function<Object, CommandUser> commandUserSupplier = (user) -> new FabricCommandUser((ServerCommandSource) user);
private FabricUniform() { private FabricUniform(@NotNull String modId) {
this.modId = modId.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_-]", "");
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) -> CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
commands.forEach(command -> dispatcher.register(command.createBuilder())) commands.forEach(command -> {
final LiteralArgumentBuilder<ServerCommandSource> builder = command.createBuilder();
dispatcher.register(builder);
final Set<String> aliases = Sets.newHashSet(command.getAliases());
command.getAliases().forEach(a -> aliases.add(modId + ":" + a));
aliases.add(modId + ":" + command.getName());
aliases.forEach(alias -> dispatcher.register(
LiteralArgumentBuilder.<ServerCommandSource>literal(alias)
.requires(builder.getRequirement()).executes(builder.getCommand())
.redirect(builder.build())
));
})
); );
} }
@ -65,8 +82,8 @@ public final class FabricUniform implements Uniform {
* @since 1.0 * @since 1.0
*/ */
@NotNull @NotNull
public static FabricUniform getInstance() { public static FabricUniform getInstance(@NotNull String modId) {
return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform()); return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform(modId));
} }
/** /**

@ -3,6 +3,6 @@ javaVersion=17
org.gradle.jvmargs='-Dfile.encoding=UTF-8' org.gradle.jvmargs='-Dfile.encoding=UTF-8'
org.gradle.daemon=true org.gradle.daemon=true
library_version=1.1.3 library_version=1.1.4
library_archive=uniform library_archive=uniform
library_description=Cross-platform wrapper for making Brigadier commands, based on BrigadierWrapper by Tofaa2, itself inspired by emortalmcs command system. library_description=Cross-platform wrapper for making Brigadier commands, based on BrigadierWrapper by Tofaa2, itself inspired by emortalmcs command system.
Loading…
Cancel
Save