Player player = (Player) sender?

Discussion in 'Plugin Development' started by Bernabeast, Jun 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Bernabeast

    What does this mean?
    I've looked everywhere and Nothing:/

    Player player = (Player) sender

    ????
     
  2. Offline

    Jake0oo0

    Bernabeast Read a tutorial and stop making multiple threads.
     
  3. Offline

    Compressions

    Bernabeast So, you've created the method onCommand with four parameters(CommandSender sender, Command cmd, String label, String[] args). CommandSender is self-explanatory and defines the object that ran the command. With
    Code:
    Player player = (Player) sender;
    This casts sender to a Player object, basically stating that an in-game player has ran the command.
     
  4. Offline

    Bernabeast

    An example of an instance this may be used?
     
  5. Offline

    Compressions

    • Unnecessary aggression
    Bernabeast Read my post and stop being ignorant.
     
  6. Offline

    TomFromCollege

    When we talk about Java Objects, we are actually talking about something that stems from the Object class... for example, A player is extending the following classes:
    Code:java
    1. public interface Player extends HumanEntity, Conversable, CommandSender, OfflinePlayer, PluginMessageRecipient


    When we're talking about commands, we know that a CommandSender is the person calling the command, if we somehow KNOW that this very CommandSender is also a Player, we can cast CommandSender to Player by using this: ((Player)sender), this would then give us access to all of the methods inside the Player class.
    If however, the CommandSender is not a player... IE: A console, you'll get an error thrown as you cannot access a method that this CommandSender does not have.
     
  7. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    thus, you check if the sender is an instance of the Player class

    Code:
    if(sender instanceof Player) {
      Player player = (Player) sender;
      // stuff here that requires the player object
    }
     
Thread Status:
Not open for further replies.

Share This Page