Solved How to get specific item from player inventory

Discussion in 'Plugin Development' started by BaHeTo0, Jul 18, 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!

    Now I am trying to make it so every X seconds the blade loses X blood from its lore!

    So far i have made an RepeatingTask but i need real help!
    @aninsanellama maybe you can help me again?

    How do I get the ThirstyBlade item from the players inventory and then change its lore please help!

    Here is my code:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void onEnable(){
    3. getServer().getPluginManager().registerEvents(this, this);
    4.  
    5. saveDefaultConfig();
    6. if(getConfig().getBoolean("gettingthirsty") == true){
    7.  
    8. BukkitScheduler scheduler = getServer().getScheduler();
    9. scheduler.scheduleAsyncRepeatingTask(this, new Runnable(){
    10.  
    11. @Override
    12. public void run() {
    13. for(Player p : getServer().getOnlinePlayers()){
    14. //itemstack here
    15. ItemStack tb = new ItemStack(Material.IRON_SWORD);
    16.  
    17. if(p.getInventory().contains(tb) && tb.getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade")){
    18. //Player has the blade
    19.  
    20. //How do i get the slot ID of the blade!
    21. int tbid = tb.getData();
    22.  
    23.  
    24. List<String> lore = p.getInventory().getItem(tbid).getItemMeta().getLore();
    25. String loreLine = lore.get(3);
    26.  
    27. String splitted = loreLine.split(ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE)[1];
    28. splitted = splitted.split("/")[0];
    29.  
    30. int blood2 = Integer.parseInt(splitted) - getConfig().getInt("thirstvalue");
    31. if(blood2 <= 0){
    32. blood2 = 0;
    33. }
    34.  
    35. ItemMeta tb2Meta = p.getInventory().getItem(tbid).getItemMeta();
    36. tb2Meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thirsty Blade");
    37. tb2Meta.setLore(Arrays.asList(ChatColor.RESET + "" + ChatColor.GRAY +"Give that blade some blood",
    38. ChatColor.RESET + "" + ChatColor.GRAY +"and it will become stronger!",
    39. "",
    40. ChatColor.RESET + "" + ChatColor.GRAY +"Blood: " + ChatColor.WHITE + blood2 + "/100"));
    41.  
    42. p.getInventory().getItem(tbid).setItemMeta(tb2Meta);
    43.  
    44.  
    45.  
    46.  
    47.  
    48. }
    49. else{
    50.  
    51. }
    52. }
    53. }
    54.  
    55. }, 0L, getConfig().getInt("thirstdelay")*20L);
    56. }
    57. else{
    58.  
    59. }
    60. }
    61.  
     
  2. Offline

    Konkz

    Didn't play with inventories for long time now but you can just loop through the content of inventory and if items item meta is same change it?
     
  3. Offline

    fireblast709

    BaHeTo0 don't use async tasks with methods that aren't thread safe (which includes most of the API)
     
  4. Offline

    BaHeTo0

    I still need some example code please
     
  5. Offline

    Gater12

    BaHeTo0
    Loop through player's inventory -> Check if items is not null and if it's the ItemStack you want -> Change lore.
     
  6. Offline

    BaHeTo0

  7. Offline

    Necrodoom

    BaHeTo0 which part you don't know how to do?
     
  8. Offline

    Gater12

  9. Offline

    BaHeTo0

    Necrodoom how to create the loop that loops through inventory for(ItemStack i : p.getInventory .... ) something?
     
  10. Offline

    Necrodoom

  11. Offline

    BaHeTo0

Thread Status:
Not open for further replies.

Share This Page