Skip to content

Commit 0875bbd

Browse files
committed
feat: Improve BuffMechanic category handling and add tests
1 parent bebd9c8 commit 0875bbd

3 files changed

Lines changed: 119 additions & 5 deletions

File tree

src/main/java/studio/magemonkey/fabled/dynamic/mechanic/BuffMechanic.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import java.util.List;
99

1010
/**
11-
* Fabled © 2024
11+
* Fabled © 2026
1212
* studio.magemonkey.fabled.dynamic.mechanic.BuffMechanic
13+
*
14+
* The BuffMechanic class is responsible for applying buffs to LivingEntity targets.
15+
* It can apply immediate buffs or timed buffs based on the configuration settings.
1316
*/
1417
public class BuffMechanic extends MechanicComponent {
1518
private static final String MODIFIER = "modifier";
@@ -44,14 +47,16 @@ public boolean execute(LivingEntity caster, int level, List<LivingEntity> target
4447
BuffType buffType = BuffType.getByNameOrLocal(rawType);
4548
double seconds = parseValues(caster, SECONDS, level, 3.0);
4649
String category = settings.getString(CATEGORY, null);
50+
51+
if (buffType != null && buffType != BuffType.SKILL_DAMAGE && buffType != BuffType.SKILL_DEFENSE) {
52+
category = null;
53+
}
54+
4755
int ticks = (int) (seconds * 20);
4856

4957
String qualifiedType;
5058
if (buffType != null) {
5159
qualifiedType = buffType.getLocalizedName();
52-
if (category != null) {
53-
qualifiedType += "_" + category;
54-
}
5560
} else {
5661
qualifiedType = rawType;
5762
}

src/main/java/studio/magemonkey/fabled/dynamic/mechanic/DamageBuffMechanic.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import java.util.List;
3535

3636
/**
37-
* Applies a flag to each target
37+
* Represents the mechanic for applying a damage buff to targets.
38+
* This class handles the logic for adding either skill or flat damage buffs
39+
* to a list of LivingEntity targets for a specified duration.
3840
*/
3941
public class DamageBuffMechanic extends MechanicComponent {
4042
private static final String TYPE = "type";
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package studio.magemonkey.fabled.dynamic.mechanic;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockbukkit.mockbukkit.entity.PlayerMock;
6+
import studio.magemonkey.fabled.api.Settings;
7+
import studio.magemonkey.fabled.api.util.BuffData;
8+
import studio.magemonkey.fabled.api.util.BuffManager;
9+
import studio.magemonkey.fabled.dynamic.DynamicSkill;
10+
import studio.magemonkey.fabled.dynamic.EffectComponent;
11+
import studio.magemonkey.fabled.testutil.MockedTest;
12+
13+
import java.lang.reflect.Field;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
class BuffMechanicTest extends MockedTest {
23+
private Settings settings;
24+
private BuffMechanic buffMechanic;
25+
private PlayerMock caster;
26+
private PlayerMock target;
27+
28+
@BeforeEach
29+
void setUp() throws Exception {
30+
caster = genPlayer("Caster");
31+
target = genPlayer("Target");
32+
33+
settings = mock(Settings.class);
34+
buffMechanic = new BuffMechanic();
35+
36+
// Inject settings
37+
Field settingsField = EffectComponent.class.getDeclaredField("settings");
38+
settingsField.setAccessible(true);
39+
settingsField.set(buffMechanic, settings);
40+
41+
// Inject skill
42+
Field skillField = EffectComponent.class.getDeclaredField("skill");
43+
skillField.setAccessible(true);
44+
DynamicSkill skill = mock(DynamicSkill.class);
45+
when(skill.getName()).thenReturn("TestSkill");
46+
skillField.set(buffMechanic, skill);
47+
}
48+
49+
@Test
50+
void testExecute_SkillDamage_AddsBuffWithCorrectKey() throws Exception {
51+
// Setup settings for a SKILL_DAMAGE buff with a category
52+
when(settings.getString("type", "DAMAGE")).thenReturn("SKILL_DAMAGE");
53+
when(settings.getString("category", null)).thenReturn("mycategory");
54+
when(settings.getString("value")).thenReturn("10");
55+
when(settings.getString("seconds")).thenReturn("5");
56+
when(settings.getString("immediate", "false")).thenReturn("false");
57+
when(settings.getString("modifier", "flat")).thenReturn("flat");
58+
59+
// Execute
60+
buffMechanic.execute(caster, 1, List.of(target), false);
61+
62+
// Verify BuffData
63+
BuffData buffData = BuffManager.getBuffData(target);
64+
Field buffsField = BuffData.class.getDeclaredField("buffs");
65+
buffsField.setAccessible(true);
66+
@SuppressWarnings("unchecked") Map<String, Map<String, Object>> buffs =
67+
(Map<String, Map<String, Object>>) buffsField.get(buffData);
68+
69+
// Check keys
70+
System.out.println("Registered Buff Keys: " + buffs.keySet());
71+
72+
// We expect "FABLED_skill_damage_mycategory"
73+
assertTrue(buffs.containsKey("FABLED_skill_damage_mycategory"),
74+
"Should contain correct key FABLED_skill_damage_mycategory");
75+
assertFalse(buffs.containsKey("FABLED_skill_damage_mycategory_mycategory"),
76+
"Should NOT contain double-appended category key FABLED_skill_damage_mycategory_mycategory");
77+
}
78+
79+
@Test
80+
void testExecute_Damage_IgnoresCategory() throws Exception {
81+
// Setup settings for a DAMAGE buff with a category
82+
when(settings.getString("type", "DAMAGE")).thenReturn("DAMAGE");
83+
when(settings.getString("category", null)).thenReturn("mycategory");
84+
when(settings.getString("value")).thenReturn("10");
85+
when(settings.getString("seconds")).thenReturn("5");
86+
when(settings.getString("immediate", "false")).thenReturn("false");
87+
when(settings.getString("modifier", "flat")).thenReturn("flat");
88+
89+
// Execute
90+
buffMechanic.execute(caster, 1, List.of(target), false);
91+
92+
// Verify BuffData
93+
BuffData buffData = BuffManager.getBuffData(target);
94+
Field buffsField = BuffData.class.getDeclaredField("buffs");
95+
buffsField.setAccessible(true);
96+
@SuppressWarnings("unchecked") Map<String, Map<String, Object>> buffs =
97+
(Map<String, Map<String, Object>>) buffsField.get(buffData);
98+
99+
// Check keys
100+
System.out.println("Registered Buff Keys: " + buffs.keySet());
101+
102+
// We expect "FABLED_damage" (category ignored)
103+
assertTrue(buffs.containsKey("FABLED_damage"), "Should contain correct key FABLED_damage");
104+
assertFalse(buffs.containsKey("FABLED_damage_mycategory"),
105+
"Should NOT contain category key FABLED_damage_mycategory");
106+
}
107+
}

0 commit comments

Comments
 (0)