Search in sources :

Example 1 with AbstractSlot

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;
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) ArrayList(java.util.ArrayList) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 2 with AbstractSlot

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"));
    }
}
Also used : LanternPlayerAdvancements(org.lanternpowered.server.advancement.LanternPlayerAdvancements) LanternPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) ValueCollection(org.lanternpowered.server.data.ValueCollection) SoundCategory(org.spongepowered.api.effect.sound.SoundCategory) BookView(org.spongepowered.api.text.BookView) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BlockRegistryModule(org.lanternpowered.server.game.registry.type.block.BlockRegistryModule) VanillaInventoryArchetypes(org.lanternpowered.server.inventory.vanilla.VanillaInventoryArchetypes) GameProfile(org.spongepowered.api.profile.GameProfile) LanternSoundType(org.lanternpowered.server.effect.sound.LanternSoundType) AdvancementProgress(org.spongepowered.api.advancement.AdvancementProgress) MessagePlayOutParticleEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutParticleEffect) EnumSet(java.util.EnumSet) GlobalTabListEntry(org.lanternpowered.server.entity.living.player.tab.GlobalTabListEntry) MessagePlayOutSetReducedDebug(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetReducedDebug) LanternBossBar(org.lanternpowered.server.boss.LanternBossBar) Location(org.spongepowered.api.world.Location) AbstractViewer(org.lanternpowered.server.effect.AbstractViewer) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) LanternWeatherUniverse(org.lanternpowered.server.world.LanternWeatherUniverse) DataContainer(org.spongepowered.api.data.DataContainer) Set(java.util.Set) WorldBorder(org.spongepowered.api.world.WorldBorder) BlockState(org.spongepowered.api.block.BlockState) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) World(org.spongepowered.api.world.World) MessagePlayOutRecord(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRecord) HandTypes(org.spongepowered.api.data.type.HandTypes) AbstractOrderedInventory(org.lanternpowered.server.inventory.AbstractOrderedInventory) Player(org.spongepowered.api.entity.living.player.Player) RespawnPlayerEvent(org.spongepowered.api.event.entity.living.humanoid.player.RespawnPlayerEvent) MessagePlayInOutBrand(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutBrand) ChangeWorldBorderEvent(org.spongepowered.api.event.world.ChangeWorldBorderEvent) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) ItemTypes(org.spongepowered.api.item.ItemTypes) GuiIdProperty(org.spongepowered.api.item.inventory.property.GuiIdProperty) Vector3d(com.flowpowered.math.vector.Vector3d) LanternCooldownTracker(org.lanternpowered.server.item.LanternCooldownTracker) AABB(org.spongepowered.api.util.AABB) LanternDifficulty(org.lanternpowered.server.world.difficulty.LanternDifficulty) ArrayList(java.util.ArrayList) Vector2i(com.flowpowered.math.vector.Vector2i) SpectateEntityEvent(org.lanternpowered.server.entity.event.SpectateEntityEvent) LanternTabListEntryBuilder(org.lanternpowered.server.entity.living.player.tab.LanternTabListEntryBuilder) LanternTitles(org.lanternpowered.server.text.title.LanternTitles) LanternTabList(org.lanternpowered.server.entity.living.player.tab.LanternTabList) CauseStack(org.lanternpowered.server.event.CauseStack) Text(org.spongepowered.api.text.Text) PlayerInventoryContainer(org.lanternpowered.server.inventory.PlayerInventoryContainer) LanternWorld(org.lanternpowered.server.world.LanternWorld) GlobalTabList(org.lanternpowered.server.entity.living.player.tab.GlobalTabList) LanternTabListEntry(org.lanternpowered.server.entity.living.player.tab.LanternTabListEntry) DimensionTypes(org.spongepowered.api.world.DimensionTypes) Nullable(javax.annotation.Nullable) MessagePlayOutPlayerRespawn(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerRespawn) MessagePlayOutStopSounds(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutStopSounds) WrittenBookItemTypeObjectSerializer(org.lanternpowered.server.data.io.store.item.WrittenBookItemTypeObjectSerializer) MessagePlayOutBlockChange(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockChange) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MessagePlayOutOpenBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutOpenBook) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) Entity(org.spongepowered.api.entity.Entity) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) DataView(org.spongepowered.api.data.DataView) MessagePlayOutUnlockRecipes(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutUnlockRecipes) Vector3i(com.flowpowered.math.vector.Vector3i) ChatVisibilities(org.spongepowered.api.text.chat.ChatVisibilities) ChatVisibility(org.spongepowered.api.text.chat.ChatVisibility) MessageEvent(org.spongepowered.api.event.message.MessageEvent) AdvancementTree(org.spongepowered.api.advancement.AdvancementTree) BanService(org.spongepowered.api.service.ban.BanService) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) NetworkIdHolder(org.lanternpowered.server.network.entity.NetworkIdHolder) ChunkTicketManager(org.spongepowered.api.world.ChunkTicketManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Ban(org.spongepowered.api.util.ban.Ban) ChatType(org.spongepowered.api.text.chat.ChatType) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) UserStorageService(org.spongepowered.api.service.user.UserStorageService) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) ElementKeyRegistration(org.lanternpowered.server.data.element.ElementKeyRegistration) MessagePlayOutSetWindowSlot(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetWindowSlot) Locale(java.util.Locale) RuleTypes(org.lanternpowered.server.world.rules.RuleTypes) Title(org.spongepowered.api.text.title.Title) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) SoundType(org.spongepowered.api.effect.sound.SoundType) Collection(java.util.Collection) ChunkLoadingTicket(org.lanternpowered.server.world.chunk.ChunkLoadingTicket) Sponge(org.spongepowered.api.Sponge) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) Sets(com.google.common.collect.Sets) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) Lantern(org.lanternpowered.server.game.Lantern) Transform(org.spongepowered.api.entity.Transform) MessagePlayOutPlayerJoinGame(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerJoinGame) RelativePositions(org.spongepowered.api.util.RelativePositions) Optional(java.util.Optional) RecordType(org.spongepowered.api.effect.sound.record.RecordType) MessagePlayOutPlayerPositionAndLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutPlayerPositionAndLook) HarvestEntityEvent(org.spongepowered.api.event.entity.HarvestEntityEvent) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) LanternChatType(org.lanternpowered.server.text.chat.LanternChatType) Inventory(org.spongepowered.api.item.inventory.Inventory) ChatTypes(org.spongepowered.api.text.chat.ChatTypes) Keys(org.spongepowered.api.data.key.Keys) PlayerContainerSession(org.lanternpowered.server.inventory.PlayerContainerSession) HashSet(java.util.HashSet) LanternContainer(org.lanternpowered.server.inventory.LanternContainer) NetworkSession(org.lanternpowered.server.network.NetworkSession) SkinPart(org.spongepowered.api.data.type.SkinPart) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) Container(org.spongepowered.api.item.inventory.Container) MessagePlayOutSelectAdvancementTree(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSelectAdvancementTree) LanternDimensionType(org.lanternpowered.server.world.dimension.LanternDimensionType) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Advancement(org.spongepowered.api.advancement.Advancement) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) Collections(java.util.Collections) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) PlayerInventoryShiftClickBehavior(org.lanternpowered.server.inventory.vanilla.PlayerInventoryShiftClickBehavior) LanternWorldBorder(org.lanternpowered.server.world.LanternWorldBorder) HarvestEntityEvent(org.spongepowered.api.event.entity.HarvestEntityEvent) ArrayList(java.util.ArrayList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) BanService(org.spongepowered.api.service.ban.BanService)

