Search in sources :

Example 1 with DispenseItemBehavior

use of net.minecraft.core.dispenser.DispenseItemBehavior in project SpongeCommon by SpongePowered.

the class DispenserSourceLogic method launch.

@SuppressWarnings("unchecked")
private <P extends Projectile> Optional<P> launch(final DispenserBlockEntity dispenser, final EntityType<P> projectileType, final Item item) {
    final DispenseItemBehavior behavior = DispenserBlockAccessor.accessor$DISPENSER_REGISTRY().get(item);
    final ServerLevel world = (ServerLevel) dispenser.getLevel();
    behavior.dispense(new BlockSourceImpl(world, dispenser.getBlockPos()), new ItemStack(item));
    final List<Entity> entities = world.getEntities((net.minecraft.world.entity.EntityType<?>) projectileType, entity -> true);
    if (entities.isEmpty()) {
        return Optional.empty();
    }
    // Hack - get the projectile that was spawned from dispense()
    return Optional.of((P) entities.get(entities.size() - 1));
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockSourceImpl(net.minecraft.core.BlockSourceImpl) Entity(net.minecraft.world.entity.Entity) DispenserBlockEntity(net.minecraft.world.level.block.entity.DispenserBlockEntity) ItemStack(net.minecraft.world.item.ItemStack) DispenseItemBehavior(net.minecraft.core.dispenser.DispenseItemBehavior)

Aggregations

BlockSourceImpl (net.minecraft.core.BlockSourceImpl)1 DispenseItemBehavior (net.minecraft.core.dispenser.DispenseItemBehavior)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 Entity (net.minecraft.world.entity.Entity)1 ItemStack (net.minecraft.world.item.ItemStack)1 DispenserBlockEntity (net.minecraft.world.level.block.entity.DispenserBlockEntity)1