Search in sources :

Example 1 with InventoryViewerListener

use of org.lanternpowered.server.inventory.InventoryViewerListener 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

ArrayList (java.util.ArrayList)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)1 InventoryViewerListener (org.lanternpowered.server.inventory.InventoryViewerListener)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 World (org.spongepowered.api.world.World)1