Skip to content

Commit

Permalink
feat: commands
Browse files Browse the repository at this point in the history
feat: ApplySuitCommand
  • Loading branch information
Duzos committed Sep 30, 2024
1 parent 79ef71f commit e095028
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mc.duzo.timeless.commands.argument;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import mc.duzo.timeless.suit.Suit;
import mc.duzo.timeless.suit.SuitRegistry;

public class SuitArgumentType implements ArgumentType<Suit> {
private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");

public static SuitArgumentType suit() {
return new SuitArgumentType();
}

public static Suit getSuit(CommandContext<ServerCommandSource> context, String name) {
return context.getArgument(name, Suit.class);
}

public Suit parse(StringReader stringReader) throws CommandSyntaxException {
Identifier id = Identifier.fromCommandInput(stringReader);
Suit found = SuitRegistry.REGISTRY.get(id);

if (found == null) throw new SimpleCommandExceptionType(Text.literal("Suit not found in registry")).create();

return found;
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return CommandSource.suggestMatching(SuitRegistry.REGISTRY.stream().map(Suit::id).map(Identifier::toString), builder);
}

public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mc.duzo.timeless.commands.argument;

import java.util.function.Supplier;

import com.mojang.brigadier.arguments.ArgumentType;
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;

import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
import net.minecraft.util.Identifier;

import mc.duzo.timeless.Timeless;

public class TimelessArgumentRegister {
public static void register() {
register("suit", SuitArgumentType.class, SuitArgumentType::suit);
}

private static <T extends ArgumentType<?>> void register(String name, Class<T> t, Supplier<T> supplier) {
ArgumentTypeRegistry.registerArgumentType(new Identifier(Timeless.MOD_ID, name), t, ConstantArgumentSerializer.of(supplier));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package mc.duzo.timeless.commands.command;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;

import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

import mc.duzo.timeless.Timeless;
import mc.duzo.timeless.commands.argument.SuitArgumentType;
import mc.duzo.timeless.suit.Suit;
public class ApplySuitCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal(Timeless.MOD_ID)
.then(literal("suit").requires(source -> source.hasPermissionLevel(2))
.then(literal("wear")
.then(argument("suit", SuitArgumentType.suit())
.executes(ApplySuitCommand::runCommand)))
));
}

private static int runCommand(CommandContext<ServerCommandSource> context) {
ServerPlayerEntity player = context.getSource().getPlayer();

if (player == null) return 0;

Suit suit = SuitArgumentType.getSuit(context, "suit");
suit.getSet().wear(player);

return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mc.duzo.timeless.commands.command;

import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;

import net.minecraft.server.command.ServerCommandSource;

import mc.duzo.timeless.commands.argument.TimelessArgumentRegister;

public class TimelessCommands {
public static void init() {
TimelessArgumentRegister.register();
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> registerCommands(dispatcher)));
}

private static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
ApplySuitCommand.register(dispatcher);
}
}
2 changes: 2 additions & 0 deletions src/main/java/mc/duzo/timeless/registry/Register.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.util.Identifier;

import mc.duzo.timeless.Timeless;
import mc.duzo.timeless.commands.command.TimelessCommands;
import mc.duzo.timeless.power.PowerRegistry;
import mc.duzo.timeless.suit.SuitRegistry;
import mc.duzo.timeless.suit.client.animation.SuitAnimationTracker;
Expand Down Expand Up @@ -106,6 +107,7 @@ public static void init() {
Sounds.init();
Trackers.init();
Entities.init();
TimelessCommands.init();
}

public static <V, T extends V> T register(Registry<V> registry, String name, T entry) {
Expand Down

0 comments on commit e095028

Please sign in to comment.