Some quick help

Discussion in 'Plugin Development' started by johnny boy, Dec 27, 2016.

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

    johnny boy

    I need to get all the arguments above 0 (so if i did /motd set hello world it would get the arguments 1 and 2 (Hello and world). Would I use a for loop? (Or foreach loop). Thanks!
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    johnny boy

    so
    Code:
    for (int x = 0; x <= args; x++ { // do something } 
     
  4. Offline

    Zombie_Striker

    @MemeplexOwner
    x <= args.length, and yes. That should do it.

    Remember to use a StringBuilder
     
  5. Offline

    johnny boy

    A string builder can only hold 16 characters.
     
  6. Offline

    mcdorli

    That's not true, it can virtually hold an infinite amounts of characters. It's better than a String, because it doesn't get "saved" whenever you change it, thus it avoids frequent garbage collection (at this scale this doesn't matter that much, but it's a good practice)
     
  7. @MemeplexOwner
    No, not true. The javadoc states that the initial capacity of the StringBuilder is 16 characters, but also states that,

    "Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger."
     
  8. Offline

    johnny boy

    Oh, I didn't read the second bit. Thanks! I'll update you guys when I either get something wrong or I get it right! :p

    EDIT: How would I make a variable public so I can use my StringBuilder outside my for loop?
     
    Last edited by a moderator: Dec 27, 2016
  9. Offline

    johnny boy

    How is that going to help me though, because then I can't access the StringBuilder variable inside the loop.
     
  10. @MemeplexOwner
    Yes you can. If you declare a variable in a scope, you will always be able to access the variable in nested scopes. Have a look at this:
    Code:java
    1. StringBuilder stringBuilder = new StringBuilder();
    2. for (int i; i <...) {
    3. stringBuilder.append(...);
    4. }
    5. stringBuilder.toString();
     
  11. Offline

    Zombie_Striker

    @MemeplexOwner
    Please post code. The StringBuilder should work outside of the for loop.
     
  12. Offline

    johnny boy

    Think I've got it. I'll update you soon. Thanks so far.

    EDIT: First how did you make it look like java code, second.. Unhanded exception.. Here is my code:
    Code:
    package me.memplexowner.motd;
    
    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 MotdChange implements CommandExecutor {
      
        MainClass config;
      
        public MotdChange(MainClass config) {
            this.config = config;
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Sorry! Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
    
            if (args.length == 0) {
                String motd = config.getConfig().getString("MOTD");
                String motd2 = ChatColor.translateAlternateColorCodes('&', motd);
                player.sendMessage(motd2);
                return true;
            } else {
                if (args.length >= 2) {
                    StringBuilder motd = new StringBuilder();
                if ((args[0].equalsIgnoreCase("set"))) {
                    for (int x = 1; x <= args.length; x++) {
                        String motdMessage = args[x];
                        motd.append(motdMessage);
                    }
                    config.getConfig().set("MOTD", motd);
                    String motdNoColour = config.getConfig().getString("MOTD");
                    String  motdColour= ChatColor.translateAlternateColorCodes('&', motdNoColour);
                    player.sendMessage("MOTD set to: " + motdColour);
                    return true;
                        } else {
                            player.sendMessage("You have not put the MOTD");
                            return false;
                        }
                    }
                }
            player.sendMessage(ChatColor.RED + "Error! No motd supplied! Do /motd set <Message> to set the motd!");
                return false;
        }
    }
    
    
          
    
    
      
      
    
    
    And error
    Code:
    [21:13:53] [Server thread/INFO]: _P250_ issued server command: /motd set a
    [21:13:53] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'motd' in plugin MOTD v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.jar:git-Bukkit-7019900]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.jar:git-Bukkit-7019900]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:609) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:517) [craftbukkit-1.8.jar:git-Bukkit-7019900]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at me.memplexowner.motd.MotdChange.onCommand(MotdChange.java:34) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.jar:git-Bukkit-7019900]
        ... 14 more
    
     
    Last edited by a moderator: Dec 27, 2016
  13. Offline

    mcdorli

    Code:
    for (int x = 1; x <= args.length; x++) {
    This should be

    Code:
    for (int x = 1; x < args.length; x++) {
    Because of the <=, it goes until x is less than or equal to the length of the args array, and because the last element's index is smaller, than the length of the array, it will throw an ArrayOutOfBoundsException

    BTW, to make it colored, put it between

    [ code=java ]

    and

    [/ code ]
     
    Last edited: Dec 27, 2016
  14. Offline

    johnny boy

    It works great!
    But ever since I've added /motd help when I do /motd it says another Unhanded Exception.
    Code:
    package me.memplexowner.motd;
    
    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 MotdChange implements CommandExecutor {
       
        MainClass config;
       
        public MotdChange(MainClass config) {
            this.config = config;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Sorry! Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (args[0].equalsIgnoreCase("help")) {
                player.sendMessage(ChatColor.GOLD + "MOTD Help");
                player.sendMessage(ChatColor.BLUE + "/motd help " + ChatColor.GRAY + "- Shows help for motd.");
                player.sendMessage(ChatColor.BLUE + "/motd" + ChatColor.GRAY + "- Shows the motd.");
                player.sendMessage(ChatColor.BLUE + "/motd set <Message>" + ChatColor.GRAY + "- Allows you to set a new MOTD.");
                return true;
            } 
            if (args.length == 0) {
                String motd = config.getConfig().getString("MOTD");
                String motd2 = ChatColor.translateAlternateColorCodes('&', motd);
                player.sendMessage(motd2);
                return true;
            } else {
                if (args.length >= 2) {
                    StringBuilder motd = new StringBuilder();
                if ((args[0].equalsIgnoreCase("set"))) {
                    for (int x = 1; x < args.length; x++) {
                        String motdMessage = args[x] + " ";
                        motd.append(motdMessage);
                    }
                    config.getConfig().set("MOTD", motd);
                    String motdNoColour = config.getConfig().getString("MOTD");
                    String  motdColour= ChatColor.translateAlternateColorCodes('&', motdNoColour);
                    player.sendMessage("MOTD set to: " + motdColour);
                    return true;
                        } else {
                            player.sendMessage("You have not put the MOTD");
                            return false;
                        }
                    }
                }
            player.sendMessage(ChatColor.RED + "Error! No motd supplied! Do /motd set <Message> to set the motd!");
                return false;
        } 
     }
    
    
    
           
    
    
       
       
    
    
     
  15. Online

    timtower Administrator Administrator Moderator

    @MemeplexOwner Because now you don't check the length before you check the argument itself.
     
  16. Offline

    johnny boy

    Fixed. I think I have made myself a nicely working plugin. Thank you so much for all your help guys. I can finally mark this thread as solved. :)
     
  17. Offline

    Zombie_Striker

    dart2112 likes this.
  18. Offline

    johnny boy

    How do I do that?
     
Thread Status:
Not open for further replies.

Share This Page