diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index 484166544..95c656387 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -75,6 +75,7 @@ public static void registerCommands(CommandDispatcher SnakeCommand.register(dispatcher); CTitleCommand.register(dispatcher); TooltipCommand.register(dispatcher, registryAccess); + BlockStateCommand.register(dispatcher, registryAccess); 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..d54b643c0 --- /dev/null +++ b/src/main/java/net/earthcomputer/clientcommands/command/BlockStateCommand.java @@ -0,0 +1,57 @@ +package net.earthcomputer.clientcommands.command; + +import static dev.xpple.clientarguments.arguments.CBlockStateArgumentType.*; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; + +import java.util.Map; + +import com.mojang.brigadier.CommandDispatcher; + +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.block.BlockState; +import net.minecraft.command.CommandRegistryAccess; +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, CommandRegistryAccess registryAccess) { + dispatcher.register(literal("cblockstate") + .then(argument("block", blockState(registryAccess)) + .executes(ctx -> showBlockState(ctx.getSource(), getCBlockState(ctx, "block").getBlockState())))); + } + + private static int showBlockState(FabricClientCommandSource source, BlockState state) { + Text name = state.getBlock().getName(); + + Identifier id = Registry.BLOCK.getId(state.getBlock()); + String idStr = id == null ? "Unregistered" : id.toString(); + + source.sendFeedback(Text.translatable("commands.cblockstate.header", name, idStr, state.getProperties().size())); + + for (Map.Entry, Comparable> entry : state.getEntries().entrySet()) { + source.sendFeedback(getPropertyLine(entry)); + } + + return 0; + } + + private static Text getPropertyLine(Map.Entry, Comparable> entry) { + Property property = entry.getKey(); + Comparable value = entry.getValue(); + + MutableText valueText = Text.literal(Util.getValueAsString(property, value)); + if (value == Boolean.TRUE) { + valueText.formatted(Formatting.GREEN); + } else if (value == Boolean.FALSE) { + valueText.formatted(Formatting.RED); + } + + return Text.literal("- " + 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 4e8541d76..4ea98b290 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",