]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/BeaconCommand.java
Add logic to load structures from save files
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / BeaconCommand.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.CommandDispatcher;
6 import com.mojang.brigadier.StringReader;
7 import com.mojang.brigadier.arguments.IntegerArgumentType;
8 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
9 import com.mojang.brigadier.context.CommandContext;
10 import com.mojang.brigadier.exceptions.CommandSyntaxException;
11 import net.minecraft.command.CommandSource;
12 import net.minecraft.command.Commands;
13 import net.minecraft.command.ISuggestionProvider;
14 import net.minecraft.command.arguments.BlockPosArgument;
15 import net.minecraft.util.math.BlockPos;
16
17 public class BeaconCommand {
18     private static final String COMMAND = "bbor:beacon";
19     private static final String ADD = "add";
20     private static final String CLEAR = "clear";
21     private static final String POS = "pos";
22     private static final String LEVEL = "level";
23
24     public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
25         LiteralArgumentBuilder command = Commands.literal(COMMAND)
26                 .then(Commands.literal(ADD)
27                         .then(Commands.argument(LEVEL, IntegerArgumentType.integer())
28                                 .executes(context -> {
29                                     BlockPos pos = new BlockPos(context.getSource().getPos());
30                                     int level = IntegerArgumentType.getInteger(context, LEVEL);
31                                     addBeacon(context, pos, level);
32                                     return 0;
33                                 })
34                                 .then(Commands.argument(POS, BlockPosArgument.blockPos())
35                                         .executes(context -> {
36                                             BlockPos pos = BlockPosArgument.getBlockPos(context, POS);
37                                             int level = IntegerArgumentType.getInteger(context, LEVEL);
38                                             addBeacon(context, pos, level);
39                                             return 0;
40                                         })))
41                 )
42                 .then(Commands.literal(CLEAR)
43                         .executes(context -> {
44                             CustomBeaconProvider.clear();
45
46                             CommandHelper.feedback(context, "bbor.commands.beacon.cleared.all");
47                             return 0;
48                         })
49                         .then(Commands.argument(POS, BlockPosArgument.blockPos())
50                                 .executes(context -> {
51                                     BlockPos pos = BlockPosArgument.getBlockPos(context, POS);
52                                     boolean removed = CustomBeaconProvider.remove(new Coords(pos));
53
54                                     String format = removed ? "bbor.commands.beacon.cleared" : "bbor.commands.beacon.notFound";
55                                     CommandHelper.feedback(context, format, pos.getX(), pos.getY(), pos.getZ());
56                                     return 0;
57                                 })));
58         commandDispatcher.register(command);
59     }
60
61     private static void addBeacon(CommandContext<CommandSource> context, BlockPos pos, int level) throws CommandSyntaxException {
62         if (level < 1 || level > 4) {
63             throw CommandHelper.getException("bbor.commandArgument.invalid",
64                     "bbor.commands.beacon.expectedLevelInRange")
65                     .createWithContext(new StringReader(context.getInput()));
66         }
67
68         CustomBeaconProvider.add(new Coords(pos), level);
69         CommandHelper.feedback(context, "bbor.commands.beacon.added", pos.getX(), pos.getY(), pos.getZ(), level);
70     }
71 }
72