Cuberite Forum
Need help to my first plugin ^^ - 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: Need help to my first plugin ^^ (/thread-1785.html)



Need help to my first plugin ^^ - Infinity-Codeur - 02-16-2015

Hello everyone,

I try to create my first plugin for McServer :p

My project add 2 comands "/getposition" ant "getTp"

/getposition save the x,y,z position of player
/getTp teleport to x,y,z position that have been saved by /getposition

This is my code :

PlayerPos = {}
function Initialize(Plugin)
PLUGIN = Plugin
	Plugin:SetName("Position")
	Plugin:SetVersion(1)
	--Command
	cPluginManager.BindCommand("/getposition", "permissionnode", GetPos, " - Get your position")
	cPluginManager.BindCommand("/getTp", "permissionnode", GetTp, " - Tp to position")
	LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
return true
end

function GetPos(Player)
	PlayerPos = Vector3d(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ())
	Player:SendMessage("Position Register")
end

function GetTp(Player)
	Player:TeleportToCoords(PlayerPos.x, PlayerPos.y, PlayerPos.z)
	Player:SendMessage("You have tp into you old position")
end

But i have this error when i execute /getposition :
Code:
[19:10:05] LUA: Plugins/Position/Position.lua:14: attempt to call method 'GetPosX' (a nil value)
[19:10:05] Stack trace:
[19:10:05]   Plugins/Position/Position.lua(14): (no name)
[19:10:05] Stack trace end
[19:10:05] Error in plugin Position calling function <callback>()

What is the problem with my code ? ^^

Thank in advance


RE: Need help to my first plugin ^^ - xoft - 02-16-2015

Hello, welcome to the forum.

You have the callback function signatures wrong. The first parameter is the split (array-table of strings that represent the command broken on individual spaces), the player is in the second parameter. This should work:

...
function GetPos(Split, Player)
    PlayerPos = Vector3d(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ())
    Player:SendMessage("Position Register")
end

function GetTp(Split, Player)
...

Also, your plugin will store the position for all players in a single variable, so the players will overwrite each other's last saved position. Is that intentional?

One last thing, the next time you post lua code, please wrap it in [ shcode=lua ] tag instead of [ php ] tag. I have edited your post to fix that. The code is then properly syntax-highlighted and doesn't scroll.


RE: Need help to my first plugin ^^ - Infinity-Codeur - 02-16-2015

Thank the 2 commands work ! ^^
This is my new code :

 PlayerPos = {}
function Initialize(Plugin)
PLUGIN = Plugin
	Plugin:SetName("Position")
	Plugin:SetVersion(1)
	--Command
	cPluginManager.BindCommand("/getposition", "permissionnode", GetPos, " - Get your position")
	cPluginManager.BindCommand("/getTp", "permissionnode", GetTp, " - Tp to position")
	LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
return true
end

function GetPos(Split,Player)
	PlayerPos[Player:GetUniqueID()] = Vector3d(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ())
	Player:SendMessage("Position Register")
end
 
function GetTp(Split,Player)
	Player:TeleportToCoords(PlayerPos[Player:GetUniqueID()].x, PlayerPos[Player:GetUniqueID()].y, PlayerPos[Player:GetUniqueID()].z)
	Player:SendMessage("You have tp into you old position")

But in chat i have "position register" or "You have tp into you old position" And "Info : something went wrong while executing command" in my server i don't have any error just in minecraft chat What is the probleme ?

Thank in advance Wink


RE: Need help to my first plugin ^^ - NiLSPACE - 02-16-2015

You have to return true in the command handlers
function MyCommand(a_Split, a_Player)
   -- Do stuff
   return true
end



RE: Need help to my first plugin ^^ - Infinity-Codeur - 02-16-2015

Thank a lot my probleme is fixed !

edit : Now i want to add to my script a condition if player don't have position saved my code :

Code:
    if PlayerPos[Player:GetUniqueID()] != 0 then
        Player:SendMessage("You don't have any position !")
    end

But it's not work Sad


RE: Need help to my first plugin ^^ - xoft - 02-16-2015

Line 19 of your last code is somewhat needlessly complicated. You should store PlayerPos[Player:GetUniqueID()] into a local variable and use that to query the X, Y and Z coords. This will allow you to save 2 calls into C++ and two table lookups, not to mention that the line will become much shorter.