Code isn't working

Discussion in 'Plugin Development' started by PimpDuck, May 20, 2014.

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

    PimpDuck

    Okay, so on my plugin I'm trying to make it loop through bowtypes that the player has permission for. So how would I make it skip the ones the player doesn't have permission for in the loop?

    I tried this http://paste.md-5.net/sarecetaya.avrasm but it isn't working.
    Please help,
    Thanks!

    Help?

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

    ImPhantom

    PimpDuck
    Please only bump once every 24 hours.
     
  3. Offline

    PimpDuck

    I just really want to finish this plugin. I've been stuck on this bug for the last 2 days :(
     
  4. Offline

    teej107

    if/else statements are your friends. Use them.

    Edit: Also by looking at your code, for loops might be a help as well.
     
  5. Offline

    PimpDuck

    I don't know how I'd do the loop
     
  6. Offline

    MrSparkzz

    PimpDuck
    I would change this to a switch statement, since you can switch on enumerators
    Code:java
    1. if(getNextBowType(player) == BowType.NORMAL){
    2. setCurrentBowType(BowType.NORMAL, player);
    3. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Normal" + ChatColor.GOLD + " arrows!");
    4. }else if(getNextBowType(player) == BowType.SLOWNESS){
    5. setCurrentBowType(BowType.SLOWNESS, player);
    6. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Slowness" + ChatColor.GOLD + " arrows!");
    7. }else if(getNextBowType(player) == BowType.EXPLOSION){
    8. setCurrentBowType(BowType.EXPLOSION, player);
    9. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Explosion" + ChatColor.GOLD + " arrows!");
    10. }else if(getNextBowType(player) == BowType.FIRE){
    11. setCurrentBowType(BowType.FIRE, player);
    12. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Fire" + ChatColor.GOLD + " arrows!");
    13. }else if(getNextBowType(player) == BowType.POISON){
    14. setCurrentBowType(BowType.POISON, player);
    15. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Poison" + ChatColor.GOLD + " arrows!");
    16. }else if(getNextBowType(player) == BowType.LIGHTNING){
    17. setCurrentBowType(BowType.LIGHTNING, player);
    18. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Lightning" + ChatColor.GOLD + " arrows!");
    19. }else if(getNextBowType(player) == BowType.BLINDING){
    20. setCurrentBowType(BowType.BLINDING, player);
    21. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Blindness" + ChatColor.GOLD + " arrows!");
    22. }else if(getNextBowType(player) == BowType.LIFESTEAL){
    23. setCurrentBowType(BowType.LIFESTEAL, player);
    24. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Lifesteal" + ChatColor.GOLD + " arrows!");
    25. }else if(getNextBowType(player) == BowType.DAZE){
    26. setCurrentBowType(BowType.DAZE, player);
    27. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Daze" + ChatColor.GOLD + " arrows!");
    28. }else if(getNextBowType(player) == BowType.BOUNCE){
    29. setCurrentBowType(BowType.BOUNCE, player);
    30. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Bounce" + ChatColor.GOLD + " arrows!");
    31. }else if(getNextBowType(player) == BowType.NORMAL){
    32. setCurrentBowType(BowType.NORMAL, player);
    33. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Normal" + ChatColor.GOLD + " arrows!");
    34. }
     
  7. Offline

    xize

    I probably would do this just because its looks more efficient for me to read:p:

    Code:
    for(BowType type : BowType.Values()) {
          if(getNextBowType(player) == type) {
                  //do something
                //the break means that it stops looping.
                    break;
          }
    }
    
    -edit-

    it seems ive misread something, in your case you may want to remove the break; since you want to check all permissions and compair it with per enum?
     
  8. Offline

    MrSparkzz

    PimpDuck xize
    Should be something like this for a switch statement, I don't quite remember since I haven't used switch statements in a long while.
    Code:java
    1. BowType bowtype = getNextBowType(player);
    2.  
    3. switch (bowtype) {
    4. case NORMAL:
    5. setCurrentBowType(BowType.NORMAL, player);
    6. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Normal" + ChatColor.GOLD + " arrows!");
    7. break;
    8. case SLOWNESS:
    9. setCurrentBowType(BowType.SLOWNESS, player);
    10. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Slowness" + ChatColor.GOLD + " arrows!");
    11. break;
    12. default:
    13. break;
    14. }
     
  9. Offline

    PimpDuck

  10. Offline

    MrSparkzz

    PimpDuck
    I think what you should do with the cases other than what you have now is this
    Code:java
    1. BowType bowtype = get[I]Current[/I]BowType(player);

    then when it checks the current bow type, it'll switch to the next one
    Code:java
    1. case NORMAL:
    2. setCurrentBowType(BowType.SLOWNESS, player);
    3. index = 0;
    4. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Slowness" + ChatColor.GOLD + " arrows!");
    5. break;
    6. case SLOWNESS:
    7. setCurrentBowType(BowType.EXPLOSION, player);
    8. index = 1;
    9. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + "Explosive" + ChatColor.GOLD + " arrows!");
    10. break;

    Basically if you don't understand this, it will check the current bow type, then set the current bow type to the next bow type in the list. Once you get down to the BOUNCE bow type, you can have it set it back to normal and it will just keep looping every time you click.

    This also gets rid of the need for the getNextBowType method, unless it's used somewhere else or you plan on using it in the future.. if you plan on using it in the future, personally I would use the deprecation annotation just so I know not to use it for the time being.
     
  11. Offline

    Rocoty

    What about just dropping switch and conditionals, and instead do this:
    Code:java
    1. setCurrentBowType(bowtype, player);
    2. player.sendMessage(plugin.prefix + ChatColor.GOLD + "You have selected " + ChatColor.GREEN + bowtype.name() + ChatColor.GOLD + " arrows!");
     
    jimuskin likes this.
  12. Offline

    MrSparkzz

    Rocoty
    Well I guess that could work, but he's still getting the problem of obtaining the next bow type when he gets to the end of the list. So the switch statements would just be easier and I just adore them.. your method would work if he coded in a way to start at the beginning of the list after he reaches the end.
     
  13. Offline

    Rocoty

    MrSparkzz Well....he did. If you read the code...
     
  14. Offline

    MrSparkzz

    Rocoty
    Yeah, but he stated that he was having trouble looping back to the first bow type. That's why I said to do it the second way I stated. The second way I stated removed all need for the getNextBowType method. Maybe there was an error in that method which was stopping it from looping.
     
  15. Offline

    Rocoty

    MrSparkzz Even so, that logic should be put in the getNextBowType method, don't you agree? That way you don't have to write a new switch-statement every time you want to get the next bow type.
     
  16. Offline

    MrSparkzz

    Rocoty
    I guess you're right. I just love using switch statements. That's how I did the looping in my ServerControl plugin.. I had no problems with it and it was very simple. I've seen some people write hundreds of lines of complicated code to do something very simple and I see no point to it. For example with plugins like Factions and such, they have hundreds of lines of code in like 3+ classes that all call eachother doing something simple that could be done in say 50 lines of code or less.
     
  17. Offline

    Rocoty

    MrSparkzz Yes. Always look for the simplest solution unless performance is critical. That's what I code by.
     
    MrSparkzz likes this.
Thread Status:
Not open for further replies.

Share This Page