Solved Lore multiplying when I give myself the item.

Discussion in 'Spigot Plugin Development' started by allstar2005, Mar 17, 2019 at 1:46 PM.

  1. Hello, So my issue is that when I give my self the custom item I created the lore multiplies by one if anyone knows why this is happening please let me know. Thanks!!!

    items class:
    Code (Text):
    package com.allstar2005.MoItems;

    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class items implements Listener {
        ArrayList<String> Ralore = new ArrayList<>();
     
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            //Array Lists
         
            Ralore.add(ChatColor.RED + "Right click to");
            Ralore.add(ChatColor.RED + "fly through the sky!!!");
         

            if (e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (p.getItemInHand().getType() == Material.ARROW && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Rush Arrow") && p.getItemInHand().getItemMeta().getLore().equals(Ralore)) {
                    Arrow aw = (Arrow) p.launchProjectile(Arrow.class);
                    aw.setPassenger(p);
                    aw.setVelocity(p.getLocation().getDirection().multiply(3.5));
                   p.setItemInHand(new ItemStack(Material.AIR));
                   if (p.isInsideVehicle()) {
                        p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,20 ,100));
                    }
               
                 
                }
            }
         
        }

    }
    MoItemsCMD class:
    Code (Text):
    package com.allstar2005.MoItems;

    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class MoItemsCMD implements CommandExecutor {
        ArrayList<String> Ralore = new ArrayList<>();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            // ArrayList start
            //items.add(ChatColor.GREEN + "");

         
            Ralore.add(ChatColor.RED + "Right click to");
            Ralore.add(ChatColor.RED + "fly through the sky!!!");
         
         
         
            // ArrayList end
         
         
            Player p = (Player) sender;
            if (sender instanceof Player) {
                if (p.hasPermission("moitems.command") || p.hasPermission("moitems.*")) {
                 
                    if (args.length == 0) {
                        p.sendMessage(ChatColor.RED + "/moitems <give> <Item name.>");
                        p.sendMessage(ChatColor.RED + "/moitems <list>");
                        return true;    
                    }
                 
                    // Items
                    ItemStack Ra = new ItemStack(Material.ARROW);
                    ItemMeta Ram = Ra.getItemMeta();
                    Ram.setDisplayName(ChatColor.AQUA + "Rush Arrow");
                    Ram.setLore(Ralore);
                    Ram.addEnchant(Enchantment.PROTECTION_FALL, 1, true);
                 
                    Ra.setItemMeta(Ram);
                 
                    if (args[0].equalsIgnoreCase("give")) {
                        if (args.length == 1) {
                            p.sendMessage(ChatColor.RED + "Please enter the name of an item!!!");
                            }
                     
                        if (args[1].equalsIgnoreCase("rideable_arrow")) {
                            p.getInventory().addItem(Ra);
                            p.updateInventory();
                        }
                     
                     
                     
                     
                     
                     
                    } else if (args[0].equalsIgnoreCase("list")) {
                        p.sendMessage(ChatColor.GOLD + "Rideable_Arrow");
                     
                    } else {
                        p.sendMessage(ChatColor.RED + "Invaled arguments!!!");
                    }
                 
                } else {
                    p.sendMessage(ChatColor.RED + "You lack the permission <moitems.command>!!!");
                 
                }
             
            } else {
                System.out.println("Only players may use this command!!!");
            }
     
         
            return false;
        }
     

     
     
     
     
     

    }
     
     
    #1 allstar2005, Mar 17, 2019 at 1:46 PM
    Last edited: Mar 17, 2019 at 10:25 PM
  2. AsyncJay

    AsyncJay Previously Shockzeh

    You're using an instance field so you're just adding to the same list every time you execute the command or interact. You're also creating the item every time the command is executed, and even sometimes when it's unnecessary. What I'd do is define the item once and initialize it, setting its meta including your lore (you can do all this locally within a method, perhaps in your onEnable), and then access it through a getter. You can then also use ItemStack#isSimilar to compare your items in your interact listener.
     
  3. Hey AsyncJay,

    Would you mind giving me an example I'm fairly new to java and not sure how to make a getter using the spigot api. Thanks
     
  4. AsyncJay

    AsyncJay Previously Shockzeh

    They're not part of the Spigot API. Getters and setters are a basic concept of encapsulation and OOP, you might want to read up on them here, alongside more Java and OOP concepts before diving into the API.
     
  5. I know getters and setters aren't part of the spigot api I'm just not sure how to use it with an item stack I created the getter and setter, and tried what I thought might work but it did the same thing.
     
  6. AsyncJay

    AsyncJay Previously Shockzeh

    OK, I'll give you an example which you can adjust to your needs:

    Code (Java):
    private ItemStack customSword;

    @Override
    public void onEnable() {
        this.customSword = new ItemStack(Material.DIAMOND_SWORD);
     
        ItemMeta meta = this.customSword.getItemMeta();
        meta.setLore(Arrays.asList(ChatColor.RED + "Line 1", ChatColor.AQUA + "Line 2"));
        // Set display name, etc.

        this.customSword.setItemMeta(meta);
    }

    public ItemStack getCustomSword() {
        return this.customSword;
    }
     
    #6 AsyncJay, Mar 17, 2019 at 10:09 PM
    Last edited: Mar 17, 2019 at 10:23 PM
  7. Thanks :)
     
    • Like Like x 1

Share This Page