Search in sources :

Example 1 with Jukebox

use of org.spongepowered.api.block.tileentity.Jukebox in project SpongeCommon by SpongePowered.

the class JukeboxDataProcessor method remove.

private boolean remove(BlockJukebox.TileEntityJukebox jukebox) {
    ((Jukebox) jukebox).ejectRecord();
    IBlockState block = jukebox.getWorld().getBlockState(jukebox.getPos());
    return block.getBlock() instanceof BlockJukebox && !block.getValue(BlockJukebox.HAS_RECORD);
}
Also used : TileEntityJukebox(net.minecraft.block.BlockJukebox.TileEntityJukebox) Jukebox(org.spongepowered.api.block.tileentity.Jukebox) BlockJukebox(net.minecraft.block.BlockJukebox) IBlockState(net.minecraft.block.state.IBlockState) BlockJukebox(net.minecraft.block.BlockJukebox)

Example 2 with Jukebox

use of org.spongepowered.api.block.tileentity.Jukebox in project SpongeCommon by SpongePowered.

the class JukeboxDataProcessor method set.

@Override
protected boolean set(BlockJukebox.TileEntityJukebox jukebox, ItemStackSnapshot stackSnapshot) {
    IBlockState block = jukebox.getWorld().getBlockState(jukebox.getPos());
    if (stackSnapshot == ItemStackSnapshot.NONE) {
        if (jukebox.getRecord() == null) {
            return true;
        }
        return remove(jukebox);
    }
    if (!(stackSnapshot.getType() instanceof ItemRecord)) {
        return false;
    }
    ((Jukebox) jukebox).insertRecord(stackSnapshot.createStack());
    block = jukebox.getWorld().getBlockState(jukebox.getPos());
    return block.getBlock() instanceof BlockJukebox && block.getValue(BlockJukebox.HAS_RECORD);
}
Also used : TileEntityJukebox(net.minecraft.block.BlockJukebox.TileEntityJukebox) Jukebox(org.spongepowered.api.block.tileentity.Jukebox) BlockJukebox(net.minecraft.block.BlockJukebox) IBlockState(net.minecraft.block.state.IBlockState) BlockJukebox(net.minecraft.block.BlockJukebox) ItemRecord(net.minecraft.item.ItemRecord)

Example 3 with Jukebox

use of org.spongepowered.api.block.tileentity.Jukebox 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

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