]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Add support for custom sphere
authorIrtimaled <irtimaled@gmail.com>
Thu, 14 May 2020 19:14:26 +0000 (12:14 -0700)
committerIrtimaled <irtimaled@gmail.com>
Mon, 18 May 2020 00:31:27 +0000 (17:31 -0700)
src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
src/main/java/com/irtimaled/bbor/client/commands/CustomCommand.java
src/main/java/com/irtimaled/bbor/client/commands/SphereCommandBuilder.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/renderers/SphereRenderer.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/common/models/BoundingBoxSphere.java
src/main/resources/assets/bbor/lang/en_us.json

index 2bf9a962e648117fde9fd6bd2a9aac604f5aeb94..66a5d1f0606b1c3a01f6ac02dc66b8ffb0566bbb 100644 (file)
@@ -8,6 +8,7 @@ import com.irtimaled.bbor.client.renderers.*;
 import com.irtimaled.bbor.common.MathHelper;
 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
 import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
+import com.irtimaled.bbor.common.models.BoundingBoxSphere;
 import com.irtimaled.bbor.common.models.BoundingBoxVillage;
 import org.lwjgl.opengl.GL11;
 
@@ -50,6 +51,7 @@ public class ClientRenderer {
         registerRenderer(BoundingBoxConduit.class, new ConduitRenderer());
         registerRenderer(BoundingBoxSpawnableBlocks.class, new SpawnableBlocksRenderer());
         registerRenderer(BoundingBoxLine.class, new LineRenderer());
+        registerRenderer(BoundingBoxSphere.class, new SphereRenderer());
 
         registerProvider(new SlimeChunkProvider());
         registerProvider(new WorldSpawnProvider());
@@ -62,6 +64,7 @@ public class ClientRenderer {
         registerProvider(new ConduitProvider());
         registerProvider(new SpawnableBlocksProvider());
         registerProvider(new CustomLineProvider());
+        registerProvider(new CustomSphereProvider());
     }
 
     public static <T extends AbstractBoundingBox> void registerProvider(IBoundingBoxProvider<T> provider) {
index 2cd9af9c5c904a7f4b37e7264d69cca256b9e03f..56620ed084fff3879b00ccf0ae73616fb1384e82 100644 (file)
@@ -3,6 +3,7 @@ package com.irtimaled.bbor.client.commands;
 import com.irtimaled.bbor.client.providers.CustomBeaconProvider;
 import com.irtimaled.bbor.client.providers.CustomBoxProvider;
 import com.irtimaled.bbor.client.providers.CustomLineProvider;
+import com.irtimaled.bbor.client.providers.CustomSphereProvider;
 import com.mojang.brigadier.CommandDispatcher;
 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
 import net.minecraft.command.Commands;
@@ -13,17 +14,20 @@ public class CustomCommand {
     private static final String BOX = "box";
     private static final String BEACON = "beacon";
     private static final String LINE = "line";
+    private static final String SPHERE = "sphere";
 
     public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
         LiteralArgumentBuilder command = Commands.literal(COMMAND)
                 .then(BoxCommandBuilder.build(BOX))
                 .then(BeaconCommandBuilder.build(BEACON))
                 .then(LineCommandBuilder.build(LINE))
+                .then(SphereCommandBuilder.build(SPHERE))
                 .then(Commands.literal(ArgumentNames.CLEAR)
                         .executes(context -> {
                             CustomBoxProvider.clear();
                             CustomBeaconProvider.clear();
                             CustomLineProvider.clear();
+                            CustomSphereProvider.clear();
 
                             CommandHelper.feedback(context, "bbor.commands.custom.cleared.all");
                             return 0;
diff --git a/src/main/java/com/irtimaled/bbor/client/commands/SphereCommandBuilder.java b/src/main/java/com/irtimaled/bbor/client/commands/SphereCommandBuilder.java
new file mode 100644 (file)
index 0000000..a96b754
--- /dev/null
@@ -0,0 +1,51 @@
+package com.irtimaled.bbor.client.commands;
+
+import com.irtimaled.bbor.client.providers.CustomSphereProvider;
+import com.irtimaled.bbor.common.models.Point;
+import com.mojang.brigadier.builder.LiteralArgumentBuilder;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import net.minecraft.command.CommandSource;
+import net.minecraft.command.Commands;
+
+class SphereCommandBuilder {
+    public static final String RADIUS = "radius";
+
+    static LiteralArgumentBuilder<CommandSource> build(String command) {
+        return Commands.literal(command)
+                .then(Commands.literal(ArgumentNames.ADD)
+                        .then(Commands.argument(ArgumentNames.POS, Arguments.point())
+                                .then(Commands.argument(RADIUS, Arguments.integer())
+                                        .executes(SphereCommandBuilder::addSphere)))
+                        .then(Commands.argument(RADIUS, Arguments.integer())
+                                .executes(SphereCommandBuilder::addSphere)))
+                .then(Commands.literal(ArgumentNames.CLEAR)
+                        .executes(context -> {
+                            CustomSphereProvider.clear();
+
+                            CommandHelper.feedback(context, "bbor.commands.sphere.cleared.all");
+                            return 0;
+                        })
+                        .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
+                                .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
+                                        .executes(context -> {
+                                            Point pos = Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d);
+                                            boolean removed = CustomSphereProvider.remove(pos);
+
+                                            String format = removed ? "bbor.commands.sphere.cleared" : "bbor.commands.sphere.notFound";
+                                            CommandHelper.feedback(context, format,
+                                                    pos.getX(), pos.getY(), pos.getZ());
+                                            return 0;
+                                        }))));
+    }
+
+    private static int addSphere(CommandContext<CommandSource> context) throws CommandSyntaxException {
+        Point pos = Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d);
+        int radius = Arguments.getInteger(context, RADIUS);
+        CustomSphereProvider.add(pos, radius);
+
+        CommandHelper.feedback(context, "bbor.commands.sphere.added",
+                pos.getX(), pos.getY(), pos.getZ(), radius);
+        return 0;
+    }
+}
diff --git a/src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java b/src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java
new file mode 100644 (file)
index 0000000..1f59e77
--- /dev/null
@@ -0,0 +1,39 @@
+package com.irtimaled.bbor.client.providers;
+
+import com.irtimaled.bbor.client.Player;
+import com.irtimaled.bbor.common.BoundingBoxType;
+import com.irtimaled.bbor.common.models.BoundingBoxSphere;
+import com.irtimaled.bbor.common.models.Point;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class CustomSphereProvider implements IBoundingBoxProvider<BoundingBoxSphere> {
+    private static final Map<Integer, Map<Integer, BoundingBoxSphere>> dimensionCache = new HashMap<>();
+
+    private static Map<Integer, BoundingBoxSphere> getCache(int dimensionId) {
+        return dimensionCache.computeIfAbsent(dimensionId, i -> new ConcurrentHashMap<>());
+    }
+
+    public static void add(Point center, double radius) {
+        int dimensionId = Player.getDimensionId();
+        int cacheKey = center.hashCode();
+        BoundingBoxSphere sphere = new BoundingBoxSphere(center, radius, BoundingBoxType.Custom);
+        getCache(dimensionId).put(cacheKey, sphere);
+    }
+
+    public static boolean remove(Point center) {
+        int dimensionId = Player.getDimensionId();
+        int cacheKey = center.hashCode();
+        return getCache(dimensionId).remove(cacheKey) != null;
+    }
+
+    public static void clear() {
+        dimensionCache.values().forEach(Map::clear);
+    }
+
+    public Iterable<BoundingBoxSphere> get(int dimensionId) {
+        return getCache(dimensionId).values();
+    }
+}
diff --git a/src/main/java/com/irtimaled/bbor/client/renderers/SphereRenderer.java b/src/main/java/com/irtimaled/bbor/client/renderers/SphereRenderer.java
new file mode 100644 (file)
index 0000000..97cceaf
--- /dev/null
@@ -0,0 +1,13 @@
+package com.irtimaled.bbor.client.renderers;
+
+import com.irtimaled.bbor.common.models.BoundingBoxSphere;
+import com.irtimaled.bbor.common.models.Point;
+
+public class SphereRenderer extends AbstractRenderer<BoundingBoxSphere> {
+    @Override
+    public void render(BoundingBoxSphere boundingBox) {
+        Point point = boundingBox.getPoint();
+        double radius = boundingBox.getRadius();
+        renderSphere(point, radius, boundingBox.getColor(), 5, 5);
+    }
+}
index 9c08b1fad94f4748e45e1165eabee639a1227e7c..5c550f666f8e88da087e9e896d76902e501070dc 100644 (file)
@@ -10,7 +10,7 @@ public class BoundingBoxSphere extends AbstractBoundingBox {
     private final double maxZ;
     private final Point point;
 
-    protected BoundingBoxSphere(Point point, double radius, BoundingBoxType type) {
+    public BoundingBoxSphere(Point point, double radius, BoundingBoxType type) {
         super(type);
         this.radius = radius;
         this.point = point;
index 70930d3c37b1ffc70b86daae7f292d3d0c4f976d..ac14fa30a4c46f51e5f38a066e11003ae920db68 100644 (file)
   "bbor.commands.line.cleared.all": "Line cleared with start [x=%d, y=%d, z=%d] and end [x=%d, y=%d, z=%d]",
   "bbor.commands.line.notFound":  "No line found with start [x=%d, y=%d, z=%d] and end [x=%d, y=%d, z=%d]",
 
+  "bbor.commands.sphere.added": "Sphere added with center [x=%d, y=%d, z=%d] and radius %d",
+  "bbor.commands.sphere.cleared":  "All spheres cleared",
+  "bbor.commands.sphere.cleared.all": "Sphere cleared with center [x=%d, y=%d, z=%d]",
+  "bbor.commands.sphere.notFound":  "No sphere found with center [x=%d, y=%d, z=%d]",
+
   "bbor.commands.spawningSphere.set": "Spawning sphere set",
   "bbor.commands.spawningSphere.notSet": "No spawning sphere set",
   "bbor.commands.spawningSphere.cleared": "Spawning sphere cleared",