Sending a player a message?

Discussion in 'Plugin Development' started by Chipstik, Mar 13, 2011.

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

    Chipstik

    I can get this to work when I have the Player object 'player' from something such as CommandSender, but how would I send the player a message in this method?

    Code:
        private void sendPlayerMessage(String message) {
    
            Player player = PlayerEvent.getPlayer(); //THIS BIT DOESN'T WORK, how can I make it so that it does?
            player.sendMessage(ChatColor.BLUE + message);
    
        }
     
  2. Offline

    MadMonkeyCo

    The problem is that without a defined event the getPlayer method will return null.
    You need to use an event or some other way of getting the player, not using an undefined variable.
    PHP:
    public void sendPlayerMessage(Player recieverString message) {

            
    Player player reciever;
            
    player.sendMessage(ChatColor.BLUE message);

        }
    Now you can use the method in your listeners and your main class.
     
  3. Offline

    Chipstik

    Thanks, but how do I get that to work? I still cant get the player, so how will I use it as the arguement? Here's the code for this section so far.

    Code:
        public void blockCounterAdd()
        {
            blocksPlaced++;
            if(blocksPlaced == 10)
            {
                sendPlayerMessage("You have placed more than 10 blocks this session!");
            }
        }
    
        public void sendPlayerMessage(Player reciever, String message) {
    
            Player player = Reciever;
            player.sendMessage(ChatColor.BLUE + message);
    
        }
    }
    
    
     
  4. Offline

    Sammy

    Code:
    sendPlayerMessage("You have placed more than 10 blocks this session!");
    
    If you want to do that check you must already have a blockListener right ?

    So you must do something like this:

    Code:
    public class MoneyBukkitBlockListener extends BlockListener {
    
        @Override
        public void onBlockPlace(BlockPlaceEvent event) {
           Player player = event.getPlayer();
           sendPlayerMessage(player,"You have placed more than 10 blocks this session!");
        }
    
    }
    but you don't really need to make that method you can just do:

    Code:
    player.sendMessage("You have placed more than 10 blocks this session!");
     
  5. Offline

    Chipstik

    So when i'm calling

    Code:
    sendPlayerMessage("You have placed more than 10 blocks this session!");
    
    What do I use as the first argument? For the Player reciever?
     
  6. Offline

    Sammy

    Sorry I had to edit my post. take a look at it again ;)
     
  7. Offline

    Chipstik

    Ah, get it. Realised that I needed to place the code in the onBlockPlace method.

    Thanks.
     
Thread Status:
Not open for further replies.

Share This Page