Advertisement
Guest User

BadgeChecker

a guest
Jan 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. function splitStr(inputstr, sep)
  2.     if sep == nil then
  3.         sep = "%s"
  4.     end
  5.     local t={} ; i=1
  6.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  7.         t[i] = str
  8.         i = i + 1
  9.     end
  10.     return t
  11. end
  12.  
  13. function tablelength(T)
  14.     local count = 0
  15.     for _ in pairs(T) do
  16.         count = count + 1
  17.     end
  18.     return count
  19. end
  20.  
  21. function getItemData()
  22.     local unused, output = commands.exec("/entitydata @e[type=Item,r=3] {}")
  23.    
  24.  if output[1] == nil then
  25.   return nil
  26.  end
  27.  
  28.  local str = ""..output[1]
  29.  return str
  30. end
  31.  
  32. function isBadgeCase(data)
  33.  if data == nil then
  34.   return false
  35.  else
  36.      return string.find(data, "badgecase") ~= nil
  37.  end
  38. end
  39.  
  40. function getCaseOwner(data)
  41.     local unused, start = string.find(data, "Username")
  42.     if start == nil then
  43.         return ""
  44.     end
  45.     start = start + 3
  46.    
  47.     local fromStart = string.sub(data, start)
  48.    
  49.     return string.sub(fromStart, 0, string.find(fromStart, "\"") - 1)
  50. end
  51.  
  52. function getPlayerName()
  53.     local unused, output = commands.exec("/entitydata @p {}")
  54.     local str = ""..output[1]
  55.     return string.sub(str, 0, string.find(str, "is") - 2)
  56. end
  57.  
  58. function getBadges(data)
  59.     local str = data
  60.     local begin = string.find(str,"Badges:")
  61.     if begin == nil then
  62.         return {}
  63.     end
  64.    
  65.     str = string.sub(str,begin)
  66.  
  67.     local start = string.find(str, "\"") + 1
  68.     local fromstart = string.sub(str,string.find(str, "\"") + 1)
  69.  
  70.     local finish = string.find(fromstart.."", "\"") - 1
  71.     local badgesStr = string.sub(fromstart, 1, finish)
  72.  
  73.     return splitStr(badgesStr, ',')
  74. end
  75.  
  76. function containsDuplicates(T)
  77.     for i = 1, tablelength(T) do
  78.         for j = i + 1, tablelength(T) do
  79.             if(T[i] == T[j]) then
  80.                 return true
  81.             end
  82.         end
  83.     end
  84.    
  85.     return false
  86. end
  87.  
  88.  
  89. --------------------main--------------------
  90. local scoreboardCommand = "/scoreboard players tag @p add Has8Badges"
  91. local itemData = getItemData()
  92. local playerName = getPlayerName()
  93.  
  94. if isBadgeCase(itemData) then
  95.     if getCaseOwner(itemData) == playerName then
  96.         local badges = getBadges(itemData)
  97.         if tablelength(badges) >= 8 then
  98.             if not containsDuplicates(badges) then
  99.                 commands.exec(scoreboardCommand)
  100.                 commands.exec("/tell @p Congratulations! You are now officially recognized as having all 8 Gym Badges!")
  101.             else
  102.             commands.exec("/tell @p One or more of your badges are the same badge!")
  103.             end
  104.         else
  105.             commands.exec("/tell @p You don't have 8 badges!")
  106.         end
  107.     else
  108.         commands.exec("/tell @p This is not your badge case!")
  109.       end
  110.     else
  111.         commands.exec("/tell @p Please drop your badge case on the ground!")
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement