Using Strings in a sendMessage method?

Discussion in 'Plugin Development' started by Encrylize, Sep 16, 2013.

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

    Encrylize

    Lately, I've been experimenting with a very simple messaging plugin, however, after testing with it and attempting to get it to work, I've come to the realization that you are in fact not able to use Strings as an argument in a sendMessage method. It won't return an error, it'll simply return nothing. Meaning that this won't work:

    Code:
    String test = "This is a test.";
    player.sendMessage(test);
    This is quite a problem for me, since I somehow need to store all the arguments in some sort of variable, but at the same time be able to actually use it as valid input in a sendMessage method.

    I'm fairly new to the Bukkit API (and programming in general), so I would really like some help and suggestions for what to use and do in this situation.

    Thanks.
     
  2. Offline

    Tarestudio

    Encrylize
    Are you sure that this code is really used and the player is you? Because using String-variable with sendMessage() works.
     
  3. Offline

    Chinwe

    It most definitely does take a String, check that 'player' is actually a Player as Tarestudio said :)
     
  4. Offline

    tommycake50

    doesn't take a string :rolleyes:,
    String is the only argument it takes:D.
     
  5. Offline

    Encrylize

    Well, then it must be a fault on my part. Could somebody explain to me why this code doesn't work? It doesn't report an error, it simply reports nothing when you give it a valid amount of arguments.

    Code:java
    1. package me.Encrylize.tutorial;
    2.  
    3. import java.util.logging.Logger;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class msg_test extends JavaPlugin{
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13. public static tutorial_1 plugin;
    14.  
    15. @Override
    16. public void onDisable(){
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " has been disabled!");
    19. }
    20. @Override
    21. public void onEnable(){
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled!");
    24. }
    25. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    26. Player player = (Player) sender;
    27. if(commandLabel.equalsIgnoreCase("msg")){
    28. if(args.length == 0 || args.length == 1){
    29. player.sendMessage(ChatColor.RED + "Too few arguments! Usage: /msg [name] [message]");
    30. }
    31. else if(args.length >= 2){
    32. Player targetPlayer = getServer().getPlayer(args[0]);
    33. String msg = "";
    34. for(int i = 1 ; i <= args.length ; i++){
    35. msg += args[I];[/I]
    36. [I] msg += " ";[/I]
    37. [I] }[/I]
    38. [I] targetPlayer.sendMessage(ChatColor.YELLOW + "[" + player.getDisplayName() + " > Me] " + msg);[/I]
    39. [I] player.sendMessage(ChatColor.YELLOW + "[Me > " + targetPlayer.getDisplayName() + "] " + msg);[/I]
    40. [I] }[/I]
    41. [I] }[/I]
    42. [I] return false;[/I]
    43. [I] }[/I]
    44. [I]}[/I]
     
  6. Offline

    EdenCampo

    Encrylize

    It should've work.

    Try makin' a test like this:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args)
    2. {
    3. if(sender instanceof Player)
    4. {
    5. Player player = (Player) sender;
    6.  
    7. // Our command: /testcmd <player> <message-to-send>
    8.  
    9. if(cmd.getName().equalsIgnoreCase("testcmd"))
    10. {
    11. if(args.length < 3)
    12. {
    13. player.sendMessage(ChatColor.RED + "ARGS ERROR: Usage: /testcmd <player> <message-to-send>")
    14.  
    15. return true;
    16. }
    17.  
    18. else
    19. {
    20. String playername = args[0];
    21. String msg = args[1];
    22.  
    23. Player target = Bukkit.getPlayerExact(playername);
    24.  
    25. if(target != null)
    26. {
    27. target.sendMessage(msg);
    28. }
    29. }
    30.  
    31. return true;
    32. }
    33. }
    34.  
    35. return false;
    36. }
     
  7. Offline

    Encrylize

    Doesn't seem to work sadly.

    I've been playing with it for a bit now and it would appear that it didn't work, because "/msg" is already a Bukkit command and I didn't make it override the existing command. By simply changing the name, I eliminated the problem of it not returning anything, however, upon executing the command, this error now shows up in console:
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'messagetest' in plugin msg_test v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:191)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:962)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:880)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
        at me.Encrylize.tutorial.msg_test.onCommand(msg_test.java:35)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    Any idea what could cause this? Code is still the same as earlier, except for a name change of the command.
     
  8. Offline

    EdenCampo

    Encrylize

    You have too much arguments or somewhat?

    Code:java


    Can you post your new code? Because msg_test.java[line]35 at my code is return false;
     
  9. Offline

    Encrylize

    This is my new code:
    Code:java
    1. package me.Encrylize.tutorial;
    2.  
    3. import java.util.logging.Logger;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class msg_test extends JavaPlugin{
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13. public static tutorial_1 plugin;
    14.  
    15. @Override
    16. public void onDisable(){
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " has been disabled!");
    19. }
    20. @Override
    21. public void onEnable(){
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled!");
    24. }
    25. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    26. Player player = (Player) sender;
    27. if(commandLabel.equalsIgnoreCase("msgtest")){
    28. if(args.length == 0 || args.length == 1){
    29. player.sendMessage(ChatColor.RED + "Too few arguments! Usage: /msgtest [name] [message]");
    30. }
    31. else if(args.length >= 2){
    32. Player targetPlayer = getServer().getPlayer(args[0]);
    33. String msg = "";
    34. for(int i = 1 ; i <= args.length ; i++){
    35. msg += args[i];
    36. msg += " ";
    37. }
    38. targetPlayer.sendMessage(ChatColor.YELLOW + "[" + player.getDisplayName() + " > Me] " + msg);
    39. player.sendMessage(ChatColor.YELLOW + "[Me > " + targetPlayer.getDisplayName() + "] " + msg);
    40. }
    41. }
    42. return false;
    43. }
    44. }[/i]
     
  10. Offline

    EdenCampo

    Encrylize
    Change in the loop args.length to args.length-1
     
  11. Offline

    Encrylize

    Thanks a lot! Works like a charm now.
     
Thread Status:
Not open for further replies.

Share This Page