Advertisement
Guest User

hasItem

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function hasItem(event, player, item)
  2. {
  3.     // 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)
  4.     itemString = item.getItemNbt().toJsonString()
  5.     playerName = player.getDisplayName()
  6.     var result = 0
  7.  
  8.     if(itemString.indexOf('Name:') > -1){itemName = 'Name:"' + item.getDisplayName() + '"'}
  9.     else{itemName = ""}
  10.  
  11.     // Find the lore section in the JSON string
  12.     index = itemString.indexOf('"Lore":')
  13.     var str = ""
  14.     if(index > -1)
  15.     {
  16.         for(i = index + 8; i < itemString.length - 1; i++) // Starting just after Lore, grab every character for processing
  17.         {
  18.             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
  19.             {
  20.                 if(itemString[i] == '"') // If a quotation is found stop ignoring spaces
  21.                 {
  22.                     do
  23.                     {
  24.                         str = str + itemString[i]
  25.                         i++
  26.                     }
  27.                     while(itemString[i] != '"') // After we reach the end of the string for the line of lore go back to normal
  28.                     str = str + itemString[i] // If we don't grab this now it will be lost and screw up the formatting
  29.                 }
  30.                 else // Commas are rather important to grab too
  31.                 {
  32.                     str = str + itemString[i]
  33.                 }
  34.             }
  35.             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
  36.         }
  37.         if(itemName == "") // Check if the item name is different
  38.         {
  39.             str = 'Lore:' + str
  40.         }
  41.         else
  42.         {
  43.             str = ',Lore:' + str
  44.         }
  45.     }
  46.     // Command block output will be "Found playerX" or "playerX did not match the required data structure"
  47.     if(itemName != "" || str != "")
  48.     {
  49.     output = event.API.executeCommand(event.player.world, 'testfor '+ playerName + ' {Inventory:[{id:"' + item.getName() + '",tag:{display:{' + itemName + str + '}}}]}')
  50.     }
  51.     else
  52.     {
  53.         output = event.API.executeCommand(event.player.world, 'testfor '+ playerName + ' {Inventory:[{id:"' + item.getName() + '"}]}')
  54.     }
  55.     if(output.indexOf("Found " + playerName) > -1){result = 1}
  56.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement