Skip to content

Commit

Permalink
Implement damage API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Feb 8, 2025
1 parent 797c8c3 commit 25fb89a
Show file tree
Hide file tree
Showing 16 changed files with 371 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public abstract class LivingEntityMixin_Forge_Damage implements TrackedDamageBri
return ForgeEventFactory.onShieldBlock(self, source, originalDamage);
}

final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, originalDamage, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyModifiersBefore();
final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyChildrenBefore(originalDamage);
final ShieldBlockEvent event;
if (step.isSkipped()) {
event = new ShieldBlockEvent(self, source, damage);
Expand All @@ -71,7 +71,7 @@ public abstract class LivingEntityMixin_Forge_Damage implements TrackedDamageBri
damage -= event.getBlockedDamage();
}
}
step.applyModifiersAfter(damage);
step.applyChildrenAfter(damage);
return event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public abstract class LivingEntityMixin_Neo_Damage implements TrackedDamageBridg
}

final float originalDamage = container.getNewDamage();
final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, originalDamage, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyModifiersBefore();
final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyChildrenBefore(originalDamage);
container.setNewDamage(damage);
final LivingShieldBlockEvent event;
if (step.isSkipped()) {
Expand All @@ -78,7 +78,7 @@ public abstract class LivingEntityMixin_Neo_Damage implements TrackedDamageBridg
container.setBlockedDamage(event);
damage = container.getNewDamage();
}
step.applyModifiersAfter(damage);
step.applyChildrenAfter(damage);
return event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.cause.entity.damage.DamageStep;
import org.spongepowered.api.event.cause.entity.damage.DamageStepTypes;
import org.spongepowered.api.event.entity.AttackEntityEvent;
import org.spongepowered.api.event.entity.DamageCalculationEvent;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
Expand All @@ -39,8 +39,6 @@
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.item.util.ItemStackUtil;

import java.util.List;

public class SpongeAttackTracker extends SpongeDamageTracker {
private final ItemStack weapon;
private final ItemStackSnapshot weaponSnapshot;
Expand Down Expand Up @@ -88,31 +86,39 @@ public void setStrongSprint(final boolean strongSprint) {
this.strongSprint = strongSprint;
}

public boolean callAttackPostEvent(final Entity entity, final DamageSource source, final float finalDamage, final float knockbackModifier) {
final List<DamageStep> steps = this.preparePostEvent();
public boolean callAttackPostEvent(final Entity entity, final DamageSource source, float finalDamage, final float knockbackModifier) {
if (this.postEvent != null) {
throw new IllegalStateException("Post event already fired");
}

finalDamage = (float) this.newStep(DamageStepTypes.END, this).apply(finalDamage);

try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
SpongeDamageTracker.generateCauseFor(source, frame);

final AttackEntityEvent.Post event = SpongeEventFactory.createAttackEntityEventPost(frame.currentCause(),
this.preEvent.originalBaseDamage(), this.preEvent.baseDamage(), finalDamage, finalDamage, knockbackModifier, knockbackModifier, entity, steps);
knockbackModifier, knockbackModifier, entity, this, this.preEvent.baseDamage(), finalDamage);

this.postEvent = event;
return SpongeCommon.post(event);
}
}

public static @Nullable SpongeAttackTracker callAttackPreEvent(final Entity entity, final DamageSource source, final float baseDamage, final ItemStack weapon) {
final SpongeAttackTracker tracker;
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
SpongeDamageTracker.generateCauseFor(source, frame);

final AttackEntityEvent.Pre event = SpongeEventFactory.createAttackEntityEventPre(frame.currentCause(), baseDamage, baseDamage, entity);
final AttackEntityEvent.Pre event = SpongeEventFactory.createAttackEntityEventPre(frame.currentCause(), entity, baseDamage);
if (SpongeCommon.post(event)) {
return null;
}

return new SpongeAttackTracker(event, weapon);
tracker = new SpongeAttackTracker(event, weapon);
}

tracker.newStep(DamageStepTypes.START, tracker).apply(baseDamage);
return tracker;
}

public static @Nullable SpongeAttackTracker of(final DamageSource source) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.event.cause.entity.damage;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.cause.entity.damage.DamageModifier;
import org.spongepowered.api.event.cause.entity.damage.DamageStepType;

import java.util.Objects;
import java.util.Optional;

public class SpongeDamageModifier implements DamageModifier {
protected final DamageStepType type;
protected final Cause cause;
protected final @Nullable Function function;

public SpongeDamageModifier(final DamageStepType type, final Cause cause, final @Nullable Function function) {
this.type = type;
this.cause = cause;
this.function = function;
}

@Override
public final DamageStepType type() {
return this.type;
}

@Override
public final Cause cause() {
return this.cause;
}

@Override
public final Optional<Function> function() {
return Optional.ofNullable(this.function);
}

public static class Builder implements DamageModifier.Builder {
private @Nullable DamageStepType type;
private @Nullable Cause cause;
private Function function;

public Builder() {
this.reset();
}

@Override
public DamageModifier.Builder reset() {
this.type = null;
this.cause = null;
this.function = (step, damage) -> damage;
return this;
}

@Override
public DamageModifier.Builder type(DamageStepType type) {
this.type = Objects.requireNonNull(type, "type");
return this;
}

@Override
public DamageModifier.Builder cause(Cause cause) {
this.cause = Objects.requireNonNull(cause, "cause");
return this;
}

@Override
public DamageModifier.Builder function(Function function) {
this.function = Objects.requireNonNull(function, "function");
return this;
}

@Override
public DamageModifier build() {
if (this.type == null) {
throw new IllegalStateException("type must be set");
}
if (this.cause == null) {
throw new IllegalStateException("cause must be set");
}
return new SpongeDamageModifier(this.type, this.cause, this.function);
}
}
}
Loading

0 comments on commit 25fb89a

Please sign in to comment.