Transfer dropped item to nearby special hopper

Discussion in 'Plugin Development' started by Hex_27, Mar 5, 2015.

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

    Hex_27

    This is what I got so far:
    Code:
        @EventHandler
        public void onKingFisherPlace(BlockPlaceEvent event){
            if(event.getItemInHand().getItemMeta().getDisplayName() != null){
                if(event.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Kingfisher Attraction Beam")){
                   
                    Block kingfisher = event.getBlock();
                    event.getPlayer().sendMessage(ChatColor.GREEN + "Kingfisher Attraction Beam module placed.");
                  
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
                            public void run() {
                                World world = kingfisher.getWorld();
                                int squared = 4;
                                Entity pigmark = world.spawnEntity(kingfisher.getLocation(), EntityType.PIG);
                                pigmark.remove();
                                Location loc = kingfisher.getLocation();
                                    for(Entity e : pigmark.getNearbyEntities(2, 2, 2)) {
                                        if(e.getType() == EntityType.DROPPED_ITEM) {
                                            if(e.getLocation().distanceSquared(loc) <= squared) {
                                               ItemStack is = ((ItemStack) e).clone();
                                               ((InventoryHolder) kingfisher).getInventory().addItem(is);
                                               e.remove();
                                              
                                               }
                                        }
                                    }
                               
                            }
                        }, 1, 1);
               
                }
            }
        }
    
       @EventHandler
        public void onPlayerMove(PlayerInteractEvent event){
            if(event.getClickedBlock() != null){
                event.getPlayer().sendMessage(ChatColor.GREEN + "1");
                if(event.getClickedBlock() instanceof Hopper){
                    event.getPlayer().sendMessage(ChatColor.RED + "1");
                    Hopper kingfisher = (Hopper) event.getClickedBlock();
                    if(kingfisher.getInventory().getName().equalsIgnoreCase(ChatColor.BLUE + "Kingfisher Attraction Beam")){
                       
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
                            public void run() {
                                World world = kingfisher.getWorld();
                                int squared = 4;
                                Entity pigmark = world.spawnEntity(kingfisher.getLocation(), EntityType.PIG);
                                pigmark.remove();
                                Location loc = kingfisher.getLocation();
                                    for(Entity e : pigmark.getNearbyEntities(2, 2, 2)) {
                                        if(e.getType() == EntityType.DROPPED_ITEM) {
                                            if(e.getLocation().distanceSquared(loc) <= squared) {
                                               ItemStack is = ((ItemStack) e).clone();
                                               kingfisher.getInventory().addItem(is);
                                               e.remove();
                                              
                                               }
                                        }
                                    }
                               
                            }
                        }, 1, 1);
                        event.getPlayer().sendMessage(ChatColor.GREEN + "Hopper reactivated.");
                       
                       
                    }
                }
            }
           
        }
    First off, the pig "kills" itself, but there would be this glitchy image of it still there.
    And the block can't be casted to a hopper. Items with that displayname are hoppers (Stuff in my plugin..)
    And the "nearby" entity when close by the hopper can't be casted to itemstack.
     
  2. Offline

    pie_flavor

    @Hex_27 Hopper does not extend Block, it extends BlockState. Use (Hopper) block.getState();
    Second, there are more than one entity. Try EnderPearl, it'll hit the ground and destroy itself.
    Third, ItemStack is an inventory item representation, while Item extends Entity. Cast your entity to Item, then use #getItemStack().
     
Thread Status:
Not open for further replies.

Share This Page