From 6524a308ae71ba57cebb7d05ce79cd00ce970235 Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Sat, 18 Jun 2022 09:11:55 +0200 Subject: [PATCH] Prototype a way to get the name of inventories --- .../generic/methods/InventoryMethods.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java index e37843598c..80ab905719 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java @@ -16,6 +16,7 @@ import dan200.computercraft.shared.peripheral.generic.data.ItemData; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.Container; +import net.minecraft.world.Nameable; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.common.capabilities.ICapabilityProvider; @@ -265,6 +266,27 @@ public static int pullItems( return moveItem( from, fromSlot - 1, to, toSlot.orElse( 0 ) - 1, actualLimit ); } + /** + * Get the display name of the current inventory. + * + * This allows you to get the name of inventories renamed in an anvil. + * + * @param peripheral The current peripheral. + * @return The name of the inventory, or nil. + * @cc.usage find a chest, and print it's (custom) name. + *
{@code
+     * local chest = peripheral.find("minecraft:chest")
+     *
+     * print(chest.displayName())
+     * }
+ */ + @LuaFunction( mainThread = true ) + public static String displayName(IPeripheral peripheral) + { + if (peripheral.getTarget() instanceof Nameable nameable) return nameable.getDisplayName().getString(); + return null; + } + @Nullable private static IItemHandler extractHandler( @Nullable Object object ) {