From ca0b6b8351a1d3f8eabb1444687eca80b5da17a5 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 2 Mar 2024 12:03:57 +0100 Subject: [PATCH] Add more info about blocks / fluids to /kubejs hand Supersedes #800 Co-authored-by: Ryan Jones-Ward --- .../mods/kubejs/command/KubeJSCommands.java | 73 +++++++++++++++---- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index e0244d37a..016a6f0a8 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -43,20 +43,23 @@ import net.minecraft.commands.arguments.ResourceLocationArgument; import net.minecraft.commands.arguments.ScoreHolderArgument; import net.minecraft.core.BlockPos; +import net.minecraft.core.HolderSet; import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.network.chat.ClickEvent; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.HoverEvent; import net.minecraft.network.chat.Style; -import net.minecraft.network.chat.TextColor; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.tags.TagKey; import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.BucketItem; import net.minecraft.world.item.ItemStack; import org.apache.commons.io.FileUtils; @@ -485,12 +488,15 @@ private static Stream> allTags(CommandSourceStack source, Resource } private static Component copy(String s, ChatFormatting col, String info) { - var component = Component.literal("- "); - component.setStyle(component.getStyle().withColor(TextColor.fromLegacyFormat(ChatFormatting.GRAY))); - component.setStyle(component.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, s))); - component.setStyle(component.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(info + " (Click to copy)")))); - component.append(Component.literal(s).withStyle(col)); - return component; + return copy(Component.literal(s).withStyle(col), info); + } + + private static Component copy(Component c, String info) { + return Component.literal("- ") + .withStyle(ChatFormatting.GRAY) + .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, c.getString()))) + .withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(info + " (Click to copy)")))) + .append(c); } private static void link(CommandSourceStack source, ChatFormatting color, String name, String url) { @@ -522,23 +528,58 @@ private static int customCommand(CommandSourceStack source, String id) { private static int hand(ServerPlayer player, InteractionHand hand) { player.sendSystemMessage(Component.literal("Item in hand:")); var stack = player.getItemInHand(hand); - player.sendSystemMessage(copy(ItemStackJS.toItemString(stack), ChatFormatting.GREEN, "Item ID")); - - List tags = new ArrayList<>(stack.kjs$getTags()); - tags.sort(null); + var holder = stack.getItemHolder(); - for (var id : tags) { - player.sendSystemMessage(copy("'#" + id + "'", ChatFormatting.YELLOW, "Item Tag [" + IngredientPlatformHelper.get().tag(id.toString()).kjs$getStacks().size() + " items]")); + // item info + // id + player.sendSystemMessage(copy(ItemStackJS.toItemString(stack), ChatFormatting.GREEN, "Item ID")); + // item tags + var itemTags = holder.tags().toList(); + for (var tag : itemTags) { + var id = "'#%s'".formatted(tag.location()); + var size = BuiltInRegistries.ITEM.getTag(tag).map(HolderSet::size).orElse(0); + player.sendSystemMessage(copy(id, ChatFormatting.YELLOW, "Item Tag [" + size + " items]")); } - + // mod player.sendSystemMessage(copy("'@" + stack.kjs$getMod() + "'", ChatFormatting.AQUA, "Mod [" + IngredientPlatformHelper.get().mod(stack.kjs$getMod()).kjs$getStacks().size() + " items]")); - + // TODO: creative tabs (neo has made them client only in 1.20.1, this is fixed in 1.20.4) /*var cat = stack.getItem().getItemCategory(); - if (cat != null) { player.sendSystemMessage(copy("'%" + cat.getRecipeFolderName() + "'", ChatFormatting.LIGHT_PURPLE, "Item Group [" + IngredientPlatformHelper.get().creativeTab(cat).kjs$getStacks().size() + " items]")); }*/ + // block info + if (stack.getItem() instanceof BlockItem blockItem) { + player.sendSystemMessage(Component.literal("Held block:")); + var block = blockItem.getBlock(); + var blockHolder = block.builtInRegistryHolder(); + // id + player.sendSystemMessage(copy(block.kjs$getId(), ChatFormatting.GREEN, "Block ID")); + // block tags + var blockTags = blockHolder.tags().toList(); + for (var tag : blockTags) { + var id = "'#%s'".formatted(tag.location()); + var size = BuiltInRegistries.BLOCK.getTag(tag).map(HolderSet::size).orElse(0); + player.sendSystemMessage(copy(id, ChatFormatting.YELLOW, "Block Tag [" + size + " items]")); + } + } + + // fluid info (TODO: bucket only for now, make generic in 1.20.4) + if (stack.getItem() instanceof BucketItem bucket) { + player.sendSystemMessage(Component.literal("Held fluid:")); + var fluid = bucket.arch$getFluid(); + var fluidHolder = fluid.builtInRegistryHolder(); + // id + player.sendSystemMessage(copy(fluidHolder.key().location().toString(), ChatFormatting.GREEN, "Fluid ID")); + // fluid tags + var fluidTags = fluidHolder.tags().toList(); + for (var tag : fluidTags) { + var id = "'#%s'".formatted(tag.location()); + var size = BuiltInRegistries.FLUID.getTag(tag).map(HolderSet::size).orElse(0); + player.sendSystemMessage(copy(id, ChatFormatting.YELLOW, "Fluid Tag [" + size + " items]")); + } + } + return 1; }