From eb0121b6715ddc72bebb2afd69a92f602421f126 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Wed, 7 Jun 2023 23:59:08 +0200 Subject: [PATCH 01/10] Implemented simple //write for binary values (#268) --- .../commands/BinaryBlockWriteFeature.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java new file mode 100644 index 00000000..f83d0f71 --- /dev/null +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -0,0 +1,107 @@ +package tools.redstone.redstonetools.features.commands; + +import com.google.auto.service.AutoService; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.sk89q.worldedit.math.BlockVector3; +import net.minecraft.block.Blocks; +import net.minecraft.block.RedstoneLampBlock; +import net.minecraft.command.argument.BlockStateArgument; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.util.math.BlockPos; +import tools.redstone.redstonetools.features.AbstractFeature; +import tools.redstone.redstonetools.features.Feature; +import tools.redstone.redstonetools.features.arguments.Argument; +import tools.redstone.redstonetools.features.feedback.Feedback; +import tools.redstone.redstonetools.utils.WorldEditUtils; + +import java.math.BigInteger; +import java.util.Collections; + +import static tools.redstone.redstonetools.features.arguments.serializers.BigIntegerSerializer.bigInteger; +import static tools.redstone.redstonetools.features.arguments.serializers.BlockStateArgumentSerializer.blockState; +import static tools.redstone.redstonetools.features.arguments.serializers.BoolSerializer.bool; +import static tools.redstone.redstonetools.features.arguments.serializers.IntegerSerializer.integer; +import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase; + +@AutoService(AbstractFeature.class) +@Feature(name = "Binary Block Write", description = "Interprets your WorldEdit selection as a binary number.", command = "/write") +public class BinaryBlockWriteFeature extends CommandFeature { + private static final BlockStateArgument LIT_LAMP_ARG = new BlockStateArgument( + Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, true), + Collections.singleton(RedstoneLampBlock.LIT), + null + ); + + private static final BlockStateArgument UNLIT_LAMP_ARG = new BlockStateArgument( + Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, false), + Collections.singleton(RedstoneLampBlock.LIT), + null + ); + + public static final Argument offset = Argument + .ofType(integer(1)) + .withDefault(2); + public static final Argument onBlock = Argument + .ofType(blockState()) + .withDefault(LIT_LAMP_ARG); + + public static final Argument offBlock = Argument + .ofType(blockState()) + .withDefault(UNLIT_LAMP_ARG); + + public static final Argument number = Argument + .ofType(bigInteger()); + + @Override + protected Feedback execute(ServerCommandSource source) throws CommandSyntaxException { + var selectionOrFeedback = WorldEditUtils.getSelection(source.getPlayer()); + + if (selectionOrFeedback.right().isPresent()) { + return selectionOrFeedback.right().get(); + } + + assert selectionOrFeedback.left().isPresent(); + var selection = selectionOrFeedback.left().get(); + + var boundingBox = selection.getBoundingBox(); + var pos1 = boundingBox.getPos1(); + var pos2 = boundingBox.getPos2(); + var direction = pos2.subtract(pos1).normalize(); + + // prevent infinite loop + if (direction.lengthSq() == 0) { + direction = BlockVector3.at(0, 0, 1); + } + + var spacingVector = direction.multiply(offset.getValue()); + + if (direction.getBlockX() + direction.getBlockY() + direction.getBlockZ() > 1) { + return Feedback.invalidUsage("The selection must have 2 axis the same"); + } + + var bits = number.getValue().toString(2); + + if (spacingVector.length() * boundingBox.getVolume() < bits.length()) { + return Feedback.invalidUsage("The selection is too small to write the number"); + } + + var stringIndex = 0; + for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { + var pos = new BlockPos(point.getBlockX(), point.getBlockY(), point.getBlockZ()); + + BlockStateArgument state; + + if (stringIndex >= bits.length()) { + state = offBlock.getValue(); + } else { + state = bits.charAt(stringIndex) == '1' ? onBlock.getValue() : offBlock.getValue(); + stringIndex++; + } + + source.getPlayer().getWorld().setBlockState(pos, state.getBlockState()); + } + + return Feedback.success("Wrote " + bits); + } + +} \ No newline at end of file From 3b3c7dd506097de00b5ac6a08c95b3ecfc9ee3b6 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Thu, 8 Jun 2023 13:17:23 +0200 Subject: [PATCH 02/10] Redstone Blocks and Air are now the default Blocks --- .../commands/BinaryBlockWriteFeature.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index f83d0f71..f2b2e190 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -19,22 +19,20 @@ import static tools.redstone.redstonetools.features.arguments.serializers.BigIntegerSerializer.bigInteger; import static tools.redstone.redstonetools.features.arguments.serializers.BlockStateArgumentSerializer.blockState; -import static tools.redstone.redstonetools.features.arguments.serializers.BoolSerializer.bool; import static tools.redstone.redstonetools.features.arguments.serializers.IntegerSerializer.integer; -import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase; @AutoService(AbstractFeature.class) @Feature(name = "Binary Block Write", description = "Interprets your WorldEdit selection as a binary number.", command = "/write") public class BinaryBlockWriteFeature extends CommandFeature { - private static final BlockStateArgument LIT_LAMP_ARG = new BlockStateArgument( - Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, true), - Collections.singleton(RedstoneLampBlock.LIT), + private static final BlockStateArgument DEFAULT_ON_ARG = new BlockStateArgument( + Blocks.REDSTONE_BLOCK.getDefaultState(), + Collections.emptySet(), null ); - private static final BlockStateArgument UNLIT_LAMP_ARG = new BlockStateArgument( - Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, false), - Collections.singleton(RedstoneLampBlock.LIT), + private static final BlockStateArgument DEFAULT_OFF_ARG = new BlockStateArgument( + Blocks.AIR.getDefaultState(), + Collections.emptySet(), null ); @@ -43,11 +41,11 @@ public class BinaryBlockWriteFeature extends CommandFeature { .withDefault(2); public static final Argument onBlock = Argument .ofType(blockState()) - .withDefault(LIT_LAMP_ARG); + .withDefault(DEFAULT_ON_ARG); public static final Argument offBlock = Argument .ofType(blockState()) - .withDefault(UNLIT_LAMP_ARG); + .withDefault(DEFAULT_OFF_ARG); public static final Argument number = Argument .ofType(bigInteger()); From 35c098f45d5a69c6b2be4388194833777e67cb24 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Thu, 8 Jun 2023 22:59:58 +0200 Subject: [PATCH 03/10] Fixed alignment of write, so it's starting on the right --- .../features/commands/BinaryBlockWriteFeature.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index f2b2e190..af733b29 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -83,6 +83,8 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep return Feedback.invalidUsage("The selection is too small to write the number"); } + bits = "0".repeat((int) spacingVector.length() * (int) boundingBox.getVolume() - bits.length()) + bits; + var stringIndex = 0; for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { var pos = new BlockPos(point.getBlockX(), point.getBlockY(), point.getBlockZ()); From c0439984f5fa1e91d9c9b44d3825597f29efebe3 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Wed, 7 Jun 2023 23:59:08 +0200 Subject: [PATCH 04/10] Implemented simple //write for binary values (#268) --- .../commands/BinaryBlockWriteFeature.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java new file mode 100644 index 00000000..f83d0f71 --- /dev/null +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -0,0 +1,107 @@ +package tools.redstone.redstonetools.features.commands; + +import com.google.auto.service.AutoService; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.sk89q.worldedit.math.BlockVector3; +import net.minecraft.block.Blocks; +import net.minecraft.block.RedstoneLampBlock; +import net.minecraft.command.argument.BlockStateArgument; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.util.math.BlockPos; +import tools.redstone.redstonetools.features.AbstractFeature; +import tools.redstone.redstonetools.features.Feature; +import tools.redstone.redstonetools.features.arguments.Argument; +import tools.redstone.redstonetools.features.feedback.Feedback; +import tools.redstone.redstonetools.utils.WorldEditUtils; + +import java.math.BigInteger; +import java.util.Collections; + +import static tools.redstone.redstonetools.features.arguments.serializers.BigIntegerSerializer.bigInteger; +import static tools.redstone.redstonetools.features.arguments.serializers.BlockStateArgumentSerializer.blockState; +import static tools.redstone.redstonetools.features.arguments.serializers.BoolSerializer.bool; +import static tools.redstone.redstonetools.features.arguments.serializers.IntegerSerializer.integer; +import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase; + +@AutoService(AbstractFeature.class) +@Feature(name = "Binary Block Write", description = "Interprets your WorldEdit selection as a binary number.", command = "/write") +public class BinaryBlockWriteFeature extends CommandFeature { + private static final BlockStateArgument LIT_LAMP_ARG = new BlockStateArgument( + Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, true), + Collections.singleton(RedstoneLampBlock.LIT), + null + ); + + private static final BlockStateArgument UNLIT_LAMP_ARG = new BlockStateArgument( + Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, false), + Collections.singleton(RedstoneLampBlock.LIT), + null + ); + + public static final Argument offset = Argument + .ofType(integer(1)) + .withDefault(2); + public static final Argument onBlock = Argument + .ofType(blockState()) + .withDefault(LIT_LAMP_ARG); + + public static final Argument offBlock = Argument + .ofType(blockState()) + .withDefault(UNLIT_LAMP_ARG); + + public static final Argument number = Argument + .ofType(bigInteger()); + + @Override + protected Feedback execute(ServerCommandSource source) throws CommandSyntaxException { + var selectionOrFeedback = WorldEditUtils.getSelection(source.getPlayer()); + + if (selectionOrFeedback.right().isPresent()) { + return selectionOrFeedback.right().get(); + } + + assert selectionOrFeedback.left().isPresent(); + var selection = selectionOrFeedback.left().get(); + + var boundingBox = selection.getBoundingBox(); + var pos1 = boundingBox.getPos1(); + var pos2 = boundingBox.getPos2(); + var direction = pos2.subtract(pos1).normalize(); + + // prevent infinite loop + if (direction.lengthSq() == 0) { + direction = BlockVector3.at(0, 0, 1); + } + + var spacingVector = direction.multiply(offset.getValue()); + + if (direction.getBlockX() + direction.getBlockY() + direction.getBlockZ() > 1) { + return Feedback.invalidUsage("The selection must have 2 axis the same"); + } + + var bits = number.getValue().toString(2); + + if (spacingVector.length() * boundingBox.getVolume() < bits.length()) { + return Feedback.invalidUsage("The selection is too small to write the number"); + } + + var stringIndex = 0; + for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { + var pos = new BlockPos(point.getBlockX(), point.getBlockY(), point.getBlockZ()); + + BlockStateArgument state; + + if (stringIndex >= bits.length()) { + state = offBlock.getValue(); + } else { + state = bits.charAt(stringIndex) == '1' ? onBlock.getValue() : offBlock.getValue(); + stringIndex++; + } + + source.getPlayer().getWorld().setBlockState(pos, state.getBlockState()); + } + + return Feedback.success("Wrote " + bits); + } + +} \ No newline at end of file From 9564d2734a59605173ece50e65aea4e464a49394 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Thu, 8 Jun 2023 13:17:23 +0200 Subject: [PATCH 05/10] Redstone Blocks and Air are now the default Blocks --- .../commands/BinaryBlockWriteFeature.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index f83d0f71..f2b2e190 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -19,22 +19,20 @@ import static tools.redstone.redstonetools.features.arguments.serializers.BigIntegerSerializer.bigInteger; import static tools.redstone.redstonetools.features.arguments.serializers.BlockStateArgumentSerializer.blockState; -import static tools.redstone.redstonetools.features.arguments.serializers.BoolSerializer.bool; import static tools.redstone.redstonetools.features.arguments.serializers.IntegerSerializer.integer; -import static tools.redstone.redstonetools.features.arguments.serializers.NumberBaseSerializer.numberBase; @AutoService(AbstractFeature.class) @Feature(name = "Binary Block Write", description = "Interprets your WorldEdit selection as a binary number.", command = "/write") public class BinaryBlockWriteFeature extends CommandFeature { - private static final BlockStateArgument LIT_LAMP_ARG = new BlockStateArgument( - Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, true), - Collections.singleton(RedstoneLampBlock.LIT), + private static final BlockStateArgument DEFAULT_ON_ARG = new BlockStateArgument( + Blocks.REDSTONE_BLOCK.getDefaultState(), + Collections.emptySet(), null ); - private static final BlockStateArgument UNLIT_LAMP_ARG = new BlockStateArgument( - Blocks.REDSTONE_LAMP.getDefaultState().with(RedstoneLampBlock.LIT, false), - Collections.singleton(RedstoneLampBlock.LIT), + private static final BlockStateArgument DEFAULT_OFF_ARG = new BlockStateArgument( + Blocks.AIR.getDefaultState(), + Collections.emptySet(), null ); @@ -43,11 +41,11 @@ public class BinaryBlockWriteFeature extends CommandFeature { .withDefault(2); public static final Argument onBlock = Argument .ofType(blockState()) - .withDefault(LIT_LAMP_ARG); + .withDefault(DEFAULT_ON_ARG); public static final Argument offBlock = Argument .ofType(blockState()) - .withDefault(UNLIT_LAMP_ARG); + .withDefault(DEFAULT_OFF_ARG); public static final Argument number = Argument .ofType(bigInteger()); From 848dd1e8c007943d3f8acb18867e49a3d12c7e8d Mon Sep 17 00:00:00 2001 From: DreiMu Date: Thu, 8 Jun 2023 22:59:58 +0200 Subject: [PATCH 06/10] Fixed alignment of write, so it's starting on the right --- .../features/commands/BinaryBlockWriteFeature.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index f2b2e190..af733b29 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -83,6 +83,8 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep return Feedback.invalidUsage("The selection is too small to write the number"); } + bits = "0".repeat((int) spacingVector.length() * (int) boundingBox.getVolume() - bits.length()) + bits; + var stringIndex = 0; for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { var pos = new BlockPos(point.getBlockX(), point.getBlockY(), point.getBlockZ()); From 79f7e6eb0c6588df96d9e81125c89bfd1bcac7a4 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Sat, 10 Jun 2023 15:32:51 +0200 Subject: [PATCH 07/10] Offset values other than one are now working too --- .../commands/BinaryBlockWriteFeature.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index af733b29..e431f042 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -4,7 +4,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.sk89q.worldedit.math.BlockVector3; import net.minecraft.block.Blocks; -import net.minecraft.block.RedstoneLampBlock; import net.minecraft.command.argument.BlockStateArgument; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.math.BlockPos; @@ -79,26 +78,20 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep var bits = number.getValue().toString(2); - if (spacingVector.length() * boundingBox.getVolume() < bits.length()) { + if (Math.ceil((double) boundingBox.getVolume() / offset.getValue()) < bits.length()) { return Feedback.invalidUsage("The selection is too small to write the number"); } - bits = "0".repeat((int) spacingVector.length() * (int) boundingBox.getVolume() - bits.length()) + bits; + bits = "0".repeat((int) (Math.ceil((double) boundingBox.getVolume() / offset.getValue()) - bits.length())) + bits; var stringIndex = 0; for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { var pos = new BlockPos(point.getBlockX(), point.getBlockY(), point.getBlockZ()); - BlockStateArgument state; - - if (stringIndex >= bits.length()) { - state = offBlock.getValue(); - } else { - state = bits.charAt(stringIndex) == '1' ? onBlock.getValue() : offBlock.getValue(); - stringIndex++; - } + BlockStateArgument state = bits.charAt(stringIndex) == '1' ? onBlock.getValue() : offBlock.getValue(); source.getPlayer().getWorld().setBlockState(pos, state.getBlockState()); + stringIndex++; } return Feedback.success("Wrote " + bits); From b08ab8c4b7dc6ed4c3348bb8c95d4711e17216f2 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Sun, 11 Jun 2023 19:39:40 +0200 Subject: [PATCH 08/10] Changed offset to bitCount --- .../commands/BinaryBlockWriteFeature.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index e431f042..973dc3ac 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -35,9 +35,10 @@ public class BinaryBlockWriteFeature extends CommandFeature { null ); - public static final Argument offset = Argument + public static final Argument bitCount = Argument .ofType(integer(1)) - .withDefault(2); + .withDefault(8); + public static final Argument onBlock = Argument .ofType(blockState()) .withDefault(DEFAULT_ON_ARG); @@ -70,19 +71,23 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep direction = BlockVector3.at(0, 0, 1); } - var spacingVector = direction.multiply(offset.getValue()); - if (direction.getBlockX() + direction.getBlockY() + direction.getBlockZ() > 1) { return Feedback.invalidUsage("The selection must have 2 axis the same"); } - var bits = number.getValue().toString(2); + if (boundingBox.getVolume() % bitCount.getValue() != 0) { + return Feedback.invalidUsage("The selection must be a multiple of the bit count"); + } - if (Math.ceil((double) boundingBox.getVolume() / offset.getValue()) < bits.length()) { + if (boundingBox.getVolume() < bitCount.getValue()) { return Feedback.invalidUsage("The selection is too small to write the number"); } - bits = "0".repeat((int) (Math.ceil((double) boundingBox.getVolume() / offset.getValue()) - bits.length())) + bits; + var spacingVector = direction.multiply((int) (boundingBox.getVolume() / bitCount.getValue())); + + var bits = number.getValue().toString(2); + + bits = "0".repeat(bitCount.getValue() - bits.length()) + bits; var stringIndex = 0; for (BlockVector3 point = pos1; boundingBox.contains(point); point = point.add(spacingVector)) { From e914f41d98dace7ecb1c801c0c59ec46f6144e44 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Mon, 12 Jun 2023 22:37:48 +0200 Subject: [PATCH 09/10] set the minimum value to 0 and added check for numbers too large --- .../features/commands/BinaryBlockWriteFeature.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index 973dc3ac..29036092 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -48,7 +48,7 @@ public class BinaryBlockWriteFeature extends CommandFeature { .withDefault(DEFAULT_OFF_ARG); public static final Argument number = Argument - .ofType(bigInteger()); + .ofType(bigInteger(BigInteger.valueOf(0))); @Override protected Feedback execute(ServerCommandSource source) throws CommandSyntaxException { @@ -87,6 +87,10 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep var bits = number.getValue().toString(2); + if (bits.length() > bitCount.getValue()) { + return Feedback.invalidUsage("The number is too large to fit in the selection"); + } + bits = "0".repeat(bitCount.getValue() - bits.length()) + bits; var stringIndex = 0; From d28b27bf71222ce916d371e5b44db8035af34896 Mon Sep 17 00:00:00 2001 From: DreiMu Date: Mon, 19 Jun 2023 15:16:42 +0200 Subject: [PATCH 10/10] Not only multiples of the bit count work but also selections from the first to the last bit --- .../features/commands/BinaryBlockWriteFeature.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java index 29036092..8c3528aa 100644 --- a/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/commands/BinaryBlockWriteFeature.java @@ -75,8 +75,11 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep return Feedback.invalidUsage("The selection must have 2 axis the same"); } - if (boundingBox.getVolume() % bitCount.getValue() != 0) { - return Feedback.invalidUsage("The selection must be a multiple of the bit count"); + // if the selection ends on a bit and not the air, so it isn't a multiple of the bit count, this is true + boolean edge = ((boundingBox.getVolume() - 1) / (bitCount.getValue() - 1) - 1 + boundingBox.getVolume()) % bitCount.getValue() == 0; + + if (boundingBox.getVolume() % bitCount.getValue() != 0 && !edge) { + return Feedback.invalidUsage("The selection must be a multiple of the bit count or end on a bit"); } if (boundingBox.getVolume() < bitCount.getValue()) { @@ -85,6 +88,10 @@ protected Feedback execute(ServerCommandSource source) throws CommandSyntaxExcep var spacingVector = direction.multiply((int) (boundingBox.getVolume() / bitCount.getValue())); + if (edge) { + spacingVector = direction.multiply((int) ((boundingBox.getVolume() - 1) / (bitCount.getValue() - 1))); + } + var bits = number.getValue().toString(2); if (bits.length() > bitCount.getValue()) {