--- /dev/null
+package com.irtimaled.bbor.client.commands;
+
+import com.irtimaled.bbor.common.models.Coords;
+import com.irtimaled.bbor.common.models.Point;
+import com.mojang.brigadier.arguments.BoolArgumentType;
+import com.mojang.brigadier.arguments.DoubleArgumentType;
+import com.mojang.brigadier.arguments.IntegerArgumentType;
+import com.mojang.brigadier.arguments.StringArgumentType;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import net.minecraft.command.CommandSource;
+import net.minecraft.command.arguments.BlockPosArgument;
+import net.minecraft.command.arguments.Vec3Argument;
+
+import java.util.function.Supplier;
+
+public class Arguments {
+ public static BlockPosArgument coords() {
+ return BlockPosArgument.blockPos();
+ }
+
+ public static Vec3Argument point() {
+ return Vec3Argument.vec3();
+ }
+
+ public static IntegerArgumentType integer() {
+ return IntegerArgumentType.integer();
+ }
+
+ public static IntegerArgumentType integer(int min, int max) {
+ return IntegerArgumentType.integer(min, max);
+ }
+
+ public static DoubleArgumentType doubleArg() {
+ return DoubleArgumentType.doubleArg();
+ }
+
+ public static StringArgumentType string() {
+ return StringArgumentType.greedyString();
+ }
+
+ public static BoolArgumentType bool() {
+ return BoolArgumentType.bool();
+ }
+
+ public static Coords getCoords(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return new Coords(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
+ }
+
+ public static Point getPoint(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return new Point(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
+ }
+
+ public static int getInteger(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return getArgumentValueOrDefault(context, name, IntegerArgumentType::getInteger, () -> 0);
+ }
+
+ public static double getDouble(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return getArgumentValueOrDefault(context, name, DoubleArgumentType::getDouble, () -> 0.0D);
+ }
+
+ public static String getString(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return getArgumentValueOrDefault(context, name, StringArgumentType::getString, () -> "");
+ }
+
+ public static boolean getBool(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
+ return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
+ }
+
+
+ public static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
+ String name,
+ ArgumentFetcher<T> getValue,
+ Supplier<T> defaultValue) throws CommandSyntaxException {
+ try {
+ return getValue.get(context, name);
+ } catch (IllegalArgumentException exception) {
+ return defaultValue.get();
+ }
+ }
+
+ @FunctionalInterface
+ private interface ArgumentFetcher<T> {
+ T get(CommandContext<CommandSource> context, String name) throws CommandSyntaxException;
+ }
+}
import com.irtimaled.bbor.client.providers.CustomBeaconProvider;
import com.irtimaled.bbor.common.models.Coords;
-import com.mojang.brigadier.StringReader;
-import com.mojang.brigadier.arguments.IntegerArgumentType;
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;
-import net.minecraft.command.arguments.BlockPosArgument;
-import net.minecraft.util.math.BlockPos;
class BeaconCommandBuilder {
private static final String LEVEL = "level";
static LiteralArgumentBuilder<CommandSource> build(String command) {
return Commands.literal(command)
.then(Commands.literal(ArgumentNames.ADD)
- .then(Commands.argument(LEVEL, IntegerArgumentType.integer())
- .executes(context -> {
- BlockPos pos = new BlockPos(context.getSource().getPos());
- int level = IntegerArgumentType.getInteger(context, LEVEL);
- addBeacon(context, pos, level);
- return 0;
- })
- .then(Commands.argument(ArgumentNames.POS, BlockPosArgument.blockPos())
- .executes(context -> {
- BlockPos pos = BlockPosArgument.getBlockPos(context, ArgumentNames.POS);
- int level = IntegerArgumentType.getInteger(context, LEVEL);
- addBeacon(context, pos, level);
- return 0;
- })))
+ .then(Commands.argument(LEVEL, Arguments.integer(1,4))
+ .executes(BeaconCommandBuilder::addBeacon)
+ .then(Commands.argument(ArgumentNames.POS, Arguments.coords())
+ .executes(BeaconCommandBuilder::addBeacon)))
)
.then(Commands.literal(ArgumentNames.CLEAR)
.executes(context -> {
CommandHelper.feedback(context, "bbor.commands.beacon.cleared.all");
return 0;
})
- .then(Commands.argument(ArgumentNames.POS, BlockPosArgument.blockPos())
+ .then(Commands.argument(ArgumentNames.POS, Arguments.coords())
.executes(context -> {
- BlockPos pos = BlockPosArgument.getBlockPos(context, ArgumentNames.POS);
- boolean removed = CustomBeaconProvider.remove(new Coords(pos));
+ Coords coords = Arguments.getCoords(context, ArgumentNames.POS);
+ boolean removed = CustomBeaconProvider.remove(coords);
String format = removed ? "bbor.commands.beacon.cleared" : "bbor.commands.beacon.notFound";
- CommandHelper.feedback(context, format, pos.getX(), pos.getY(), pos.getZ());
+ CommandHelper.feedback(context, format, coords.getX(), coords.getY(), coords.getZ());
return 0;
})));
}
- private static void addBeacon(CommandContext<CommandSource> context, BlockPos pos, int level) throws CommandSyntaxException {
- if (level < 1 || level > 4) {
- throw CommandHelper.getException("bbor.commandArgument.invalid",
- "bbor.commands.beacon.expectedLevelInRange")
- .createWithContext(new StringReader(context.getInput()));
- }
+ private static int addBeacon(CommandContext<CommandSource> context) throws CommandSyntaxException {
+ Coords coords = Arguments.getCoords(context, ArgumentNames.POS);
+ int level = Arguments.getInteger(context, LEVEL);
- CustomBeaconProvider.add(new Coords(pos), level);
- CommandHelper.feedback(context, "bbor.commands.beacon.added", pos.getX(), pos.getY(), pos.getZ(), level);
+ CustomBeaconProvider.add(coords, level);
+ CommandHelper.feedback(context, "bbor.commands.beacon.added", coords.getX(), coords.getY(), coords.getZ(), level);
+ return 0;
}
}
import com.irtimaled.bbor.client.providers.CustomBoxProvider;
import com.irtimaled.bbor.common.models.Coords;
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;
-import net.minecraft.command.arguments.BlockPosArgument;
-import net.minecraft.util.math.BlockPos;
class BoxCommandBuilder {
static LiteralArgumentBuilder<CommandSource> build(String command) {
return Commands.literal(command)
.then(Commands.literal(ArgumentNames.ADD)
- .then(Commands.argument(ArgumentNames.FROM, BlockPosArgument.blockPos())
- .then(Commands.argument(ArgumentNames.TO, BlockPosArgument.blockPos())
- .executes(context -> {
- BlockPos from = BlockPosArgument.getBlockPos(context, ArgumentNames.FROM);
- BlockPos to = BlockPosArgument.getBlockPos(context, ArgumentNames.TO);
- Coords minCoords = getMinCoords(from, to);
- Coords maxCoords = getMaxCoords(from, to);
- CustomBoxProvider.add(minCoords, maxCoords);
-
- CommandHelper.feedback(context, "bbor.commands.box.added",
- from.getX(), from.getY(), from.getZ(),
- to.getX(), to.getY(), to.getZ());
- return 0;
- }))))
+ .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
+ .executes(BoxCommandBuilder::addBox)
+ .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
+ .executes(BoxCommandBuilder::addBox))))
.then(Commands.literal(ArgumentNames.CLEAR)
.executes(context -> {
CustomBoxProvider.clear();
CommandHelper.feedback(context, "bbor.commands.box.cleared.all");
return 0;
})
- .then(Commands.argument(ArgumentNames.FROM, BlockPosArgument.blockPos())
- .then(Commands.argument(ArgumentNames.TO, BlockPosArgument.blockPos())
+ .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
+ .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
.executes(context -> {
- BlockPos from = BlockPosArgument.getBlockPos(context, ArgumentNames.FROM);
- BlockPos to = BlockPosArgument.getBlockPos(context, ArgumentNames.TO);
+ Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
+ Coords to = Arguments.getCoords(context, ArgumentNames.TO);
Coords minCoords = getMinCoords(from, to);
Coords maxCoords = getMaxCoords(from, to);
boolean removed = CustomBoxProvider.remove(minCoords, maxCoords);
}))));
}
- private static Coords getMaxCoords(BlockPos from, BlockPos to) {
+ private static int addBox(CommandContext<CommandSource> context) throws CommandSyntaxException {
+ Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
+ Coords to = Arguments.getCoords(context, ArgumentNames.TO);
+ Coords minCoords = getMinCoords(from, to);
+ Coords maxCoords = getMaxCoords(from, to);
+ CustomBoxProvider.add(minCoords, maxCoords);
+
+ CommandHelper.feedback(context, "bbor.commands.box.added",
+ from.getX(), from.getY(), from.getZ(),
+ to.getX(), to.getY(), to.getZ());
+ return 0;
+ }
+
+ private static Coords getMaxCoords(Coords from, Coords to) {
return new Coords(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
}
- private static Coords getMinCoords(BlockPos from, BlockPos to) {
+ private static Coords getMinCoords(Coords from, Coords to) {
return new Coords(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
}
}
package com.irtimaled.bbor.client.commands;
import com.mojang.brigadier.context.CommandContext;
-import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.command.CommandSource;
-import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
class CommandHelper {
static void feedback(CommandContext<CommandSource> context, String format, Object... values) {
context.getSource().sendFeedback(new TextComponentTranslation(format, values), false);
}
-
- static SimpleCommandExceptionType getException(String message, String... values) {
- ITextComponent textComponent = new TextComponentTranslation(message);
-
- int length = values.length;
- if (length > 0) {
- textComponent.appendText(" (");
- for (int idx = 0; idx < length; idx++) {
- if (idx > 0) textComponent.appendText(", ");
- ITextComponent suggestion = new TextComponentTranslation(values[idx]);
- textComponent.appendSibling(suggestion);
- }
- textComponent.appendText(")");
- }
- return new SimpleCommandExceptionType(textComponent);
- }
}
import com.irtimaled.bbor.client.config.Setting;
import com.irtimaled.bbor.client.gui.SettingsScreen;
import com.mojang.brigadier.CommandDispatcher;
-import com.mojang.brigadier.arguments.BoolArgumentType;
-import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
LiteralArgumentBuilder<CommandSource> command = Commands.literal(setting.getName());
switch (setting.getType()) {
case 'B':
- return command.then(Commands.argument(VALUE, BoolArgumentType.bool())
+ return command.then(Commands.argument(VALUE, Arguments.bool())
.executes(context -> {
- boolean value = BoolArgumentType.getBool(context, VALUE);
+ boolean value = Arguments.getBool(context, VALUE);
((Setting<Boolean>) setting).set(value);
return 0;
}));
case 'I':
- return command.then(Commands.argument(VALUE, IntegerArgumentType.integer())
+ return command.then(Commands.argument(VALUE, Arguments.integer())
.executes(context -> {
- int value = IntegerArgumentType.getInteger(context, VALUE);
+ int value = Arguments.getInteger(context, VALUE);
((Setting<Integer>) setting).set(value);
return 0;
}));
CustomBoxProvider.clear();
CustomBeaconProvider.clear();
- CommandHelper.feedback(context, "bbor.commands.box.cleared.all");
+ CommandHelper.feedback(context, "bbor.commands.custom.cleared.all");
return 0;
}));
commandDispatcher.register(command);
import com.irtimaled.bbor.client.providers.SlimeChunkProvider;
import com.mojang.brigadier.CommandDispatcher;
-import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
LiteralArgumentBuilder command = Commands.literal(COMMAND)
- .then(Commands.argument(SEED, StringArgumentType.string())
+ .then(Commands.argument(SEED, Arguments.string())
.executes(context -> {
- String argument = StringArgumentType.getString(context, SEED);
+ String argument = Arguments.getString(context, SEED);
handleSeedCommand(argument);
return 0;
}));
import com.irtimaled.bbor.client.providers.SpawningSphereProvider;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.client.Minecraft;
+import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
-import net.minecraft.command.arguments.Vec3Argument;
-import net.minecraft.util.math.Vec3d;
import net.minecraft.world.EnumLightType;
import net.minecraft.world.World;
public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
LiteralArgumentBuilder command = Commands.literal(COMMAND)
.then(Commands.literal(ArgumentNames.SET)
- .then(Commands.argument(ArgumentNames.POS, Vec3Argument.vec3())
- .executes(context -> {
- Vec3d pos = Vec3Argument.getVec3(context, ArgumentNames.POS);
- SpawningSphereProvider.setSphere(pos.x, pos.y, pos.z);
-
- CommandHelper.feedback(context, "bbor.commands.spawningSphere.set");
- return 0;
- }))
- .executes(context -> {
- Vec3d pos = context.getSource().getPos();
- SpawningSphereProvider.setSphere(pos.x, pos.y, pos.z);
-
- CommandHelper.feedback(context, "bbor.commands.spawningSphere.set");
- return 0;
- }))
+ .then(Commands.argument(ArgumentNames.POS, Arguments.point())
+ .executes(SpawningSphereCommand::setSphere))
+ .executes(SpawningSphereCommand::setSphere))
.then(Commands.literal(ArgumentNames.CLEAR)
.executes(context -> {
boolean cleared = SpawningSphereProvider.clear();
commandDispatcher.register(command);
}
+ public static int setSphere(CommandContext<CommandSource> context) throws CommandSyntaxException {
+ SpawningSphereProvider.setSphere(Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d));
+
+ CommandHelper.feedback(context, "bbor.commands.spawningSphere.set");
+ return 0;
+ }
+
private static class Counts {
private int spawnable = 0;
private int nightSpawnable = 0;
private static BoundingBoxSpawningSphere spawningSphere;
private static Integer dimensionId;
- public static void setSphere(double x, double y, double z) {
- Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
-
+ public static void setSphere(Point point) {
if (spawningSphere != null && spawningSphere.getPoint().equals(point)) return;
clear();
lastBoundingBox = null;
}
- private static double snapToNearestHalf(double value) {
- int floor = MathHelper.floor(value);
- int fraction = MathHelper.floor((value - floor) * 4.0);
- if (fraction % 2 == 1) fraction++;
- return floor + (fraction / 4.0);
- }
-
public static boolean clear() {
if (spawningSphere != null) {
lastBoundingBox = null;
}
private void renderActivationLine(OffsetPoint centerPoint) {
- OffsetPoint playerPos = new OffsetPoint(Player.getX(), Player.getY(), Player.getZ());
+ OffsetPoint playerPos = new OffsetPoint(Player.getPoint());
double distance = centerPoint.getDistance(playerPos);
if (distance <= 20) {
Color color = distance > 18 ? Color.RED : distance > 16 ? Colors.DARK_ORANGE : Color.GREEN;
int intValue = (int) value;
return value >= intValue ? intValue : intValue - 1;
}
+
+ public static double snapToNearest(double value, double nearest) {
+ double multiplier = 2.0 / nearest;
+ int floor = floor(value);
+ int fraction = floor((value - floor) * multiplier);
+ int midpoint = (int) (multiplier / 2);
+ if (fraction % midpoint == 1) fraction++;
+ return floor + (fraction / multiplier);
+ }
}
import com.irtimaled.bbor.common.MathHelper;
import com.irtimaled.bbor.common.TypeHelper;
-import net.minecraft.util.math.BlockPos;
+import net.minecraft.util.math.Vec3d;
+import net.minecraft.util.math.Vec3i;
public class Coords {
private final int x;
this.z = MathHelper.floor(z);
}
- public Coords(BlockPos blockPos) {
- this(blockPos.getX(), blockPos.getY(), blockPos.getZ());
+ public Coords(Vec3i pos) {
+ this(pos.getX(), pos.getY(), pos.getZ());
}
+ public Coords(Vec3d pos) { this(pos.x, pos.y, pos.z); }
+
public int getX() {
return x;
}
package com.irtimaled.bbor.common.models;
+import com.irtimaled.bbor.common.MathHelper;
import com.irtimaled.bbor.common.TypeHelper;
+import net.minecraft.util.math.Vec3d;
public class Point {
private final double x;
this.z = Coords.getZ();
}
+ public Point(Vec3d pos) {
+ this.x = pos.x;
+ this.y = pos.y;
+ this.z = pos.z;
+ }
+
public double getX() {
return x;
}
return new Point(this.x + x, this.y + y, this.z + z);
}
+ public Point snapXZ(double nearest) {
+ double x = MathHelper.snapToNearest(this.x, nearest);
+ double z = MathHelper.snapToNearest(this.z, nearest);
+ return new Point(x, this.y, z);
+ }
+
public double getDistance(Point point) {
double dx = this.x - point.x;
double dy = this.y - point.y;
"bbor.commands.beacon.cleared.all": "All custom beacons cleared",
"bbor.commands.beacon.cleared": "Custom beacon cleared at [x=%d, y=%d, z=%d]",
"bbor.commands.beacon.notFound": "No custom beacon found at [x=%d, y=%d, z=%d]",
- "bbor.commands.beacon.expectedLevelInRange": "expected level between 1 and 4",
"bbor.commands.box.added": "Box added with start [x=%d, y=%d, z=%d] and end [x=%d, y=%d, z=%d]",
"bbor.commands.box.cleared.all": "All boxes cleared",
"bbor.commands.box.cleared": "Box cleared with start [x=%d, y=%d, z=%d] and end [x=%d, y=%d, z=%d]",
"bbor.commands.box.notFound": "No box found with start [x=%d, y=%d, z=%d] and end [x=%d, y=%d, z=%d]",
- "bbor.commandArgument.invalid": "Invalid argument",
-
"bbor.commands.spawningSphere.set": "Spawning sphere set",
"bbor.commands.spawningSphere.notSet": "No spawning sphere set",
"bbor.commands.spawningSphere.cleared": "Spawning sphere cleared",