int counter or somethin

Discussion in 'Plugin Development' started by drillzy, May 1, 2017.

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

    drillzy

    Hello guys, i need some help with a code. So on my command i have a randomizer for the list so it grabs random string from the list on command. i want to make it go down as in int = int -1
    I have no idea how to apply that to my code. Help me out here please!

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    Player p = (Player) sender;
    
    if(cmd.getName().equalsIgnoreCase("joke")){
    
    
    List<String> Jokes = getConfig().getStringList("Jokes");
    
    Random random = new Random();
    
    int index = 0;
    
    if(Jokes.size() > 0){
    
          index = random.nextInt(Jokes.size());
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Jokes.get(index)));
    
    } else {
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lJokes &8» &cNo Jokes SetUp! Create one by doing: &3/jokesubmit <joke>"));
    
    cooldowns.remove(p.getName());
    
    returntrue;
    
    }
    
    }
    
     
  2. Offline

    Zombie_Striker

    @drillzy
    INT--; will subtract 1 from an int, but I do not think this is what you meant. What exactly are you trying to achieve?
     
  3. Offline

    drillzy

    I want to get a string from the list, Jokes: [] in config on command, /joke. as i keep doing that it keeps going down the list giving me the strings. then starts over when it reaches the end of strings list
     
  4. Offline

    Zombie_Striker

    @drillzy
    Okay, then do this:
    1. Move index out of the onCommand. Make it a field. Set it equal to -2
    2. Then in the onCommand, if the index is equal to -2, set it equal to a random index, else subtract 1 (using index--;)
    3. If the index is equal to -1, set index equal to the list's size.
     
  5. Offline

    drillzy

    so is this good? what else to do/fix?

    Code:
    int index = -2;
    
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    Player p = (Player) sender;
    
    if(cmd.getName().equalsIgnoreCase("joke")){
    
    
    List<String> Jokes = getConfig().getStringList("Jokes");
    
    if(index == -2){
    
    int jokes = getConfig().getStringList("Jokes").size();
    
    index = jokes;
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Jokes.get(index)));
    
    
    index = index -1;
    
    } else {
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lJokes &8» &cNo Jokes SetUp! Create one by doing: &3/jokesubmit <joke>"));
    
    cooldowns.remove(p.getName());
    
    returntrue;
    
    }
    
    }
    
     
  6. Offline

    Zombie_Striker

    @drillzy
    Move the send message line outside of the if statement. Also , do the subtraction before you print out the message.
     
  7. Offline

    drillzy

    @Zombie_Striker

    so would this be the new code as of now?
    Code:
    int index = -2;
    
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    Player p = (Player) sender;
    
    if(cmd.getName().equalsIgnoreCase("joke")){
    
    
    List<String> Jokes = getConfig().getStringList("Jokes");
    
    if(index == -2){
    
    int jokes = getConfig().getStringList("Jokes").size();
    
    index = jokes;
    
    
    index = index -1;
    
    
    
    }else{
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', Jokes.get(index)));
    
    }
    
    } else {
    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lJokes &8» &cNo Jokes SetUp! Create one by doing: &3/jokesubmit <joke>"));
    
    cooldowns.remove(p.getName());
    
    returntrue;
    
    
    }
    

    EDIT: Ok i see i got this wrong with the last bracket but how would i separate the if statement and the printed msg? i will need more brackets unless i placed some wrong
     
    Last edited: May 1, 2017
  8. Offline

    MrGeneralQ

    Okay as far as I can see you just try to show a random joke right? IF SO, why don't you use the Random() class? It's much easier then what you are currently trying todo.
     
  9. Offline

    drillzy

    @MrGeneralQ I don't be want random because it can send me the same string up to 5 times over and over and then move on and it gets annoying. If there is a way so it doesn't send me the same string over and over, then plz help me with da code :)
     
  10. Offline

    MrGeneralQ

    Store all Your messages in a list. Then randomize a number and save it somewhere.

    On Your next spin check if the random number is the same as Your stored one, if it is randomize again until it is different. Then use that value for Your random message. Now it is impossible to have the same message twice In a row but it is still random.
     
  11. Create a config file and create an array list there. Get here your jokes and save them. Create an if statement with condition that the array list hasn't reached the last entry. In the brackets you tell you want do increase the array list integer with one so it will display the message of array list : array list current (could be stored in an integer) + 1. And if you reach the last entry of array list you tell it to go to the first entry (0)

    EDIT. Also don't forget to tell at else statement to say the first joke because it will always leave the first if not so!
     
Thread Status:
Not open for further replies.

Share This Page