Setting MobSpawner Attributes on an Item

I’m dropping a Mob Spawner somewhat like so:

// ...

Optional<Entity> oItem = location.getExtent().createEntity(EntityTypes.ITEM, location.getPosition());

if (oItem.isPresent()) {
	Item item = (Item) oItem.get();
	item.offer(Keys.REPRESENTED_ITEM, ItemStack.of(ItemTypes.MOB_SPAWNER, 1).createSnapshot());

	location.getExtent().spawnEntity(item, cause);

// ...

This is working as is. However; how would I change the entities to spawn from this Mob Spawner? Can it be set to the Item?

You must obtain a spawner BlockState, and then use Keys.ITEM_BLOCKSTATE.

Doesn’t seem to change it. Using

item.offer(Keys.ITEM_BLOCKSTATE, block.getState());

Gained using

BlockSnapshot block = trans.getOriginal();

In a ChangeBlockEvent.Break event.

In fact, that is the wrong way to represent this. BlockState does not store the complex data that a spawner can - this data is contained within a BlockSnapshot. So you’ll want to build your snapshot, adding the data you want (eg Keys.SPAWNER_NEXT_ENTITY_TO_SPAWN for the EntityType) .

In writing this, I realise though… I can’t seem to find a data manipulator for representing a BlockSnapshot on an item… My guess it’s that it’s because BlockSnapshot inherently has a location in the world. I expect that it would be made a part of the upcoming schematic API which also includes “archetypes” for entities such as this.

@gabizou, am I correct?

A block snapshot not currently placed in the world can still be a blocksnapshot.

means that the location might not be set.

Was feeling really confident about this - got me excited for a moment!

However, just gave it a go…





Were not present on the existing Mob Spawner (java.util.NoSuchElementException: No value present) using 1.8.9-4.2.0-BETA-336

Here was my use method:

	public Item alterItem(BlockSnapshot block, Item item) {
		if (item.getItemType() != ItemTypes.MOB_SPAWNER) return item;
		item.offer(Keys.SPAWNER_ENTITIES, block.get(Keys.SPAWNER_ENTITIES).get());
		item.offer(Keys.SPAWNER_NEXT_ENTITY_TO_SPAWN, block.get(Keys.SPAWNER_NEXT_ENTITY_TO_SPAWN).get());
		return item;

Yep, the implementation of MobSpawnerData isn’t in Sponge yet anyway, I’m still waiting on it to be added here:

So basically at the moment you’re down on two counts… :confused:

Oh my… life goes on :stuck_out_tongue: !

I will keep the code commented out for Mob Spawners until that is implemented. If there is anything I can do to assist, let me know. I’ll be sure to subscribe to that thread on GitHub.

You’re partially correct, the point of the MobSpawnerData is to change the tile entity information without having to rely on going from one DataHolder to an ImmutableDataHolder and then finally reaching the point where you can interact with data.