Advertisement
ZNZNCOOP

Jail

Sep 22nd, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. masters={Zer0Galaxy=true,AlexCC=true}
  2. jail_x=-1469 jail_y=5 jail_z=90 R=5
  3. spawn_x=-1459 spawn_y=5 spawn_z=90
  4.  
  5. p=peripheral.wrap('bottom')
  6. WorldID = p.getPeripheralWorldID()
  7.  
  8. function save()
  9.   local file=fs.open('jailed','w')
  10.   for name,dat in pairs(jailed) do
  11.     file.writeLine(name..' '..jailed[name].time..' '..jailed[name].by)
  12.   end
  13.   file.close()
  14. end
  15.  
  16. function check(username)
  17.   local online= p.getPlayerUsernames()
  18.   for i=1,#online do
  19.     if username==online[i] then
  20.       local user=p.getPlayerByName(username)
  21.       jailed[username].ent=user.asEntity()
  22.       jailed[username].wid=jailed[username].ent.getWorldID()
  23.       return
  24.     end
  25.   end
  26.   jailed[username].ent=nil
  27.   jailed[username].wid=nil
  28. end
  29.  
  30. function load()
  31.   jailed={}
  32. --  local online= p.getPlayerUsernames()
  33.   local file=fs.open('jailed','r')
  34.   if not file then return end
  35.   local line=file.readLine()
  36.   while line do
  37.     local username,jail_time,jailed_by=line:match('([_%w]+)%s+(%d+)%s+([_%w]+)')
  38.     if username then
  39.       jailed[username]={time=tonumber(jail_time),by=jailed_by}
  40.       check(username)
  41.     end
  42.     line=file.readLine()
  43.   end
  44.   file.close()
  45. end
  46.  
  47. function events()
  48.   while true do
  49.     local event,username,mess=os.pullEvent()
  50.     if event=="chat_message" and masters[username] then
  51.       local user,time=mess:match("$$jail%s*([_%w]*)%s*(%d*)")
  52.       if user then
  53.         if user=="" then
  54.           user=p.getPlayerByName(username)
  55.           for n,u in pairs(jailed) do
  56.             user.sendChat(n..' '..(u.time/60)..' by '..u.by..' status-'..(u.wid or 'ofline'))
  57.           end
  58.         else
  59.           jailed[user]={time=(tonumber(time) or 0)*60,by=username}
  60.           check(user)
  61.         end
  62.       end
  63.     elseif event=="player_login" or event=="player_change_world" or event=="player_respawn" then
  64.       if jailed[username] then check(username) end
  65.     elseif event=="player_logout" then
  66.       if jailed[username] then
  67.         jailed[username].ent=nil
  68.         jailed[username].wid=nil
  69.       end
  70.     elseif event=="char" and username=="q" then return
  71.     end
  72.   end
  73. end
  74.  
  75. function guard()
  76.   local change=false
  77.   local x,y,z
  78.   while true do
  79.     for i=1,100 do
  80.       for n,u in pairs(jailed) do
  81.         if u.wid==WorldID and u.time>=0 then
  82.           x,y,z=u.ent.getPosition()
  83.           if math.abs(jail_x-x)>R or math.abs(jail_y-y)>R or math.abs(jail_z-z)>R then
  84.             u.ent.setPosition(jail_x,jail_y,jail_z)
  85.           else
  86.             change=true
  87.             u.time=u.time-3
  88.             if u.time<=0 then
  89.               u.ent.setPosition(spawn_x,spawn_y,spawn_z)
  90.               jailed[n]=nil
  91.               save()
  92.               break
  93.             end
  94.           end
  95.         end
  96.       end
  97.       sleep(3)
  98.     end
  99.     if change then
  100.       save()
  101.       change=false
  102.     end
  103.   end
  104. end
  105.  
  106. load()
  107. parallel.waitForAny(events,guard)
  108. save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement