diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java index 4c96f35f9..15ae9320d 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java @@ -5,10 +5,7 @@ */ package dan200.computercraft.shared.peripheral.generic; -import dan200.computercraft.api.lua.IArguments; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.lua.LuaException; -import dan200.computercraft.api.lua.MethodResult; +import dan200.computercraft.api.lua.*; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IDynamicPeripheral; import dan200.computercraft.api.peripheral.IPeripheral; @@ -35,14 +32,7 @@ class GenericPeripheral implements IDynamicPeripheral { Identifier type = BlockEntityType.getId(tile.getType()); this.tile = tile; - - if ( tile instanceof Nameable && ((Nameable) tile).hasCustomName() ) - { - this.type = ((Nameable) tile).getName().asString(); - } else { - this.type = type == null ? "unknown" : type.toString(); - } - + this.type = type == null ? "unknown" : type.toString(); this.methods = methods; } 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 9d6923d9a..152e77148 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.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; +import net.minecraft.util.Nameable; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -54,6 +55,18 @@ public class InventoryMethods implements GenericSource return inventory.size(); } + /** + * Get the name of this inventory. + * + * @param inventory The current inventory. + * @return The name of this inventory, or {@code nil} if not present. + */ + @LuaFunction( mainThread = true ) + public static String name( Nameable inventory ) + { + return inventory.hasCustomName() ? inventory.getName().asString() : null; + } + /** * List all items in this inventory. This returns a table, with an entry for each slot. *