Random Numbers

Discussion in 'Plugin Development' started by andreaskal, Jul 17, 2015.

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

    andreaskal

    Hi, im new at developing plugins so can you please say why this wouldn't work:
    Text (open)

    public boolean onCommand2(CommandSender sender, Command cmd, String label, String args[]){
    Player player = (Player)sender;
    if(label.equalsIgnoreCase("bar")) {
    Random object = new Random();
    int Andy_got_swag;

    for(int counter = 1; counter <=1;counter++){
    Andy_got_swag = 1+object.nextInt(2);

    if(Andy_got_swag == 1){
    player.sendMessage(ChatColor.RED + "HEADS!");
    }else if(Andy_got_swag == 2){
    player.sendMessage(ChatColor.GOLD + "TAILS!");
    }
    }
    }
    return false;
    }

    it should work because there were no errors and the console says nothing when i write /bar or when i reload.
    i got this from TheBCBroz:
    Thanks, :D
     
  2. @andreaskal The one major thing we advise people not to do here is not watch TheBCBroz. The videos are filled with many bad practices.

    Check before casting
    Use cmd.getName() not label
    You don't need the for loop.
    Also please, please follow naming conventions.
     
    mine-care likes this.
  3. Offline

    andreaskal

    ok ill stop watching TheBCBroz, but this will come up if i put cmd.getName() in instead of label
    Udklip.PNG
    and what did you mean with "You don't need the for loop. "
    Thanks, :D
     

    Attached Files:

  4. @andreaskal You can leave it as label in the method arguments. And I mean you can remove the "for(int counter ..." as it isn't needed (remember to remove the closing bracket as well)

    And make sure to not leave the getInstance returning null.
     
  5. Offline

    andreaskal

    like i said before im new at bukkit coding so this was what i got:
    Udklip.PNG
     
  6. @andreaskal Add a integer which gets it from the random (Follow naming conventions) and replace the "Andy_got_swag" with the integer.
     
  7. Offline

    MajorSkillage

    Just use
    float random = Math.random();
    if(random <= 0.5){
    //heads
    } else {
    //tails
    }
     
  8. Offline

    _Error

    I watched him in the 2012 days but I did not learn much
     
  9. Offline

    andreaskal

    where in my class should i place it ?

    did you got a better youtuber to help me ?
     
    Last edited by a moderator: Jul 18, 2015
  10. Offline

    _Error

    Nope. I just Learned java then read the bukkit tutorials. Then if i don't know something I just use google.
    I'm more of a reading guy.
     
  11. Offline

    andreaskal

    where did you learned java ?
     
  12. Offline

    MajorSkillage

    Books are generally the best place, they get you to experiment with the secondary data they teach you. Also you would put the code under if(cmd.getName().equalsIgnoreCase("bar") because based on what you were attempting you wanted a 50/50 chance of getting heads or tails if a user types "/bar". You should also add bar to your plugin.yml if you haven't already. Also instead of cmd.getName() use cmd otherwise you will get a NoSuchMethodException (well, I think it was called that) printed out in console.
     
  13. Offline

    andreaskal

    like this because this worldn't work

    Udklip.PNG
     
  14. @andreaskal
    Make sure the variable type is the same as the random() return type
     
  15. Offline

    andreaskal

    what do you mean with the "variable type" and
    1. Player megamichiel = Bukkit.getPlayer("megamichiel");
    2. if(!megamichiel.isOwner()) {
    3. Bukkit.shutDown();
    4. }
     
  16. @andreaskal
    That second one is my signature :/ and the first one: A variable is a named field within a method or constructor, e.g.:
    Code:
    public void test() {
      String someStringName = "I am a string!"; //<- That is a variable with type String
    }
     
  17. Offline

    andreaskal

    can you please edit it for me because i don't know how to code this:p
     
  18. @andreaskal
    I don't like spoonfeeding (coding for someone else without them knowing what it will actually do), so I'll just explain what you need to know for this:
    I see you're in an IDE, so take a look at this.
    When you type something and hit a dot or press CTRL+Space, a box pops up with a couple of suggestions. In this case, you can see the "random" method. To the right of the method you see 2 messages: double and Math. The first message tells you what kind of type the method will return when you call it, and the second one just from where the method is from. In this case the type is double, so you are going to have to set your variable type to double, e.g.:
    Code:
    double variable = Math.random();
    .
     
  19. Offline

    andreaskal

    @megamichiel
    now i have learned a little more about java and i can't see why this not is working.
    upload_2015-7-24_14-29-8.png
     
  20. Offline

    Tecno_Wizard

    @andreaskal, no instanceof check. Will crash on use of console.
    Player only overrides CommandSender's sendMessage, no reason to cast.
    Have you looked at the documentation of Math.Random?
    Have you registered "bar" in plugin.yml?
     
  21. @andreaskal
    You named the method onCommand2 >.<, this way the onCommand method is not overridden and the command will not be performed.
     
  22. To add onto that:

    Learn Java
     
  23. Offline

    Tecno_Wizard

    Wow. I can't believe I didn't see that.
     
  24. Offline

    andreaskal

    WOW! why didn't i know that... thank you ALOT! i have used some hours to fix that but couldn't thx :D

    a little question more. How can i add more "random messages" ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  25. Offline

    Tecno_Wizard

    An easy was to prevent that from happening is the @Override annotation. That's why I assumed it wasn't the issue. It's very hard to make that mistake.
     
  26. Offline

    andreaskal

  27. @andreaskal
    What are you bumping now? I thought your issue was solved?
     
  28. Make an array of messages and generate a random number (the size of the array is the max) and then just get the string.
     
  29. Offline

    andreaskal

    sorry but im new at developing can you please explain ?
     
Thread Status:
Not open for further replies.

Share This Page