Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/COMBAT_RESTRUCTURE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ lift the cap in the test and restore it.
Divinity's defense value lowers defense where a server expects it to rise. `melee-defense` and
`projectile-defense` are already applied that way; do not extend the pattern without a decision.

- **Vanilla Thorns is not a swing (4.2b).** A request-less hit with cause `THORNS` (any version) or vanilla
damage type `thorns` (1.20.4+) takes the non-swing path in `VanillaWrapperListener`, whatever the damager;
a hit with a request is decided by the request's origin. The damage type is read once per event.

- **`VanillaDamageBridge` owns every `DamageModifier` call.** It only wraps access; callers decide when to
call it, and the order relative to event firing is unchanged. Plain `getDamage()` and `setDamage(double)`
(which are BASE, and which Bukkit may scale other modifiers for) are not modifier calls and were left
Expand Down
13 changes: 9 additions & 4 deletions doc/COMBAT_RESTRUCTURE_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ Left for the shared contract in Phase 2, because changing them alters live behav
- Shields are not consulted for environmental damage (fire, fall and so on), only for hits that have an
attacker.

Found later and deliberately left for Phase 4, because fixing it changes live numbers: a hit from the
vanilla Thorns enchantment is treated as a full weapon swing by the armour's wearer. The wrapper uses the
attacker's weapon damage for any hit with a living attacker and does not look at the cause. Divinity's own
Thornmail stat is not affected; it deals damage with no attacker.
Found later and fixed in 4.2b (Phase 4, because it changes live numbers): a hit from the vanilla Thorns
enchantment used to be treated as a full weapon swing by the armour's wearer, because the wrapper used the
attacker's weapon damage for any hit with a living attacker and did not look at the cause. Now a hit with no
damage request whose cause is `THORNS` (any version), or whose vanilla damage type is `thorns` (1.20.4 and
newer), takes the non-swing path environmental damage takes: the event's own damage, typed by
`attached-damage-types`, `attached-damage-causes` or the default type, with the defender's defenses applied.
A hit that carries a request is decided by the request's origin, as before. Divinity's own Thornmail stat is
not affected; it deals damage through a request with no attacker. Other causes that may be non-swings are
still treated as swings and are left for a follow-up.

