use of org.lanternpowered.server.inventory.AbstractSlot in project LanternServer by LanternPowered.
the class ArmorQuickEquipInteractionBehavior method tryInteract.
@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
final LanternPlayer player = (LanternPlayer) context.requireContext(ContextKeys.PLAYER);
final ItemStack itemStack = context.requireContext(ContextKeys.USED_ITEM_STACK);
final PeekedOfferTransactionResult peekResult = player.getInventory().getEquipment().peekOffer(itemStack.copy());
if (peekResult.isSuccess()) {
final List<SlotTransaction> transactions = new ArrayList<>(peekResult.getTransactions());
final AbstractSlot slot = (AbstractSlot) context.getContext(ContextKeys.USED_SLOT).orElse(null);
if (slot != null) {
transactions.add(new SlotTransaction(slot, itemStack.createSnapshot(), LanternItemStack.toSnapshot(peekResult.getRejectedItem().orElse(null))));
}
final ChangeInventoryEvent.Equipment event = SpongeEventFactory.createChangeInventoryEventEquipment(context.getCurrentCause(), player.getInventory(), transactions);
if (event.isCancelled()) {
return BehaviorResult.CONTINUE;
}
event.getTransactions().stream().filter(Transaction::isValid).forEach(slotTransaction -> slotTransaction.getSlot().set(slotTransaction.getFinal().createStack()));
return BehaviorResult.SUCCESS;
}
return BehaviorResult.CONTINUE;
}
use of org.lanternpowered.server.inventory.AbstractSlot in project LanternServer by LanternPowered.
the class LanternPlayer method handleDeath.
@Override
protected void handleDeath(CauseStack causeStack) {
// Call the harvest event
final boolean keepsInventory = getWorld().getOrCreateRule(RuleTypes.KEEP_INVENTORY).getValue();
final int exp = keepsInventory ? 0 : Math.min(100, get(Keys.EXPERIENCE_LEVEL).orElse(0) * 7);
// Humanoids get their own sub-interface for the event
final HarvestEntityEvent.TargetPlayer harvestEvent = SpongeEventFactory.createHarvestEntityEventTargetPlayer(causeStack.getCurrentCause(), exp, exp, this, keepsInventory, keepsInventory, 0);
Sponge.getEventManager().post(harvestEvent);
if (!harvestEvent.isCancelled()) {
final List<ItemStackSnapshot> drops = new ArrayList<>();
if (!harvestEvent.keepsInventory()) {
// Make a copy of all the items in the players inventory, and put them in the drops
getInventory().<AbstractSlot>slots().forEach(slot -> slot.peek().ifPresent(itemStack -> drops.add(LanternItemStackSnapshot.wrap(itemStack))));
}
if (!harvestEvent.keepsLevel()) {
offer(Keys.EXPERIENCE_LEVEL, harvestEvent.getLevel());
}
// Finalize the harvest event
finalizeHarvestEvent(causeStack, harvestEvent, drops);
}
// Ban the player if the world is hardcode
if (getWorld().getProperties().isHardcore()) {
final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
// Add a permanent ban
banService.addBan(Ban.of(getProfile(), t("gameMode.hardcore.banMessage")));
// Bye, bye!
kick(t("deathScreen.title.hardcore"));
}
}
use of org.lanternpowered.server.inventory.AbstractSlot in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method handleFinishItemInteraction.
public void handleFinishItemInteraction(MessagePlayInOutFinishUsingItem message) {
final Optional<HandType> activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty());
// The player is already interacting
if (!activeHand.isPresent() || this.activeHandStartTime == -1L) {
return;
}
// Try the action of the hotbar item first
final AbstractSlot slot = activeHand.get() == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
final ItemStack rawItemStack = slot.getRawItemStack();
if (rawItemStack == null) {
return;
}
// Require a minimum amount of ticks for the interaction to succeed
final MinimumUseDurationProperty property = rawItemStack.getProperty(MinimumUseDurationProperty.class).orElse(null);
if (property != null) {
final long time = LanternGame.currentTimeTicks();
if (time - this.activeHandStartTime < property.getValue()) {
resetItemUseTime();
return;
}
}
handleFinishItemInteraction0(slot, activeHand.get());
}
use of org.lanternpowered.server.inventory.AbstractSlot in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method pulse.
/**
* Pulses the interaction handler.
*/
void pulse() {
if (this.diggingBlock != null) {
final int breakState = (int) Math.round(((double) Math.max(0, this.diggingEndTime - System.nanoTime()) / (double) this.diggingDuration) * 10.0);
if (this.lastBreakState != breakState) {
sendBreakUpdate(breakState);
this.lastBreakState = breakState;
}
}
final HandType activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty()).orElse(null);
final AbstractSlot slot = activeHand == null ? null : activeHand == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
// The interaction just started
if (!Objects.equals(activeHand, this.lastActiveHand)) {
this.lastActiveHand = activeHand;
this.lastActiveItemStack = slot == null ? null : slot.getRawItemStack();
} else if (activeHand != null) {
if (this.activeHandStartTime == -1L) {
this.activeHandStartTime = LanternGame.currentTimeTicks();
}
final ItemStack itemStack = slot.getRawItemStack();
if (itemStack == null || this.lastActiveItemStack != itemStack) {
// Stop the interaction
resetItemUseTime();
} else {
final MaximumUseDurationProperty property = itemStack.getProperty(MaximumUseDurationProperty.class).orElse(null);
if (property != null) {
// Check if the interaction reached it's max time
final long time = LanternGame.currentTimeTicks();
if (time - this.activeHandStartTime > property.getValue()) {
handleFinishItemInteraction0(slot, activeHand);
}
}
}
}
}
use of org.lanternpowered.server.inventory.AbstractSlot in project LanternServer by LanternPowered.
the class AbstractEquipable method equip.
@Override
default boolean equip(EquipmentType type, @Nullable ItemStack equipment) {
checkNotNull(type, "type");
final Inventory inventory = getInventory().query(Holder.EQUIPMENT_INVENTORY_OPERATION);
if (inventory instanceof EmptyInventory) {
return false;
}
final AbstractSlot slot = (AbstractSlot) inventory.<IEquipmentInventory>first().getSlot(type).orElse(null);
if (slot == null) {
return false;
}
final InventoryTransactionResult result = slot.set(equipment);
return result.getType().equals(InventoryTransactionResult.Type.SUCCESS);
}
Aggregations