Solved Method for integer arguments...

Discussion in 'Plugin Help/Development/Requests' started by Scorpionvssub, Sep 7, 2015.

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

    Scorpionvssub

    Title of this is a bit weird but it comes from a method i was given to attempt and fix my issue but only kinda made it worse... I tried finding decent randomizers for money but couldnt really find em so thought instead of going through additional plugins that may be bigger then the method needs to be due to the onenable and such, i thought i'd do it myself but...no luck, tried google and checked vault for answers but couldnt find the right call for this:

    What i wanted to do was a randomizer for ints via arguments so:

    /<command> money <player> <min amount> <max amount>

    upon this call it would randomize a number btween the min and max and give that random in cash to the person using the command or otherwise the 1 given so <player> would change to scorpionvssub or otherwise claire if i put claire there which is simple the min and max randomizer aint...cant find anything about it. :/ i tried several things and i know how to make normal randomizers aswell as runnables but since this requires a different type of randomizer then i use normally...

    Trying another attempt to this i tried:

    public int min;
    public int max;

    Code:
                    Random rand = new Random();
    
    max = args[3]
    min = args[2]
    
                    int money = rand.nextInt((max - min) + 1) + min;
                    System.out.println(money);
                    return true;
    
                }
    but this only makes it wanna return in String never ints..

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 8, 2015
  2. Offline

    timtower Administrator Administrator Moderator

    @Scorpionvssub Integer.parseInt(string input)
    Make sure to put a try catch around it, it throws an exception if it isn't a number
     
  3. Offline

    Scorpionvssub

    @timtower Like this? :/

    Code:
                    try {
                        min = Integer.parseInt(args[2]);
                    }
                    catch (NumberFormatException e) {
                        //must be number? idk...
                       //and should it have a return?
                    }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Scorpionvssub That is indeed how, and I don't know what you want to do when it isn't a number, maybe send a message and then return?
     
  5. Offline

    Scorpionvssub

    @timtower yea if its like 4389f3f3 or something not complete numbers just say "hey that should be a number!" then cancel the event. but also can 1 try do 2 things like, can it try both min and max at the same time or does each need its own?

    so basicly try {
    min
    max
    }
    catch { } or does each need its own
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Scorpionvssub They can be together, unless you want to tell: <value> is not a number
     
  7. Offline

    Scorpionvssub

    @timtower Okay i got it to work somewhat but...i got something that confuses me now...

    Code:
                    int money = rand.nextInt((max - min) + 1) + min;
                    String name = args[1];
                    System.out.println(name);
                    System.out.println(money);
                    //noinspection deprecation
                    econ.depositPlayer(name, money);
    This works fine aslong as its under 1 billion :/ but i wanted it to allow basicly any number amount but i cant go over 999.999.999 :/ anything over it fires a numberformatexception error..
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Scorpionvssub Check the length of Integer.MAX_INT, which one is bigger?
     
  9. Offline

    Scorpionvssub

  10. Offline

    TheWolfBadger

    System.out.print(Integer.MAX_VALUE);

    Just a suggestion, Doubles are probably better suited for an economy plugin.

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
    DOUBLE POST: @timtower @Scorpionvssub
     
    Last edited: Sep 8, 2015
  11. Offline

    Scorpionvssub

    This aint an economy plugin persé its a crates plugin but since i couldnt find a plugin that has a command like /amount value1 value2 which randomizes btween the 2 while still being less or equal to 1kb since it shouldnt be really big... i thought i add that in.. it uses vault to add money to the persons bank account which is similar to how u give money via essentials..
     
  12. Offline

    TheWolfBadger

    1. int money = rand.nextInt((max - min) + 1) + min;
    2. String name = args[1];
    3. System.out.println(name);
    4. System.out.println(money);
    5. //noinspection deprecation
    6. econ.depositPlayer(name, money);
    You're using an economy plugin though right? The money should be a double, as it supports higher values and it is like money because it includes decimals. It's not an economy plugin, but it uses economy?..
    @Scorpionvssub
     
    Last edited: Sep 8, 2015
  13. Offline

    Scorpionvssub

    Code:
    08-09 14:55:36 ] [Server] [Informatie] No new version available
    [08-09 14:56:04 ] [Server] [Informatie] arguments 2 and 3 must contain numbers only!
    [08-09 14:56:04 ] [Server] [Informatie] Argument 1: 300000
    [08-09 14:56:04 ] [Server] [Informatie] Argument 2: 30000000000000000
    [08-09 14:56:04 ] [Server] [Informatie] 2147483647hellooooo
    System.out.println(Integer.MAX_VALUE + "hellooooo");
     
  14. Offline

    TheWolfBadger

    So it only supports below that value (2147483647). I would recommend, again, using Doubles.
     
  15. Offline

    timtower Administrator Administrator Moderator

    Why not long?
     
  16. Offline

    TheWolfBadger

    Either one, most economy plugins, such as vault, use Doubles though.
     
  17. Offline

    Scorpionvssub

    Code:
                if (args[0].equalsIgnoreCase("money")) {
                    if (!(sender.hasPermission("crate.create"))) {
                        sendConfigMessage(sender, "nopermission");
                        return true;
                    }
                    if (!(args.length >= 3)) {
                        sendConfigMessage(sender, "econerror");
                        return true;
                    }
    
                    Random rand = new Random();
    
                    try {
                        min = Integer.parseInt(args[2]);
                        max = Integer.parseInt(args[3]);
                    }
                    catch (NumberFormatException e) {
                        sender.sendMessage(ChatColor.DARK_AQUA + "arguments" + ChatColor.GOLD + " 2" + ChatColor.DARK_AQUA + " and" +
                        ChatColor.GOLD + " 3" + ChatColor.DARK_AQUA + " must contain numbers only!");
                        sender.sendMessage(ChatColor.DARK_AQUA + "Argument 1:" + ChatColor.GOLD + " " + args[2]);
                        sender.sendMessage(ChatColor.DARK_AQUA + "Argument 2:" + ChatColor.GOLD + " " + args[3]);
                        return true;
                    }
    
                    int money = rand.nextInt((max - min) + 1) + min;
                    String name = args[1];
                    System.out.println(name);
                    System.out.println(money);
                    //noinspection deprecation
                    econ.depositPlayer(name, money);
                    return true;
    
                }
     
  18. Offline

    TheWolfBadger

    Code:
                if (args[0].equalsIgnoreCase("money")) {
                    if (!(sender.hasPermission("crate.create"))) {
                        sendConfigMessage(sender, "nopermission");
                        return true;
                    }
                    if (!(args.length >= 3)) {
                        sendConfigMessage(sender, "econerror");
                        return true;
                    }
                    Random rand = new Random();
                    Double min = null;
                    Double max = null;
                    try {
                        min = Double.parseDouble(args[2]);
                        max = Double.parseDouble(args[3]);
                    }
                    catch (NumberFormatException e) {
                        sender.sendMessage(ChatColor.DARK_AQUA + "arguments" + ChatColor.GOLD + " 2" + ChatColor.DARK_AQUA + " and" +
                        ChatColor.GOLD + " 3" + ChatColor.DARK_AQUA + " must contain numbers only!");
                        sender.sendMessage(ChatColor.DARK_AQUA + "Argument 1:" + ChatColor.GOLD + " " + args[2]);
                        sender.sendMessage(ChatColor.DARK_AQUA + "Argument 2:" + ChatColor.GOLD + " " + args[3]);
                        return true;
                    }
                    if(max !=null && min !=null) {
                        double money  = rand.nextInt((max - min) + 1) + min;
                        String name = args[1];
                        System.out.println(name);
                        System.out.println(money);
                        //noinspection deprecation
                        econ.depositPlayer(name, money);
                        return true;
                    }
                }
     
  19. Offline

    Scorpionvssub

    idk if its cause of this "new" version u just showed or why otherwise but... if i do crate money name 3 instead of crate money name 3 6 it gives an internal error while args.length is checked to if it doesnt contain atleast 3 arguments. :/

    Edit: fixed that.

    It works :) and for some funny reason allowed a letter at the very end and still works so im kinda glad :D
     
    TheWolfBadger likes this.
  20. Offline

    TheWolfBadger

    You're welcome
     
  21. Offline

    xXLightbulbXx

    This thread helped me a lot :D
     
  22. Offline

    Scorpionvssub

    was it really needed to bump up an old post for that? X) not that i mind personally
     
Thread Status:
Not open for further replies.

Share This Page