1 package com.irtimaled.bbor.client.commands;
3 import com.irtimaled.bbor.client.providers.CustomBeaconProvider;
4 import com.irtimaled.bbor.common.models.Coords;
5 import com.mojang.brigadier.StringReader;
6 import com.mojang.brigadier.arguments.IntegerArgumentType;
7 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
8 import com.mojang.brigadier.context.CommandContext;
9 import com.mojang.brigadier.exceptions.CommandSyntaxException;
10 import net.minecraft.command.CommandSource;
11 import net.minecraft.command.Commands;
12 import net.minecraft.command.arguments.BlockPosArgument;
13 import net.minecraft.util.math.BlockPos;
15 class BeaconCommandBuilder {
16 private static final String LEVEL = "level";
18 static LiteralArgumentBuilder<CommandSource> build(String command) {
19 return Commands.literal(command)
20 .then(Commands.literal(ArgumentNames.ADD)
21 .then(Commands.argument(LEVEL, IntegerArgumentType.integer())
22 .executes(context -> {
23 BlockPos pos = new BlockPos(context.getSource().getPos());
24 int level = IntegerArgumentType.getInteger(context, LEVEL);
25 addBeacon(context, pos, level);
28 .then(Commands.argument(ArgumentNames.POS, BlockPosArgument.blockPos())
29 .executes(context -> {
30 BlockPos pos = BlockPosArgument.getBlockPos(context, ArgumentNames.POS);
31 int level = IntegerArgumentType.getInteger(context, LEVEL);
32 addBeacon(context, pos, level);
36 .then(Commands.literal(ArgumentNames.CLEAR)
37 .executes(context -> {
38 CustomBeaconProvider.clear();
40 CommandHelper.feedback(context, "bbor.commands.beacon.cleared.all");
43 .then(Commands.argument(ArgumentNames.POS, BlockPosArgument.blockPos())
44 .executes(context -> {
45 BlockPos pos = BlockPosArgument.getBlockPos(context, ArgumentNames.POS);
46 boolean removed = CustomBeaconProvider.remove(new Coords(pos));
48 String format = removed ? "bbor.commands.beacon.cleared" : "bbor.commands.beacon.notFound";
49 CommandHelper.feedback(context, format, pos.getX(), pos.getY(), pos.getZ());
54 private static void addBeacon(CommandContext<CommandSource> context, BlockPos pos, int level) throws CommandSyntaxException {
55 if (level < 1 || level > 4) {
56 throw CommandHelper.getException("bbor.commandArgument.invalid",
57 "bbor.commands.beacon.expectedLevelInRange")
58 .createWithContext(new StringReader(context.getInput()));
61 CustomBeaconProvider.add(new Coords(pos), level);
62 CommandHelper.feedback(context, "bbor.commands.beacon.added", pos.getX(), pos.getY(), pos.getZ(), level);