Jump to content




Pulling Enchant Names from Enchanted Books


  • You cannot reply to this topic
23 replies to this topic

#1 JDCollie

  • New Members
  • 1 posts

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?

#2 Himself12794

  • Members
  • 56 posts

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.

#3 Guest_cameron2621_*

  • Guests

Posted 25 June 2014 - 01:23 AM

@ JDCollie Did you ever figure this out? I am trying to do almost the same thing. Trying to get it to display the "Enchantment" not the "Item Name"

Please if you did message me!

Thanks,
Cameron2621

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 June 2014 - 02: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, 25 June 2014 - 02:12 AM.
better example code


#5 Guest_cameron2621_*

  • Guests

Posted 25 June 2014 - 07:00 AM

View Posttheoriginalbit, on 25 June 2014 - 02:10 AM, said:

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.

Thanks, I tried this and I am getting "startup:2: attempt to call nil"

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 June 2014 - 07: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)

#7 Guest_cameron2621_*

  • Guests

Posted 25 June 2014 - 07:31 AM

View Posttheoriginalbit, on 25 June 2014 - 07:05 AM, said:

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)

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?

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 June 2014 - 07:53 AM

View Postcameron2621, on 25 June 2014 - 07:31 AM, said:

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"), ", ")


#9 Guest_cameron2621_*

  • Guests

Posted 25 June 2014 - 07:31 PM

Im using the FTB Unleashed 1.1.7 on a SMP server. The only output is what I posted above.

#10 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 25 June 2014 - 09:16 PM

Probably what is wrong then is that the version of OpenPeripherals is a little outdated.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 June 2014 - 02:02 AM

View Postcameron2621, on 25 June 2014 - 07:31 PM, said:

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.

#12 Guest_cameron2621_*

  • Guests

Posted 26 June 2014 - 02:17 AM

Oh Sorry, I thought you were asking something different

The output I got:
listMethods, pullIntoSlot, pull, pushIntoSlot, push, condense, swapStacks, getInvName, getStackInSlot, getSizeInventory

Edited by cameron2621, 26 June 2014 - 02:20 AM.


#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 June 2014 - 02: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 theoriginalbit, 26 June 2014 - 02:21 AM.
more code comments


#14 Guest_cameron2621_*

  • Guests

Posted 26 June 2014 - 02:35 AM

Nope this time I got:

startup:3:
java.lang.reflect.InvocationTargetException

Edit: I just saw in your code:

local item = chest.getStackInSlot(i)

I changed it to just do slot one by putting a 0 there, Did not get that error but nothing happend after that. It did not write anuthing

So does this just mean I can't do it with this version. Wait untill the pack updates?
Thanks,
Cameron

Edited by cameron2621, 26 June 2014 - 02:41 AM.


#15 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 June 2014 - 02: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 by Bomb Bloke, 26 June 2014 - 02:38 AM.


#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 June 2014 - 02: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.

#17 Guest_cameron2621_*

  • Guests

Posted 26 June 2014 - 02:49 AM

Just edited the code too:


http://pastebin.com/yxiN1dEk#

Got that same error of the line that says:

local item = chest.getStackInSlot(i)

Edited by cameron2621, 26 June 2014 - 02:50 AM.


#18 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 June 2014 - 02:52 AM

Are we talking about a standard vanilla chest here, or some other sort of chest?

#19 Guest_cameron2621_*

  • Guests

Posted 26 June 2014 - 02:59 AM

Vanilla chest, I can screenshot if you want the setup

#20 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 June 2014 - 04: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users