Solved PotionEffect.toString()

Discussion in 'Plugin Development' started by ZodiacTheories, Jul 28, 2014.

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

    ZodiacTheories

    Hi, I have tried using a switch statement to compare the name of the PotionEffect. I used the .toString() method, but that didn't seem to work. What will PotionEffect.toString() method result in? I tried this:

    Code:java
    1. for(PotionEffect pe : p.getActivePotionEffects()) {
    2. switch(pe.toString()) {
    3. case "ABSORPTION":
    4. potionMeta.setDisplayName(ChatColor.GOLD + "Absorption");
    5. potion.setItemMeta(potionMeta);
    6. case "BLINDNESS":
    7. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Blindness");
    8. potion.setItemMeta(potionMeta);
    9. case "CONFUSION":
    10. potionMeta.setDisplayName(ChatColor.DARK_GREEN + "Confusion");
    11. potion.setItemMeta(potionMeta);
    12. case "DAMAGE_RESISTANCE":
    13. potionMeta.setDisplayName(ChatColor.GOLD + "Damage Resistance");
    14. potion.setItemMeta(potionMeta);
    15. case "FAST_DIGGING":
    16. potionMeta.setDisplayName(ChatColor.YELLOW + "Haste");
    17. potion.setItemMeta(potionMeta);
    18. case "FIRE_RESISTANCE":
    19. potionMeta.setDisplayName(ChatColor.DARK_RED + "Fire Resistance");
    20. potion.setItemMeta(potionMeta);
    21. case "HEALH_BOOTS":
    22. potionMeta.setDisplayName(ChatColor.RED + "Health Boost");
    23. potion.setItemMeta(potionMeta);
    24. case "HUNGER":
    25. potionMeta.setDisplayName(ChatColor.GREEN + "Hunger");
    26. potion.setItemMeta(potionMeta);
    27. case "INCREASE_DAMAGE":
    28. potionMeta.setDisplayName(ChatColor.DARK_RED + "Strength");
    29. potion.setItemMeta(potionMeta);
    30. case "INVISIBILITY":
    31. potionMeta.setDisplayName(ChatColor.BLUE + "Invisibility");
    32. potion.setItemMeta(potionMeta);
    33. case "JUMP":
    34. potionMeta.setDisplayName(ChatColor.DARK_GRAY + "Jump Boost");
    35. potion.setItemMeta(potionMeta);
    36. case "NIGHT_VISION":
    37. potionMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Night Vision");
    38. potion.setItemMeta(potionMeta);
    39. case "POISON":
    40. potionMeta.setDisplayName(ChatColor.DARK_GREEN + "Poison");
    41. potion.setItemMeta(potionMeta);
    42. case "REGENERATION":
    43. potionMeta.setDisplayName(ChatColor.RED + "Regeneration");
    44. potion.setItemMeta(potionMeta);
    45. case "SATURATION":
    46. potionMeta.setDisplayName(ChatColor.GREEN + "Saturation");
    47. potion.setItemMeta(potionMeta);
    48. case "SLOW":
    49. potionMeta.setDisplayName(ChatColor.GRAY + "Slowness");
    50. potion.setItemMeta(potionMeta);
    51. case "SLOW_DIGGING":
    52. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Mining Fatigue");
    53. potion.setItemMeta(potionMeta);
    54. case "SPEED":
    55. potionMeta.setDisplayName(ChatColor.BLUE + "Speed");
    56. potion.setItemMeta(potionMeta);
    57. case "WATER_BREATHING":
    58. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Water Breathing");
    59. potion.setItemMeta(potionMeta);
    60. case "Weakness":
    61. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    62. potion.setItemMeta(potionMeta);
    63. case "WITHER":
    64. potionMeta.setDisplayName(ChatColor.BLACK + "Wither");
    65. potion.setItemMeta(potionMeta);
    66. }
    67. }


    but it didn't work.

    Thanks
     
  2. Offline

    theguynextdoor

  3. Offline

    ZodiacTheories

    theguynextdoor

    Hmm, seems like it I can't use the .toString() method? :eek:
     
  4. Offline

    theguynextdoor

  5. Offline

    ZodiacTheories

    theguynextdoor

    This doesn't seem to work:

    Code:java
    1. if(pe.getType() == PotionEffectType.WEAKNESS) {
    2. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    3. potion.setItemMeta(potionMeta);
    4. }
    5. }
    6.  
     
  6. Offline

    theguynextdoor

    Print out the value of pe.getType(), if it is weakness, then it should look a bit like

    Code:
    PotionEffectType[18, WEAKNESS]
     
  7. Offline

    GameplayJDK

    ZodiacTheories You should be able to switch the PotionEffectType enum..
    Code:java
    1. for(PotionEffect pe : p.getActivePotionEffects()) {
    2. switch(pe.getType()) {
    3. case PotionEffectType.ABSORPTION:
    4. potionMeta.setDisplayName(ChatColor.GOLD + "Absorption");
    5. potion.setItemMeta(potionMeta);
    6. case PotionEffectType.BLINDNESS:
    7. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Blindness");
    8. potion.setItemMeta(potionMeta);
    9. case PotionEffectType.CONFUSION:
    10. potionMeta.setDisplayName(ChatColor.DARK_GREEN + "Confusion");
    11. potion.setItemMeta(potionMeta);
    12. case PotionEffectType.DAMAGE_RESISTANCE:
    13. potionMeta.setDisplayName(ChatColor.GOLD + "Damage Resistance");
    14. potion.setItemMeta(potionMeta);
    15. case PotionEffectType.FAST_DIGGING:
    16. potionMeta.setDisplayName(ChatColor.YELLOW + "Haste");
    17. potion.setItemMeta(potionMeta);
    18. case PotionEffectType.FIRE_RESISTANCE:
    19. potionMeta.setDisplayName(ChatColor.DARK_RED + "Fire Resistance");
    20. potion.setItemMeta(potionMeta);
    21. case PotionEffectType.HEALH_BOOTS:
    22. potionMeta.setDisplayName(ChatColor.RED + "Health Boost");
    23. potion.setItemMeta(potionMeta);
    24. case PotionEffectType.HUNGER:
    25. potionMeta.setDisplayName(ChatColor.GREEN + "Hunger");
    26. potion.setItemMeta(potionMeta);
    27. case PotionEffectType.INCREASE_DAMAGE:
    28. potionMeta.setDisplayName(ChatColor.DARK_RED + "Strength");
    29. potion.setItemMeta(potionMeta);
    30. case PotionEffectType.INVISIBILITY:
    31. potionMeta.setDisplayName(ChatColor.BLUE + "Invisibility");
    32. potion.setItemMeta(potionMeta);
    33. case PotionEffectType.JUMP:
    34. potionMeta.setDisplayName(ChatColor.DARK_GRAY + "Jump Boost");
    35. potion.setItemMeta(potionMeta);
    36. case PotionEffectType.NIGHT_VISION:
    37. potionMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Night Vision");
    38. potion.setItemMeta(potionMeta);
    39. case PotionEffectType.POISON:
    40. potionMeta.setDisplayName(ChatColor.DARK_GREEN + "Poison");
    41. potion.setItemMeta(potionMeta);
    42. case PotionEffectType.REGENERATION:
    43. potionMeta.setDisplayName(ChatColor.RED + "Regeneration");
    44. potion.setItemMeta(potionMeta);
    45. case PotionEffectType.SATURATION:
    46. potionMeta.setDisplayName(ChatColor.GREEN + "Saturation");
    47. potion.setItemMeta(potionMeta);
    48. case PotionEffectType.SLOW:
    49. potionMeta.setDisplayName(ChatColor.GRAY + "Slowness");
    50. potion.setItemMeta(potionMeta);
    51. case PotionEffectType.SLOW_DIGGING:
    52. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Mining Fatigue");
    53. potion.setItemMeta(potionMeta);
    54. case PotionEffectType.SPEED:
    55. potionMeta.setDisplayName(ChatColor.BLUE + "Speed");
    56. potion.setItemMeta(potionMeta);
    57. case PotionEffectType.WATER_BREATHING:
    58. potionMeta.setDisplayName(ChatColor.DARK_BLUE + "Water Breathing");
    59. potion.setItemMeta(potionMeta);
    60. case PotionEffectType.Weakness:
    61. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    62. potion.setItemMeta(potionMeta);
    63. case PotionEffectType.WITHER:
    64. potionMeta.setDisplayName(ChatColor.BLACK + "Wither");
    65. potion.setItemMeta(potionMeta);
    66. }
    67. }
     
  8. Offline

    theguynextdoor

  9. Offline

    ZodiacTheories

    theguynextdoor

    How am I meant to print that to the console? pe.getType() returns a PotionEffectType not a string, and the .toString() method doesn't print anything for some reason

    GameplayJDK theguynextdoor

    Yep, I tried to use a switch statement earlier on but it wouldn't work

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

    fireblast709

    ZodiacTheories you can switch over the id for Java 6 support (though its deprecated). Otherwise if...else if...else over the effects yourself
     
  11. Offline

    ZodiacTheories

    fireblast709

    What are the ids of potions though? Also, this didn't work:

    Code:java
    1. if(pe.getType() == PotionEffectType.WEAKNESS) {
    2. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    3. potion.setItemMeta(potionMeta);
    4. }
    5. }
     
  12. Offline

    fireblast709

    ZodiacTheories what exactly didn't work? Also PotionEffectType has a getId() method
     
  13. Offline

    ZodiacTheories

    fireblast709

    What would be the id though?

    Code:java
    1. public static void openGUI(Player p, Player sender) {
    2.  
    3. Inventory inv = Bukkit.createInventory(null, 54, p.getName());
    4.  
    5. ItemStack helmet = p.getInventory().getHelmet();
    6. ItemStack chestplate = p.getInventory().getChestplate();
    7. ItemStack leggings = p.getInventory().getLeggings();
    8. ItemStack boots = p.getInventory().getBoots();
    9.  
    10. ItemStack potion = new ItemStack(Material.POTION);
    11. ItemMeta potionMeta = potion.getItemMeta();
    12.  
    13. for(PotionEffect pe : p.getActivePotionEffects()) {
    14.  
    15. if(pe.getType() == PotionEffectType.WEAKNESS) {
    16. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    17. potion.setItemMeta(potionMeta);
    18. }
    19. }
    20.  
    21.  
    22. int health = (int) p.getHealth();
    23. int hunger = (int) p.getFoodLevel();
    24.  
    25. ItemStack hearts = new ItemStack(Material.GOLD_NUGGET, health);
    26. ItemStack food = new ItemStack(Material.RAW_FISH, hunger);
    27.  
    28. inv.setItem(0, helmet);
    29. inv.setItem(1, chestplate);
    30. inv.setItem(2, leggings);
    31. inv.setItem(3, boots);
    32. inv.setItem(6, potion);
    33. inv.setItem(7, hearts);
    34. inv.setItem(8, food);
    35. sender.openInventory(inv);
    36. }
    37. }


    The potion's display name doesn't get set
     
  14. Offline

    fireblast709

    ZodiacTheories debug the type. The id would be the number found as the Wrapper argument in the docs, but since it's deprecated, the code you have currently is better.
     
  15. Offline

    ZodiacTheories

    fireblast709

    When I tried to debug, nothing appeared in the console, where do you recommend I debug?
     
  16. Offline

    fireblast709

    ZodiacTheories in the for loop, print the types it loops over (so pe.getType().name())
     
  17. Offline

    ZodiacTheories

    fireblast709

    Nothing appears in console:

    Code:java
    1. for(PotionEffect pe : p.getActivePotionEffects()) {
    2. if(pe.getType() == PotionEffectType.WEAKNESS) {
    3. Core.getInstance().debug(pe.getType().getName());
    4. potionMeta.setDisplayName(ChatColor.GRAY + "Weakness");
    5. potion.setItemMeta(potionMeta);
    6. }
    7. }
     
  18. Offline

    theguynextdoor

    ZodiacTheories Move the debug statement to outside the if statement but inside the loop still.
     
  19. Offline

    ZodiacTheories

    theguynextdoor

    Thanks, now it says WEAKNESS in the console, still nothing on the potion though
     
  20. Offline

    theguynextdoor

    Compare the names of each

    Code:java
    1. for (PotionEffect pe : player.getActivePotionEffects()) {
    2. System.out.println(pe.getType().getName());
    3. System.out.println(PotionEffectType.WEAKNESS.getName());
    4.  
    5. if (pe.getType().getName().equals(PotionEffectType.WEAKNESS.getName())) {
    6. System.out.println("lol");
    7. }
    8. }



    EDIT:

    Heck, just use the getName() on the potion effect from the loop and then use your initial code
     
    ZodiacTheories and Konkz like this.
  21. Offline

    ZodiacTheories

Thread Status:
Not open for further replies.

Share This Page