Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the cblockstate command #331

Open
wants to merge 4 commits into
base: fabric
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
SnakeCommand.register(dispatcher);
CTitleCommand.register(dispatcher);
TooltipCommand.register(dispatcher, registryAccess);
BlockStateCommand.register(dispatcher, registryAccess);

CrackRNGCommand.register(dispatcher);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.earthcomputer.clientcommands.command;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import order is a bit weird.


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<FabricClientCommandSource> 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();
Comment on lines +31 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id is never null.


source.sendFeedback(Text.translatable("commands.cblockstate.header", name, idStr, state.getProperties().size()));

for (Map.Entry<Property<?>, Comparable<?>> entry : state.getEntries().entrySet()) {
source.sendFeedback(getPropertyLine(entry));
}

return 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 0;
return Command.SINGLE_SUCCESS;

}

private static Text getPropertyLine(Map.Entry<Property<?>, 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);
}

}
2 changes: 2 additions & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down