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