Changing drops for block break

Hey Sponge forums! (my first post here!)

I’m pretty new to SpongeAPI, however I have couple years experience from the Spigot world. I’ve been trying to change the drop on block break, however I just can’t figure it out.

Couple things I’ve tried (after asking for help on discord):

  • ChangeBlockEvent.Break: can’t change drop there
  • DropItemEvent.Destruct: doesn’t trigger on block break
  • DropItemEvent.Pre: doesn’t trigger on block break
  • SpawnEntityEvent: doesn’t trigger on block break

I’ve already spent couple of hours trying to figure this out so any help is appreciated :heart:

Tassu

1 Like

Try just

ItemDropEvent

From there (it is gets triggered) then try upcasting it when you know it works.
If it doesnt get triggered then put a issue on the spongeapi github page.

One of the great things about sponge is that you can listen for combined events and everything works fine. Another great thing is that you can report issues

Try just ItemDropEvent

Doesn’t trigger.

Another great thing is that you can report issues.

Not marked as done. Just opened this post to see if there were any workarounds.

I know its not the best idea in the world but you could listen for a BlockBreakEvent then a tick later check for all items that have been spawned near the location of the block and then change the item

This line says otherwise, I’m fairly sure that block drops are cancellable for block changes.

Regarding the code here from the implementation, you can see that item drops are captured and processed based on the block snapshot. Granted, this is an event thrown after the change block event, because we don’t actually process anything until the event has been thrown and determined to actually go through.

You can then modify the entities spawned for the event with the provided lists from the event.

DropItemEvent.Destruct is the correct event. Perhaps you are using an incorrect cause filter?

First off, sorry for a late response. I’ve been away for a few days.

Second, it does not trigger with this code:

as seen by this log; no DropItemEvent.Destruct is triggered.

I found a solution to our problem, but I do not know how true it is.

@Listener
public final void onBreakBlock(ChangeBlockEvent.Break event) {
	Objects.requireNonNull(event, "event");
	
	event.getTransactions().forEach(transaction -> {
		Location<World> blockLocation = transaction.getOriginal().getLocation().get();
		Vector3i blockPosition = blockLocation.getBlockPosition();
		
		blockLocation.getExtent().getEntities(entity -> {
			Vector3i entityBlockPosition = entity.getLocation().getBlockPosition();
			
			return entityBlockPosition.equals(blockPosition);
		}).forEach(entity -> {
			Location<World> entityLocation = entity.getLocation();
			
			entity.remove();
			
			Item itemEntity = (Item) entityLocation.createEntity(EntityTypes.ITEM);
			
			itemEntity.offer(Keys.REPRESENTED_ITEM, ItemTypes.APPLE.getTemplate());
			entityLocation.spawnEntity(itemEntity);
		});
	});
}

But there is one problem. That only works on 96 build. @gabizou what happens to the platform? Constantly everything breaks down…