-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ApplySuitCommand
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/mc/duzo/timeless/commands/argument/SuitArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/mc/duzo/timeless/commands/argument/TimelessArgumentRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/mc/duzo/timeless/commands/command/ApplySuitCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/mc/duzo/timeless/commands/command/TimelessCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters