]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/BoxCommandBuilder.java
422eda0fca8f0db10ea48efb54cd9c79766841b3
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / BoxCommandBuilder.java
1 package com.irtimaled.bbor.client.commands;
2
3 import com.irtimaled.bbor.client.providers.CustomBoxProvider;
4 import com.irtimaled.bbor.common.models.Coords;
5 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
6 import com.mojang.brigadier.context.CommandContext;
7 import com.mojang.brigadier.exceptions.CommandSyntaxException;
8 import net.minecraft.command.CommandSource;
9 import net.minecraft.command.Commands;
10
11 class BoxCommandBuilder {
12     static LiteralArgumentBuilder<CommandSource> build(String command) {
13         return Commands.literal(command)
14                 .then(Commands.literal(ArgumentNames.ADD)
15                         .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
16                                 .executes(BoxCommandBuilder::addBox)
17                                 .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
18                                         .executes(BoxCommandBuilder::addBox))))
19                 .then(Commands.literal(ArgumentNames.CLEAR)
20                         .executes(context -> {
21                             CustomBoxProvider.clear();
22
23                             CommandHelper.feedback(context, "bbor.commands.box.cleared.all");
24                             return 0;
25                         })
26                         .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
27                                 .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
28                                         .executes(context -> {
29                                             Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
30                                             Coords to = Arguments.getCoords(context, ArgumentNames.TO);
31                                             Coords minCoords = getMinCoords(from, to);
32                                             Coords maxCoords = getMaxCoords(from, to);
33                                             boolean removed = CustomBoxProvider.remove(minCoords, maxCoords);
34
35                                             String format = removed ? "bbor.commands.box.cleared" : "bbor.commands.box.notFound";
36                                             CommandHelper.feedback(context, format,
37                                                     from.getX(), from.getY(), from.getZ(),
38                                                     to.getX(), to.getY(), to.getZ());
39                                             return 0;
40                                         }))));
41     }
42
43     private static int addBox(CommandContext<CommandSource> context) throws CommandSyntaxException {
44         Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
45         Coords to = Arguments.getCoords(context, ArgumentNames.TO);
46         Coords minCoords = getMinCoords(from, to);
47         Coords maxCoords = getMaxCoords(from, to);
48         CustomBoxProvider.add(minCoords, maxCoords);
49
50         CommandHelper.feedback(context, "bbor.commands.box.added",
51                 from.getX(), from.getY(), from.getZ(),
52                 to.getX(), to.getY(), to.getZ());
53         return 0;
54     }
55
56     private static Coords getMaxCoords(Coords from, Coords to) {
57         return new Coords(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
58     }
59
60     private static Coords getMinCoords(Coords from, Coords to) {
61         return new Coords(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
62     }
63 }