Getting an Internal Error when trying to Message an Offline player

Discussion in 'Plugin Development' started by Munnzeh, Dec 4, 2013.

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

    Munnzeh

    I fixed my previous bug with the code, now i am stuck with an internal error when trying to send a message to an offline User.
    However i want the code to allow me to send a message to an offline player, instead of sending a message to the sender.

    package me.Munnzeh.Serveressentials;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class MSGCommandExecutor implements CommandExecutor {

    @SuppressWarnings("unused")
    private Serveressentials plugin;

    public MSGCommandExecutor(Serveressentials plugin) {
    this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player s = (Player)sender;
    //Player target = s.getServer().getPlayer(args[0]);
    if(cmd.getName().equalsIgnoreCase("Msg")){
    if(args.length > 1){

    StringBuilder message = new StringBuilder();

    for (int i = 1; i < args.length; i++) {
    if (i > 1) message.append(" ");
    message.append(args);
    }

    String result = ChatColor.GREEN + "[" + sender.getName() + " > " + s.getServer().getPlayer(args[0]).getName() + "] - " + message ;

    s.getServer().getPlayer(args[0]).sendMessage(result);
    Bukkit.broadcast(ChatColor.GREEN + "[Admin Debug] User " + sender.getName() + " sent a message to: " + s.getServer().getPlayer(args[0]).getName());
    }else{
    s.sendMessage(ChatColor.DARK_RED + "Command Error: /Msg <Player> <Message>");

    }

    return false;
    }


    {




    }

    {

    }
    {

    }
    return false;
    }
    }
    How could i fix it to allow me to send the message to the offline user, so if i did /msg Munnzeh 1 hi, it would still broadcast the message.
     
  2. Offline

    Likaos

    Just check if the player is online.
    Code:java
    1. Player target = Bukkit.getPlayer(args[0]);
    2. if (target != null) target.sendMessage("blabla");
    3. else s.sendMessage("this player is offline");
     
  3. Offline

    the_merciless

    If the player is offline how can it send them a message, what you need to do is save the message into a file, then when a player logs in check if that file contains a message for that player
     
  4. Offline

    RainoBoy97

    Why would you ever want to send an offline player a message?
     
  5. If you want to save messages when a player is offline, and send it to them when they log on, use this:

    Code:java
    1.  
    2. HashMap<String,String> messagesToSend = new HashMap<>();
    3.  
    4. public void sendMessageToWhenOnline(OfflinePlayer o, String message){
    5. if(o.getPlayer() != null){
    6. Player p = o.getPlayer();
    7. p.sendMessage(message);
    8. }
    9. messagesToSend.put(o.getName(), message);
    10. }
    11.  
    12. @EventHandler
    13. public void onPlayerJoin(PlayerJoinEvent e){
    14. Player p = e.getPlayer();
    15. if(messagesToSend.containsKey(p.getName())){
    16. p.sendMessage(messagesToSend.remove(p.getName()));
    17. }
    18. }


    (not tested)
     
Thread Status:
Not open for further replies.

Share This Page