Drop/Place enchanted beacon, remember when pickup

Discussion in 'Plugin Development' started by A5000, Apr 22, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    A5000

    Hello,
    I am currently writing a Bukkit Plugin (1.8) which deals with enchanted beacons.
    I want that the player can drop enchanted beacons and pick them up (when destroyed).
    If the player picks up an 'enchanted beacon' there should be an enchanted beacon and not a normal beacon in the players inventory.

    I have no problem with creating an enchanted beacon in the player's inventory.
    Code:
    ItemStack enchantedBeacon = new ItemStack(Material.BEACON, 1);
    ItemMeta metaData = enchantedBeacon.getItemMeta();
    metaData.addEnchant(Enchantment.DURABILITY, 1, true);
    enchantedBeacon.setItemMeta(metaData);
    
    If the player drops an enchanted beacon and it is placed in the world, I will be informed by the event onBlockPlace and save the information that the block is based on an enchanted beacon (and not on a normal beacon).
    Code:
    @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
    
        Block block = event.getBlockPlaced();
        ItemStack stack = event.getItemInHand();
    
        if (block.getType() == Material.BEACON && stack.hasItemMeta()) {
        ItemMeta metaData = stack.getItemMeta();
        if (metaData.hasEnchant(...
       
        //block... Save somehow that this Block is based on an enchanted beacon
    

    Now my problem:

    If the block (marked as an enchanted beacon) is destroyed and spawned as itemstack in the world then of course the beacon is no longer enchanted.

    If I want to convert it back into an enchanted beacon, I have to know whether the original block was marked as enchanted or not.

    So, when the spawned itemstack is picked up by a player, how can I find out that it results from an enchanted beacon block?

    Code:
    @EventHandler
        public void onPickup(PlayerPickupItemEvent event) {
        Item item = event.getItem();
        ItemStack stack = item.getItemStack();
        Material material = stack.getType();
    
        if (material == Material.BEACON) {
            //How can I find out that stack results from an 'enchanted beacon block' ?
    
     
    Last edited: Apr 22, 2020
  2. Offline

    NukerFall

    use setMetaData() on block.
    and on break use getMetaData() from block
    So u would get some keys, which u can transform with some brain to new enchantment from string
     
Thread Status:
Not open for further replies.

Share This Page