Search in sources :

Example 6 with AbstractInventory

use of org.lanternpowered.server.inventory.AbstractInventory in project LanternServer by LanternPowered.

the class EnderChestInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final LanternPlayer player = (LanternPlayer) context.getContext(ContextKeys.PLAYER).orElse(null);
    if (player == null) {
        return BehaviorResult.CONTINUE;
    }
    final List<Runnable> tasks = new ArrayList<>();
    if (!ChestInteractionBehavior.validateOpenableChestSpace(context, context.requireContext(ContextKeys.BLOCK_LOCATION), tasks)) {
        return BehaviorResult.CONTINUE;
    }
    final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
    final AbstractInventory inventory = player.getEnderChestInventory();
    final Optional<TileEntity> optTileEntity = location.getTileEntity();
    if (optTileEntity.isPresent() && optTileEntity.get() instanceof InventoryViewerListener) {
        inventory.addViewListener((InventoryViewerListener) optTileEntity.get());
    }
    if (!player.openInventory(inventory).isPresent()) {
        tasks.forEach(Runnable::run);
        return BehaviorResult.CONTINUE;
    }
    return BehaviorResult.SUCCESS;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) AbstractInventory(org.lanternpowered.server.inventory.AbstractInventory) InventoryViewerListener(org.lanternpowered.server.inventory.InventoryViewerListener) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)6 ArrayList (java.util.ArrayList)5 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)4 World (org.spongepowered.api.world.World)4 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 Streams (com.google.common.collect.Streams)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3 CauseStack (org.lanternpowered.server.event.CauseStack)3 LanternEventHelper (org.lanternpowered.server.event.LanternEventHelper)3 Lantern (org.lanternpowered.server.game.Lantern)3 AbstractInventorySlot (org.lanternpowered.server.inventory.AbstractInventorySlot)3 AbstractOrderedInventory (org.lanternpowered.server.inventory.AbstractOrderedInventory)3 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)3 IInventory (org.lanternpowered.server.inventory.IInventory)3 LanternContainer (org.lanternpowered.server.inventory.LanternContainer)3 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)3 LanternItemStackSnapshot (org.lanternpowered.server.inventory.LanternItemStackSnapshot)3