108 lines
2.8 KiB
Lua
108 lines
2.8 KiB
Lua
local WH_URL = "https://discord.com/api/webhooks/id/token"
|
|
local IGNORE = { "player1", "player2" }
|
|
local DIMENSION = "minecraft:overworld"
|
|
local AREA = {
|
|
[0] = { x = 100, y = 0, z = 200 },
|
|
[1] = { x = 200, y = 200, z = 300 }
|
|
}
|
|
local REDSTONE_SIDE = "front"
|
|
local MODEM_PORT = 12345
|
|
local modem = peripheral.wrap("back")
|
|
local sensor = peripheral.find("playerDetector")
|
|
local chatbox = peripheral.find("chatBox")
|
|
local KNOWN_PLAYERS = {}
|
|
local COOLDOWNS = {}
|
|
local COOLDOWN = 0.1
|
|
|
|
function sendWhMessage(msg)
|
|
local payload = { content = msg }
|
|
http.post(WH_URL,
|
|
textutils.serializeJSON(payload),
|
|
{ ["Content-Type"] = "application/json" })
|
|
end
|
|
|
|
function modemMessage(msg)
|
|
modem.transmit(MODEM_PORT, MODEM_PORT + 1, msg)
|
|
end
|
|
|
|
function findPlayers()
|
|
local players = sensor.getPlayersInCoords(AREA[0], AREA[1])
|
|
return players
|
|
end
|
|
|
|
function getPlayerPos(player)
|
|
local pos = sensor.getPlayerPos(player)
|
|
return pos
|
|
end
|
|
|
|
function isInArr(arr, val)
|
|
for k,v in pairs(arr) do
|
|
if v == val then return k end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function timestamp()
|
|
return (os.day() * 24) + os.time()
|
|
end
|
|
|
|
function isCooldown(player)
|
|
local item = COOLDOWNS[player]
|
|
local time = timestamp()
|
|
return not (not item or item < time)
|
|
end
|
|
|
|
function setCooldown(player)
|
|
COOLDOWNS[player] = timestamp() + COOLDOWN
|
|
end
|
|
|
|
function isIgnore(player)
|
|
return isInArr(IGNORE, player) ~= false
|
|
end
|
|
|
|
function randomRound(num)
|
|
return num + math.random(-5, 5)
|
|
end
|
|
|
|
while true do
|
|
local found = findPlayers()
|
|
|
|
-- check for new players
|
|
for k,v in pairs(found) do
|
|
local pos = getPlayerPos(v)
|
|
local inArr = isInArr(KNOWN_PLAYERS, v)
|
|
|
|
if pos ~= nil and pos.dimension == DIMENSION and not inArr and not isCooldown(v) and not isIgnore(v) then
|
|
setCooldown(v)
|
|
print("Player "..v.." entered area")
|
|
|
|
sendWhMessage("Player `"..v.."` entered area "..
|
|
"at `"..pos.x..","..pos.y..","..pos.z.."`.")
|
|
|
|
table.insert(KNOWN_PLAYERS, v)
|
|
|
|
chatbox.sendMessageToPlayer("You have entered a restricted area. Leave now if you value your balls.", v, "FSF")
|
|
|
|
modemMessage(v.." entered at "..randomRound(pos.x)
|
|
..","..randomRound(pos.y)..","..
|
|
randomRound(pos.z))
|
|
end
|
|
end
|
|
|
|
-- check if players left the area
|
|
for k,v in pairs(KNOWN_PLAYERS) do
|
|
local pos = getPlayerPos(v)
|
|
if pos ~= nil and (not isInArr(found, v) or pos.dimension ~= DIMENSION) and not isCooldown(v) and not isIgnore(v) then
|
|
setCooldown(v)
|
|
print("Player "..v.." left area")
|
|
sendWhMessage("Player `"..v.."` left area at `"..
|
|
pos.x..","..pos.y..","..pos.z.."`.")
|
|
table.remove(KNOWN_PLAYERS, isInArr(KNOWN_PLAYERS, v))
|
|
|
|
modemMessage(v.." left at "..randomRound(pos.x)
|
|
..","..randomRound(pos.y)..","
|
|
..randomRound(pos.z))
|
|
end
|
|
end
|
|
end
|