]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/LineCommandBuilder.java
Add support for custom line
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / LineCommandBuilder.java
1 package com.irtimaled.bbor.client.commands;
2
3 import com.irtimaled.bbor.client.providers.CustomLineProvider;
4 import com.irtimaled.bbor.common.models.Point;
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 LineCommandBuilder {
12     private static final String WIDTH = "width";
13
14     static LiteralArgumentBuilder<CommandSource> build(String command) {
15         return Commands.literal(command)
16                 .then(Commands.literal(ArgumentNames.ADD)
17                         .then(Commands.argument(ArgumentNames.FROM, Arguments.point())
18                                 .then(Commands.argument(ArgumentNames.TO, Arguments.point())
19                                         .executes(LineCommandBuilder::addLine)
20                                         .then(Commands.argument(WIDTH, Arguments.doubleArg())
21                                                 .executes(LineCommandBuilder::addLine)))))
22                 .then(Commands.literal(ArgumentNames.CLEAR)
23                         .executes(context -> {
24                             CustomLineProvider.clear();
25
26                             CommandHelper.feedback(context, "bbor.commands.line.cleared.all");
27                             return 0;
28                         })
29                         .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
30                                 .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
31                                         .executes(context -> {
32                                             Point from = Arguments.getPoint(context, ArgumentNames.FROM).snapXZ(0.5d);
33                                             Point to = Arguments.getPoint(context, ArgumentNames.TO).snapXZ(0.5d);
34                                             boolean removed = CustomLineProvider.remove(from, to);
35
36                                             String format = removed ? "bbor.commands.line.cleared" : "bbor.commands.line.notFound";
37                                             CommandHelper.feedback(context, format,
38                                                     from.getX(), from.getY(), from.getZ(),
39                                                     to.getX(), to.getY(), to.getZ());
40                                             return 0;
41                                         }))));
42     }
43
44     private static int addLine(CommandContext<CommandSource> context) throws CommandSyntaxException {
45         Point from = Arguments.getPoint(context, ArgumentNames.FROM).snapXZ(0.5d);
46         Point to = Arguments.getPoint(context, ArgumentNames.TO).snapXZ(0.5d);
47         Double width = Arguments.getDouble(context, WIDTH);
48         CustomLineProvider.add(from, to, width);
49
50         CommandHelper.feedback(context, "bbor.commands.line.added",
51                 from.getX(), from.getY(), from.getZ(),
52                 to.getX(), to.getY(), to.getZ());
53         return 0;
54     }
55 }