Solved Showing custom name of a dropped item

Discussion in 'Spigot Plugin Development' started by YunusGG, Jun 11, 2018.

  1. Hey, I coded a money drop plugin and I want to show drop's name like this "Gold x2". Help pls.

    Code (Java):
        @EventHandler
        public void moneyEvent(EntityDeathEvent event) {
            int r = (int) (Math.random() * 100);
            ItemStack gold = new ItemStack(Material.GOLD_NUGGET);
           
            if(r < 50) {
                gold.setAmount(1);
            }
            else if(r < 90) {
                gold.setAmount(2);
            }
            else if(r < 96) {
                gold.setAmount(3);
            }
            else if(r < 99) {
                gold.setAmount(5);
            }
            else if(r < 100) {
                gold.setAmount(10);
            }
           
            ItemMeta goldmeta = gold.getItemMeta();
            goldmeta.setDisplayName("money");
            gold.setItemMeta(goldmeta);
            event.getDrops().add(gold);
        }
     
    • Optimistic Optimistic x 1
  2. Code (Text):
    gold.setCustomNameVisible(true);
    Here's how I do it. Ignore the data values on the item, I use a texture pack to have actual money (coins and dollars) :p
     
    • Useful Useful x 1
  3. same process you just need to change the event to EntityDeathEvent then get the drop object then change the content itemstacks
     
    • Optimistic Optimistic x 1
  4. Not same process u cannot cast an itemstack to entity.

    Oh I changed code like ur code and
    Code (Java):
        @EventHandler
        public void onPara(EntityDeathEvent event) {
            int r = (int) (Math.random() * 100);
            ItemStack gold = new ItemStack(Material.GOLD_NUGGET);
           
            if(r < 50) {
                gold.setAmount(1);
            }
            else if(r < 90) {
                gold.setAmount(2);
            }
            else if(r < 96) {
                gold.setAmount(3);
            }
            else if(r < 99) {
                gold.setAmount(5);
            }
            else if(r < 100) {
                gold.setAmount(10);
            }
           
            ItemMeta goldmeta = gold.getItemMeta();
            goldmeta.setDisplayName("money");
            gold.setItemMeta(goldmeta);
            Item item = event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), gold);
            item.setCustomName("ยง6Gold x" + altin.getAmount());
            item.setCustomNameVisible(true);
        }
    and it works! Thanks there! Ur very helpful.
     
    • Agree Agree x 1
  5. Sorry for the misunderstanding I thought you were getting the items being drop by the entity

    I get it now that your creating a new item stack then dropping it it on the entities location but still same solution as the link I sent you

    .setCustomNameVisible(true);

    Good that you found that answer