This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
JDCollie's profile picture

Pulling Enchant Names from Enchanted Books

Started by JDCollie, 27 January 2014 - 06:55 PM
JDCollie #1
Posted 27 January 2014 - 07:55 PM
I'm trying to find a way to pull the enchant names from enchanted books. I.E., "Protection I", or "Aqua Affinity II".

I'm using openperipherals, but the website seems to be down right now, and I can't find any other documentation about the various functions it adds. I've tried using getStackInSlot, but the table it returns does not contain the information needed to distinguish between individual book enchants.

My question is, is there a way to get this info?
Himself12794 #2
Posted 27 January 2014 - 11:28 PM
Well if you want to get an idea of the functions a peripheral adds, you can use:
for i,v in ipairs(peripheral.getMethods(side)) do print(i..". "..v) end

That will show you all the functions the peripheral adds, although how to use them, I cannot say.
theoriginalbit #3
Posted 25 June 2014 - 03:10 AM
when you get the item OpenPeripheral will return a table of information on the book. it will be something like so

{
  id = 403,
  name = "Enchanted Book",
  maxdmg = 0,
  rawName = "item.enchantedbook",
  dmg = 0,
  maxSize = 1,
  ench = {
	"Knockback II",
  }
  qty = 1
}

so the following code will loop through a chest and print out all the enchantments on any enchanted item

local chest = peripheral.wrap("left")
local contents = chest.getAllStacks()
for index, item in pairs(contents) do
  if item.ench then --# if there is an enchantment(s) on the item
	print("Found a ", item.name, " with the enchantments: ", table.concat(item.ench, ', '))
  end
end
so the above code will also output enchantments on tools, armour, etc.
Edited by
theoriginalbit #4
Posted 25 June 2014 - 08:05 AM
this was simply example code. the error is coming from the fact that you most likely don't have the inventory on the left of the computer, replace the "left" with whatever side the inventory is on (or the network name)
theoriginalbit #5
Posted 25 June 2014 - 08:53 AM
Nope I have the chest on the left side, I just need something simple like this code to expand on but need the simple stuff working first. It wouldn't matter that I am using a advanced computer would it?
no the type of computer does not matter. make sure you didn't typo with the function name getAllStacks? if you didn't, what version of OpenPeripheral are you running? what is the output of this code? do you see a method in there that indicates being able to get all the stacks?

table.concat( peripheral.getMethods("left"), ", ")
GamerNebulae #6
Posted 25 June 2014 - 10:16 PM
Probably what is wrong then is that the version of OpenPeripherals is a little outdated.
theoriginalbit #7
Posted 26 June 2014 - 03:02 AM
Im using the FTB Unleashed 1.1.7 on a SMP server. The only output is what I posted above.
you posted nothing. table.concat(peripheral.getMethods("left"), ", ") should output something. if it doesn't output anything or errors with attempt to index ? (a nil value) then OpenPeripheral isn't installed, however you said its erroring with attempt to call nil meaning that the method you're trying to call (getAllStacks) doesn't exist, which that function did exist, but it may have been named differently, and since I can't remember what the methods were called in old versions of OpenPeripheral I cannot help you further until you run that code I asked and show the output.
theoriginalbit #8
Posted 26 June 2014 - 03:20 AM
hmmm, interesting. i swear that one existed. okay so do this.

local chest = peripheral.wrap("left")
for i = 1, chest.getSizeInventory() do --# loop over all the slot indexes
  local item = chest.getStackInSlot(i) --# get the item in the slow
  if item.ench then --# if there is an enchantment(s) on the item
	print("Found a ", item.name, " with the enchantments: ", table.concat(item.ench, ', '))
  end
end
though considering how old of a version of OpenPeripheral you're using there's every chance that we may not have been collecting information on the enchantments at that point.
Edited by
Bomb Bloke #9
Posted 26 June 2014 - 03:36 AM
I've got somewhat of a memory that "item" will end up as nil if a given chest slot is empty. Might need to change this:

if item.ench then

to this:

if item and item.ench then

Edit: Re that error you just ninja'd me with, try sticking a sleep(1) just before and after wrapping the chest.
Edited on 26 June 2014 - 01:38 AM
theoriginalbit #10
Posted 26 June 2014 - 03:39 AM
ah yes, I forgot to add that check, thanks Bomb Bloke.

however a reflection exception means something else entirely, not too sure what would cause it on something so simple for you that no one else had problems with.

FTB Unleashed won't update, it has been discontinued. you'd have to move to a different pack.
Bomb Bloke #11
Posted 26 June 2014 - 03:52 AM
Are we talking about a standard vanilla chest here, or some other sort of chest?
Bomb Bloke #12
Posted 26 June 2014 - 05:17 AM
Nah, that's ok. If you weren't positioning things correctly you wouldn't've gotten the methods list.

Perhaps try running the code on an empty chest. If that works, try one with some items other than enchanted books in it.
Bomb Bloke #13
Posted 26 June 2014 - 06:43 AM
Sorry, but it sounds like you may be SOL as far as that installation's concerned.

It's odd, though. It certainly worked in older versions of the pack, and enough people used 1.1.7 that I'm surprised I haven't seen this reported before.
Bomb Bloke #14
Posted 26 June 2014 - 07:23 AM
Sorry, I believe that function OpenPeripheral is offering you already is the only option.