Not a bug after all: `DivinityDamageEvent.Exit` does not fire for `ignore-divinity` skill damage. Firing
it would make those hits start costing weapon and armor durability, which is a behaviour change. It is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public void onVanillaDamage(EntityDamageEvent e) {

boolean isFullDamage = false;

// The vanilla damage type, read at most once per event. It is read for the Thorns check on a hit with
// no request, and reused by the attached-damage-types fallback below.
String vanillaType = null;
boolean vanillaTypeKnown = false;

FabledHook skillApi = (FabledHook) Divinity.getInstance().getHook(EHook.SKILL_API);
// Here only check for damager
// to be sure to use his stats or not.
Expand All @@ -228,6 +233,20 @@ public void onVanillaDamage(EntityDamageEvent e) {
return;
}

// Vanilla Thorns reports its wearer as the damager, but it is not a swing: it takes the path
// environmental damage takes, with no weapon damage, crit or on-hit effects from the wearer. A hit
// that has a request is left to its origin, as every request-carrying hit is.
if (request == null) {
// The cause decides on every version, so the damage type is only read where it is still needed
if (cause != DamageCause.THORNS) {
vanillaType = DamageSources.get().getDamageType(e);
vanillaTypeKnown = true;
}
if (isThornsHit(cause, vanillaType)) {
break labelFullDamage;
}
}

if (edeDamager instanceof LivingEntity) {
if (cause == DamageCause.ENTITY_SWEEP_ATTACK && EngineCfg.COMBAT_DISABLE_VANILLA_SWEEP) {
ede.setCancelled(true);
Expand Down Expand Up @@ -316,7 +335,7 @@ public void onVanillaDamage(EntityDamageEvent e) {
// and the config attaches it to a damage type
DamageAttribute dmgCause = null;
if (ItemStats.getDamages().stream().anyMatch(dmg -> !dmg.getAttachedDamageTypes().isEmpty())) {
String vanillaType = DamageSources.get().getDamageType(e);
if (!vanillaTypeKnown) vanillaType = DamageSources.get().getDamageType(e);
if (vanillaType != null) dmgCause = ItemStats.getDamageByVanillaType(vanillaType);
}
if (dmgCause == null) dmgCause = ItemStats.getDamageByCause(cause);
Expand Down Expand Up @@ -440,6 +459,18 @@ public void onVanillaDamage(EntityDamageEvent e) {
// Divinity.getInstance().info("event took: " + (System.currentTimeMillis() - l1) + " millis");
}

/**
* Whether this is the damage vanilla Thorns deals back to an attacker: cause {@code THORNS} on every
* version, or, where the server has vanilla damage types (1.20.4 and newer), the type
* {@code minecraft:thorns}.
*/
private static boolean isThornsHit(@NotNull DamageCause cause, @Nullable String vanillaType) {
if (cause == DamageCause.THORNS) return true;

return vanillaType != null && ("minecraft:thorns".equalsIgnoreCase(vanillaType)
|| "thorns".equalsIgnoreCase(vanillaType));
}

/**
* Removes the custom arrow bonus applied for the duration of one hit. This has to happen on every
* way out of the damage handler, including a cancelled hit such as a dodge, or the bonus would keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,122 @@ void environment_isReducedByMatchingDefense() {
assertEquals(2.72D, hit(environmentEvent(DamageCause.FIRE, 4), victim).damage("fire"), DELTA);
}

// ------------------------------------------------------------------ //
// Vanilla Thorns: the Thorns wearer is the damager of the reflected hit
// ------------------------------------------------------------------ //

/**
* Combat 4.2b: a Thorns hit is not a weapon swing. Before, it was one: with this weapon it was physical 60
* (20 tripled by a certain critical hit), the wearer was the hit's damager and the hit counted as
* critical. Now it is the event's own damage, typed by the cause and not critical.
*/
@Test
void thorns_reflectedHit_isNotAWeaponSwing_onADefaultInstall() {
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());

// The wearer is the attacker here; the reflected hit lands on whoever struck the wearer
Hit hit = hit(thornsEvent(attacker, victim, 2), victim);

String byCause = ItemStats.getDamageByCause(DamageCause.THORNS).getId();
assertEquals("_vanilla_generic", byCause, "Test setup: the shipped config attaches THORNS to this type");
assertEquals(Map.of(byCause, 2D), hit.damages());
assertFalse(hit.meta().isCritical());
assertNull(hit.meta().getDamager(), "Same as environmental damage: no damager stats are read");
assertEquals(FULL_PIPELINE, hit.events());
assertEquals(2D, hit.finalDamage(), DELTA);
}

@Test
void thorns_vanillaDamageTypeAlone_isNotAWeaponSwingEither() {
// 1.20.4 and newer: the cause is whatever the server chose, the damage type says thorns
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(attacker,
victim,
DamageCause.ENTITY_ATTACK,
DamageSource.builder(DamageType.THORNS).withCausingEntity(attacker).withDirectEntity(attacker).build(),
modifiers(2),
functions(modifiers(2)));

Hit hit = hit(event, victim);

assertEquals(Map.of(ItemStats.getDamageByDefault().getId(), 2D), hit.damages());
assertFalse(hit.meta().isCritical());
}

@Test
void thorns_causeMappedToADamageType_takesPrecedenceAndDefensesStillApply() throws Exception {
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());
victim.getInventory().setChestplate(armor(Map.of("fire", 10D)));
DamageAttribute generic = Objects.requireNonNull(ItemStats.getDamageById("_vanilla_generic"));
DamageAttribute fire = Objects.requireNonNull(ItemStats.getDamageById("fire"));
Set<String> genericCauses = attachedCauses(generic);
Set<String> fireCauses = attachedCauses(fire);
setAttachedCauses(generic, withoutThorns(genericCauses));
setAttachedCauses(fire, withThorns(fireCauses));
try {
Hit hit = hit(thornsEvent(attacker, victim, 4), victim);

// 1 - max(10 / 5, 10 - 4 * 4 / 8) * 0.8 * 0.05 = 0.68, as in environment_isReducedByMatchingDefense
assertEquals(2.72D, hit.damage("fire"), DELTA);
assertEquals(Set.of("fire"), hit.damages().keySet());
assertFalse(hit.meta().isCritical());
} finally {
setAttachedCauses(generic, genericCauses);
setAttachedCauses(fire, fireCauses);
}
}

@Test
void thorns_vanillaDamageTypeMappedToADamageType_takesPrecedenceOverTheCause() {
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());
DamageAttribute fire = Objects.requireNonNull(ItemStats.getDamageById("fire"));
fire.setAttachedDamageTypes(List.of("thorns"));
try {
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(attacker,
victim,
DamageCause.THORNS,
DamageSource.builder(DamageType.THORNS).withCausingEntity(attacker).build(),
modifiers(4),
functions(modifiers(4)));

// The cause alone would give _vanilla_generic, as in the default install test
assertEquals(Map.of("fire", 4D), hit(event, victim).damages());
} finally {
fire.setAttachedDamageTypes(List.of());
}
}

@Test
void thorns_hitCarryingAnEffectRequest_isLeftToTheRequest() {
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());
refresh(attacker);
refresh(victim);
DamageRequest request = DamageRequest.builder().origin(DamageOrigin.EFFECT).source(attacker).build();

Hit hit;
try (DamageScope scope = DamageContexts.open(victim, request)) {
hit = hitWithoutRefresh(thornsEvent(attacker, victim, 2), victim);
assertTrue(scope.isClaimed());
}

// Same numbers as before Combat 4.2b: the request's origin decides, not the cause
assertEquals(Map.of("physical", 60D), hit.damages());
assertTrue(hit.meta().isCritical());
assertSame(request, hit.meta().getRequest());
assertSame(attacker, hit.meta().getDamager());
}

