Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Player Detector Chat Command Script
- -- Requires: Advanced Peripherals (Player Detector peripheral)
- -- Place Player Detector adjacent to the computer, then run this script.
- local detector = peripheral.find("playerDetector")
- if not detector then
- error("No Player Detector found! Attach one to this computer.", 0)
- end
- -- Helper: get readable dimension name
- local function formatDimension(dim)
- if not dim then return "Unknown" end
- local names = {
- ["minecraft:overworld"] = "Overworld",
- ["minecraft:the_nether"] = "The Nether",
- ["minecraft:the_end"] = "The End",
- }
- return names[dim] or dim
- end
- -- Helper: parse "where is <name>" from chat message
- local function parseQuery(message)
- local name = message:match("^%s*where is%s+(%S+)%s*$")
- if not name then
- name = message:match("^%s*pos%s+(%S+)%s*$")
- end
- return name
- end
- print("Player Detector active.")
- print('Say "where is <player>" or "pos <player>" in chat.')
- print("Press Ctrl+T to stop.")
- -- Listen for chat events
- while true do
- local event, username, message = os.pullEvent("chat")
- local target = parseQuery(message:lower())
- -- preserve original casing by re-parsing with original message
- if not target then
- target = parseQuery(message)
- else
- -- re-extract with original casing
- target = message:match("[Ww]here%s+[Ii]s%s+(%S+)") or
- message:match("[Pp]os%s+(%S+)")
- end
- if target then
- -- Get online players and do case-insensitive match
- local players = detector.getOnlinePlayers()
- local matched = nil
- for _, p in ipairs(players) do
- if p:lower() == target:lower() then
- matched = p
- break
- end
- end
- if not matched then
- print("[" .. username .. "] Player '" .. target .. "' is not online.")
- else
- local pos = detector.getPlayerPos(matched)
- if pos then
- local dim = formatDimension(pos.dimension)
- local x = math.floor(pos.x * 10 + 0.5) / 10
- local y = math.floor(pos.y * 10 + 0.5) / 10
- local z = math.floor(pos.z * 10 + 0.5) / 10
- local reply = string.format(
- "[Detector] %s is at X: %.1f Y: %.1f Z: %.1f | Dimension: %s",
- matched, x, y, z, dim
- )
- -- Print to terminal
- print(reply)
- -- If a chat peripheral (e.g. Chat Box from AP) is available, respond in chat
- local chatBox = peripheral.find("chatBox")
- if chatBox then
- chatBox.sendMessage(reply, "Detector")
- end
- else
- print("[" .. username .. "] Could not get position for " .. matched .. ".")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment