computercraft-scripts/player-sensor/ligma.lua

108 lines
2.8 KiB
Lua
Raw Normal View History

2021-12-22 22:20:20 +00:00
local WH_URL = "https://discord.com/api/webhooks/id/token"
2021-12-25 16:04:08 +00:00
local IGNORE = { "player1", "player2" }
local DIMENSION = "minecraft:overworld"
local AREA = {
[0] = { x = 100, y = 0, z = 200 },
[1] = { x = 200, y = 200, z = 300 }
2021-12-22 22:20:20 +00:00
}
2021-12-25 16:04:08 +00:00
local REDSTONE_SIDE = "front"
local MODEM_PORT = 12345
local modem = peripheral.wrap("back")
2021-12-22 22:20:20 +00:00
local sensor = peripheral.find("playerDetector")
2021-12-25 16:04:08 +00:00
local chatbox = peripheral.find("chatBox")
2021-12-22 22:20:20 +00:00
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
2021-12-25 16:04:08 +00:00
function modemMessage(msg)
modem.transmit(MODEM_PORT, MODEM_PORT + 1, msg)
end
2021-12-22 22:20:20 +00:00
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
2021-12-25 16:04:08 +00:00
function randomRound(num)
return num + math.random(-5, 5)
end
2021-12-22 22:20:20 +00:00
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")
2021-12-25 16:04:08 +00:00
2021-12-22 22:20:20 +00:00
sendWhMessage("Player `"..v.."` entered area "..
"at `"..pos.x..","..pos.y..","..pos.z.."`.")
table.insert(KNOWN_PLAYERS, v)
2021-12-25 16:04:08 +00:00
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))
2021-12-22 22:20:20 +00:00
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))
2021-12-25 16:04:08 +00:00
modemMessage(v.." left at "..randomRound(pos.x)
..","..randomRound(pos.y)..","
..randomRound(pos.z))
2021-12-22 22:20:20 +00:00
end
end
end