From 4172f715dff809fcfed7592a601b3deef7434942 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Tue, 2 Nov 2021 18:11:42 -0400 Subject: [PATCH 1/2] Add the cblockstate command --- .../clientcommands/ClientCommands.java | 1 + .../command/BlockStateCommand.java | 59 +++++++++++++++++++ .../assets/clientcommands/lang/en_us.json | 2 + 3 files changed, 62 insertions(+) create mode 100644 src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index ea6b1901c..398bcac9d 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -60,6 +60,7 @@ public static void registerCommands(CommandDispatcher dispa AreaStatsCommand.register(dispatcher); CTeleportCommand.register(dispatcher); PlayerInfoCommand.register(dispatcher); + BlockStateCommand.register(dispatcher); CrackRNGCommand.register(dispatcher); diff --git a/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java new file mode 100644 index 000000000..c0f05a6d6 --- /dev/null +++ b/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java @@ -0,0 +1,59 @@ +package net.earthcomputer.clientcommands.command; + +import static net.earthcomputer.clientcommands.command.ClientCommandManager.*; +import static net.minecraft.command.argument.BlockStateArgumentType.*; +import static net.minecraft.server.command.CommandManager.*; + +import java.util.Map; + +import com.mojang.brigadier.CommandDispatcher; + +import net.minecraft.block.BlockState; +import net.minecraft.server.command.*; +import net.minecraft.state.property.Property; +import net.minecraft.text.*; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; +import net.minecraft.util.Util; +import net.minecraft.util.registry.Registry; + +public class BlockStateCommand { + + public static void register(CommandDispatcher dispatcher) { + addClientSideCommand("cblockstate"); + + dispatcher.register(literal("cblockstate") + .then(argument("block", blockState()) + .executes(ctx -> showBlockState(ctx.getSource(), getBlockState(ctx, "block").getBlockState())))); + } + + private static int showBlockState(ServerCommandSource source, BlockState state) { + Text name = state.getBlock().getName(); + + Identifier id = Registry.BLOCK.getId(state.getBlock()); + String idStr = id == null ? "Unregistered" : id.toString(); + + sendFeedback(new TranslatableText("commands.cblockstate.header", name, idStr, state.getProperties().size())); + + for (Map.Entry, Comparable> entry : state.getEntries().entrySet()) { + sendFeedback(getPropertyLine(entry)); + } + + return 0; + } + + private static Text getPropertyLine(Map.Entry, Comparable> entry) { + Property property = entry.getKey(); + Comparable value = entry.getValue(); + + MutableText valueText = new LiteralText(Util.getValueAsString(property, value)); + if (Boolean.TRUE.equals(value)) { + valueText.formatted(Formatting.GREEN); + } else if (Boolean.FALSE.equals(value)) { + valueText.formatted(Formatting.RED); + } + + return new LiteralText("- " + property.getName() + ": ").append(valueText); + } + +} diff --git a/src/main/resources/assets/clientcommands/lang/en_us.json b/src/main/resources/assets/clientcommands/lang/en_us.json index 27d1902a8..e68b16f89 100644 --- a/src/main/resources/assets/clientcommands/lang/en_us.json +++ b/src/main/resources/assets/clientcommands/lang/en_us.json @@ -15,6 +15,8 @@ "commands.careastats.output.blocksMatched": "Matched %d out of %d total blocks", "commands.careastats.output.entitiesFound": "Found %d entities in this area", + "commands.cblockstate.header": "%s (%s) with %d properties:", + "commands.cbook.success": "Successfully edited book", "commands.cbook.commandException": "You are not holding a book", From e65aa6dfe4c9a3bfa7defa03f15ea328bbe6772a Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:24:17 -0400 Subject: [PATCH 2/2] Use identity comparison for boolean block state properties --- .../clientcommands/command/BlockStateCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java index 2600201d0..d54b643c0 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java @@ -45,9 +45,9 @@ private static Text getPropertyLine(Map.Entry, Comparable> entry) Comparable value = entry.getValue(); MutableText valueText = Text.literal(Util.getValueAsString(property, value)); - if (Boolean.TRUE.equals(value)) { + if (value == Boolean.TRUE) { valueText.formatted(Formatting.GREEN); - } else if (Boolean.FALSE.equals(value)) { + } else if (value == Boolean.FALSE) { valueText.formatted(Formatting.RED); }