residentsleeper/scripts/warps.lua

158 lines
3.6 KiB
Lua
Raw Normal View History

local event = require('event')
local commands = require('commands')
timers = require('timers')
minecraft = require('minecraft')
config = require('config')
warpDelay = 60 * 5
homes = {}
warps = {}
lastHomeTime = {}
lastWarpTime = {}
objectiveName = "damageTaken"
event.on('init', function()
local loadedHomes, err = config.load('homes')
if loadedHomes and not err then
homes = loadedHomes
end
local loadedWarps, err = config.load('warps')
if loadedWarps and not err then
warps = loadedWarps
end
rcon:SendCommand('scoreboard objectives add ' .. objectiveName .. ' minecraft.custom:minecraft.damage_taken')
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:SendColorfulMessage(user, 'green', 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:SendColorfulMessage(user, 'green', '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:SendColorfulMessage(user, 'red', "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:SendColorfulMessage(user, 'red', "You need to wait " .. delay .. " seconds before warping again.")
return
end
lastHomeTime[user] = os.time()
local delayLeft = 5
local originalDamage, err = rcon:SendCommand("scoreboard players get " .. user .. " " .. objectiveName)
local interval = nil
interval = timers.setInterval(function()
if delayLeft == 0 then
local newDamage, err = rcon:SendCommand("scoreboard players get " .. user .. " " .. objectiveName)
timers.clearInterval(interval)
if newDamage ~= originalDamage then
rcon:SendColorfulMessage(user, 'red', "Teleport was interrupted by combat.")
return
end
rcon:SendCommand('execute in minecraft:overworld run tp ' .. user .. ' ' .. string.format("%f %d %f", loc[1], loc[2], loc[3]))
else
rcon:SendColorfulMessage(user, 'gray', "Teleporting in " .. delayLeft .. " seconds")
delayLeft = delayLeft - 1
end
end, 1000)
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:SendColorfulMessage(user, 'green', 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:SendColorfulMessage(user, 'red', "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:SendColorfulMessage(user, 'red', "You need to wait " .. delay .. " seconds before warping again.")
return
end
lastWarpTime[user] = os.time()
rcon:SendCommand('execute in minecraft:overworld run tp ' .. user .. ' ' .. string.format("%f %d %f", loc[1], loc[2], loc[3]))
end)