Solved How to get int from item lore?

Discussion in 'Plugin Development' started by BaHeTo0, Jul 17, 2014.

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

    BaHeTo0

    So im am trying to make a plugin about a sword that is called Thursty Blade!
    This sword is thirsty for blood and it gets blood by hitting players and(or) creatures!

    The amount of blood it has is stored in the item's lore and i need help with this!
    How do i get the amount of blood in the item lore please help!
    Here is my code:
    Code:java
    1. package me.BaHeTo0.ThirsyBlade;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener{
    19.  
    20. public void onEnable(){
    21. getServer().getPluginManager().registerEvents(this, this);
    22.  
    23. }
    24.  
    25. public void onDisable(){
    26.  
    27. }
    28.  
    29. @SuppressWarnings("deprecation")
    30. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    31. Player player = (Player) sender;
    32. if(commandLabel.equalsIgnoreCase("tb")){
    33. if(args.length == 0){
    34. player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "ThirstyBlade " + ChatColor.RESET + "[" + ChatColor.YELLOW + "0.1" + ChatColor.RESET + "]" + "help menu:");
    35. player.sendMessage(ChatColor.YELLOW + "/tb" + ChatColor.WHITE + ": brings up the help menu(this).");
    36. player.sendMessage(ChatColor.YELLOW + "/tb give" + ChatColor.WHITE + ": gives you a thirsty blade.");
    37. player.sendMessage(ChatColor.YELLOW + "/tb give <player>" + ChatColor.WHITE + ": gives the player a thirsty blade.");
    38. }
    39. else if(args.length == 1){
    40. ItemStack tb = new ItemStack(Material.IRON_SWORD);
    41. ItemMeta tbMeta = tb.getItemMeta();
    42. tbMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade");
    43. tbMeta.setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    44. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    45. "",
    46. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + "0/100"));
    47. tb.setItemMeta(tbMeta);
    48. player.getInventory().addItem(tb);
    49. }
    50. else if(args.length == 2){
    51. if(player.getServer().getPlayer(args[1]) != null){
    52. Player target = getServer().getPlayer(args[1]);
    53.  
    54. ItemStack tb = new ItemStack(Material.IRON_SWORD);
    55. ItemMeta tbMeta = tb.getItemMeta();
    56. tbMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade");
    57. tbMeta.setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    58. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    59. "",
    60. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + "0/100"));
    61. tb.setItemMeta(tbMeta);
    62.  
    63. target.getInventory().addItem(tb);
    64. }
    65. else{
    66. player.sendMessage(ChatColor.RED + "Player not found!");
    67. }
    68. }
    69. else{
    70. player.sendMessage(ChatColor.DARK_RED + "Error!");
    71. }
    72. }
    73. return false;
    74.  
    75. }
    76.  
    77. @EventHandler
    78. public void onFight(EntityDamageByEntityEvent event){
    79. Entity damager = event.getDamager();
    80. Entity taker = event.getEntity();
    81. if(damager instanceof Player && ((Player) damager).getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade")){
    82. //Adding blood to the blade!
    83. if(taker instanceof Player && getConfig().getBoolean("pvponly") == true){
    84.  
    85. int bloodvalue;
    86. bloodvalue = getConfig().getInt("bloodperhit")/*+ the current blood of the blade*/;
    87. //trying to get the current blood in the blade!
    88. ((Player) damager).getItemInHand().getItemMeta().getLore()/*how to get the current blood of the blade please help?*/;
    89.  
    90. ((Player) damager).getItemInHand().getItemMeta().setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    91. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    92. "",
    93. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + bloodvalue + "/100"));
    94. }
    95. }
    96.  
    97. }
    98.  
    99. }
    100.  


    Good, but im still new and I want an example that fits my code please

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

    aninsanellama

    I'd just create a method that returns an integer and takes an ItemStack within it's parameters.

    To get the blood value, let's say that it is ALWAYS on line 2.

    Code:
    String[] lore = itemMeta.getLore();
    String loreLine = lore[1];
    Otherwise, you could iterate through the array until you find the correct line:

    Code:
    String loreLine = "";
    for(String line : lore)
    {
      if(line.startsWith(ChatColor.GRAY + "Blood"))
      {
        loreLine = line; // you can now use this variable in the later pieces of code
        break; // get out of the loop
      }
    }
    You'd also want to split that line as follows:
    Code:
    String splitted = loreLine.split("Blood: " + ChatColor.WHITE)[1];
    splitted        = splitted.split("/")[0];
    Let's pretend the lore has a 29/100 blood value, you will now have the number 29 stored as a string value.
    This is where you can simply use the earlier provided method:

    Code:
    int blood = Integer.parseInt(splitted);
    Please note that if the splitted value is not a number then that will throw a NumberFormatException, which you could catch if you wished to tell someone that something failed (or do anything else for that matter).
     
  3. Offline

    BaHeTo0

    aninsanellama Thank you so much

    aninsanellama im getting an error here

    Code:java
    1. String[] lore = ((Player) damager).getItemInHand().getItemMeta().getLore();
    2. String loreLine = lore[3];


    cannot convert from list<string> to string[]

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

    aninsanellama

    Sorry, I thought the ItemMeta#getLore() method returned a String array, it actually returns an ArrayList.
    Change:

    Code:java
    1. String[] lore = ((Player) damager).getItemInHand().getItemMeta().getLore(); // to:
    2. List<String> lore = ((Player) damager).getItemInHand().getItemMeta().getLore();
    3. //and
    4. String loreLine = lore[3] // to
    5. String loreLine = lore.get(3);
     
  5. Offline

    BaHeTo0

    aninsanellama Thanks again it shows no errors let me test it now

    aninsanellama for some reason it does not change the lore

    here us ny code again:
    Code:java
    1. package me.BaHeTo0.ThirsyBlade;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Main extends JavaPlugin implements Listener{
    20.  
    21. public void onEnable(){
    22. getServer().getPluginManager().registerEvents(this, this);
    23.  
    24. saveDefaultConfig();
    25.  
    26. }
    27.  
    28. public void onDisable(){
    29.  
    30. }
    31.  
    32. @SuppressWarnings("deprecation")
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    34. Player player = (Player) sender;
    35. if(commandLabel.equalsIgnoreCase("tb")){
    36. if(args.length == 0){
    37. player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "ThirstyBlade " + ChatColor.RESET + "[" + ChatColor.YELLOW + "0.1" + ChatColor.RESET + "]" + "help menu:");
    38. player.sendMessage(ChatColor.YELLOW + "/tb" + ChatColor.WHITE + ": brings up the help menu(this).");
    39. player.sendMessage(ChatColor.YELLOW + "/tb give" + ChatColor.WHITE + ": gives you a thirsty blade.");
    40. player.sendMessage(ChatColor.YELLOW + "/tb give <player>" + ChatColor.WHITE + ": gives the player a thirsty blade.");
    41. }
    42. else if(args.length == 1){
    43. ItemStack tb = new ItemStack(Material.IRON_SWORD);
    44. ItemMeta tbMeta = tb.getItemMeta();
    45. tbMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade");
    46. tbMeta.setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    47. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    48. "",
    49. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + "0/100"));
    50. tb.setItemMeta(tbMeta);
    51. player.getInventory().addItem(tb);
    52. }
    53. else if(args.length == 2){
    54. if(player.getServer().getPlayer(args[1]) != null){
    55. Player target = getServer().getPlayer(args[1]);
    56.  
    57. ItemStack tb = new ItemStack(Material.IRON_SWORD);
    58. ItemMeta tbMeta = tb.getItemMeta();
    59. tbMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade");
    60. tbMeta.setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    61. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    62. "",
    63. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + "0/100"));
    64. tb.setItemMeta(tbMeta);
    65.  
    66. target.getInventory().addItem(tb);
    67. }
    68. else{
    69. player.sendMessage(ChatColor.RED + "Player not found!");
    70. }
    71. }
    72. else{
    73. player.sendMessage(ChatColor.DARK_RED + "Error!");
    74. }
    75. }
    76. return false;
    77.  
    78. }
    79.  
    80. @EventHandler
    81. public void onFight(EntityDamageByEntityEvent event){
    82. Entity damager = event.getDamager();
    83. Entity taker = event.getEntity();
    84. if(damager instanceof Player && ((Player) damager).getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade")){
    85. //Adding blood to the blade!
    86. if(taker instanceof Player && getConfig().getBoolean("pvponly") == true){
    87.  
    88. List<String> lore = ((Player) damager).getItemInHand().getItemMeta().getLore();
    89. String loreLine = lore.get(3);
    90.  
    91. String splitted = loreLine.split(ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE)[1];
    92. splitted = splitted.split("/")[0];
    93.  
    94. int blood = Integer.parseInt(splitted) + getConfig().getInt("bloodperhit");
    95.  
    96. ((Player) damager).getItemInHand().getItemMeta().setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    97. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    98. "",
    99. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + blood + "/100"));
    100. }
    101. if(getConfig().getBoolean("pvponly") == false){
    102.  
    103. List<String> lore = ((Player) damager).getItemInHand().getItemMeta().getLore();
    104. String loreLine = lore.get(3);
    105.  
    106. String splitted = loreLine.split(ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE)[1];
    107. splitted = splitted.split("/")[0];
    108.  
    109. int blood = Integer.parseInt(splitted) + getConfig().getInt("bloodperhit");
    110.  
    111. ((Player) damager).getItemInHand().getItemMeta().setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    112. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    113. "",
    114. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + blood + "/100"));
    115.  
    116. }
    117. }
    118.  
    119. }
    120.  
    121. }
    122.  
    123.  
    124.  


    NeverMind I have find the solution

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page