use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method createArmorModifiers.
public static Optional<DamageFunction> createArmorModifiers(final LivingEntity living, final DamageSource damageSource) {
if (damageSource.isBypassArmor()) {
return Optional.empty();
}
final DoubleUnaryOperator function = incomingDamage -> -(incomingDamage - CombatRules.getDamageAfterAbsorb((float) incomingDamage, living.getArmorValue(), (float) living.getAttributeValue(Attributes.ARMOR_TOUGHNESS)));
final DamageFunction armorModifier;
try (final CauseStackManager.StackFrame frame = ((Server) living.getServer()).causeStackManager().pushCauseFrame()) {
frame.pushCause(living);
frame.pushCause(Attributes.ARMOR_TOUGHNESS);
armorModifier = DamageFunction.of(DamageModifier.builder().cause(frame.currentCause()).type(DamageModifierTypes.ARMOR).build(), function);
}
return Optional.of(armorModifier);
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method provideCooldownAttackStrengthFunction.
public static DamageFunction provideCooldownAttackStrengthFunction(final Player player, final float attackStrength) {
final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), player)).type(DamageModifierTypes.ATTACK_COOLDOWN).build();
// The formula is as follows:
// Since damage needs to be "multiplied", this needs to basically add negative damage but re-add the "reduced" damage.
final DoubleUnaryOperator function = (damage) -> -damage + (damage * (0.2F + attackStrength * attackStrength * 0.8F));
return new DamageFunction(modifier, function);
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method provideCriticalAttackTuple.
public static DamageFunction provideCriticalAttackTuple(final Player player, double criticalModifier) {
final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), player)).type(DamageModifierTypes.CRITICAL_HIT).build();
final DoubleUnaryOperator function = (damage) -> damage * criticalModifier;
return new DamageFunction(modifier, function);
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin method impl$onReturnSleep.
@Inject(method = "startSleepInBed", at = @At(value = "RETURN"), cancellable = true)
private void impl$onReturnSleep(final BlockPos param0, final CallbackInfoReturnable<Either<Player.BedSleepingProblem, Unit>> cir) {
final Either<Player.BedSleepingProblem, Unit> returnValue = cir.getReturnValue();
if (returnValue.left().isPresent()) {
switch(returnValue.left().get()) {
case NOT_POSSIBLE_HERE:
case TOO_FAR_AWAY:
case NOT_POSSIBLE_NOW:
case OBSTRUCTED:
case NOT_SAFE:
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final BlockSnapshot snapshot = ((ServerWorld) this.level).createSnapshot(param0.getX(), param0.getY(), param0.getZ());
if (Sponge.eventManager().post(SpongeEventFactory.createSleepingEventFailed(currentCause, snapshot, (Living) this))) {
final Either<Player.BedSleepingProblem, Unit> var5 = super.shadow$startSleepInBed(param0).ifRight((param0x) -> {
this.shadow$awardStat(Stats.SLEEP_IN_BED);
CriteriaTriggers.SLEPT_IN_BED.trigger((net.minecraft.server.level.ServerPlayer) (Object) this);
});
((ServerLevel) this.level).updateSleepingPlayerList();
cir.setReturnValue(var5);
}
break;
case // ignore
OTHER_PROBLEM:
break;
}
}
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class ServerLevelMixin method impl$onSetWeatherParameters.
@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/server/level/ServerLevel;oRainLevel:F", shift = At.Shift.BEFORE, ordinal = 1))
public void impl$onSetWeatherParameters(final BooleanSupplier param0, final CallbackInfo ci, final ProfilerFiller var0, final boolean wasRaining) {
final boolean isRaining = this.shadow$isRaining();
if (this.oRainLevel != this.rainLevel || this.oThunderLevel != this.thunderLevel || wasRaining != isRaining) {
Weather newWeather = ((ServerWorld) this).properties().weather();
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final Transaction<Weather> weatherTransaction = new Transaction<>(this.impl$prevWeather, newWeather);
final ChangeWeatherEvent event = SpongeEventFactory.createChangeWeatherEvent(currentCause, ((ServerWorld) this), weatherTransaction);
if (Sponge.eventManager().post(event)) {
newWeather = event.weather().original();
} else {
newWeather = event.weather().finalReplacement();
}
// Set event results
this.impl$prevWeather = newWeather;
if (newWeather.type() == WeatherTypes.CLEAR.get()) {
this.serverLevelData.setThunderTime(0);
this.serverLevelData.setRainTime(0);
this.serverLevelData.setClearWeatherTime((int) newWeather.remainingDuration().ticks());
this.serverLevelData.setThundering(false);
this.serverLevelData.setRaining(false);
} else {
final int newTime = (int) newWeather.remainingDuration().ticks();
this.serverLevelData.setRaining(true);
this.serverLevelData.setClearWeatherTime(0);
this.serverLevelData.setRainTime(newTime);
if (newWeather.type() == WeatherTypes.THUNDER.get()) {
this.serverLevelData.setThunderTime(newTime);
this.serverLevelData.setThundering(true);
} else {
this.serverLevelData.setThunderTime(0);
this.serverLevelData.setThundering(false);
}
}
}
}
Aggregations