Search in sources :

Example 1 with RecordType

use of org.spongepowered.api.effect.sound.record.RecordType in project LanternServer by LanternPowered.

the class LanternJukebox method playRecord.

@Override
public void playRecord() {
    final ItemStack recordItem = this.inventory.getRawItemStack();
    if (recordItem == null) {
        return;
    }
    this.playing = true;
    final Location<World> location = getLocation();
    final RecordProperty property = recordItem.getProperty(RecordProperty.class).orElse(null);
    final RecordType recordType = property == null ? null : property.getValue();
    if (recordType != null) {
        location.getExtent().playRecord(location.getBlockPosition(), recordType);
    }
}
Also used : RecordType(org.spongepowered.api.effect.sound.record.RecordType) RecordProperty(org.spongepowered.api.data.property.item.RecordProperty) ItemStack(org.spongepowered.api.item.inventory.ItemStack) World(org.spongepowered.api.world.World)

Example 2 with RecordType

use of org.spongepowered.api.effect.sound.record.RecordType in project LanternServer by LanternPowered.

the class JukeboxInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
    final Optional<TileEntity> optTile = location.getTileEntity();
    if (optTile.isPresent()) {
        final TileEntity tile = optTile.get();
        if (tile instanceof Jukebox) {
            final LanternJukebox jukebox = (LanternJukebox) tile;
            final Optional<Entity> optEjectedItem = jukebox.ejectRecordItem();
            boolean success = false;
            if (optEjectedItem.isPresent()) {
                final Entity entity = optEjectedItem.get();
                entity.getWorld().spawnEntity(optEjectedItem.get());
                // TODO: Include the entity in the behavior context
                success = true;
            }
            final Optional<ItemStack> optItemStack = context.getContext(ContextKeys.USED_ITEM_STACK);
            if (optItemStack.isPresent()) {
                final ItemStack itemStack = optItemStack.get();
                final RecordProperty property = itemStack.getProperty(RecordProperty.class).orElse(null);
                final RecordType recordType = property == null ? null : property.getValue();
                if (recordType != null) {
                    final ItemStackSnapshot oldSnapshot = itemStack.createSnapshot();
                    itemStack.setQuantity(itemStack.getQuantity() - 1);
                    final ItemStackSnapshot newSnapshot = itemStack.createSnapshot();
                    context.getContext(ContextKeys.PLAYER).ifPresent(player -> {
                        if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
                            context.getContext(ContextKeys.USED_SLOT).ifPresent(slot -> context.addSlotChange(new SlotTransaction(slot, oldSnapshot, newSnapshot)));
                        }
                    });
                    itemStack.setQuantity(1);
                    jukebox.insertRecord(itemStack);
                    jukebox.playRecord();
                    success = true;
                }
            }
            if (success) {
                return BehaviorResult.SUCCESS;
            }
        }
    }
    return BehaviorResult.PASS;
}
Also used : LanternJukebox(org.lanternpowered.server.block.tile.vanilla.LanternJukebox) Jukebox(org.spongepowered.api.block.tileentity.Jukebox) Entity(org.spongepowered.api.entity.Entity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) World(org.spongepowered.api.world.World) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternJukebox(org.lanternpowered.server.block.tile.vanilla.LanternJukebox) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) RecordType(org.spongepowered.api.effect.sound.record.RecordType) RecordProperty(org.spongepowered.api.data.property.item.RecordProperty) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

RecordProperty (org.spongepowered.api.data.property.item.RecordProperty)2 RecordType (org.spongepowered.api.effect.sound.record.RecordType)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 World (org.spongepowered.api.world.World)2 LanternJukebox (org.lanternpowered.server.block.tile.vanilla.LanternJukebox)1 Jukebox (org.spongepowered.api.block.tileentity.Jukebox)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 Entity (org.spongepowered.api.entity.Entity)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1