Cuberite Forum
RandomTP - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: RandomTP (/thread-2093.html)

Pages: 1 2


RandomTP - Keith - 08-30-2015

Hi, its my first plugin in LUA.
I don't know how to check the ID of block in random XYZ position.
Sometimes its move me into blocks. I want to teleport olny on grass, sand, dirt.
Please help Smile
PHP Code:
function Randomtp(Split, Player) if (#Split ~= 1) then Player:SendMessage("Usage: /rtp") return true else X = math.random(-2000, 2000) Y = math.random(40,70) Z = math.random(-2000, 2000) Player:TeleportToCoords(X, Y, Z) Player:SendMessage(cChatColor.Yellow .. "Cords") Player:SendMessage("X: " .. X) Player:SendMessage("Y: " .. Y) Player:SendMessage("Z: " .. Z) end return true end



RE: RandomTP - NiLSPACE - 08-30-2015

Hello, and welcome to the forum Smile

You can use this:
local BlockID = Player:GetWorld():GetBlock(X, Y, Z)
if ((BlockID == E_BLOCK_GRASS) or (BlockID == E_BLOCK_DIRT) or (BLOCKID == E_BLOCK_SAND)) then
   -- Teleport the player
end

But keep in mind that a player can now be teleported inside block. If you use this:
local X = math.random(-2000, 2000)
local Z = math.random(-2000, 2000)
local Succes, Y = Player:GetWorld():TryGetHeight(X, Z)

the player should teleport to the highest block instead.


RE: RandomTP - Keith - 08-30-2015

I used :
PHP Code:
X = math.random(-2000, 2000) Z = math.random(-2000, 2000) Y = Player:GetWorld():GetHeight(X, Z)

Trying to use TryGetHeight(X, Z), returned bool value and error in colsole.


RE: RandomTP - DiamondToaster - 08-30-2015

TryGetHeight() returns two different variables on completion. The first is a boolean which indicates if the chunk is even loaded and a second which is the highest block point on those X and Z coordinates. You would have to set TryGetHeight() equal to 2 variables and use the second one to get the right value.

local IsChunkLoaded
local Height

IsChunkLoaded, Height = Player:GetWorld():TryGetHeight(X, Z)




RE: RandomTP - Keith - 08-30-2015

[Image: err.png]

PHP Code:
function Initialize(Plugin) Plugin:SetName("RandomTP") Plugin:SetVersion(1) cPluginManager.BindCommand("/rtp", "randomTP.rtp", Randomtp, " ~ Random Teleport"); LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion()) return true end function Randomtp(Split, Player) if (#Split ~= 1) then Player:SendMessage("Usage: /rtp") return true else X = math.random(-2000, 2000) Z = math.random(-2000, 2000) local IsChunkLoaded local Height IsChunkLoaded, Height = Player:GetWorld():TryGetHeight(X, Z) Player:TeleportToCoords(X, Height, Z) Player:SendMessage(cChatColor.Yellow .. "Cords") Player:SendMessage("X: " .. X) Player:SendMessage("Y: " .. Y) Player:SendMessage("Z: " .. Z) end return true end



RE: RandomTP - NiLSPACE - 08-30-2015

The chunk is most likely not loaded. Try this:
local X = math.random(-2000, 2000)
local Z = math.random(-2000, 2000)
local _, Height

local World = Player:GetWorld()
World:ChunkStay({math.floor(X / 16), math.floor(Z / 16)}, nil, 
	function()
		_, Height = World:TryGetHeight(X, Z)
	end
)

It will ensure that the chunks are loaded and then get the height.


Btw, I didn't test this code. There might be a syntax error in it.


RE: RandomTP - Keith - 08-30-2015

NiLSPACE,
[Image: errr.png]

It works with GetHeight(X, Z). But sometimes it is teleporting me before chunk load, and im locking in blocks


RE: RandomTP - NiLSPACE - 08-30-2015

Ooh, I see the problem. Try:
World:ChunkStay({{math.floor(X / 16), math.floor(Z / 16)}}, nil, 



RE: RandomTP - Keith - 08-30-2015

PHP Code:
argument #3 is nil ; 'number' expected



RE: RandomTP - NiLSPACE - 08-30-2015

That's weird. Could you post the whole code + whole error?