@Test
void thorns_sameWearersOrdinaryMeleeHit_isStillAFullWeaponSwing() {
attacker.getInventory().setItemInMainHand(thornsWearerWeapon());

Hit hit = melee(VANILLA_SWORD_DAMAGE);

assertEquals(Map.of("physical", 60D), hit.damages());
assertTrue(hit.meta().isCritical());
assertSame(attacker, hit.meta().getDamager());
}

// ------------------------------------------------------------------ //
// Projectiles
// ------------------------------------------------------------------ //
Expand Down Expand Up @@ -1561,6 +1677,49 @@ private EntityDamageByEntityEvent meleeEvent(Entity damager, Entity target, doub
functions(modifiers(eventDamage)));
}

/**
* A weapon whose swing would be conspicuous if a Thorns hit used it: 20 physical, a certain critical hit.
*/
private ItemStack thornsWearerWeapon() {
ItemStack sword = weapon(Map.of("physical", 20D));
stat(sword, TypedStat.Type.CRITICAL_RATE, 100);
stat(sword, TypedStat.Type.CRITICAL_DAMAGE, 2);
return sword;
}

@SuppressWarnings("unchecked")
private static Set<String> attachedCauses(DamageAttribute damage) throws Exception {
Field field = DamageAttribute.class.getDeclaredField("attachedDamageCauses");
field.setAccessible(true);
return (Set<String>) field.get(damage);
}

private static void setAttachedCauses(DamageAttribute damage, Set<String> causes) throws Exception {
Field field = DamageAttribute.class.getDeclaredField("attachedDamageCauses");
field.setAccessible(true);
field.set(damage, causes);
}

private static Set<String> withThorns(Set<String> causes) {
Set<String> copy = new HashSet<>(causes);
copy.add("THORNS");
return copy;
}

private static Set<String> withoutThorns(Set<String> causes) {
Set<String> copy = new HashSet<>(causes);
copy.remove("THORNS");
return copy;
}

private EntityDamageByEntityEvent thornsEvent(Entity wearer, Entity target, double eventDamage) {
return new EntityDamageByEntityEvent(wearer,
target,
DamageCause.THORNS,
modifiers(eventDamage),
functions(modifiers(eventDamage)));
}

private EntityDamageEvent environmentEvent(DamageCause cause, double eventDamage) {
return new EntityDamageEvent(victim, cause, modifiers(eventDamage), functions(modifiers(eventDamage)));
}
Expand Down
Loading