This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HPWebcamAble's profile picture

Command Utils - Get players' coordinates, and more

Started by HPWebcamAble, 06 September 2015 - 11:43 PM
HPWebcamAble #1
Posted 07 September 2015 - 01:43 AM
The command computer is really cool. It is. But it has a few flaws.

This API is designed to let you get around those flaws!
(Yes this is an API. But it is only for Command Computers, so I decided it was a Command Program)


Here's the functions:
(There's no pastebin because its so short. Simply copy and paste the functions you'd like into your program)
SpoilerIf you don't pass any arguments, it gives you all players

Returns: A table of the players

function getPlayers(radius,x,y,z)
  if x then x = "x="..x else x = "" end
  if y then y = ",y="..y else y = "" end
  if z then z = ",z="..z else z = "" end
  local command = "0 @a"
  if radius then
	command = "0 @a["..x..y..z..(",r="..radius).."]"
  end
  local state,result = commands.xp(command)
  local players = {}
  for i = 1, #result do
	table.insert(players,string.match(result[i],"Given 0 experience to (%w+)"))
  end
  return players
end

Spoilername: The name of the player as a string

Returns: the x,y,z of the player (decimals included), or false if the player couldn't be found

function getPlayerCoords(name)
  local state,result = commands.tp(name.." ~ ~ ~")
  if not state then return false end
  return result[1]:match( "Teleported %S+ to (.?%d+%.?%d*),(.?%d+%.?%d*),(.?%d+%.?%d*)" )
end

SpoilerWhat is this? You ask. What about commands.kill()?
commands.kill() doesn't work! Has to do with how the command gets run

This kills the player by applying a damage potion effect to them.
(It also sets them to survival first)

Returns: true if the player was killed, false if not

function kill(name)
  local state1,result1 = commands.gamemode("0 "..name)
  sleep(0.1)
  local state2,result2 = commands.effect(name.." 7 4 100")
  return state1 and state2
end


These were just a few that I thought of.
Any suggestions for other functions?
Edited on 07 September 2015 - 04:48 AM
MKlegoman357 #2
Posted 07 September 2015 - 06:28 AM
To get the coordinates of the computer you'd simply use commands.getBlockPosition(). Also, are you sure testforblock works like that in MC 1.7?
HPWebcamAble #3
Posted 07 September 2015 - 06:48 AM
To get the coordinates of the computer you'd simply use commands.getBlockPosition()

Wow. Cant believe I forgot about that :P/>

Yes, my way works, but its ridiculous if there's already a function for it.
Lupus590 #4
Posted 07 September 2015 - 12:17 PM
local coordinates to world coordinates converter, local coordinates use the command computer as 0,0,0 (can have negative y)
HPWebcamAble #5
Posted 07 September 2015 - 05:35 PM
local coordinates to world coordinates converter, local coordinates use the command computer as 0,0,0 (can have negative y)

Err, wouldn't you just add the local coords to the world coords?

What exactly are 'local coordinates' anyway?
Lupus590 #6
Posted 07 September 2015 - 06:58 PM
What exactly are 'local coordinates' anyway?

coordinates which use the command computer as the reference point (therefore the computer is 0,0,0 in local coordinates but could be anything in world coordinates)
HPWebcamAble #7
Posted 07 September 2015 - 07:16 PM
coordinates which use the command computer as the reference point (therefore the computer is 0,0,0 in local coordinates but could be anything in world coordinates)

Seems like I'd just use GPS at that point.

Is this something you've used in a program before?
Lupus590 #8
Posted 07 September 2015 - 09:24 PM
it would mean that people using a command computer to make a box can put the computer where they want the box, and not have to type in 3 sets of (potentially) 4 digit coordinates

or you could support pairing with a pocket PC and make a client on it which transmits gps coords for the box
Edited on 07 September 2015 - 07:26 PM
HPWebcamAble #9
Posted 07 September 2015 - 09:45 PM
it would mean that people using a command computer to make a box can put the computer where they want the box, and not have to type in 3 sets of (potentially) 4 digit coordinates

Thats a little too specific for a Command computer utility program. At least I think so.

or you could support pairing with a pocket PC and make a client on it which transmits gps coords for the box

I was actually thinking about making a program like that, I'm waiting for MC 1.8 / 1.9, when we'll have access to additional commands like /fill or /clone, just to name a few.

Like I said, this doesn't really fit with a Command utility program, the idea is that these functions can provide the functionality usually available through something like a command block or a plugin/mod
Lupus590 #10
Posted 08 September 2015 - 12:27 AM
I only see a need for local coordinates for if I was using a command computer.

Edit: I guess that if I was building with a turtle then local coordinates could use the turtles start position
Edited on 07 September 2015 - 10:29 PM
HPWebcamAble #11
Posted 08 September 2015 - 12:34 AM
I think a better term here is 'Relative Coordinates', as 0,0,0 is the block / turtle's starting position, not 0,0,0 in the MC world.
Edited on 07 September 2015 - 10:34 PM