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);
}
}
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);
}
}
}
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());
}
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);
}
}
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());
}
}
Aggregations