Posts: 4,628
Threads: 115
Joined: Dec 2011
Thanks: 693
Given 494 thank(s) in 423 post(s)
06-27-2013, 04:39 AM
(This post was last modified: 06-27-2013, 04:39 AM by NiLSPACE.)
then it should be more like this:
if Split[2] == "creative" then
Player:MoveToWorld(Split[2])
Player:SetGameMode(1)
Player:SendMessage( cChatColor.Green .. "Moved successfully to \'" .. Split[2] .. "\'! " )
return true
end
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
Thank you veery much
Posts: 6,485
Threads: 176
Joined: Jan 2012
Thanks: 131
Given 1075 thank(s) in 852 post(s)
06-27-2013, 05:40 AM
(This post was last modified: 06-27-2013, 05:43 AM by xoft.)
1, Don't use magic numbers in your code, use named constants:
Player:SetGameMode(gmCreative);
2, Players should already inherit the gamemode of the world they are moved to, unless they issued the /gm command, which pins their gamemode forever. So for regular players there's no need to set their gamemode. There's even a special value for SetGameMode() that has this effect, gmNotSet.
3, If you want to set the gamemode, read the gamemode of the destination world; this way it'll work for any world names and any number of worlds:
Player:SetGameMode(cRoot:Get():GetWorld(WorldName):GetGameMode());
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
(06-27-2013, 05:40 AM)xoft Wrote:
Player:SetGameMode(cRoot:Get():GetWorld(WorldName):GetGameMode());
Thank you but don't work.
LUA: Plugins/Core/gotoworld.lua:21: attemp to index a nil value
LUA error in cPlugin_NewLua::HandleCommand. Stack size: 3
my gotoworld.lua.
function HandleGotoWorldCommand( Split, Player )
if( #Split ~= 2 ) then
Player:SendMessage( cChatColor.Green .. "Usage: /gotoworld [WorldName]" )
return true
end
if( Player:MoveToWorld(Split[2]) == false ) then
Player:SendMessage( cChatColor.Green .. "Could not move to world '" .. Split[2] .. "'!" )
return true
end
Player:SendMessage( cChatColor.Green .. "Moved successfully to '" .. Split[2] .. "'!" )
World = Player:GetWorld()
X[Player:GetName()] = Player:GetPosX()
Y[Player:GetName()] = Player:GetPosY()
Z[Player:GetName()] = Player:GetPosZ()
Player:TeleportTo( World:GetSpawnX(), World:GetSpawnY(), World:GetSpawnZ() )
Player:SetGameMode(cRoot:Get():GetWorld(WorldName):GetGameMode());
return true
end
Posts: 4,628
Threads: 115
Joined: Dec 2011
Thanks: 693
Given 494 thank(s) in 423 post(s)
change the world = player:getworld() to
WorldName = Player:GetWorld():GetName()
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
(06-27-2013, 06:02 PM)STR_Warrior Wrote: change the world = player:getworld() to
WorldName = Player:GetWorld():GetName() Don't work, If i do it I get an error and player is not teleported to the spawn on changing world.
I tried with this but player gamemode doesn't change.
function HandleGotoWorldCommand( Split, Player )
if( #Split ~= 2 ) then
Player:SendMessage( cChatColor.Green .. "Usage: /gotoworld [WorldName]" )
return true
end
if( Player:MoveToWorld(Split[2]) == false ) then
Player:SendMessage( cChatColor.Green .. "Could not move to world '" .. Split[2] .. "'!" )
return true
end
Player:SendMessage( cChatColor.Green .. "Moved successfully to '" .. Split[2] .. "'!" )
WorldName = Player:GetWorld():GetName()
World = Player:GetWorld()
X[Player:GetName()] = Player:GetPosX()
Y[Player:GetName()] = Player:GetPosY()
Z[Player:GetName()] = Player:GetPosZ()
Player:TeleportTo( World:GetSpawnX(), World:GetSpawnY(), World:GetSpawnZ() )
Player:SetGameMode(cRoot:Get():GetWorld(WorldName):GetGameMode());
return true
end
Posts: 6,485
Threads: 176
Joined: Jan 2012
Thanks: 131
Given 1075 thank(s) in 852 post(s)
06-27-2013, 06:24 PM
(This post was last modified: 06-27-2013, 06:28 PM by xoft.)
What is the error message, and on what line of the code snippet?
Line 7: Don't compare boolean values, use them directly:
if (not(Player:MoveToWorld(Split[2]))) then
Line 15: The message seems badly escaped at the HTML quotes:
Player:SendMessage( cChatColor.Green .. "Moved successfully to '" .. Split[2] .. "'! <img src=\"images/smilies/biggrin.gif\" style=\"vertical-align: middle;\" border=\"0\" alt=\"Big Grin\" title=\"Big Grin\">" )
On a side note, it seems that the cPlayer::MoveToWorld() function could benefit from additional parameters:
Player:MoveToWorld(WorldName, PosX, PosY, PosZ);
Oh, it seems the forum's Lua syntax highlighter does some escaping on its own. Anyway, get the Decoda Lua IDE and edit the Lua files in it, it will help you by syntax-highlighting and you can even debug the code there. Here's a guide:
https://forum.cuberite.org/showthread.php?tid=1141
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
I did it , now gamemode changes.
The correct code is the latest I posted. What I did wrong was that all worlds had gamemode set to 1 (the default) so gamemode don't changed.
Now I need to clear inventory when a player changes from a survival world to a creative world
Posts: 6,485
Threads: 176
Joined: Jan 2012
Thanks: 131
Given 1075 thank(s) in 852 post(s)
That will be difficult, if not impossible, if you wanted to do it properly and completely. There's no way to save the entire inventory in one API call, and if I remember correctly, there's still no way of specifying enchantments in item strings understood by StringToItem() / ItemToString(), so you'll need to handle enchantments by hand.
Posts: 1,162
Threads: 68
Joined: Mar 2013
Thanks: 245
Given 125 thank(s) in 100 post(s)
(06-27-2013, 07:29 PM)xoft Wrote: That will be difficult, if not impossible, if you wanted to do it properly and completely. There's no way to save the entire inventory in one API call, and if I remember correctly, there's still no way of specifying enchantments in item strings understood by StringToItem() / ItemToString(), so you'll need to handle enchantments by hand. I will basically clear inventory when changing from a creative world to a survival.
|