From f6e738ca0e2b915a7cefd1a9c88fe4e0c5d2d937 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 19:43:09 +0000 Subject: [PATCH 1/2] Combat 4.2b: Stop treating vanilla Thorns as a weapon swing A hit with cause THORNS (or, on 1.20.4+, vanilla damage type thorns) and no damage request reports the Thorns wearer as its damager. The wrapper took that for a weapon swing and applied the wearer's weapon damage, crit and on-hit effects to the attacker. Such a hit now takes the path environmental damage takes: the event's own damage, typed by attached-damage-types, attached-damage-causes or the default type, reduced by the victim's defenses. Hits that carry a damage request are unchanged: the request's origin decides. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01FaEyTt5JAkynCUVt6Z3jZK --- .../object/VanillaWrapperListener.java | 20 +++ .../damage/CombatCharacterizationTest.java | 159 ++++++++++++++++++ 2 files changed, 179 insertions(+) diff --git a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java index 4001cb7a..4df90c62 100644 --- a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java +++ b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java @@ -228,6 +228,13 @@ 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 && isThornsHit(ede)) { + break labelFullDamage; + } + if (edeDamager instanceof LivingEntity) { if (cause == DamageCause.ENTITY_SWEEP_ATTACK && EngineCfg.COMBAT_DISABLE_VANILLA_SWEEP) { ede.setCancelled(true); @@ -440,6 +447,19 @@ 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 EntityDamageEvent e) { + if (e.getCause() == DamageCause.THORNS) return true; + + String vanillaType = DamageSources.get().getDamageType(e); + 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 diff --git a/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java b/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java index 2677514c..a55db806 100644 --- a/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java +++ b/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java @@ -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 changed this test. Before, a Thorns hit was a full weapon swing by the wearer: 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 genericCauses = attachedCauses(generic); + Set 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 // ------------------------------------------------------------------ // @@ -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 attachedCauses(DamageAttribute damage) throws Exception { + Field field = DamageAttribute.class.getDeclaredField("attachedDamageCauses"); + field.setAccessible(true); + return (Set) field.get(damage); + } + + private static void setAttachedCauses(DamageAttribute damage, Set causes) throws Exception { + Field field = DamageAttribute.class.getDeclaredField("attachedDamageCauses"); + field.setAccessible(true); + field.set(damage, causes); + } + + private static Set withThorns(Set causes) { + Set copy = new HashSet<>(causes); + copy.add("THORNS"); + return copy; + } + + private static Set withoutThorns(Set causes) { + Set 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))); } From ceb7d4bc60f47f8ec8d50d686b993941ac4f5690 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 20:41:15 +0000 Subject: [PATCH 2/2] Combat 4.2b: Read the vanilla damage type once, update the Thorns docs Look the vanilla damage type up at most once per event and reuse it for the attached-damage-types fallback. Reword the plan paragraph on Thorns and add a note, now that 4.2b fixes it. Correct the javadoc of the new Thorns test. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01FaEyTt5JAkynCUVt6Z3jZK --- doc/COMBAT_RESTRUCTURE_NOTES.md | 4 ++++ doc/COMBAT_RESTRUCTURE_PLAN.md | 13 +++++++---- .../object/VanillaWrapperListener.java | 23 ++++++++++++++----- .../damage/CombatCharacterizationTest.java | 6 ++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/doc/COMBAT_RESTRUCTURE_NOTES.md b/doc/COMBAT_RESTRUCTURE_NOTES.md index e68501e1..9b4ec25c 100644 --- a/doc/COMBAT_RESTRUCTURE_NOTES.md +++ b/doc/COMBAT_RESTRUCTURE_NOTES.md @@ -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 diff --git a/doc/COMBAT_RESTRUCTURE_PLAN.md b/doc/COMBAT_RESTRUCTURE_PLAN.md index d387d6b6..ae9e6bfd 100644 --- a/doc/COMBAT_RESTRUCTURE_PLAN.md +++ b/doc/COMBAT_RESTRUCTURE_PLAN.md @@ -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 diff --git a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java index 4df90c62..a4eb5629 100644 --- a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java +++ b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java @@ -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. @@ -231,8 +236,15 @@ public void onVanillaDamage(EntityDamageEvent e) { // 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 && isThornsHit(ede)) { - break labelFullDamage; + 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) { @@ -323,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); @@ -452,10 +464,9 @@ public void onVanillaDamage(EntityDamageEvent e) { * version, or, where the server has vanilla damage types (1.20.4 and newer), the type * {@code minecraft:thorns}. */ - private static boolean isThornsHit(@NotNull EntityDamageEvent e) { - if (e.getCause() == DamageCause.THORNS) return true; + private static boolean isThornsHit(@NotNull DamageCause cause, @Nullable String vanillaType) { + if (cause == DamageCause.THORNS) return true; - String vanillaType = DamageSources.get().getDamageType(e); return vanillaType != null && ("minecraft:thorns".equalsIgnoreCase(vanillaType) || "thorns".equalsIgnoreCase(vanillaType)); } diff --git a/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java b/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java index a55db806..de6cbaa1 100644 --- a/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java +++ b/src/test/java/studio/magemonkey/divinity/manager/damage/CombatCharacterizationTest.java @@ -1030,9 +1030,9 @@ void environment_isReducedByMatchingDefense() { // ------------------------------------------------------------------ // /** - * Combat 4.2b changed this test. Before, a Thorns hit was a full weapon swing by the wearer: 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. + * 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() {