]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/BeaconCommandBuilder.java
2259458eeb6c9fd9fe31b41984ad9ba95ef55534
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / BeaconCommandBuilder.java
1 package com.irtimaled.bbor.client.commands;
2
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;
14
15 class BeaconCommandBuilder {
16     private static final String LEVEL = "level";
17
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);
26                                     return 0;
27                                 })
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);
33                                             return 0;
34                                         })))
35                 )
36                 .then(Commands.literal(ArgumentNames.CLEAR)
37                         .executes(context -> {
38                             CustomBeaconProvider.clear();
39
40                             CommandHelper.feedback(context, "bbor.commands.beacon.cleared.all");
41                             return 0;
42                         })
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));
47
48                                     String format = removed ? "bbor.commands.beacon.cleared" : "bbor.commands.beacon.notFound";
49                                     CommandHelper.feedback(context, format, pos.getX(), pos.getY(), pos.getZ());
50                                     return 0;
51                                 })));
52     }
53
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()));
59         }
60
61         CustomBeaconProvider.add(new Coords(pos), level);
62         CommandHelper.feedback(context, "bbor.commands.beacon.added", pos.getX(), pos.getY(), pos.getZ(), level);
63     }
64 }