Creating Two of the Same Items

Discussion in 'Spigot Plugin Development' started by bmac20, May 3, 2017.

  1. Hello, I'm creating a market plugin where if someone types /market create, it takes the item from their hand and puts it on the market. This is the code I am having an issue with. I have two variables, fakeItem and actualItem. The fakeItem is what goes on the market, and the actual item is the actual item. I mapped both of these two together, but the ItemMeta is set on the item in the hand. So when a player types /market create, the item lore in the players hand changes to what it should be on the market. How can I make it so that the "fake item" has the lore placed on it, but the actual item the player has does not?

    Code (Text):
    ArrayList<String> itemLore = new ArrayList<String>();
                        itemLore.add(
                                ChatColor.RESET.toString() + ChatColor.BLUE + "Seller: " + ChatColor.YELLOW + p.getName());
                        itemLore.add(ChatColor.RESET.toString() + ChatColor.BLUE + "Price: " + ChatColor.YELLOW + "$"
                                + Integer.parseInt(args[2]) + ".00");

                        MainMarket.takeMoney(p, Integer.parseInt(args[1]));

                        ItemStack actualItem = p.getItemInHand();
                        ItemStack fakeItem = p.getItemInHand();
                        ItemMeta meta = fakeItem.getItemMeta();
                        meta.setLore(itemLore);
                        fakeItem.setItemMeta(meta);

                        MainMarket.addItem(p, fakeItem, Integer.parseInt(args[1]));

                        items.put(actualItem, fakeItem);
     
  2. Choco

    Moderator

    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html#clone() ?

    Also, Arrays.asList("value1", "value2"); to construct a List rather than creating a list and adding to it
    Code (Java):
    List<String> lore = Arrays.asList(
                            ChatColor.RESET.toString() + ChatColor.BLUE + "Seller: " + ChatColor.YELLOW + p.getName(),
                            ChatColor.RESET.toString() + ChatColor.BLUE + "Price: " + ChatColor.YELLOW + "$" + Integer.parseInt(args[2]) + ".00"
                        );
     
    • Like Like x 1
  3. New problem. Whenever they click the item to be purchased, it gives me an error saying item cannot be null because it's a cloned item.
     
  4. Hey although I would be glad to help you, I suggest you show some of the code so we as the spigot community can see what goes wrong where. You dont have to upload you whole file but some code inwhich you take the item etc. would be nice. Btw why are you exactly copying the item?? Is there any specific reason for that? ~luc