Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hasItem(event, player, item)
- {
- // This function is intended to replace the inventoryItemCount method in searching for a specific named item (Note: unlike inventoryItemCount it only returns true or false)
- itemString = item.getItemNbt().toJsonString()
- playerName = player.getDisplayName()
- var result = 0
- if(itemString.indexOf('Name:') > -1){itemName = 'Name:"' + item.getDisplayName() + '"'}
- else{itemName = ""}
- // Find the lore section in the JSON string
- index = itemString.indexOf('"Lore":')
- var str = ""
- if(index > -1)
- {
- for(i = index + 8; i < itemString.length - 1; i++) // Starting just after Lore, grab every character for processing
- {
- if(itemString[i] != "\n" && itemString[i] != " ") // New lines and a binch of spaces are included that break the search and need to be filtered out
- {
- if(itemString[i] == '"') // If a quotation is found stop ignoring spaces
- {
- do
- {
- str = str + itemString[i]
- i++
- }
- while(itemString[i] != '"') // After we reach the end of the string for the line of lore go back to normal
- str = str + itemString[i] // If we don't grab this now it will be lost and screw up the formatting
- }
- else // Commas are rather important to grab too
- {
- str = str + itemString[i]
- }
- }
- if(itemString[i] == "]"){i = itemString.length} // People seem to get upset when I use a break, so I'm going out of my way to upset everyone here
- }
- if(itemName == "") // Check if the item name is different
- {
- str = 'Lore:' + str
- }
- else
- {
- str = ',Lore:' + str
- }
- }
- // Command block output will be "Found playerX" or "playerX did not match the required data structure"
- if(itemName != "" || str != "")
- {
- output = event.API.executeCommand(event.player.world, 'testfor '+ playerName + ' {Inventory:[{id:"' + item.getName() + '",tag:{display:{' + itemName + str + '}}}]}')
- }
- else
- {
- output = event.API.executeCommand(event.player.world, 'testfor '+ playerName + ' {Inventory:[{id:"' + item.getName() + '"}]}')
- }
- if(output.indexOf("Found " + playerName) > -1){result = 1}
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement