How to change settings for Experience Orb

Discussion in 'Plugin Development' started by GRENKA, Jan 15, 2022 at 1:29 AM.

  1. Offline

    GRENKA

    blockBroken.getWorld().spawnEntity(blockBroken.getLocation(), EntityType.EXPERIENCE_ORB);
    how to assign the amount of experience that will give experience orb

    and

    ItemMeta diamondsItemMeta = diamonds.getItemMeta();
    diamondsItemMeta.setDisplayName(ChatColor.DARK_GRAY + "Lucky Diamond");

    I can't name the diamond here.

    Code:
    package com.grenka.doubledrops;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class BreakBlock implements Listener {
        @EventHandler
        public void onPlayerBreakBlock(BlockBreakEvent event) {
            Block blockBroken = event.getBlock();
            Player player = event.getPlayer();
            Inventory inventory = event.getPlayer().getInventory();
            ItemStack itemStack = player.getItemInHand();
    
    
            if (blockBroken.getType() == Material.DIAMOND_ORE && player.getGameMode() == GameMode.SURVIVAL && player.getInventory().getItemInMainHand().getType() == Material.STONE_PICKAXE) {
                double d = Math.random() * 2;
                event.setCancelled(true);
                blockBroken.setType(Material.AIR);
                ItemStack diamonds = new ItemStack(Material.DIAMOND, (int) d);
                ItemMeta diamondsItemMeta = diamonds.getItemMeta();
                diamondsItemMeta.setDisplayName(ChatColor.DARK_GRAY + "Lucky Diamond");
    
                blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), diamonds);
    
                blockBroken.getWorld().spawnEntity(blockBroken.getLocation(), EntityType.EXPERIENCE_ORB);
            }
        }
    }
    
     

    Attached Files:

  2. Offline

    KarimAKL

    #spawnEntity returns the entity spawned. You can assign that to a variable and modify it.
    Code:Java
    1. ExperienceOrb orb = (ExperienceOrb) world.spawnEntity(location, EntityType.EXPERIENCE_ORB);
    2. orb.setExperience(amountOfExperience);


    #getItemMeta returns a copy of the ItemMeta, so you need to assign the modified instance to the item.
    Code:Java
    1. ItemMeta meta = item.getItemMeta();
    2. meta.setDisplayName("Name");
    3. item.setItemMeta(meta);
     

Share This Page