Example 3 with AbstractSlot

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());
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 4 with AbstractSlot

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);
                }
            }
        }
    }
}
Also used : MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) HandType(org.spongepowered.api.data.type.HandType) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 5 with AbstractSlot

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);
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) EmptyInventory(org.spongepowered.api.item.inventory.EmptyInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) IEquipmentInventory(org.lanternpowered.server.inventory.IEquipmentInventory) EmptyInventory(org.spongepowered.api.item.inventory.EmptyInventory) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Aggregations

AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)21 ItemStack (org.spongepowered.api.item.inventory.ItemStack)15 CauseStack (org.lanternpowered.server.event.CauseStack)12 ArrayList (java.util.ArrayList)10 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)10 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Optional (java.util.Optional)8 Nullable (javax.annotation.Nullable)8 Lantern (org.lanternpowered.server.game.Lantern)8 LanternWorld (org.lanternpowered.server.world.LanternWorld)8 Keys (org.spongepowered.api.data.key.Keys)8 EventContextKeys (org.spongepowered.api.event.cause.EventContextKeys)8 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)8 World (org.spongepowered.api.world.World)8 LanternItemStackSnapshot (org.lanternpowered.server.inventory.LanternItemStackSnapshot)7 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)7 ClientSlot (org.lanternpowered.server.inventory.client.ClientSlot)7 LanternPlayerInventory (org.lanternpowered.server.inventory.vanilla.LanternPlayerInventory)7 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)7