more efficient way to check lore?

Discussion in 'Plugin Development' started by drpk, Aug 10, 2014.

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

    drpk

    I want to am making a tokens enchants, and I am checking for a lore, this code is very inefficienct, as I have to check for a lot of lores, anyone have a more efficienct method?(referencing the last part)
    Code:java
    1. if (lore1.contains(ChatColor.AQUA + "Speed 4")) {
    2. lore.addAll(im.getLore());
    3. lore.remove(ChatColor.AQUA + "Speed 4");
    4. lore.add(ChatColor.AQUA + "Speed 5");
    5. meta.setLore(lore);
    6. hand.setItemMeta(meta);
    7. item.getItemMeta().getLore();
    8.  
    9. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    10. p.updateInventory();
    11. return;
    12. }
    13. if (lore1.contains(ChatColor.AQUA + "Speed 3")) {
    14. lore.addAll(im.getLore());
    15. lore.remove(ChatColor.AQUA + "Speed 3");
    16. lore.add(ChatColor.AQUA + "Speed 4");
    17. meta.setLore(lore);
    18. hand.setItemMeta(meta);
    19. item.getItemMeta().getLore();
    20.  
    21. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    22. p.updateInventory();
    23. return;
    24. }
    25. if (lore1.contains(ChatColor.AQUA + "Speed 2")) {
    26. lore.addAll(im.getLore());
    27. lore.remove(ChatColor.AQUA + "Speed 2");
    28. lore.add(ChatColor.AQUA + "Speed 3");
    29. meta.setLore(lore);
    30. hand.setItemMeta(meta);
    31. item.getItemMeta().getLore();
    32.  
    33. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    34. p.updateInventory();
    35. return;
    36. }
    37.  
    38. if (lore1.contains(ChatColor.AQUA + "Speed!")) {
    39. lore.addAll(im.getLore());
    40. lore.remove(ChatColor.AQUA + "Speed!");
    41. lore.add(ChatColor.AQUA + "Speed 2");
    42. meta.setLore(lore);
    43. hand.setItemMeta(meta);
    44. item.getItemMeta().getLore();
    45.  
    46. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    47. p.updateInventory();
    48.  
    49.  
    50. return;
    51. }
    52.  
    53.  
    54. if (item.getItemMeta().hasLore()) {
    55. if (!lore1.contains(ChatColor.AQUA + "Speed!")) {
    56. if (!lore1.contains(ChatColor.AQUA + "Speed 2")) {
    57. if (!lore1.contains(ChatColor.AQUA + "Speed 3")) {
    58. if (!lore1.contains(ChatColor.AQUA + "Speed 4")) {
    59. if (!lore1.contains(ChatColor.AQUA + "Speed 5")) {
    60. if (lore1.contains(ChatColor.YELLOW + "Haste!")){
    61.  
    62.  
    63. lore.addAll(im.getLore());
    64. lore.add(ChatColor.AQUA + "Speed!");
    65. meta.setLore(lore);
    66. hand.setItemMeta(meta);
    67. item.getItemMeta().getLore();
    68.  
    69. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    70. p.updateInventory();
    71.  
    72. return;
    73. }
    74. }
    75. }
    76. }
    77. }
    78.  
    79. if (!item.getItemMeta().hasLore()) {
    80.  
    81.  
    82. lore.add(ChatColor.AQUA + "Speed!");
    83. meta.setLore(lore);
    84. hand.setItemMeta(meta);
    85. item.getItemMeta().getLore();
    86. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    87. p.updateInventory();
    88.  
    89.  
    90. return;
    91. }
    92.  
    93. }
    94. }
    95. }
    96. }
     
  2. Offline

    IkBenHarm

    drpk
    you could make 2 methods addLore(Some parameters) and hasLore(Speed1towhatever) and use these two
     
  3. Offline

    Forseth11

    Try these:

    Code:java
    1. public static ItemStack addLore(List<String> lore, ItemStack item){
    2. ItemMeta meta = item.getItemMeta();
    3. meta.setLore(lore);
    4. item.setItemMeta(meta);
    5. return item;
    6. }


    Code:java
    1. public static boolean hasLore(List<String> lore, ItemStack item){
    2. ItemMeta meta = item.getItemMeta();
    3. if(meta.hasLore()){
    4. if(meta.getLore().size() == lore.size()){
    5. for(int i = 0; i < lore.size(); i++){
    6. if(!meta.getLore().get(i).equalsIgnoreCase(lore.get(i))){
    7. return false;
    8. }
    9. }
    10. return true;
    11. }else{
    12. return false;
    13. }
    14. }else{
    15. return false;
    16. }
    17. }


    Code:java
    1. public static boolean containsLore(String loreLine, ItemStack item){
    2. ItemMeta meta = item.getItemMeta();
    3. if(meta.hasLore()){
    4. if(meta.getLore().contains(loreLine)){
    5. return true;
    6. }else{
    7. return false;
    8. }
    9. }else{
    10. return false;
    11. }
    12. }
     
    drpk likes this.
  4. Offline

    drpk

    IkBenHarm ia it possible to check only some characters in the lore? Cause I have Haste 1, 2 etc. So would I be able to make it just check the Haste part?

    And Forseth11 saves the day!

    Forseth11 no problem! and for hasLore, how would I use p.getItemInHand?

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

    Forseth11

    Like this:
    Code:java
    1. List<String> lore = new ArrayList<String>();
    2. lore.add("Line 1");
    3. lore.add("Line 2");
    4. if(hasLore(lore, p.getItemInHand())){
    5. //do stuff here
    6. }
     
    drpk likes this.
  6. Offline

    drpk

    Forseth11 strange, it only adds the lore.add part here's my code (for the laste code)
    Code:java
    1. if (item != null) { // So you don't get errors in the console about NullErrorException
    2.  
    3. if (p.getInventory().contains(Material.BLAZE_ROD, 4)) {
    4.  
    5. ItemStack hand = p.getItemInHand();
    6. ItemMeta meta = hand.getItemMeta();
    7. ItemStack is = new ItemStack(Material.BLAZE_ROD, 4);
    8. ItemMeta isMeta = is.getItemMeta();
    9. p.sendMessage("e");
    10.  
    11. ArrayList<String> lore = new ArrayList<String>();
    12. for (String lore1 : p.getItemInHand().getItemMeta().getLore()) {
    13. if (lore1.contains(ChatColor.AQUA + "Speed 5")) {
    14. p.sendMessage(ChatColor.DARK_RED + "You have reached the maximum level.");
    15.  
    16. return;
    17.  
    18. }
    19.  
    20. if (lore1.contains(ChatColor.AQUA + "Speed 4")) {
    21. lore.addAll(im.getLore());
    22. lore.remove(ChatColor.AQUA + "Speed 4");
    23. lore.add(ChatColor.AQUA + "Speed 5");
    24. meta.setLore(lore);
    25. hand.setItemMeta(meta);
    26. item.getItemMeta().getLore();
    27.  
    28. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    29. p.updateInventory();
    30. return;
    31. }
    32. if (lore1.contains(ChatColor.AQUA + "Speed 3")) {
    33. lore.addAll(im.getLore());
    34. lore.remove(ChatColor.AQUA + "Speed 3");
    35. lore.add(ChatColor.AQUA + "Speed 4");
    36. meta.setLore(lore);
    37. hand.setItemMeta(meta);
    38. item.getItemMeta().getLore();
    39.  
    40. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    41. p.updateInventory();
    42. return;
    43. }
    44. if (lore1.contains(ChatColor.AQUA + "Speed 2")) {
    45. lore.addAll(im.getLore());
    46. lore.remove(ChatColor.AQUA + "Speed 2");
    47. lore.add(ChatColor.AQUA + "Speed 3");
    48. meta.setLore(lore);
    49. hand.setItemMeta(meta);
    50. item.getItemMeta().getLore();
    51.  
    52. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    53. p.updateInventory();
    54. return;
    55. }
    56.  
    57. if (lore1.contains(ChatColor.AQUA + "Speed!")) {
    58. lore.addAll(im.getLore());
    59. lore.remove(ChatColor.AQUA + "Speed!");
    60. lore.add(ChatColor.AQUA + "Speed 2");
    61. meta.setLore(lore);
    62. hand.setItemMeta(meta);
    63. item.getItemMeta().getLore();
    64.  
    65. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    66. p.updateInventory();
    67.  
    68.  
    69. return;
    70. }
    71.  
    72.  
    73. if(hasLore(lore, p.getItemInHand())){
    74.  
    75.  
    76. lore.addAll(im.getLore());
    77. lore.add(ChatColor.AQUA + "Speed!");
    78. meta.setLore(lore);
    79. hand.setItemMeta(meta);
    80. item.getItemMeta().getLore();
    81.  
    82. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    83. p.updateInventory();
    84.  
    85. return;
    86. }
    87.  
    88.  
    89. if(!hasLore(lore, p.getItemInHand())){
    90.  
    91.  
    92. lore.add(ChatColor.AQUA + "Speed!");
    93. meta.setLore(lore);
    94. hand.setItemMeta(meta);
    95. item.getItemMeta().getLore();
    96. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    97. p.updateInventory();
    98.  
    99.  
    100. return;
    101. }
    102.  
    103. }
    104. }
    105. }
    106. }
    107. }
     
  7. Offline

    Forseth11

    The methods I sent are not apart of the List object or the ItemMeta object. You have to use it like addLore(LISTLORE, ItemStack);
     
  8. Offline

    drpk

    Forseth11 I'm using the regular lore.add for this, do I have to use the addLore method?
     
  9. Offline

    Forseth11

    No. I am just saying to use the methods I have it is no in another class so it is not SOMETHING.Method() it is just Method(). Use what ever you want to use.
     
  10. Offline

    drpk

    Forseth11 now that parts works, but it keeps adding Speed! instead of progressing.
     
  11. Offline

    xigsag

    drpk what're you trying to achieve? maybe i can help?
     
  12. Offline

    drpk

    xigsag basically, when a player clicks a sign (already implemented) and has a set amount of an item(also implemented) it gives the item in hand a lore and if the player already has that lore, it will progress. In this case, it wont progress, but will just keep adding speed! instead of adding speed 2 and 3 etc.

    apparently, this part always returns true, even though it checks to make sure the lore doesn't contain Speed or Speed 1
    Code:java
    1.  
    2. if (meta.hasLore()) {
    3. if (!lore1.contains(ChatColor.AQUA + "Speed!")) {
    4. if (!lore1.contains(ChatColor.AQUA + "Speed 5")) {
    5. if (!lore1.contains(ChatColor.AQUA + "Speed 4")) {
    6. if (!lore1.contains(ChatColor.AQUA + "Speed 3")) {
    7. if (!lore1.contains(ChatColor.AQUA + "Speed 2")) {
    8.  
    9.  
    10. lore.addAll(im.getLore());
    11. lore.add(ChatColor.AQUA + "Speed!");
    12. meta.setLore(lore);
    13. hand.setItemMeta(meta);
    14. item.getItemMeta().getLore();
    15.  
    16. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    17. p.updateInventory();
    18.  
    19. return;
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }


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

    drpk

    bump?
     
Thread Status:
Not open for further replies.

Share This Page