Two problems

Discussion in 'Plugin Development' started by TheDiamond06, Mar 4, 2015.

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

    TheDiamond06

    So here is my first problem.
    Code:java
    1.  
    2. if(p.getInventory().getItemInHand().getItemMeta().hasLore())
    3. {
    4.  
    5. }
    6. else
    7. {
    8. List<String>lore = new ArrayList<String>();
    9. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")) + "1");
    10.  
    11. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    12. p.updateInventory();
    13. }
    14.  

    Ive tested to see if this code ran, and it did. What seems to be the problem? I've tried make an ItemStack out of their iteminhand and resetting their item in hand to it, but that also didn't work.

    Second problem, this one gives me a stack trace on this line
    Code:java
    1.  
    2. if(p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.GRAY + "Infinite I") && p.hasPermission("prisonprofessional.enchantments.infinite") && p.getInventory().getItemInHand().getType() != null && p.getInventory().getItemInHand().getType() != Material.AIR && p.hasPermission("prisonprofessional.enchantments.infinite.level.1"))
    3. {
    4.  
    5. }
    6. else
    7. {
    8. p.getInventory().getItemInHand().setDurability((short) (p.getInventory().getItemInHand().getDurability() + 1));
    9. }
    10.  

    I don't see why this gives me a stack trace when an Item I am not holding doesn't have that lore... It says something is null, but I don't see how anything here can be null, except the lore not being that. I checked if it had a lore also..
     
  2. Offline

    Skionz

  3. Offline

    TheDiamond06

    @TheDiamond06 Thanks for the Second problem. I had to check if the ItemMeta wasn't null and also I forgot to check if it eve has a lore...

    I'll try this on the first problem.

    EDIT: I guess I fixed future problems on the fist problem, but it is still not changing/adding the lore.
     
  4. Offline

    Skionz

    @TheDiamond06
    1. You tagged yourself :p
    2. Post the entire class and try debugging.
     
  5. Offline

    TheDiamond06

    @Skionz
    lol tagged myself, didn't see.

    This code is running, Ive already debugged it with a sendMessage
    Code:java
    1.  
    2. List<String>lore = new ArrayList<String>();
    3. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")) + "1");
    4.  
    5. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    6. p.updateInventory();
    7.  
    8.  

    Its just not working..
     
  6. Offline

    TheDiamond06

    @CodePlaysMinecraft Not exactly, putting '1' in a string, anyway I removed that, I was putting it for testing. The lore still does not add


    @Skionz Full code if you really need it
    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void BlockBreakLore(BlockBreakEvent e)
    5. {
    6. Player p = e.getPlayer();
    7. Block b = e.getBlock();
    8.  
    9. if(p.getInventory().getItemInHand().getType() == Material.WOOD_PICKAXE || p.getInventory().getItemInHand().getType() == Material.STONE_PICKAXE || p.getInventory().getItemInHand().getType() == Material.GOLD_PICKAXE || p.getInventory().getItemInHand().getType() == Material.DIAMOND_PICKAXE)
    10. {
    11. if(p.getInventory().getItemInHand() != null && p.getInventory().getItemInHand().getType() != Material.AIR && p.getInventory().getItemInHand().hasItemMeta() && p.getInventory().getItemInHand().getItemMeta().hasLore())
    12. {
    13. List<String>lore = p.getInventory().getItemInHand().getItemMeta().getLore();
    14. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")));
    15.  
    16. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    17. p.updateInventory();
    18. }
    19. else
    20. {
    21. List<String>lore = new ArrayList<String>();
    22. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")));
    23.  
    24. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    25. p.updateInventory();
    26. }
    27. }
    28. }
    29.  
    30.  
     
  7. Offline

    Skionz

     
  8. Offline

    TheDiamond06

    @Skionz I don't know exactly what you mean by debugging. Every singe line in that code is running, every line testing with p.sendMessage, the messages all send...
     
  9. Offline

    Skionz

    @TheDiamond06 I have no idea what colorize(String) does; therefore, you need to post the entire class.
     
  10. Offline

    TheDiamond06

    @Skionz

    Code:java
    1.  
    2. package dev.td6.prisonprofessional.events;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13.  
    14. import dev.td6.prisonprofessional.main.PrisonProfessionalMain;
    15.  
    16. public class PPEventBlockBreakLore implements Listener
    17. {
    18.  
    19. PrisonProfessionalMain plugin;
    20.  
    21. public PPEventBlockBreakLore(PrisonProfessionalMain instance)
    22. {
    23. plugin = instance;
    24. }
    25.  
    26. public String colorize(String msg)
    27. {
    28. String coloredMsg = "";
    29. for(int i = 0; i < msg.length(); i++)
    30. {
    31. if(msg.charAt(i) == '&')
    32. coloredMsg += 'ยง';
    33. else
    34. coloredMsg += msg.charAt(i);
    35. }
    36. return coloredMsg;
    37. }
    38.  
    39. @SuppressWarnings("deprecation")
    40. @EventHandler
    41. public void BlockBreakLore(BlockBreakEvent e)
    42. {
    43. Player p = e.getPlayer();
    44. Block b = e.getBlock();
    45.  
    46. if(p.getInventory().getItemInHand().getType() == Material.WOOD_PICKAXE || p.getInventory().getItemInHand().getType() == Material.STONE_PICKAXE || p.getInventory().getItemInHand().getType() == Material.GOLD_PICKAXE || p.getInventory().getItemInHand().getType() == Material.DIAMOND_PICKAXE)
    47. {
    48. if(p.getInventory().getItemInHand() != null && p.getInventory().getItemInHand().getType() != Material.AIR && p.getInventory().getItemInHand().hasItemMeta() && p.getInventory().getItemInHand().getItemMeta().hasLore())
    49. {
    50. List<String>lore = p.getInventory().getItemInHand().getItemMeta().getLore();
    51. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")));
    52.  
    53. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    54. p.updateInventory();
    55. }
    56. else
    57. {
    58. List<String>lore = new ArrayList<String>();
    59. lore.add(colorize(plugin.getConfig().getString("pickblockbreak")));
    60.  
    61. p.getInventory().getItemInHand().getItemMeta().setLore(lore);
    62. p.updateInventory();
    63. }
    64. }
    65. }
    66.  
    67. }
    68.  
    69.  
     
Thread Status:
Not open for further replies.

Share This Page