Teleport to Spawn if lastBed doesnt exist
#1
I will teleport the Player to WorldSpawn if they lastBed doesnt exist.
But the Player.TeleportToCoords doesnt work for me, i copy the SpawnTeleport Code from plugins/core/spawn.lua


Code:
19:37:01] LUA: Plugins/Test1/main.lua:49: error in function 'TeleportToCoords'.
     argument #1 is 'string'; 'cEntity' expected.

[19:37:01] Stack trace:
[19:37:01]   [C](-1): TeleportToCoords
[19:37:01]   Plugins/Test1/main.lua(49): (no name)
[19:37:01] Stack trace end
[19:37:01] Error in <attached> calling function <callback>()



PHP Code:
PLUGIN nil

function Initialize(Plugin)
    
Plugin:SetName("Test1")
    
Plugin:SetVersion(1)

    -- 
Hooks
    cPluginManager
.AddHook(cPluginManager.HOOK_PLAYER_SPAWNEDOnPlayerSpawned)
    
PLUGIN Plugin -- NOTEonly needed if you want OnDisable() to use GetName() or something like that

    LOG
("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
    return 
true
end

function OnDisable()
    
LOG(PLUGIN:GetName() .. " is shutting down...")
end

function OnPlayerSpawned(Player) -- See API docs for parameters of all hooks
    local lastBedPos 
Player:GetLastBedPos()
    
local world=Player:GetWorld()
    
local block=world:GetBlock(lastBedPos.xlastBedPos.ylastBedPos.z)

    
local WorldIni cIniFile()
    
WorldIni:ReadFile(Player:GetWorld():GetIniFileName())

    
local SpawnX WorldIni:GetValue("SpawnPosition""X")
    
local SpawnY WorldIni:GetValue("SpawnPosition""Y")
    
local SpawnZ WorldIni:GetValue("SpawnPosition""Z")

    if (
block == 26then
        
-- do stuff
    
else
        
Player.TeleportToCoords(SpawnXSpawnYSpawnZ)
    
end
end 
Reply
Thanks given by:
#2
The error message basically says it all. You're calling TeleportToCoords with wrong arguments, it expects a cEntity as its first parameter, but you're giving it a string.
Note that in Lua, there are two "calling conventions":
obj.fn("a") -- calls obj.fn with param "a"
obj:fn("a") -- calls obj.fn with params obj, "a"
The second is what Cuberite is using everywhere and is expecting from the plugins.

The SpawnX/Y/Z need to be read with a GetValueI() function, so that they are numbers, not strings.
Reply
Thanks given by:
#3
One more friendly advice: Use E_BLOCK_BED instead of 26.
Reply
Thanks given by:
#4
A list of block, item ids and more constants can you find here Globals
Reply
Thanks given by:
#5
If i use

PHP Code:
    local SpawnX WorldIni:GetValueI("SpawnPosition""X")
    
local SpawnY WorldIni:GetValueI("SpawnPosition""Y")
    
local SpawnZ WorldIni:GetValueI("SpawnPosition""Z"


i become this msg


Code:
[20:06:37] LUA: Plugins/Test1/main.lua:27: attempt to call method 'GetValuelI' (a nil value)
[20:06:37] Stack trace:
[20:06:37]   Plugins/Test1/main.lua(27): (no name)
[20:06:37] Stack trace end
[20:06:37] Error in <attached> calling function <callback>()


can you give me an example?
Reply
Thanks given by:
#6
Do you want to read the spawn position from the world ini?
If yes you could directly use the functions: GetSpawnX, GetSpawnY and GetSpawnZ from cWorld.

if not about world spawn.The error means that WorldIni is not initialized. Post the code where you are initializing WorldIni.

Edit: Ah sorry didn't saw the whole code in first post, will look at it.
Reply
Thanks given by:
#7
(05-14-2017, 04:22 AM)Seadragon91 Wrote: Do you want to read the spawn position from the world ini?
If yes you could directly use the functions: GetSpawnX, GetSpawnY and GetSpawnZ from cWorld.

if not about world spawn.The error means that WorldIni is not initialized. Post the code where you are initializing WorldIni.

Edit: Ah sorry didn't saw the whole code in first post, will look at it.

Server Start Log

Code:
[20:41:41] --- Started Log ---
[20:41:41] Cuberite Jenkins clang x64 Release (master) build id: #578
[20:41:41] from commit id: c83b051c1a3e68fc9315ffffb277dc3da86cf02a built at: Thu  8 Sep 21:30:46 UTC 2016
[20:41:41] Creating new server instance...
[20:41:41] Reading server config...
[20:41:41] Starting server...
[20:41:41] Compatible clients: 1.8.x, 1.9.x, 1.10.x
[20:41:41] Compatible protocol versions 47, 107, 108, 109, 110, 210
[20:41:41] WebServer: The server will run in unsecured HTTP mode.
[20:41:41] Put a valid HTTPS certificate in file 'webadmin/httpscert.crt' and its corresponding private key to 'webadmin/httpskey.pem' (without any password) to enable HTTPS support
[20:41:41] Loaded 465 crafting recipes
[20:41:41] Loaded 22 furnace recipes and 56 fuels
[20:41:41] Loaded 88 brewing recipes
[20:41:41] -- Loading Plugins --
[20:41:41] Initialised Core v.15
[20:41:41] Initialized TransAPI v.1
[20:41:41] Initialized ChatLog v.3
[20:41:41] Initialised Test1 v.1
[20:41:41] -- Loaded 4 Plugins --
[20:41:42] Did not find an acceptable spawnpoint. Generated a random spawnpoint position at {0.00, 168.00, 0.00}
[20:41:42] WebAdmin is running on port(s) 8080
[20:41:42] Startup complete, took 1662ms!


Code with GetSpawn


PHP Code:
local SpawnX world:GetSpawnX()
local SpawnY world:GetSpawnY()
local SpawnZ world:GetSpawnZ()
Player.TeleportToCoords(SpawnXSpawnYSpawnZ

Error with GetSpawn

Code:
[20:39:31] WorldSpawnX:-2.1523170155827 WorldSpawnY:65 WorldSpawnZ:-32.89404058136
[20:39:31] LUA: Plugins/Test1/main.lua:37: error in function 'TeleportToCoords'.
    argument #1 is 'number'; 'cEntity' expected.

[20:39:31] Stack trace:
[20:39:31]   [C](-1): TeleportToCoords
[20:39:31]   Plugins/Test1/main.lua(37): (no name)
[20:39:31] Stack trace end
[20:39:31] Error in <attached> calling function <callback>()


What i can do? Huh
Reply
Thanks given by:
#8
Change
Player.TeleportToCoords(SpawnX, SpawnY, SpawnZ)
to
Player:TeleportToCoords(SpawnX, SpawnY, SpawnZ)

Has to be a colon :
Reply
Thanks given by:
#9
(05-14-2017, 04:45 AM)Seadragon91 Wrote: Change
Player.TeleportToCoords(SpawnX, SpawnY, SpawnZ)
to
Player:TeleportToCoords(SpawnX, SpawnY, SpawnZ)

Has to be a colon :


Waaah.... now i see the different in xoft's post too.....

obj.fn("a") -- calls obj.fn with param "a"
obj:fn("a") -- calls obj.fn with params obj, "a"



I understand... thanksConfusedick:
Reply
Thanks given by:
#10
I'm quite concerned with the "GetValuelI" not found, even with the INI file not read, the method should be there.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)