Solved Get last item launched from dispenser.

Discussion in 'Plugin Development' started by plobnob, Jul 30, 2015.

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

    plobnob

    I am having problems with a ProjectileLaunchEvent (firing bottles of enchanting). My code for the event:

    Code:
        @EventHandler
        public void projectileLaunchEvent(ProjectileLaunchEvent event)
        {
            if(event.getEntity().getShooter() instanceof Player)
            {
                if(event.getEntityType().equals(EntityType.THROWN_EXP_BOTTLE))
                {
                    Player player = (Player) event.getEntity().getShooter();
                    if(player.getItemInHand().getItemMeta().hasLore())
                    {
                        ItemStack heldItem = new ItemStack(player.getItemInHand().getType());
                        heldItem.setItemMeta(player.getItemInHand().getItemMeta());
                        if(player.hasPermission("exp.use"))
                        {
                            bottles.add((player.getItemInHand()));
                            bottlesUUID.add(event.getEntity().getUniqueId());
                        }
                        else
                        {
                            String cantUse = getConfig().getString("NoPermissionThrow");
                            player.sendMessage(configureMessage(cantUse));
                            event.setCancelled(true);
                            player.getInventory().addItem(heldItem);
                        }
                    }
                }
            }
            else if(event.getEntity().getShooter() instanceof CraftBlockProjectileSource)
            {
                if(event.getEntityType().equals(EntityType.THROWN_EXP_BOTTLE))
                {
                    CraftBlockProjectileSource ps = (CraftBlockProjectileSource) event.getEntity().getShooter();
                    Block sourceBlock = ps.getBlock();
                    if (sourceBlock.getType().equals(Material.DISPENSER))
                    {
                        Dispenser dispenser = (Dispenser) sourceBlock;
                       
                    }
                }
            }
        }
    This triggers and allows me to get the dispenser fine. However, I am unaware of a way to work out the ItemStack launched from the dispenser. I can't find a method to get this from the entity launched, but since I can get the inventory, I am hoping there might be a way.

    Thanks, Plob.
     
  2. I'm pretty sure you need the BlockDispenseEvent
     
    plobnob likes this.
  3. Offline

    plobnob

    @FisheyLP
    Thanks, wasn't sure if an event existed for that, but it seems to work fine. It looks like the BlockDispenseEvent doesn't have a getEntity() method, only a getItem() method, so I have kept the old method to get the UUID from the Entity and stored the item, and it seems to be working fine! Only problem I have now is one that has arisen with the exp calculations, but that shouldn't be too hard to solve. Will mark this thread as solved.

    Plob.
     
Thread Status:
Not open for further replies.

Share This Page