Drop an anvil and then change it into air when it lands?

Discussion in 'Spigot Plugin Development' started by TheMrJezza, Jun 29, 2015.

  1. I have made a plugin that drops an anvil on a command. Now I would like to go a step further and get rid of the anvil when it lands. But I can't work out how. Does anyone know how to change anvils to air when they land, only if they were dropped by the use of the command?
     
  2. Well, the falling anvil is a falling sand entity, so i would assume, you could somehow check to see if it has landed or add it to an array list, and then work out how long it will take to land based on maths or something.
     
  3. @Camoleopard Well, how would I add it to an array?

    I use
    Code (Text):
    Material anvil = Material.ANVIL
    and then I just use 'anvil' to spawn the anvil.
    but if I do list.add(anvil); it just adds an anvil to the list and not the anvil I dropped. Do you understand what I am saying? How can I get the anvil that was dropped?

    Edit: Also right now, this is what I use to get when the anvil lands:
    Code (Text):
    @EventHandler
        public void onBlockFall(EntityChangeBlockEvent event) {
            if (list.contains(event.getEntity())) {
                event.getBlock().getLocation().getWorld()
                        .getBlockAt(event.getEntity().getLocation().add(0, 0, 0))
                        .setType(Material.AIR);
                return;
            }
        }
     
  4. Code (Text):
    event.getBlock().getLocation().getWorld()
                        .getBlockAt(event.getEntity().getLocation().add(0, 0, 0))
                        .setType(Material.AIR);
    Delay this by one Tick

    And also, why is this necessary?
    Code (Text):
    getLocation().add(0, 0, 0)
     
  5. Code (Text):
    getLocation().add(0, 0, 0)
    @Shawlaf that was when I had different code, I was a bit too lazy to remove it.
     
    • Funny Funny x 1
  6. Well, without testing this myself, I think you are on the right path, my method of using an array list is probably not the best, but if I did, it would probably be like
    Code (Text):
     ArrayList<Entity> falling = new ArrayList<Entity>()
    and in the event
    Code (Text):
     if (!falling.contains(event.getEntity()){
    return true;
    }
    else{
    event.getBlock().getLocation().getWorld().setType(Material.AIR);
                       
    }
     
    This method may work, without testing I don't know!
     
  7. The event picks up entities, but the anvil is a material.. do you see my problem?
     
  8. @TheMrJezza
    Did you not read what I wrote?
    I succesfully managed to remove the block by delaying the setType() method by 1 Tick using a BukkitRunnable

    Code (Text):
    @EventHandler
        public void onEntityChangeBlockEvent(final EntityChangeBlockEvent e) {
        if (e.getEntity() instanceof FallingBlock) {
            FallingBlock fallingBlock = (FallingBlock) e.getEntity();

            if (fallingBlock.getBlockId() == Material.ANVIL.getId()) {
            new BukkitRunnable() {
                @Override
                public void run() {
                e.getEntity().getLocation().getBlock().setType(Material.AIR);
                }
            }.runTaskLater(this, 1L);
            }
        }
        }
     
  9. @Shawlaf Sorry, I didn't see it before, thank you.

    Edit: I saw the bit about the add() thing... I was just tired.