Major updates/patches, functionality, api
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Tyler
2020-07-09 21:01:29 -04:00
parent afb79eaceb
commit b52b38179a
32 changed files with 1798 additions and 302 deletions

View File

@ -0,0 +1,98 @@
local event = require('event')
local regexp = require('regexp')
zzzRe = regexp.MustCompile('^z{3,}$')
ratio = 0.30
votes = {}
onlinePlayers = 0
expireTime = 0
event.on('init', function()
-- This is called when the server is started or the script is initialized (late load)
online, max, names, err = rcon:OnlinePlayers()
onlinePlayers = online
end)
event.on('message', function(user, message)
if not zzzRe:MatchString(message) then
return
end
t = rcon:GetTime()
if t < 12000 then
rcon:SendColorfulMessage(user, 'red', "It's not night time, go mine some more.")
return
end
-- Reset votes if we got this far and the previous vote expired
if os.time() > expireTime then
votes = {}
end
local difference = 24000 - t
-- 20 ticks per second (50ms per tick)
expireTime = os.time() + ((difference * 50) / 1000)
votes[user] = true
local currentVotes = tablelength(votes)
local requiredVotes = math.ceil(onlinePlayers * ratio)
if currentVotes >= requiredVotes then
mimicSleeping(t)
else
rcon:SendColorfulMessage('@a', 'blue', string.format('%s wants to sleep (%d of %d votes)', user, currentVotes, requiredVotes))
end
end)
event.on('logged_in', function(user)
onlinePlayers = onlinePlayers + 1
end)
event.on('leave', function(user)
onlinePlayers = onlinePlayers - 1
if votes[user] then
votes[user] = nil
if os.time() > expireTime then
votes = {}
return
end
end
local requiredVotes = math.ceil(onlinePlayers * ratio)
if tablelength(votes) > requiredVotes then
mimicSleeping(-1)
end
end)
function mimicSleeping(t)
if t == -1 then
t = rcon:GetTime()
end
local difference = 24000 - t
votes = {}
local newTime = rcon:AddTime(difference)
if newTime < 0 or newTime > 12000 then
rcon:ServerMessage('Unable to set time!')
return
end
rcon:SendColorfulMessage('@a', 'green', 'Good morning, rise and shine!')
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end

124
scripts/timetracker.lua Normal file
View File

@ -0,0 +1,124 @@
local event = require('event')
local commands = require('commands')
config = require('config')
playtimes = {}
playerLogin = {}
initialized = false
function initTimeTracking()
if initialized then
return
end
initialized = true
print('Initializing time tracking')
local loadedPlaytimes, err = config.load('playtime')
if loadedPlaytimes and not err then
playtimes = loadedPlaytimes
end
online, max, names, err = rcon:OnlinePlayers()
if err ~= nil then
print('Unable to load online players: ' .. err:Error())
return
end
for index = 1, #names do
print('Setting login time for ' .. names[index])
playerLogin[names[index]] = os.time()
end
end
initTimeTracking()
event.on('init', initTimeTracking)
event.on('join', function(user)
playerLogin[user] = os.time()
end)
event.on('leave', function(user)
local timeOnline = os.time() - playerLogin[user]
if playtimes[user] ~= nil then
playtimes[user] = playtimes[user] + timeOnline
else
playtimes[user] = timeOnline
end
playerLogin[user] = nil
err = config.save('playtime', playtimes)
if err ~= nil then
print('Unable to save playtimes')
end
end)
commands.register('playtime [name]', function(user, name)
if name == nil then
name = user
end
print('User ' .. user .. ' is checking playtime of ' .. name)
if playtimes[name] == nil and playerLogin[name] == nil then
rcon:SendMessage(user, 'User has not logged any playtime.')
return
end
local playTime = playtimes[name] or 0
if playerLogin[name] ~= nil then
local timeOnline = os.time() - playerLogin[name]
playTime = playTime + timeOnline
end
print('Time played: ' .. playTime)
rcon:SendColorfulMessage('@a', 'green', name .. ' has played for ' .. formatElapsed(playTime))
end)
function formatElapsed(elapsedSeconds)
local weeks, days, hours, minutes, seconds = formatSeconds(elapsedSeconds)
local weeksTxt, daysTxt, hoursTxt, minutesTxt, secondsTxt = ""
if weeks == 1 then weeksTxt = 'week' else weeksTxt = 'weeks' end
if days == 1 then daysTxt = 'day' else daysTxt = 'days' end
if hours == 1 then hoursTxt = 'hour' else hoursTxt = 'hours' end
if minutes == 1 then minutesTxt = 'minute' else minutesTxt = 'minutes' end
if seconds == 1 then secondsTxt = 'second' else secondsTxt = 'seconds' end
if elapsedSeconds >= 604800 then
return weeks..' '..weeksTxt..', '..days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 86400 then
return days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 3600 then
return hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 60 then
return minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
else
return seconds..' '..secondsTxt
end
end
function formatSeconds(secondsArg)
local weeks = math.floor(secondsArg / 604800)
local remainder = secondsArg % 604800
local days = math.floor(remainder / 86400)
local remainder = remainder % 86400
local hours = math.floor(remainder / 3600)
local remainder = remainder % 3600
local minutes = math.floor(remainder / 60)
local seconds = remainder % 60
return weeks, days, hours, minutes, seconds
end

