SpongePowered / Sponge

The SpongeAPI implementation targeting vanilla Minecraft and 3rd party platforms.
MIT License
389 stars 211 forks source link

Setting shulkerbox to AIR causes item to drop and DropItemEvent doesn't get called #2635

Open VapidLinus opened 7 years ago

VapidLinus commented 7 years ago

Calling world.setBlockType(position, BlockTypes.AIR) on a shulkerbox block causes the shulkerbox to drop as an item. This is inconsistent as other blocks do not cause items to drop.

Trying to cancel this using DropItemEvent fails, because it never gets called during above scenario.

Code to reproduce:

@Listener
public void onTest(MessageChannelEvent.Chat event, @First Player player) {
    if (!event.getRawMessage().toPlain().equalsIgnoreCase("!test")) return;

    Vector3i blockPosition = player.getLocation().getBlockPosition().add(0, 3, 0);

    player.getWorld().setBlockType(blockPosition, BlockTypes.GREEN_SHULKER_BOX);

    // Destroy the shulkerbox next tick
    Task.builder()
            .delayTicks(1)
            .execute(() -> {
                player.getWorld().setBlockType(blockPosition, BlockTypes.AIR);
            })
            .submit(plugin);
}

@Listener
public void onItemDrop(DropItemEvent event) {
        // This never gets executed
    logger.info("Item dropped!");
}
  1. Press F5 to easily see what's going on above you
  2. Type !test in chat
  3. Shulkerbox gets placed and destroyed, causing a shulkerbox item to drop
  4. DropItemEvent doesn't get called

spongevanilla-1.12.2-7.0.0-BETA-346

VapidLinus commented 7 years ago

Quick note that the only workaround I've found to prevent the item from dropping is to use SpawnEntityEvent.

@Listener
public void onEntitySpawn(SpawnEntityEvent event) {
    for (Entity entity : event.getEntities()) {
        entity.get(Keys.REPRESENTED_ITEM).ifPresent(item -> {
            if (item.getType() == ItemTypes.GREEN_SHULKER_BOX) {
                event.setCancelled(true);
            }
        });
    }
}
phit commented 6 years ago

possibly fixed by https://github.com/SpongePowered/SpongeCommon/commit/29eabf9da8ac4bc16910f1ae96e23066695c5614

ryantheleach commented 6 years ago

@VapidLinus In future, please bring up issues with the implementation in SpongeCommon. SpongeAPI is reserved for design issues/bugs / feature planning. Can you test this with Faiths recent change?

Faithcaio commented 6 years ago

My fix was only for BlockSnapshot#restore (which is called by canceling events)