Sending a message to a user when online not sending

Discussion in 'Plugin Development' started by Snwspeckle, Oct 23, 2011.

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


    Ok so I am working on a plugin that defines a username for the player and if the user is online it sends a message. The issue is that the plugin won't even send me a message in game or in the console.

    My code:

    package lotc.anthony.linkaccount;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import java.util.logging.Logger;
    public class Link extends JavaPlugin {
        String playerName = "adonzo";
        String pinCode = "Code";
        Logger log = Logger.getLogger("Minecraft");
        public void onEnable() {
            Player player = this.getServer().getPlayer(playerName);
  "Link Account Plugin 0.1 has been enabled");
            if(this.getServer().getPlayer(playerName) != null){
      "Player is Online");
                player.sendMessage(ChatColor.BLUE + pinCode);
            } else {
                player.sendMessage(ChatColor.BLUE + "There was an error");
      "Player is offline");
        public void onDisable() {
     "Link Account Plugin 0.1 has been disabled");
  2. Offline

    Sagacious_Zed Bukkit Docs

    This code happens when your plugin's onEnable method is called when your plugin is enabled. Thus, the server will most likely be empty, and getPlayer will return null
  3. It doesn't log anything? Does it log everything on startup?
  4. Offline


    As log I meant, to write to the console. For the onEnabled() function. How do I set it to be "online". I think that's the issue but also this is being written in the console: Link Account Plugin 0.1 has been enabled. So my question is, why is anything in the if statement not working?
  5. Offline


    The code won't work like that. You check for a specific player, then if he's online you send him a message, but if he's offline you still try to send him a message!

    That last part will cause an exception every time.
  6. Offline


    I am sending a message for the offline just to see if its working at all, I am using it as a debug. Nothing is being displayed is why I am 100% confused.

    Also, I am using a variable for the players name because once I have this working, on a PHP page, you enter a username that is sent to the server and is used in the variable playerName.

    I just noticed that its not working because as soon as the server loads it runs and it will always return false so should I just make a command to test for this time?
  7. Offline


    It won't display anything because this is sending the message when your starting the server/loading plugins. Unless you plan on adding the plugin while server is running and doing a reload, its not going to show you anything. You want to register an event and send the command when the event is triggered.
  8. Offline


    That wasn't the point. It doesn't matter whether the username is a real player or from a PHP page, or hard-coded. Sending a message to a player you know is offline will always create an exception.
  9. Offline


    This has been explained and fixed already, but thanks for the responses.
  10. Offline


    I also have a question about this. Say someone on a server sends a message to someone using a command is there anyway of saving this message so that when they log back in they get a message saying "You have 1 piece of mail" or something. In otherwords saving this message to a file?
  11. Save the message to a file if it needs to go across server restarts, or store it in a hashmap with recepients name and message, and then check onJoin if the hashmap contains the joiners name, and if so send the messages.
    thehutch likes this.
  12. Offline

    Father Of Time

    Essentials has this built in fyi, its the /mail command:

  13. Offline


    idc about essentials having it im working on a project with some people and we are expanding this feature :) im just having trouble reading/writing a message to a txt file as a list under a players name. So when they log back in they can view the messages sent
  14. Offline

    Mr. X

    Paste your code into Player Join Event.
  15. Offline


Thread Status:
Not open for further replies.

Share This Page