Solved Small help with dice plugin

Discussion in 'Plugin Development' started by ravand, Oct 24, 2012.

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

    ravand

    Hey guys i started working on a personal dice plugin for our server.
    The plugin is pretty simple it rolls a number from 1-16 (predefined) and if the number 1 is rolled something happens. The plugin itself works just fine and i have been using the max number of 16 now for quite a long time.

    But now i wanted to add a command which increases or decreases the max number you can roll.
    Initialy the plugin has no errors and works fine even with the new command which should change the rollable numbers on the dice.

    However even though the number variable increases (confirmed by a broadcast) the actual number which was initialized at the beginning stays the same for the roll command (number 16)

    I have 2 commands:
    /dice NAME
    /dicenumber NUMBER

    Here is my code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            boolean success= false;
            String dicestring="16"; //standard dice number 16
            int diceint = Integer.parseInt(dicestring); //parsed int number from "String dicestring"
     
     
     
            if(cmd.getName().equalsIgnoreCase("dicenumber")) {
                Player player = null;
                if (sender instanceof Player) {
                    player = (Player) sender;
                    if (!player.hasPermission("dice.dicenumber")) {
                        player.sendMessage(ChatColor.DARK_RED + "You do not have permission to change the dice number!");
                        return true;
                    }
                }
       
              if (args[0]!=null)
                  dicestring=args[0];
              diceint = Integer.parseInt(dicestring);
                    System.out.println("Max Dicenumber: "+diceint);
                  getServer().broadcastMessage(ChatColor.GREEN+"The dice has been increased to: "+ChatColor.RED+diceint+ChatColor.GREEN+" Numbers");
                  return true;
       
     
            }
     
     
            if(cmd.getName().equalsIgnoreCase("dice")) {
                Player player = null;
       
                if (sender instanceof Player) {
                    player = (Player) sender;
                    if (!player.hasPermission("dice.dice")) {
                        player.sendMessage(ChatColor.DARK_RED + "You do not have permission to roll the dice!");
                        return true;
                    }
                }
                    if(args.length>0){
                                                    if (args[0]!=null) {
                                                    String playername = args[0];
                                                    int dicenumber = diceint;
                                                    int number = (int) (1 + (Math.random()*+dicenumber)); //the actual rolling
                                                    getServer().broadcastMessage(ChatColor.GREEN+playername+" Voted and has rolled a "+ChatColor.RED+"#"+number);
                                                                                                        if (number==1){
                                                                                                            System.out.println(ChatColor.GREEN+playername+" wins!");
                                                                                                            getServer().dispatchCommand(getServer().getConsoleSender(), "setrank "+playername+" SpecialVoter 2D");
                                                                                                            getServer().broadcastMessage(ChatColor.GREEN+playername+ChatColor.GOLD+" wins Creative for 2 days!");
                                                                                                            success= true;
                                                                                                        }
                                                                                                        else getServer().broadcastMessage(ChatColor.GREEN+playername+" had no luck :( You need the #1! Try next time!");
                                                                                                        success= true;
                                                    }
                                                    else  System.out.println("Please provide a name!");
                                                    success= true;
                    }
                    else  System.out.println("Please provide a name!");
                    success= true;
           
            }
            else success=false;
                    return success;
           
           
        }
    }
    Can anyone maybe explain why the number doesnt change for the
    "int number = (int) (1 + (Math.random()*+dicenumber));"
    and try helping me to come up with a solution?

    I would be very thankful

    Thanks in Advance
    ravand
     
  2. I suggest using
    Code:java
    1. int number = new Random().nextInt(dicenumber) + 1;


    nextInt(int) returns a value between 0 and int - 1
     
  3. Offline

    ravand

    I have tried that but it didnt change anything. Maybe you didnt quite understand my concern:
    The dice rolling itself works perfectly fine my only concern is that when i use /dicenumber 10 for example that the /dice NAME command still rolls numbers up to 16 (the original number)

    Is there maybe a way to make method read the int value again?
     
  4. Offline

    Tirelessly

    You need to define the dice number as a global variable, not in your onCommand method. Then, when someone changes it, do diceNumber=Integer.parseInt(args[0]) or whatever you're doing.
     
  5. Offline

    ravand

    Yes just putting the variables outside the onCommand method seems to work. :) thanks for your help man :)
     
  6. Offline

    Tirelessly

    No problem.
     
Thread Status:
Not open for further replies.

Share This Page