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

Need Help With If Loop

Started by subzero22, 10 October 2013 - 10:23 PM
subzero22 #1
Posted 11 October 2013 - 12:23 AM
I posted in the correct forum for peripherals but never got a response and the post below me didn't get a single response since oct 4.

Well anyway is there a way to do a code like this as for some reason the : breaks it but the item I'd requires it.

If Id == 227:3 then
Enter code here
end

How can I do this while still having the : in the code?

Here's link to orriginal post
http://www.computercraft.info/forums2/index.php?/topic/15535-miscperipheral-thermal-glasses-and-me-bridge/page__pid__148462#entry148462
immibis #2
Posted 11 October 2013 - 02:10 AM
Is the ID a string?
Anavrins #3
Posted 11 October 2013 - 03:41 AM
In MiscPeripherals, any item that has a damage value get encoded in some way, I've wrote a quick little function to decode it.

function mpDecode(nInput)
	return bit.band(2^15-1, nInput), bit.brshift(nInput, 15)
end

You would use it like this

local id, dmg = mpDecode(numberReturnedByMiscPeripheral)

So for item id "227:3", your code would look like this…

local id, dmg = mpEncode(98531) '-- That is the encoded value of 227:3'
if id == 227 and dmg == 3 then
    '-- Do stuff'
end
theoriginalbit #4
Posted 11 October 2013 - 07:21 AM
In addition to what Anavrins said… There is also a seed in MiscP which can be changed, as such this is a set of helper functions that NeverCast and I have used in the past.

local itemSeed = 0

function setItemSeed( seed )
  itemSeed = seed
end

function getItemFromUUID( uuid )
  uuid = bit.bxor(uuid, itemSeed)
  local meta = math.floor(uuid/32768)
  local id = uuid % 32768
  return id, meta
end

function getUUIDFromItem( id, meta )
  local uuid = (meta or 0) * 32768 + id
  return bit.bxor(uuid, itemSeed)
end
The usage is the same (except different function names of course)

local id, dmg = getItemFromUUID(98531)
if id == 227 and dmg == 3 then
	--# do stuff
end
subzero22 #5
Posted 13 October 2013 - 01:00 AM
Well the items I'm listing aren't damage based. Like for example coal is 263 and charcoal is 263:1.

Thanks for the help and I'm assuming the codes will still work for my purposes. If not can you please give example of one that will?
theoriginalbit #6
Posted 13 October 2013 - 01:47 AM
Well the items I'm listing aren't damage based. Like for example coal is 263 and charcoal is 263:1.
That is damage value a.k.a. metadata.