use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method createEnchantmentModifiers.
public static Optional<List<DamageFunction>> createEnchantmentModifiers(final LivingEntity living, final DamageSource damageSource) {
if (damageSource.isBypassMagic()) {
return Optional.empty();
}
final Iterable<net.minecraft.world.item.ItemStack> inventory = living.getArmorSlots();
final int damageProtection = EnchantmentHelper.getDamageProtection(inventory, damageSource);
if (damageProtection <= 0) {
return Optional.empty();
}
final List<DamageFunction> modifiers = new ArrayList<>();
final DoubleUnaryOperator enchantmentFunction = incomingDamage -> -(incomingDamage - CombatRules.getDamageAfterMagicAbsorb((float) incomingDamage, damageProtection));
try (final CauseStackManager.StackFrame frame = ((Server) living.getServer()).causeStackManager().pushCauseFrame()) {
frame.pushCause(living);
final DamageModifier enchantmentModifier = DamageModifier.builder().cause(frame.currentCause()).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
}
return Optional.of(modifiers);
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method createAbsorptionModifier.
public static Optional<DamageFunction> createAbsorptionModifier(final LivingEntity living) {
final float absorptionAmount = living.getAbsorptionAmount();
if (absorptionAmount > 0) {
final DoubleUnaryOperator function = damage -> -(Math.max(damage - Math.max(damage - absorptionAmount, 0.0F), 0.0F));
final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), living)).type(DamageModifierTypes.ABSORPTION).build();
return Optional.of(new DamageFunction(modifier, function));
}
return Optional.empty();
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class ServerLoginPacketListenerImplMixin method bridge$fireAuthEvent.
@Override
public boolean bridge$fireAuthEvent() {
final Component disconnectMessage = Component.text("You are not allowed to log in to this server.");
final Cause cause = Cause.of(EventContext.empty(), this);
final ServerSideConnectionEvent.Auth event = SpongeEventFactory.createServerSideConnectionEventAuth(cause, disconnectMessage, disconnectMessage, (ServerSideConnection) this);
SpongeCommon.post(event);
if (event.isCancelled()) {
this.impl$disconnectClient(event.message());
}
return event.isCancelled();
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$updateCauseOnWithWorld.
/*
* A note on why we're doing this with the cause manually.
*
* When the object is first constructed, we get the cause from the stack manager. However, as the command processor
* works through the nodes, this entire source may get replaced. We want to keep some of the changes in sync,
* but the original cause may have gone by the time the source changes. Really, this command source is the analogue
* of our Cause, NOT our CauseStackManager, so we just need to do `Cause.with(...)` along with their select `with*(...)`
* methods.
*/
@Inject(method = "withLevel", at = @At("RETURN"))
private void impl$updateCauseOnWithWorld(final ServerLevel serverWorld, final CallbackInfoReturnable<CommandSourceStack> cir) {
if (cir.getReturnValue() != (Object) this) {
final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, x.position())).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, VecHelper.toVector3d(cir.getReturnValue().getPosition())));
((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
}
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class AdvancementProgressMixin method impl$grantCriterion.
private boolean impl$grantCriterion(String rawCriterion) {
final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(rawCriterion);
if (criterionProgress == null || criterionProgress.isDone()) {
return false;
}
if (SpongeScoreCriterion.BYPASS_EVENT) {
criterionProgress.grant();
return true;
}
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
final ServerPlayer player = ((PlayerAdvancementsBridge) this.impl$playerAdvancements).bridge$getPlayer();
final CriterionProgress progress = (CriterionProgress) criterionProgress;
final AdvancementCriterion criterion = progress.criterion();
final CriterionBridge criterionBridge = (CriterionBridge) criterion;
// The score criterion needs special care
final SpongeScoreCriterion scoreCriterion = criterionBridge.bridge$getScoreCriterion();
final CriterionEvent event;
if (scoreCriterion != null) {
final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) this.impl$progressMap.get(scoreCriterion.name());
final int lastScore = scoreProgress.score();
final int score = lastScore + 1;
if (lastScore == scoreCriterion.goal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
} else if (score == scoreCriterion.goal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, this.bridge$getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
}
} else {
event = SpongeEventFactory.createCriterionEventGrant(cause, this.bridge$getAdvancement(), criterion, player, Instant.now());
}
if (SpongeCommon.post(event)) {
return false;
}
criterionProgress.grant();
return true;
}
Aggregations