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 ) {