Solved Count uses on item apply to all items

Discussion in 'Plugin Development' started by w0lves, Oct 22, 2020.

  1. Offline

    w0lves

    Hi! Im new at making plugins, i've been developing a few ones but im trying to do one and i have one issue that I can't fix.

    Also, as i'm new i now that my code could be more efficient / clean, so if anyone has any tip about how to make it better i would be greatful to read your suggests!

    Plugin version: 1.8.8

    Problem: I have done a stick to work as a "count tool" (just to recreate the problem im having on my real plugin). This stick has an amount of uses, in this case: 100.

    When i use the stick, the uses decrease by 1, but when i take another stick that have another amount of uses, it continue the cont of the other stick.

    How count tool works: https://gyazo.com/66ab975eac49599b7c5c4a7cfcdb20df

    The issue: https://gyazo.com/c6c2d75c8161ec90c7963c9ee40f623e

    Code:

    Main
    Code:
    package me.wolves.count;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.wolves.count.commands.CountCommand;
    import me.wolves.count.listeners.CountListener;
    
    public class Main extends JavaPlugin{
    
    
        @Override
        public void onEnable() {
            saveDefaultConfig();
    
            new CountCommand(this);
            new CountListener(this);
        }
      
    }
    
    CountCommand
    Code:
    package me.wolves.count.commands;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import me.wolves.count.Main;
    import me.wolves.count.utils.Utils;
    
    public class CountCommand implements CommandExecutor{
    
        private Main plugin;
      
        public CountCommand(Main plugin) {
            this.plugin = plugin;
    
            plugin.getCommand("count").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (!(sender instanceof Player)) {
                return true;
            }
          
            Player p = (Player) sender;
          
            try  {
                if (args.length == 2) {                                                
                    if (args[0] != null && args[0].equals("give")) {                   
                        if (args[1] != null && Bukkit.getPlayerExact(args[1]) != null) {
                            Player Player = Bukkit.getPlayerExact(args[1]);            
                          
                            List<String> LoreTool = plugin.getConfig().getStringList("count-tool.lore");
                            List<String> FinalLoreTool = new ArrayList<>();
                            for (String LoreString : LoreTool) {
                                LoreString = LoreString.replace("<uses>", "100");
                                FinalLoreTool.add(LoreString);
                            }
    
                            ItemStack CountTool = Utils.createItem(280, 1, plugin.getConfig().getString("count-tool.title"), FinalLoreTool);
    
                            Player.getInventory().addItem(CountTool);
                            return true;
                        }
                        else {
                            p.sendMessage(Utils.chat("&This user is not logged."));
                        }
                    } else {
                        p.sendMessage(Utils.chat("&c/count give nombre"));
                    }
                } else {
                    p.sendMessage(Utils.chat("&c/count give nombre"));
                }
              
            } catch(NumberFormatException ex) {
                p.sendMessage(Utils.chat("&c/count give nombre"));
            }
            return false;
          
        }
      
    }
    
    CountListener
    Code:
    package me.wolves.count.listeners;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.wolves.count.Main;
    import me.wolves.count.utils.Utils;
    
    public class CountListener implements Listener{
    
        private Main plugin;
        private int contUses;
      
      
        public CountListener(Main plugin) {
            this.plugin = plugin;
            contUses = 100;
          
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void PlayerInteract(PlayerInteractEvent  event) {
            if (event.getItem() != null) {
                if (event.getItem().getItemMeta().getDisplayName() != null) {
                    if (event.getItem().getItemMeta().getDisplayName().equals(Utils.chat(plugin.getConfig().getString("count-tool.title")))) {
                        contUses--;
                      
                        Player player = event.getPlayer();
                      
                        if (contUses == 0) {
                            //ItemStack item = event.getItem();
                            //item.setType(Material.AIR);
                            event.getPlayer().setItemInHand(new ItemStack(Material.AIR));
                            player.sendMessage(Utils.chat(plugin.getConfig().getString("count-tool.break")));
                        } else {
                            List<String> LoreBucket = plugin.getConfig().getStringList("count-tool.lore");
                            List<String> FinalLoreBucket = new ArrayList<>();
                            for (String LoreString : LoreBucket) {
                                LoreString = LoreString.replace("<uses>", Integer.toString(contUses));
                                FinalLoreBucket.add(LoreString);
                            }
    
                            ItemStack item = event.getItem();
                            ItemMeta meta = item.getItemMeta();
                            meta.setDisplayName(Utils.chat(plugin.getConfig().getString("count-tool.title")));
                            meta.setLore(Utils.chat(FinalLoreBucket));
                            item.setItemMeta(meta);
                            player.sendMessage(Utils.chat(plugin.getConfig().getString("count-tool.message").replace("<uses>", Integer.toString(contUses))));
                        }
                      
                    }
                }
            }
        }
    }
    
    Possible problematic line(s): I don't really know

    What you can do to recreate the problem:
    Previous gyazo: https://gyazo.com/c6c2d75c8161ec90c7963c9ee40f623e

    The FULL Error log: No console errors

    What you have tried:
    I don't know what can i try :/
     
  2. Offline

    timtower Moderator Moderator

    @w0lves Don't give a new item, modify the used item instead.
     
  3. Offline

    w0lves

    Modify the used item instead of giving a new one? I don't know if i dont understand it or i wrongly expressed myself haha.

    Mmm, i will have this item for example in a shop, so people can but it.

    The problem is that if player A has the tool at 24 uses, and player B buy a new tool (100 uses), when player B use it, it doesn't start counting from 100, it starts from 24.

    It's like all the "tools" use the same count when it should be independent (PlayerA tool with his own count at 24 and PlayerB tool with his count at 100)
     
  4. Offline

    timtower Moderator Moderator

    @w0lves Okay, misread there, my bad, was looking at the wrong place.
    Have you considered storing the amount of uses on the item itself instead of global? (which is your issue at the moment)
     
    w0lves likes this.
  5. Offline

    w0lves

    Yes, thats what i want to do but i don't know how... i started making plugins yesterday and i dont really know all the methods that the bukkit api has xd
     
    Last edited: Oct 22, 2020
  6. Offline

    timtower Moderator Moderator

    @w0lves If I would do this then I would look for the durability lore on the stick.
    Take the number out of it, lower by 1, put the number back.
     
    w0lves likes this.
  7. Offline

    w0lves

    Yeah, that is!

    Now i've my item working but another problem appear when i have set the durability, it looks like a random block (i don know how to call it xd

    [​IMG]

    Im pasting my code again so u can see how it's now, basically i just set the durability at 100.

    Show Spoiler
    Code:
    package me.wolves.count.utils;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Utils {
    
        public static String chat (String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }
      
        public static List<String> chat (List<String> s) {
          
            List<String> message = new ArrayList<>();
          
            for (String line : s) {
                message.add(ChatColor.translateAlternateColorCodes('&', line));
            }
          
            return message;
        }
      
      
        public static ItemStack createItem(int materialId, int amount, String displayName, String... loreString) {
          
            @SuppressWarnings("deprecation")
            ItemStack item = new ItemStack(Material.getMaterial(materialId), amount);
          
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Utils.chat(displayName));
            List<String> lore = new ArrayList<String>();
            for(String s : loreString) {
                lore.add(Utils.chat(s));
            }
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.values());
            item.setItemMeta(meta);
            item.setDurability((short) 100);
          
            return item;
        }
    
        public static ItemStack createItem(int materialId, int amount, String displayName, List<String> loreString) {
          
            @SuppressWarnings("deprecation")
            ItemStack item = new ItemStack(Material.getMaterial(materialId), amount);
          
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Utils.chat(displayName));
            List<String> lore = new ArrayList<String>();
            for(String s : loreString) {
                lore.add(Utils.chat(s));
            }
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.values());
            item.setItemMeta(meta);
            item.setDurability((short) 100);
          
            return item;
        }
    }
    
     
  8. Offline

    timtower Moderator Moderator

    @w0lves Might be due to the durability change.
     
    w0lves likes this.
  9. Offline

    w0lves

    Yes, it was. I dont how to make it look "like a real stick" so instead of changing the durability, i'm changing one enchantment (starting at level 100 and taking it one level every time i use it).

    If anyone want the code to check how it is, im gonna post down here.

    Thank u @timtower !

    Show Spoiler

    Code:
    package me.wolves.count.utils;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Utils {
    
        public static String chat (String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }
       
        public static List<String> chat (List<String> s) {
           
            List<String> message = new ArrayList<>();
           
            for (String line : s) {
                message.add(ChatColor.translateAlternateColorCodes('&', line));
            }
           
            return message;
        }
       
       
        public static ItemStack createItem(int materialId, int amount, String displayName, String... loreString) {
           
            @SuppressWarnings("deprecation")
            ItemStack item = new ItemStack(Material.getMaterial(materialId), amount);
           
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Utils.chat(displayName));
            List<String> lore = new ArrayList<String>();
            for(String s : loreString) {
                lore.add(Utils.chat(s));
            }
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.values());
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            item.setItemMeta(meta);
           
            return item;
        }
    
        public static ItemStack createItem(int materialId, int amount, String displayName, List<String> loreString) {
           
            @SuppressWarnings("deprecation")
            ItemStack item = new ItemStack(Material.getMaterial(materialId), amount);
           
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Utils.chat(displayName));
            List<String> lore = new ArrayList<String>();
            for(String s : loreString) {
                lore.add(Utils.chat(s));
            }
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.values());
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addEnchant(Enchantment.DURABILITY, 100, true);
            item.setItemMeta(meta);
           
            return item;
        }
    }
    


    Show Spoiler

    Code:
    package me.wolves.count.listeners;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.wolves.count.Main;
    import me.wolves.count.utils.Utils;
    
    public class CountListener implements Listener{
    
        private Main plugin;
       
       
        public CountListener(Main plugin) {
            this.plugin = plugin;
           
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void PlayerInteract(PlayerInteractEvent  event) {
            if (event.getItem() != null) {
                if (event.getItem().getItemMeta().getDisplayName() != null) {
                    if (event.getItem().getItemMeta().getDisplayName().equals(Utils.chat(plugin.getConfig().getString("count-tool.title")))) {
                       
                       
                        Player player = event.getPlayer();
                        ItemStack item = event.getItem();
                        ItemMeta meta = item.getItemMeta();
                        meta.addEnchant(Enchantment.DURABILITY, meta.getEnchantLevel(Enchantment.DURABILITY)-1, true);
                       
                        if (meta.getEnchantLevel(Enchantment.DURABILITY) == 0) {
                            event.getPlayer().setItemInHand(new ItemStack(Material.AIR));
                            player.sendMessage(Utils.chat(plugin.getConfig().getString("count-tool.break")));
                        } else {
                            List<String> LoreBucket = plugin.getConfig().getStringList("count-tool.lore");
                            List<String> FinalLoreBucket = new ArrayList<>();
                            for (String LoreString : LoreBucket) {
                                LoreString = LoreString.replace("<uses>", Integer.toString(meta.getEnchantLevel(Enchantment.DURABILITY)));
                                FinalLoreBucket.add(LoreString);
                            }
    
                            meta.setDisplayName(Utils.chat(plugin.getConfig().getString("count-tool.title")));
                            meta.setLore(Utils.chat(FinalLoreBucket));
                            item.setItemMeta(meta);
                            player.sendMessage(Utils.chat(plugin.getConfig().getString("count-tool.message").replace("<uses>", Integer.toString(meta.getEnchantLevel(Enchantment.DURABILITY)))));
                        }
                       
                    }
                }
            }
        }
    }
    
     
    timtower likes this.
  10. Offline

    timtower Moderator Moderator

    @w0lves Nice solution you got there!
    Clever way of doing it.
     

Share This Page