Advertisement
konalisp

Dialogue (dia.lua)

May 26th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. --[[
  2. --- INSTRUCTIONS ---
  3. Arguments: dialogue (file), any non-nil value (allows it to play more than once)
  4. Make a file for dialogue, it must follow this format strictly:
  5. Do not add quote marks around strings, just leave them bare.
  6. line 1: name, like "Announcer"
  7. line 2: radius, how far people can read the messages
  8. line 3: A color. Valid values are "black", "dark_blue", "dark_green",
  9.            "dark_aqua", "dark_red", "dark_purple", "gold", "gray", "dark_gray",
  10.            "blue", "green", "aqua", "red", "light_purple", "yellow", "white", and
  11.            "reset" (cancels out the effects of colors used by parent objects).
  12.            Technically, "bold", "underline", "italic", "strikethrough", and
  13.            "obfuscated" are also accepted, but it may be better practice to use
  14.            the tags below for such formats.
  15. line 4: This line and following lines are dialogue. Dialogue is defined like so:
  16.            "03 Hello, world!"
  17.            The first number, which must be two digits, is the timing. This specifies
  18.            how long the system will wait before writing the rest of the line. The rest
  19.            of the line is simply dialogue, nothing fancy about it.
  20.            http://minecraft.gamepedia.com/Commands#Raw_JSON_text
  21. After this file has been created, simply call it as an argument with this program.
  22. --]]
  23.  
  24. tArgs = {...}
  25.  
  26. --- Dialogue class
  27.  
  28. local dia = { }
  29.  
  30. dia.conf = { }
  31. dia.conf.name = "Announcer"
  32. dia.conf.radius = 10
  33. dia.conf.color = "gray"
  34. dia.conf.timings = { }
  35. dia.conf.dialogue = {
  36.     "03 Test.",
  37. }
  38.  
  39. function dia:say()
  40.     for n, i in pairs(self.conf.dialogue) do
  41.         os.sleep(self.conf.timings[n])
  42.         commands.exec("tellraw @a[r=" .. self.conf.radius .. "] { text:\"[" .. self.conf.name .. "] " .. i .. "\", color:" .. self.conf.color .. ", italic:true }")
  43.     end
  44. end
  45.  
  46. function dia:chkfile(cnffile)
  47.     if not fs.exists(cnffile) then
  48.         local h = fs.open(cnffile, "w")
  49.         h.write("f")
  50.         h.close()
  51.     end
  52. end
  53.  
  54. function dia:cnf(ar)
  55.     local cnffile = "talk.reg"
  56.     if ar[2] == nil then
  57.         self:chkfile(cnffile)
  58.         local h = fs.open(cnffile , "r")
  59.         if h.readLine() == "f" then
  60.             h.close()
  61.             h = fs.open(cnffile, "w")
  62.             h.write("t")
  63.             h.close()
  64.             return false
  65.         else
  66.             return true
  67.         end
  68.     else
  69.         return false
  70.     end
  71. end
  72.  
  73. function dia:enter_config(ar)
  74.     local d = nil
  75.     local c = 1
  76.     if ar[1] ~= nil then
  77.         local h = fs.open(tostring(ar[1]), "r")
  78.         self.conf.name = h.readLine()
  79.         self.conf.radius = tonumber(h.readLine())
  80.         self.conf.color = h.readLine()
  81.         while(true) do
  82.             d = h.readLine()
  83.             if d ~= nil then
  84.                 self.conf.dialogue[c] = string.sub(d, 4)
  85.                 self.conf.timings[c] = tonumber(string.sub(d, 1, 2))
  86.                 c = c + 1
  87.             else break end
  88.         end
  89.         h.close()
  90.     end
  91. end
  92.  
  93. function dia:main(ar)
  94.     self:enter_config(ar)
  95.    
  96.     while(true) do
  97.         local event, param = os.pullEvent()
  98.         if event == "redstone" and redstone.getInput("back") == true then
  99.             if self:cnf(ar) == false then
  100.                 self:say()
  101.                 if ar[2] == nil then break end
  102.             else break end
  103.         else end
  104.         os.sleep(0.1)
  105.     end
  106. end
  107.  
  108. --- Entry
  109.  
  110. local p = dia
  111. p:main(tArgs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement