Out-Feu

weather.lua

Oct 2nd, 2024
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | Gaming | 0 0
  1. --[[
  2.  
  3. WeatherBot program
  4. By Out-Feu
  5.  
  6. version 1.0.0
  7.  
  8. Free to distribute/alter
  9. so long as proper credit to original
  10. author is maintained.
  11.  
  12. This program must be used on a chatty turtle.
  13.  
  14. The turtle will place an item in the inventory in front of it.
  15. This item must be used on a Teru Teru Bozu when the turtle emit a redstone signal.
  16. Sunflowers and blue orchids must be placed inside the turtle inventory.
  17.  
  18. --]]
  19.  
  20. string.split = function(self)
  21.  local tab = {}
  22.  for str in self:gmatch("([^%s]+)") do
  23.   table.insert(tab, str)
  24.  end
  25.  return tab
  26. end
  27.  
  28. function isRainning()
  29.  if env == nil then
  30.   return nil
  31.  end
  32.  return env.isRaining() or env.isThunder()
  33. end
  34.  
  35. function setClear()
  36.  if isRainning() == false then
  37.   sendText("Il ne pleut pas")
  38.  elseif useItem("minecraft:sunflower") then
  39.   sendText("La pluie va s'en aller")
  40.  else
  41.   sendText("Je suis à court de 'Sunflower' pour retirer la pluie")
  42.  end
  43. end
  44.  
  45. function setRain()
  46.  if isRainning() == true then
  47.   sendText("Il pleut déjà")
  48.  else
  49.   repeat
  50.    if not useItem("minecraft:blue_orchid") then
  51.     sendText("Je suis à court de 'Blue Orchid' pour mettre la pluie")
  52.     return
  53.    end
  54.   until env == nil or isRainning() == true
  55.   if env == nil then
  56.    sendText("La pluie arrive peut-être (10% de chance)")
  57.   else
  58.    sendText("La pluie arrive")
  59.   end
  60.  end
  61. end
  62.  
  63. function useItem(item)
  64.  local slot = getItemSlot(item)
  65.  if slot == nil then
  66.   return false
  67.  end
  68.  turtle.select(slot)
  69.  turtle.drop(1)
  70.  setRedstoneSignal(true)
  71.  sleep(1)
  72.  setRedstoneSignal(false)
  73.  sleep(1)
  74.  return true
  75. end
  76.  
  77. function getItemSlot(item)
  78.  for slot = 1, 16 do
  79.   data = turtle.getItemDetail(slot)
  80.   if data and data.name == item then
  81.    return slot
  82.   end
  83.  end
  84.  return nil
  85. end
  86.  
  87. function setRedstoneSignal(signal)
  88.  sides = redstone.getSides()
  89.  for k, side in pairs(sides) do
  90.   redstone.setOutput(side, signal)
  91.  end
  92. end
  93.  
  94. function isWhitelisted(player)
  95.  for _, wlplayer in pairs(whitelist) do
  96.   if wlplayer == player then
  97.    return true
  98.   end
  99.  end
  100.  return false
  101. end
  102.  
  103. function showWhitelist()
  104.  if whitelist == nil or next(whitelist) == nil then
  105.   sendText("La whitelist est vide.")
  106.   return
  107.  end
  108.  list = "Whitelist:"
  109.  for _, wlplayer in  pairs(whitelist) do
  110.   list = list .. "\n - " .. wlplayer
  111.  end
  112.  sendText(list)
  113. end
  114.  
  115. function showHelp(showItem)
  116.  local help = "Utilisation: " .. cmdPrefix .. " [option]\n"
  117.  help = help .. "Options:\n"
  118.  if not showItem then
  119.   help = help .. "'clear' - Retire la pluie\n"
  120.   if env == nil then
  121.    help = help .. "'rain' - 10% de chance de mettre la pluie\n"
  122.   else
  123.    help = help .. "'rain' - Met la pluie\n"
  124.   end
  125.  else
  126.   help = help .. "'clear' - Utilise 1 'Sunflower'\n"
  127.   if env == nil then
  128.    help = help .. "'rain' - Utilise 1 'Blue Orchid'\n"
  129.   else
  130.    help = help .. "'rain' - Utilise en moyenne 10 'Blue Orchid'\n"
  131.   end
  132.  end
  133.  if not showItem then
  134.   if enableWhitelist then
  135.    help = help .. "'whitelist' - Affiche les joueurs pouvant utilser la commande '" .. cmdPrefix .. "'\n"
  136.   end
  137.   help = help .. "'help' - Affiche cette aide\n"
  138.   help = help .. "'help item' - Affiche les ressources utilisées par les commandes"
  139.  else
  140.   help = help:sub(1, -2)
  141.  end
  142.  sendText(help)
  143. end
  144.  
  145. function sendText(text)
  146.  chat.sendMessage(text, name)
  147. end
  148.  
  149. -------------------------------------------------------------------------------
  150.  
  151. per = peripheral.getNames()
  152. for k, v in pairs(per) do
  153.  if peripheral.getType(v) == "chatBox" then
  154.   chat = peripheral.wrap(v)
  155.  elseif peripheral.getType(v) == "environmentDetector" then
  156.   env = peripheral.wrap(v)
  157.  end
  158. end
  159. per = nil
  160.  
  161. if chat == nil then
  162.  term.setTextColor(colors.red)
  163.  print("Chat box not found")
  164.  term.setTextColor(colors.white)
  165.  return
  166. end
  167.  
  168. name = "WeatherBot" -- Name displayed in chat
  169. cmdPrefix = "!weather" -- Prefix for the commands
  170. whitelist = {} -- Players who can use the command
  171. enableWhitelist = false -- Restic the command to whitelisted players
  172.  
  173. --------------------------------------------------------------------------------
  174. print("Hello, I am " .. name .. "!")
  175. print("'" .. cmdPrefix .. " help' for more informations.")
  176.  
  177. repeat --main loop
  178.  local event, player, message = os.pullEvent("chat")
  179.  args = message:split()
  180.  if table.getn(args) > 1 and args[1] == cmdPrefix then
  181.   if enableWhitelist and not isWhitelisted(player) then
  182.    sendText("Le joueur '" .. player .. "' n'a pas le droit d'utiliser la commande '" .. cmdPrefix .. "'.")
  183.   elseif args[2] == "help" then
  184.    showHelp(table.getn(args) > 2 and args[3] == "item")
  185.   elseif args[2] == "whitelist" then
  186.    showWhitelist()
  187.   elseif args[2] == "clear" then
  188.    setClear()
  189.   elseif args[2] == "rain" then
  190.    setRain()
  191.   else
  192.    sendText("Option invalide, '".. cmdPrefix .." help' pour plus d'informations.")
  193.   end
  194.  end
  195. until false
Advertisement
Add Comment
Please, Sign In to add comment