Command - Get Int from Arg - not working?

Discussion in 'Plugin Development' started by ImaTimelord7, May 13, 2015.

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

    ImaTimelord7

    Code:
    if (commandLabel.equalsIgnoreCase("m")) {
                if (p.hasPermission("m.m") || p.isOp() == true) {
                    if(args.length > 0) {
                       
                        if (args[0].equalsIgnoreCase("help")) {
                            // typed /m help
                            p.sendMessage(r + "Use /m <setmessage:setdelay:setresponse:setitem:setamount> <string:int>");
                              
                        }else if (args[0].equalsIgnoreCase("setdelay")) {
                            // typed /m setdelay
                            if (args.length == 2) {
                                // typed /m setdelay ?
                                int delay = ((MemorySection) getServer()).getInt(args[1]);
                                p.sendMessage(da + "Set delay to " + delay);
                            }else{
                                p.sendMessage(r + "Use /m <setdelay> <delay>");
                            }
    Looking at the 2nd command, /m setdelay ?
    This code doesn't work, doing commands /m setdelay and /m setdelay anyletter anyletter and /m setdelay anynumber anynumber all return "Use /m <setdelay> <delay>" as it should, but doing command /m setdelay 4 won't return "Set delay to " + delay

    Any help is awesome, thanks guys.
     
  2. Offline

    LeokoTime

    Try to use:

    Code:
    Integer.parseInt(args[0]);
     
  3. Nope.
    Code:
    Integer.parseInt(args[1]);
    is the rigth way. Put this code in a try-catch to test if the input is really an int

    I don't know where you got that code but it's completely wrong
     
  4. Offline

    meguy26

    @ImaTimelord7
    What exactly is this for:
     
  5. Offline

    KingOfAdventure

    Try to broadcast args[1] and look if it's a number.

    Use this method : Plugin#getServer()#broadcastMessage(args[1])

    If you are in your plugin class this :
    Code:
    getServer().broadcastMessage(args[1]);
    If this is a command listener or something, use your plugin class
    Code:
    yourPluginClass.getServer().broadcastMessage(args[1]);
     
  6. Have you ever heard of
    Code:
    Bukkit.getServer().broadcastMessage
    or simply
    Code:
    Bukkit.broadcastMessage
    ?
    All methods in the Bukkit class refer to the server methods.
    Example method of the Bukkit class (decompiled):
    Code:
    public static int broadcastMessage(String message)
      {
        return server.broadcastMessage(message);
      }
     
    Last edited: May 14, 2015
  7. Offline

    KingOfAdventure

    Lol, I didn't know you can access it in a static way, I always use the class :p. Well, thanks I guess
     
  8. Offline

    RingOfStorms

    Did you also know that a lot of people like to use the OOP way rather than the static way that was thrown in for convenience and actually isn't all that acceptable?
     
  9. Offline

    BagduFagdu

    @FisheyLP

    Is it correct to use "int i =Integer.valueOf(args[0]);"? Happened to have similar question to String.valueOf(int).
     
  10. String.valueOf(int) casts it to a String.
    If you want the opposit result (String to int), use Integer.valueOf(string)
     
Thread Status:
Not open for further replies.

Share This Page