Search in sources :

Example 31 with LanternPlayer

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

the class HandlerPlayInSignBook method handle.

@Override
public void handle(NetworkContext context, MessagePlayInSignBook message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final AbstractSlot slot = player.getInventory().getHotbar().getSelectedSlot();
    ItemStack itemStack = slot.peek().orElse(null);
    if (itemStack != null && itemStack.getType() == ItemTypes.WRITABLE_BOOK) {
        final ItemStack itemStack1 = new LanternItemStack(ItemTypes.WRITTEN_BOOK);
        itemStack.getValues().forEach(itemStack1::offer);
        itemStack1.offer(Keys.BOOK_PAGES, message.getPages().stream().map(Text::of).collect(Collectors.toList()));
        itemStack1.offer(Keys.BOOK_AUTHOR, Text.of(message.getAuthor()));
        itemStack1.offer(Keys.DISPLAY_NAME, Text.of(message.getTitle()));
        slot.set(itemStack1);
    }
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 32 with LanternPlayer

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

the class HandlerPlayInSwapHandItems method handle.

@Override
public void handle(NetworkContext context, MessagePlayInSwapHandItems message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final LanternPlayerInventory inventory = player.getInventory();
    final AbstractSlot hotbarSlot = inventory.getHotbar().getSelectedSlot();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    final ItemStackSnapshot hotbarItem = hotbarSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
    final ItemStackSnapshot offHandItem = offHandSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
    final List<SlotTransaction> transactions = new ArrayList<>();
    transactions.add(new SlotTransaction(hotbarSlot, hotbarItem, offHandItem));
    transactions.add(new SlotTransaction(offHandSlot, offHandItem, hotbarItem));
    try (CauseStack.Frame frame = CauseStack.current().pushCauseFrame()) {
        frame.addContext(EventContextKeys.PLAYER, player);
        frame.pushCause(player);
        final ChangeInventoryEvent.SwapHand event = SpongeEventFactory.createChangeInventoryEventSwapHand(frame.getCurrentCause(), inventory, transactions);
        Sponge.getEventManager().post(event);
        if (!event.isCancelled()) {
            transactions.stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
            final PlayerInventoryContainer inventoryContainer = context.getSession().getPlayer().getInventoryContainer();
            inventoryContainer.getClientContainer().queueSilentSlotChange(hotbarSlot);
        }
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) LanternPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) PlayerInventoryContainer(org.lanternpowered.server.inventory.PlayerInventoryContainer) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 33 with LanternPlayer

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

the class HandlerPlayInUseEntityAttack method handle.

@Override
public void handle(NetworkContext context, MessagePlayInUseEntity.Attack message) {
    final LanternPlayer player = context.getSession().getPlayer();
    player.getWorld().getEntityProtocolManager().playerAttack(player, message.getEntityId());
}
Also used : LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 34 with LanternPlayer

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

the class HandlerPlayInEditBook method handle.

@Override
public void handle(NetworkContext context, MessagePlayInEditBook message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final AbstractSlot slot = player.getInventory().getHotbar().getSelectedSlot();
    final ItemStack itemStack = slot.peek().orElse(null);
    if (itemStack != null && itemStack.getType() == ItemTypes.WRITABLE_BOOK) {
        itemStack.offer(Keys.BOOK_PAGES, message.getPages().stream().map(Text::of).collect(Collectors.toList()));
        slot.set(itemStack);
    }
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 35 with LanternPlayer

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

the class HandlerPlayInPlayerAbilities method handle.

@Override
public void handle(NetworkContext context, MessagePlayInPlayerAbilities message) {
    final boolean flying = message.isFlying();
    final LanternPlayer player = context.getSession().getPlayer();
    if (!flying || player.get(Keys.CAN_FLY).orElse(false)) {
        player.offer(Keys.IS_FLYING, flying);
    } else {
        // TODO: Just set velocity once it's implemented
        if (player.get(LanternKeys.SUPER_STEVE).orElse(false)) {
            context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), 0, 1.0, 0));
            player.offer(LanternKeys.IS_ELYTRA_FLYING, true);
        } else if (player.get(LanternKeys.CAN_WALL_JUMP).orElse(false)) {
            final Location<World> location = player.getLocation();
            // Get the horizontal direction the player is looking
            final Direction direction = player.getHorizontalDirection(Direction.Division.CARDINAL);
            // Get the block location the player may step against
            final Location<World> location1 = location.add(direction.asOffset().mul(0.6, 0, 0.6));
            SolidSideProperty solidSideProperty = location1.getExtent().getProperty(location1.getBlockPosition(), direction.getOpposite(), SolidSideProperty.class).orElse(null);
            // noinspection ConstantConditions
            if (solidSideProperty != null && solidSideProperty.getValue()) {
                // Push the player a bit back in the other direction,
                // to give a more realistic feeling when pushing off
                // against a wall
                final Vector3d pushBack = direction.asBlockOffset().toDouble().mul(-0.1);
                // Push the player up
                context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), pushBack.getX(), 0.8, pushBack.getZ()));
            } else {
                // Now we try if the player can jump away from the wall
                // Get the block location the player may step against
                final Location<World> location2 = location.add(direction.asOffset().mul(-0.6, 0, -0.6));
                solidSideProperty = location2.getExtent().getProperty(location2.getBlockPosition(), direction, SolidSideProperty.class).orElse(null);
                // noinspection ConstantConditions
                if (solidSideProperty != null && solidSideProperty.getValue()) {
                    // Combine the vectors in the direction of the block face
                    // and the direction the player is looking
                    final Vector3d vector = direction.asBlockOffset().toDouble().mul(0.25).mul(1, 0, 1).add(0, 0.65, 0).add(player.getDirectionVector().mul(0.4, 0.25, 0.4));
                    // Push the player forward and up
                    context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), vector.getX(), vector.getY(), vector.getZ()));
                }
            }
        }
        player.triggerEvent(RefreshAbilitiesPlayerEvent.of());
    }
}
Also used : SolidSideProperty(org.lanternpowered.server.block.property.SolidSideProperty) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayOutEntityVelocity(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity) Direction(org.spongepowered.api.util.Direction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Location(org.spongepowered.api.world.Location)

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