Search in sources :

Example 1 with LanternPlayer

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

the class OpenableContainerInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
    final Optional<TileEntity> optTileEntity = location.getTileEntity();
    if (!optTileEntity.isPresent()) {
        return BehaviorResult.CONTINUE;
    }
    final List<Runnable> tasks = new ArrayList<>();
    if (!validateOpenableSpace(context, location, tasks)) {
        return BehaviorResult.CONTINUE;
    }
    final Optional<Inventory> optInventory = getInventoryFrom(optTileEntity.get());
    if (!optInventory.isPresent()) {
        return BehaviorResult.CONTINUE;
    }
    final LanternPlayer player = (LanternPlayer) context.getContext(ContextKeys.PLAYER).orElse(null);
    if (player == null) {
        return BehaviorResult.CONTINUE;
    }
    if (player.openInventory(optInventory.get()).isPresent()) {
        tasks.forEach(Runnable::run);
        return BehaviorResult.SUCCESS;
    }
    return BehaviorResult.CONTINUE;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) Inventory(org.spongepowered.api.item.inventory.Inventory) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 2 with LanternPlayer

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

the class LanternScoreCriterionProgress method set.

public Optional<Instant> set(int score, Runnable invalidator) {
    checkArgument(score >= 0, "Score to set may not be negative");
    checkArgument(score <= getGoal(), "Score to set may not be greater than the goal");
    if (score == this.score) {
        return get();
    }
    final Cause cause = CauseStack.current().getCurrentCause();
    final Advancement advancement = this.progress.getAdvancement();
    final LanternPlayer player = this.progress.getPlayer();
    CriterionEvent.Score.Change event;
    if (score == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, this.criterion, player, Instant.now(), score, this.score);
    } else if (this.score == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, this.criterion, player, score, this.score);
    } else {
        event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, this.criterion, player, score, this.score);
    }
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return get();
    }
    this.score = score;
    if (event instanceof CriterionEvent.Grant) {
        this.achievingTime = ((CriterionEvent.Grant) event).getTime();
        detachTrigger();
    } else if (event instanceof CriterionEvent.Revoke) {
        this.achievingTime = null;
        attachTrigger();
    }
    invalidator.run();
    return Optional.ofNullable(this.achievingTime);
}
Also used : CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) Cause(org.spongepowered.api.event.cause.Cause) Advancement(org.spongepowered.api.advancement.Advancement) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 3 with LanternPlayer

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

the class LanternTrigger method trigger.

@SuppressWarnings("unchecked")
@Override
public void trigger(Player player) {
    final LanternPlayerAdvancements playerAdvancements = ((LanternPlayer) player).getAdvancementsProgress();
    final Collection<AbstractCriterionProgress> collection = this.progress.get(playerAdvancements);
    if (!collection.isEmpty()) {
        final Cause cause = CauseStack.current().getCurrentCause();
        for (AbstractCriterionProgress progress : new ArrayList<>(this.progress.get(playerAdvancements))) {
            final Advancement advancement = progress.getAdvancementProgress().getAdvancement();
            final AdvancementCriterion criterion = progress.getCriterion();
            final FilteredTrigger filteredTrigger = criterion.getTrigger().get();
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, criterion, this.configTypeToken, player, filteredTrigger, this.eventHandler != null);
            if (this.eventHandler != null) {
                this.eventHandler.accept(event);
            }
            Sponge.getEventManager().post(event);
            if (event.getResult()) {
                if (progress instanceof ScoreCriterionProgress) {
                    ((ScoreCriterionProgress) progress).add(1);
                } else {
                    progress.grant();
                }
            }
        }
    }
}
Also used : ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) LanternPlayerAdvancements(org.lanternpowered.server.advancement.LanternPlayerAdvancements) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) Advancement(org.spongepowered.api.advancement.Advancement) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 4 with LanternPlayer

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

the class CommandStopSound method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.player(Text.of("player")), GenericArguments.optional(GenericArguments.catalogedElement(Text.of("category"), SoundCategory.class)), GenericArguments.optional(GenericArguments.catalogedElement(Text.of("sound"), SoundType.class))).executor((src, args) -> {
        SoundCategory category = args.<SoundCategory>getOne("category").orElse(null);
        String type = args.<SoundType>getOne("sound").map(CatalogType::getId).orElse(null);
        LanternPlayer player = args.<LanternPlayer>getOne("player").get();
        player.getConnection().send(new MessagePlayOutStopSounds(type, category));
        return CommandResult.success();
    });
}
Also used : SoundType(org.spongepowered.api.effect.sound.SoundType) MessagePlayOutStopSounds(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutStopSounds) SoundCategory(org.spongepowered.api.effect.sound.SoundCategory) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 5 with LanternPlayer

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

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