Solved Custom say commands.

Discussion in 'Plugin Development' started by TheNoobiestCrafters, Oct 20, 2012.

Thread Status:
Not open for further replies.
  1. Hello all, I would like to make a custom say/broadcast command. I have got it to work but I can not make it broadcast all the words I put in for arguments. For example, I can do

    /modspeak hi
    but not
    /modspeak hi hi

    Here's my code:

    Code:
            if(cmd.getName().equalsIgnoreCase("modspeak")) {
                if(sender.hasPermission("musthaves.modspeak")) {
                    if (args.length < 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /modspeak <msg>");
                        return true;
                    } else if (args.length > 1) {
                        getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Moderators" + ChatColor.GRAY + "] " + ChatColor.GREEN +  args[0]);
                        return true;
                    }
                }
     
  2. Offline

    Tirelessly

    Because all you're telling it to do is broadcast args[0]. Use a stringbuilder.
     
  3. How do I use a stringbuilder?
     
  4. Offline

    kroltan

    krazytraynz likes this.
  5. Thank you.

    EDIT: Once I clicked that link, I lulled pretty hard.
     
  6. Offline

    gomeow

  7. Offline

    lenis0012

    Code:
    String message = "";
    for(int i = 0; i <= args.length; i++)
    {
        message = message + args[i];
    }
     
    getServer().broadcastMessage(ChatColor.GREY + "[Mod] " + ChatColor.WHITE + message);
    
     
  8. Doesn't work.

    Here's my error log:

    15:39:01 [INFO] NoobiestCrafter issued server command: /modspeak test
    15:39:01 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'modspeak' in plugin MustHaves v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
    at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
    at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.noobiestcrafter.plugin.MustHaves.MustHaves.onCommand(MustHaves.java:91)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 17 more
     
  9. Offline

    chaseoes

    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
     
  10. try using a stringbuilder, instead of concating string, concating strings will eat alot of ram doing the use of java's string pool.
     
  11. And how would I do that? I've tried googling it but to no avail. I know, I'm an idiot. But could you please show me?
     
  12. Code:java
    1.  
    2. String message;
    3. StringBuilder builder = new StringBuilder();
    4. for(int i = 0; i < args.length; i++)
    5. {
    6. builder.append(args[i]).append(" ");
    7. }
    8. builder.setLength(builder.length - 1);
    9. message = builder.toString();[/i]

    a bit more code, but a lot less ram usage

    EDIT: stupid italics chancing my text, I dont see anni [.i] inside the source code of the message
     
  13. That still doesn't work. Somethings wrong with the code. Eclipse was saying was pointing out errors and when I fixed them and tested it I still got an error.
     
  14. what was the error stthis time?
     
  15. Idk, I tried decompiling the Essentials broadcast command with jad and it didn't work.

    Here's the log:
    Code:
    16:58:10 [INFO] NoobiestCrafter issued server command: /modspeak
    16:58:10 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'modspeak' in plugin MustHaves v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
        at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
        at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.noobiestcrafter.plugin.MustHaves.MustHaves.onCommand(MustHaves.java:92)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 17 more
    16:58:11 [WARNING] Can't keep up! Did the system time change, or is the server overloaded?
    16:59:35 [INFO] NoobiestCrafter issued server command: /modspeak
    16:59:35 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'modspeak' in plugin MustHaves v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
        at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
        at net.minecraftserverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:124)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.noobiestcrafter.plugin.MustHaves.MustHaves.onCommand(MustHaves.java:92)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 17 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  16. you should add an check to see if there isn't anny arguments given on your commands and print the usage instead (return false then)
     


  17. EDIT: I did not fix it. If I do two words, such as:

    /modspeak test test

    it prints out

    [Moderators] test
    [Moderators] test test

    Here's my code.

    Code:
    StringBuilder s = new StringBuilder();
                    for (int x = 0; x < args.length; x++) {
                  s.append(args[x] + " ");
                  getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Moderators" + ChatColor.GRAY + "] " + ChatColor.GREEN + s);
                    }
    UPDATE:
    I fixed it, here's my code if anyone is curious:
    Code:
    if(cmd.getName().equalsIgnoreCase("modspeak")) {
    if(sender.hasPermission("musthaves.modspeak")) {
    if (args.length < 1) {
    sender.sendMessage(ChatColor.RED + "Usage: /modspeak <msg>");
    } else if (args.length >= 1) {
    StringBuilder str = new StringBuilder(args[0]);
    for(int i = 1; i < args.length; i++) {
      str.append(' ').append(args[i]);
    }
                  getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Moderators" + ChatColor.GRAY + "] " + ChatColor.GREEN + str.toString());
                  return true;
    }
    return true;
    }
    }
    If anyone could tell me how to mark the thread as "Solved" that would be great.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  18. Offline

    kroltan

    Go to the first post and click edit. In that page, add [Solved] to the start of the thread's title.
     
Thread Status:
Not open for further replies.

Share This Page