Cuberite Forum
What's wrong with my code? - 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: What's wrong with my code? (/thread-867.html)



What's wrong with my code? - tonibm19 - 04-28-2013

What's wrong with this code?
Code:
function Initialize(Plugin)
       PLUGIN = Plugin

    Plugin:SetName( "MoreCommands" )
    Plugin:SetVersion( 1 )

       PluginManager = cRoot:Get():GetPluginManager()

       PluginManager:BindCommand("/kill",    "morecommands.kill",    HandleKillCommand,    " - Kill some player");
       LOG("Initialized " .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion())
    return true
end

function HandleKillCommand( Split, Player )
       local OtherPlayer = OtherPlayer:GetName()

       if Split[2] == nil then
           Player:SendMessage( cChatColor.Green .. "Usage: /kill [Player]" )
       end
       if OtherPlayer == Split[2] then
        OtherPlayer:TakeDamage( 100 , Player )    
           return true
    end
end



RE: What's wrong with my code? - NiLSPACE - 04-28-2013

i think you have to change local OtherPlayer = OtherPlayer:GetName() to
local OtherPlayer = Split[2] since OtherPlayer is a nil value but i'm not sure.

Maybe cRoot:Get():FindAndDoWithPlayer(Split[2], functionhere) works. like this:
local dowithplayer = function(OtherPlayer)
   OtherPlayer:TakeDamage(100, OtherPlayer)
end
if cRoot:Get():FindAndDoWithPlayer(Split[2], dowithplayer) == false then
    Player:SendMessage( "Player not found" )
else
    Player:SendMessage( "Player " .. Split[2] .. " is killed")
end
EDIT:
Not sure if it works though.


RE: What's wrong with my code? - xoft - 04-28-2013

local OtherPlayer = OtherPlayer:GetName();
This doesn't make much sense. You're creating a new variable named OtherPlayer, initialized to nil, then you call GetName() on it (which fails) and finally want to assign the name to the variable. It's very similar to this (erroneous) piece of code, that I don't think you'd ever write:
local MyNumber = MyNumber + 1;

STR_Warrior's solution should work fine, unless there are two players with similar names, such as "JohnSmith" and "JohnJones"; then trying to kill "John" could kill either. FindAndDoWithPlayer() selects the player "from a partial or complete player name" (documentation in cRoot.h). To fix that, I'd compare the OtherPlayer's name with Spli[2] again in the callback function:
local HasKilled = false;
local KillPlayer = function(OtherPlayer)
   if (OtherPlayer:GetName() == Split[2]) then
      OtherPlayer:TakeDamage(100, OtherPlayer);
      HasKilled = true;
   end
end

cRoot:Get():FindAndDoWithPlayer(Split[2], KillPlayer);
if (HasKilled) then
    Player:SendMessage( "Player " .. Split[2] .. " is killed");
else
    Player:SendMessage( "Player not found" );
end



RE: What's wrong with my code? - tonibm19 - 04-29-2013

I tried all your codes and it don't works. When I try to kill some player it gives me this error:
LUA: Plugins/MoreCommands/main.lua:16: error in function 'TakeDamage'.
argument #2 is 'number'; 'cPawn' expected.

Maybe is an error in the TakeDamage function


RE: What's wrong with my code? - NiLSPACE - 04-29-2013

then you should propably just OtherPlayer:TakeDamage(OtherPlayer) or something.


RE: What's wrong with my code? - tonibm19 - 04-29-2013

(04-29-2013, 04:20 AM)STR_Warrior Wrote: then you should propably just OtherPlayer:TakeDamage(OtherPlayer) or something.
Dont work

I also tried with cPawn_DoTakeDamage(100, OtherPlayer)
And gives me this error:
LUA: Plugins/MoreCommands/main.lua:16: attempt to call method 'cPawn__DoTakeDamage' (a nil value)


RE: What's wrong with my code? - xoft - 04-29-2013

There are three TakeDamage functions in cPawn, none of them matches what you're calling:
/// Makes this pawn take damage from an attack by a_Attacker. Damage values are calculated automatically and DoTakeDamage() called
void TakeDamage(cPawn & a_Attacker);
	
/// Makes this pawn take the specified damage. The final damage is calculated using current armor, then DoTakeDamage() called
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, double a_KnockbackAmount);

/// Makes this pawn take the specified damage. The values are packed into a TDI, knockback calculated, then sent through DoTakeDamage()
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, int a_FinalDamage, double a_KnockbackAmount);
The first one calculates damage based on what the attacker is wielding - not what you want here.
The second one subtracts armor value from RawDamage and deals that to the pawn - could be used, but the third one is better.
The third one uses everything that you specify and just deals the damage.

Note: Don't set a_Attacker to the player issuing the command, otherwise if we ever get the plugin that checks player distance before allowing damage, it would most likely forbid the kill. Use nil instead:
OtherPlayer:TakeDamage(dtInVoid, nil, 1000, 1000, 0);

In rev 1429 and later, you should also use dtAdmin or dtPlugin instead of dtInVoid.


RE: What's wrong with my code? - tonibm19 - 04-29-2013

(04-29-2013, 04:38 AM)xoft Wrote: There are three TakeDamage functions in cPawn, none of them matches what you're calling:
/// Makes this pawn take damage from an attack by a_Attacker. Damage values are calculated automatically and DoTakeDamage() called
void TakeDamage(cPawn & a_Attacker);
	
/// Makes this pawn take the specified damage. The final damage is calculated using current armor, then DoTakeDamage() called
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, double a_KnockbackAmount);

/// Makes this pawn take the specified damage. The values are packed into a TDI, knockback calculated, then sent through DoTakeDamage()
void TakeDamage(eDamageType a_DamageType, cPawn * a_Attacker, int a_RawDamage, int a_FinalDamage, double a_KnockbackAmount);
The first one calculates damage based on what the attacker is wielding - not what you want here.
The second one subtracts armor value from RawDamage and deals that to the pawn - could be used, but the third one is better.
The third one uses everything that you specify and just deals the damage.

Note: Don't set a_Attacker to the player issuing the command, otherwise if we ever get the plugin that checks player distance before allowing damage, it would most likely forbid the kill. Use nil instead:
OtherPlayer:TakeDamage(dtInVoid, nil, 1000, 1000, 0);

In rev 1429 and later, you should also use dtAdmin or dtPlugin instead of dtInVoid.
Thx it worked