]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/SpawningSphereCommand.java
1acb2ebe6a99b737fafb31a9f864219118d9eda7
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / SpawningSphereCommand.java
1 package com.irtimaled.bbor.client.commands;
2
3 import com.irtimaled.bbor.client.Player;
4 import com.irtimaled.bbor.client.providers.SpawningSphereProvider;
5 import com.mojang.brigadier.CommandDispatcher;
6 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
7 import com.mojang.brigadier.context.CommandContext;
8 import com.mojang.brigadier.exceptions.CommandSyntaxException;
9 import net.minecraft.client.Minecraft;
10 import net.minecraft.command.CommandSource;
11 import net.minecraft.command.Commands;
12 import net.minecraft.command.ISuggestionProvider;
13 import net.minecraft.world.LightType;
14 import net.minecraft.world.World;
15
16 public class SpawningSphereCommand {
17     private static final String COMMAND = "bbor:spawningSphere";
18     private static final String CALCULATE_SPAWNABLE = "calculateSpawnable";
19
20     public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
21         LiteralArgumentBuilder command = Commands.literal(COMMAND)
22                 .then(Commands.literal(ArgumentNames.SET)
23                         .then(Commands.argument(ArgumentNames.POS, Arguments.point())
24                                 .executes(SpawningSphereCommand::setSphere))
25                         .executes(SpawningSphereCommand::setSphere))
26                 .then(Commands.literal(ArgumentNames.CLEAR)
27                         .executes(context -> {
28                             boolean cleared = SpawningSphereProvider.clear();
29
30                             String format = cleared ? "bbor.commands.spawningSphere.cleared" : "bbor.commands.spawningSphere.notSet";
31                             CommandHelper.feedback(context, format);
32                             return 0;
33                         }))
34                 .then(Commands.literal(CALCULATE_SPAWNABLE)
35                         .executes(context -> {
36                             if(!SpawningSphereProvider.hasSpawningSphereInDimension(Player.getDimensionId())) {
37                                 CommandHelper.feedback(context, "bbor.commands.spawningSphere.notSet");
38                                 return 0;
39                             }
40
41                             Counts counts = new Counts();
42                             World world = Minecraft.getInstance().world;
43                             SpawningSphereProvider.calculateSpawnableSpacesCount(pos -> {
44                                 counts.spawnable++;
45                                 if(world.getLightFor(LightType.SKY, pos) > 7)
46                                     counts.nightSpawnable++;
47                             });
48                             SpawningSphereProvider.setSpawnableSpacesCount(counts.spawnable);
49
50                             CommandHelper.feedback(context, "bbor.commands.spawningSphere.calculated",
51                                     String.format("%,d", counts.spawnable),
52                                     String.format("%,d", counts.nightSpawnable));
53                             return 0;
54                         }));
55         commandDispatcher.register(command);
56     }
57
58     public static int setSphere(CommandContext<CommandSource> context) throws CommandSyntaxException {
59         SpawningSphereProvider.setSphere(Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d));
60
61         CommandHelper.feedback(context, "bbor.commands.spawningSphere.set");
62         return 0;
63     }
64
65     private static class Counts {
66         private int spawnable = 0;
67         private int nightSpawnable = 0;
68
69     }
70 }