Using an Enum

Discussion in 'Plugin Development' started by Sabersamus, Aug 20, 2012.

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

    Sabersamus

    This is more of a general java question, but its while I'm using bukkit, so here goes.

    I'm storing a players permissions in a SQL Database, (im giving them one permission, then moving it to a rank). To make checking what rank they are, i made an enum called rank with all the possible ranks they could be.

    Code:java
    1. public static enum Rank{
    2. TOURIST("bytecraft.tourist"),
    3. CHILD("bytecraft.child"),
    4. WARNED("bytecraft.warned"),
    5. HARD_WARNED("bytecraft.hardwarned"),
    6. SETTLER("bytecraft.settler"),
    7. MEMBER("bytecraft.member"),
    8. DONOR(""),
    9. GUARDIAN("bytecraft.guardian"),
    10. BUILDER("bytecraft.builder"),
    11. ADMIN("bytecraft.admin"),
    12. CODER("bytecraft.coder"),
    13. SENIOR_ADMIN("bytecraft.senioradmin");
    14. private String permission;
    15. private Rank(String permission){
    16. this.permission = permission;
    17. }
    18.  
    19. public String getPermission(){
    20. return this.permission;
    21. }
    22.  
    23. public static Set<Rank> getStaffRanks(){
    24. return EnumSet.of(Rank.ADMIN, Rank.SENIOR_ADMIN, Rank.GUARDIAN);
    25. }
    26.  
    27. public static Rank getRank(String permission){
    28. for(Rank rank: Rank.values()){
    29. if(rank.getPermission().equalsIgnoreCase(permission)){
    30. return rank;
    31. }
    32. }
    33. return null;
    34. }


    to check their rank im using this method
    Code:java
    1.  
    2. public Rank getRank(){
    3. return Rank.getRank(permission);
    4. }

    where permission is the string
    later to change the permission i use Rank.valueOf(String); and then a switch() statement.

    Its throwing no errors, everything in the case is executing, but the database isnt saving the permission change.... could someone tell me why?
     
  2. Sorry

    But what database? Which permission plugin are you using? Where's the code that changes a player's permission?

    Am I missing something here?
     
  3. Offline

    Sabersamus

    Code:java
    1.  
    2. Rank rank = Rank.valueOf(args[1].toUpperCase());
    3. switch(rank){
    4. case SETTLER:
    5. bTarget.setPermission(rank.getPermission());
    6. target.setDisplayName(ChatColor.GREEN + target.getName() + ChatColor.WHITE);
    7. target.setPlayerListName(target.getDisplayName());
    8. target.sendMessage(ChatColor.YELLOW + "You were made a settler of this server");
    9. player.sendMessage(ChatColor.YELLOW + "You made " + target.getDisplayName() + " a settler of this server");
    10. bTarget.setSettlerTime(Calendar.getInstance().getTime().toString());
    11. BasicCommands.plugin.getDatabase().save(bTarget);
    12. break;
    13. case CHILD:
    14. target.setDisplayName(ChatColor.AQUA + target.getName() + ChatColor.WHITE);
    15. target.setPlayerListName(target.getDisplayName());target.sendMessage(ChatColor.YELLOW + "You were made a child of bytecraft");
    16. player.sendMessage(ChatColor.YELLOW + "You made " + target.getDisplayName() + " a child of this server");
    17. target.sendMessage(ChatColor.YELLOW + "You were made a child of this server");
    18. bTarget.setPermission(rank.getPermission());
    19. BasicCommands.plugin.getDatabase().save(bTarget);
    20. break;
    21. case WARNED:
    22. bTarget.setPermission(rank.getPermission());
    23. target.setDisplayName(ChatColor.GRAY + target.getName() + ChatColor.WHITE);
    24. target.setPlayerListName(target.getDisplayName());
    25. target.sendMessage(ChatColor.YELLOW + "You have been warned");
    26. player.sendMessage(ChatColor.YELLOW + "You have warned " + target.getDisplayName());
    27. BasicCommands.plugin.getDatabase().save(bTarget);
    28. break;
    29. case HARD_WARNED:
    30. bTarget.setPermission(rank.getPermission());
    31. target.setDisplayName(ChatColor.GRAY + target.getName() + ChatColor.WHITE);
    32. target.setPlayerListName(target.getDisplayName());
    33. target.sendMessage(ChatColor.YELLOW + "You have been warned and your building rights have been removed");
    34. player.sendMessage(ChatColor.YELLOW + "You have warned " + target.getDisplayName() + " and removed his building rights");
    35. BasicCommands.plugin.getDatabase().save(bTarget);
    36. break;
    37. case MEMBER:
    38. bTarget.setPermission(rank.getPermission());
    39. target.setDisplayName(ChatColor.DARK_GREEN + target.getName() + ChatColor.WHITE);
    40. target.setPlayerListName(target.getDisplayName());
    41. target.sendMessage(ChatColor.YELLOW + "You were made a member of bytecraft");
    42. player.sendMessage(ChatColor.YELLOW + "You made " + target.getDisplayName() + " a member of this server");
    43. BasicCommands.plugin.getDatabase().save(bTarget);
    44. break;
    45. case DONOR:
    46. if(bPlayer.isStaff()){
    47. bTarget.setDonor(true);
    48. target.setDisplayName(ChatColor.YELLOW + target.getName() + ChatColor.WHITE);
    49. target.setPlayerListName(target.getDisplayName());
    50. player.sendMessage(ChatColor.YELLOW + "You made " + target.getDisplayName() + " a donator");
    51. target.sendMessage(ChatColor.YELLOW + "You were made a donator");
    52. BasicCommands.plugin.getDatabase().save(bTarget);
    53. }
    54. break;
    55. default:
    56. break;
    57. }
    58.  

    That changes the permission, im using MySQL through JavaPlugin.getDatabase();

    The plugin im making IS the permissions plugin.
     
  4. To be honost, I have almost never worked wiht MySQL.

    Maybe you need to do openConnection or something? I'm just guessing
     
  5. Offline

    Sabersamus

    With getDatabase() i just need to create an object (one that has been registered as an Entity) and change the information, in this case the permission.

    Then you just do getDatabase().save(Object); and that works. For some reason its not working in a switch()

    Im just too lazy NOT to do a switch statement :L
     
  6. That's weird though. Can it really be, because it's inside a switch() ?
     
  7. Offline

    Sabersamus

    Im not sure, but when i checked
    if(args[1].equalsIgnoreCase("settler"){
    //doStuff
    }

    if worked :/
     
Thread Status:
Not open for further replies.

Share This Page