Random Generator Code

Discussion in 'Plugin Development' started by Laxer21117, Apr 18, 2014.

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

    Laxer21117

    I am making a plugin that has one command and I want to know how to make that one command randomly generate a message that I set in the code. If you know this please pm me asap
     
  2. Offline

    adam753

    This is a very basic java question. Just use the Random class to make a random number and then do a switch statement on it to pick the message.
     
  3. Offline

    Laxer21117

    adam753 I am sorry I am still a bit new to coding. So I make a class, list all of my jokes in there then add a switch statement and it will randomly generate messages on that one command? I am sorry. But I still don't quite understand how to make a switch statement.
     
  4. Offline

    xAstraah

    The easiest way to do this would be to use the Random() statement then make your Strings 1 - *End Number* Then have a integer between 1 - *End number* then it will get your random joke.
     
  5. Offline

    TomFromCollege

    Code:
    private String[] jokes = new String[] {"Joke1","Joke2" };
    private static Random random = new Random();
     
    public String getRandomJoke() {
    return jokes[random.nextInt(jokes.length)];
    }
     
    Mathias Eklund likes this.
  6. Offline

    Badeye

    Firstly, google "java random number" and "java switch statement" and you will find stackoverflow pages, where people asked the exact same thing and where people gave amazingly good awnsers. Very helpful, add it to your bookmarks :)

    Now, first create a random number, where NumberOfYourMessages are just the amount of messages you have. Replace it with an integer value, like 3 or 5:
    Code:java
    1. Random rand = new Random();
    2. int random;
    3. random = rand.nextInt(NumberOfYourMessages);

    Now you can go the easy way and add if statements (probably the better way it you have only 2 or 3 messages):
    Code:java
    1. if(random == 0)
    2. System.out.println("Message1");
    3. else if(random == 1)
    4. System.out.println("Message2");
    5. else if(random == 2)
    6. System.out.println("Message3");

    Just remember, the created random number is a value between zero (included) and the number you have given (excluded). means If your NumberOfMessages is 3, the random number is either going to be 0, 1 or 2.
     
    AoH_Ruthless likes this.
  7. Offline

    Laxer21117

    Badeye I have done this but now I don't know where to put onDisable() Please help sorry.
     
  8. Offline

    Badeye

    Your onDisable() method comes into your main class (i assume you just have one class, so put it in that one). It is a normal method, just put it under onEnable(). It actually doesn't matter where it is, if it is above or under it, you do not read methods like a book from the top to the bottom, they get read when they get called. And they get called when bukkit does it and bukkit finds them as long as they are named onEnable and onDisable and they are in your main class.
    Example:
    Code:java
    1. // This just gets the plugin logger, useful for printing stuff out etc.
    2. //Replace "MyPlugin" with the name of your plugin/your project
    3. Logger log = Logger.getLogger("MyPlugin");
    4.  
    5. @Override
    6. public void onEnable() {
    7. this.log.info("[MyPlugin] is now enabled");
    8. }
    9.  
    10. @Override
    11. public void onDisable() {
    12. this.log.info("[MyPlugin] is now disabled");
    13. }

    And you should read some basic plugin development tutoials, those are basics you can learn easily.
     
  9. Offline

    Laxer21117

    Badeye Should my return statement be true or false?
     
  10. Offline

    AoH_Ruthless

    Laxer21117
    That depends on what you want to do. It's completely up to you. Like others subtly suggested, go learn Java, it will help you a lot. You will be in a better position by a hundred fold to start working with Bukkit. Honestly to me, your refusal to learn it is a display of arrogance and bad work ethic, which are two things that a programmer should not have. Maybe I'm being harsh in my assumption, so prove me wrong by spending the time by being dedicated to learn Java. Trust me, you do not want to proceed and keep learning Bukkit without a solid Java foundation. It's like learning to run before you learn to walk, it just doesn't make sense.
     
  11. Offline

    Laxer21117

    AoH_Ruthless I am learning java I had one question. Please don't comment on my post if you aren't going to help me.
     
  12. Offline

    Garris0n

    Your questions appear to be related to Bukkit even though you do not yet know Java. He is helping you by telling you that you should go learn Java first. It's your decision whether to take his advice, and it's your own fault if you end up having no idea what you're doing due to a lack of Java knowledge.
     
    AoH_Ruthless likes this.
  13. Offline

    Laxer21117

    Garris0n As I said if you aren't going to help... Don't comment!!!!!!!!!! :mad:
     
  14. Offline

    xTigerRebornx

    Laxer21117 You may not be able to see it, but we are trying to help you by telling you to learn Java. You learn Java, it makes things easier.
     
  15. Offline

    Laxer21117

    xTigerRebornx OMG I am learning java!!! I had one question!!!
     
  16. Offline

    xTigerRebornx

    Laxer21117 and that one question is one that pertains more to Java then to Bukkit, and it can be solved by going and learning Java. You asked a question, and I (and others) gave a valid solution.
     
    AoH_Ruthless likes this.
  17. Offline

    AoH_Ruthless

    Laxer21117
    I gave you helpful advice. I agree that unhelpful comments should not be posted ... which is why I told you what you can do to place yourself in a better position to learn Bukkit when you have an adept understanding of Object Oriented Programming (OOP).

    Honestly, what you are doing is equatable to learning to run before learning to walk.
     
    ChipDev and xTigerRebornx like this.
Thread Status:
Not open for further replies.

Share This Page