From 919a5b7d9dfc19401980e12d784a878ac555714d Mon Sep 17 00:00:00 2001 From: Irtimaled Date: Thu, 14 May 2020 12:14:26 -0700 Subject: [PATCH] Add support for custom sphere --- .../irtimaled/bbor/client/ClientRenderer.java | 3 ++ .../bbor/client/commands/CustomCommand.java | 4 ++ .../client/commands/SphereCommandBuilder.java | 51 +++++++++++++++++++ .../providers/CustomSphereProvider.java | 39 ++++++++++++++ .../bbor/client/renderers/SphereRenderer.java | 13 +++++ .../bbor/common/models/BoundingBoxSphere.java | 2 +- .../resources/assets/bbor/lang/en_us.json | 5 ++ 7 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/irtimaled/bbor/client/commands/SphereCommandBuilder.java create mode 100644 src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java create mode 100644 src/main/java/com/irtimaled/bbor/client/renderers/SphereRenderer.java diff --git a/src/main/java/com/irtimaled/bbor/client/ClientRenderer.java b/src/main/java/com/irtimaled/bbor/client/ClientRenderer.java index 2bf9a96..66a5d1f 100644 --- a/src/main/java/com/irtimaled/bbor/client/ClientRenderer.java +++ b/src/main/java/com/irtimaled/bbor/client/ClientRenderer.java @@ -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 void registerProvider(IBoundingBoxProvider provider) { diff --git a/src/main/java/com/irtimaled/bbor/client/commands/CustomCommand.java b/src/main/java/com/irtimaled/bbor/client/commands/CustomCommand.java index 2cd9af9..56620ed 100644 --- a/src/main/java/com/irtimaled/bbor/client/commands/CustomCommand.java +++ b/src/main/java/com/irtimaled/bbor/client/commands/CustomCommand.java @@ -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 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 index 0000000..a96b754 --- /dev/null +++ b/src/main/java/com/irtimaled/bbor/client/commands/SphereCommandBuilder.java @@ -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 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 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 index 0000000..1f59e77 --- /dev/null +++ b/src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java @@ -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 { + private static final Map> dimensionCache = new HashMap<>(); + + private static Map 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 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 index 0000000..97cceaf --- /dev/null +++ b/src/main/java/com/irtimaled/bbor/client/renderers/SphereRenderer.java @@ -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 { + @Override + public void render(BoundingBoxSphere boundingBox) { + Point point = boundingBox.getPoint(); + double radius = boundingBox.getRadius(); + renderSphere(point, radius, boundingBox.getColor(), 5, 5); + } +} diff --git a/src/main/java/com/irtimaled/bbor/common/models/BoundingBoxSphere.java b/src/main/java/com/irtimaled/bbor/common/models/BoundingBoxSphere.java index 9c08b1f..5c550f6 100644 --- a/src/main/java/com/irtimaled/bbor/common/models/BoundingBoxSphere.java +++ b/src/main/java/com/irtimaled/bbor/common/models/BoundingBoxSphere.java @@ -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; diff --git a/src/main/resources/assets/bbor/lang/en_us.json b/src/main/resources/assets/bbor/lang/en_us.json index 70930d3..ac14fa3 100644 --- a/src/main/resources/assets/bbor/lang/en_us.json +++ b/src/main/resources/assets/bbor/lang/en_us.json @@ -82,6 +82,11 @@ "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", -- 2.44.0