Search in sources :

Example 46 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer 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)

Example 47 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HorizontalRotationPlacementBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final LanternPlayer player = (LanternPlayer) context.getContext(ContextKeys.PLAYER).orElse(null);
    // Get the direction the chest should face
    final Direction facing;
    if (player != null) {
        facing = player.getHorizontalDirection(Direction.Division.CARDINAL).getOpposite();
    } else {
        facing = Direction.NORTH;
    }
    context.transformBlockChanges((snapshot, builder) -> builder.add(Keys.DIRECTION, facing));
    return BehaviorResult.CONTINUE;
}
Also used : Direction(org.spongepowered.api.util.Direction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 48 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class RotationPlacementBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final LanternPlayer player = (LanternPlayer) context.getContext(ContextKeys.PLAYER).orElse(null);
    // Get the direction the chest should face
    final Direction facing;
    if (player != null) {
        if (player.getPosition().getY() - context.requireContext(ContextKeys.BLOCK_LOCATION).getBlockPosition().getY() >= 0.5) {
            facing = player.getDirection(Direction.Division.CARDINAL).getOpposite();
        } else {
            facing = player.getHorizontalDirection(Direction.Division.CARDINAL).getOpposite();
        }
    } else {
        facing = Direction.NORTH;
    }
    context.transformBlockChanges((snapshot, builder) -> builder.add(Keys.DIRECTION, facing));
    return BehaviorResult.CONTINUE;
}
Also used : Direction(org.spongepowered.api.util.Direction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)48 ArrayList (java.util.ArrayList)13 CauseStack (org.lanternpowered.server.event.CauseStack)12 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Nullable (javax.annotation.Nullable)9 Sponge (org.spongepowered.api.Sponge)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Lantern (org.lanternpowered.server.game.Lantern)8 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)8 Player (org.spongepowered.api.entity.living.player.Player)8 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)8 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)7 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)7 ClientContainer (org.lanternpowered.server.inventory.client.ClientContainer)7