137
scripts/warps.lua Normal file
View File

@ -0,0 +1,137 @@
local event = require('event')
local commands = require('commands')
minecraft = require('minecraft')
config = require('config')
warpDelay = 60 * 5
homes = {}
warps = {}
lastHomeTime = {}
lastWarpTime = {}
event.on('init', function()
local loadedHomes, err = config.load('homes')
if loadedHomes and not err then
homes = loadedHomes
end
end)
commands.register('sethome', function(user)
local loc = rcon:GetLocation(user)
local c = {}
for i, v in loc() do
c[i] = v
end
homes[user] = c
err = config.save('homes', homes)
if err ~= nil then
print('Unable to save homes')
end
rcon:SendMessage(user, string.format("Home location set to %d, %d, %d", c[1], c[2], c[3]))
end)
commands.register('resethome', function(user)
homes[user] = nil
err = config.save('homes', homes)
if err ~= nil then
print('Unable to save homes')
end
rcon:SendMessage(user, 'Home location reset')
end)
commands.register('home', function(user)
local loc = nil
if homes[user] ~= nil then
loc = homes[user]
end
if loc == nil then
rcon:SendMessage(user, "You haven't set your spawn or home yet.")
return
end
local lastWarp = lastHomeTime[user]
if lastWarp ~= nil and lastWarp + warpDelay > os.time() and not minecraft.isOp(user) then
local delay = warpDelay - (os.time() - lastWarp)
rcon:SendMessage(user, "You need to wait " .. delay .. " seconds before warping again.")
return
end
lastHomeTime[user] = os.time()
rcon:Teleport(user, string.format("%f %d %f", loc[1], loc[2], loc[3]))
end)
commands.register('setwarp <place>', function(user, place)
if not minecraft.isOp(user) then
return
end
local loc = rcon:GetLocation(user)
local c = {}
for i, v in loc() do
c[i] = v
end
warps[place] = c
err = config.save('warps', warps)
if err ~= nil then
print('Unable to save warps')
end
rcon:SendMessage(user, string.format("Warp %s set to %d, %d, %d", place, c[1], c[2], c[3]))
end)
commands.register('warp <place>', function(user, place)
if warps[place] == nil then
rcon:SendMessage(user, "This warp point doesn't exist")
return
end
local loc = warps[place]
local lastWarp = lastWarpTime[user]
if lastWarp ~= nil and lastWarp + warpDelay > os.time() and not minecraft.isOp(user) then
local delay = warpDelay - (os.time() - lastWarp)
rcon:SendMessage(user, "You need to wait " .. delay .. " seconds before warping again.")
return
end
lastWarpTime[user] = os.time()
rcon:Teleport(user, string.format("%f %d %f", loc[1], loc[2], loc[3]))
end)
tprequests = {}
commands.register('tpa <target>', function(user, target)
end)
commands.register('tpaccept', function(user)
end)
commands.register('tpdeny', function(user)
end)

5
scripts/welcome.lua Normal file
View File

@ -0,0 +1,5 @@
local event = require('event')
event.on('join', function(user)
rcon:ServerMessage('Welcome to the server ' .. user .. '!!!')
end)