11package com .nisovin .magicspells .spells .targeted ;
22
3+ import java .util .Set ;
4+ import java .util .List ;
5+ import java .util .HashSet ;
6+ import java .util .ArrayList ;
7+
38import org .bukkit .entity .LivingEntity ;
49import org .bukkit .potion .PotionEffect ;
510import org .bukkit .potion .PotionEffectType ;
1419import com .nisovin .magicspells .spelleffects .EffectPosition ;
1520import com .nisovin .magicspells .events .SpellApplyDamageEvent ;
1621
22+ import org .apache .commons .math3 .util .FastMath ;
23+
1724public class PotionEffectSpell extends TargetedSpell implements TargetedEntitySpell {
18-
25+
26+ private Set <PotionEffect > potionEffects ;
27+
28+ private List <String > potionEffectData ;
29+
1930 private PotionEffectType type ;
31+ private PotionEffect potionEffect ;
2032
2133 private int duration ;
2234 private int strength ;
2335
36+ private boolean icon ;
2437 private boolean hidden ;
2538 private boolean ambient ;
26- private boolean targeted ;
2739
2840 private boolean override ;
2941 private boolean spellPowerAffectsDuration ;
3042 private boolean spellPowerAffectsStrength ;
3143
3244 public PotionEffectSpell (MagicConfig config , String spellName ) {
3345 super (config , spellName );
34-
35- type = Util .getPotionEffectType (getConfigString ("type" , "1" ));
46+
47+ //Format: <PotionType> <Duration> <Strength> <Hidden> <Ambient> <Icon>
48+ potionEffectData = getConfigStringList ("potion-effects" , new ArrayList <>());
49+ potionEffects = new HashSet <>();
50+
51+ type = Util .getPotionEffectType (getConfigString ("type" , "speed" ));
3652
3753 duration = getConfigInt ("duration" , 0 );
3854 strength = getConfigInt ("strength" , 0 );
3955
56+ icon = getConfigBoolean ("icon" , true );
4057 hidden = getConfigBoolean ("hidden" , false );
4158 ambient = getConfigBoolean ("ambient" , false );
42- targeted = getConfigBoolean ("targeted" , false );
4359 override = getConfigBoolean ("override" , false );
60+
4461 spellPowerAffectsDuration = getConfigBoolean ("spell-power-affects-duration" , true );
4562 spellPowerAffectsStrength = getConfigBoolean ("spell-power-affects-strength" , true );
4663 }
64+
65+ @ Override
66+ public void initialize () {
67+ super .initialize ();
68+
69+ if (type != null ) potionEffect = new PotionEffect (type , duration , strength , ambient , !hidden , icon );
70+
71+ if (potionEffectData .isEmpty ()) return ;
72+
73+ PotionEffect p ;
74+
75+ PotionEffectType t = null ;
76+ int d = 0 ;
77+ int s = 0 ;
78+ boolean h = false ;
79+ boolean a = false ;
80+ boolean i = true ;
81+
82+ for (String str : potionEffectData ) {
83+ String [] args = str .split (" " );
84+
85+ if (args .length <= 0 ) continue ;
86+
87+ if (args .length >= 1 ) t = Util .getPotionEffectType (args [0 ]);
88+ if (args .length >= 2 ) d = Integer .parseInt (args [1 ]);
89+ if (args .length >= 3 ) s = Integer .parseInt (args [2 ]);
90+ if (args .length >= 4 ) h = Boolean .parseBoolean (args [3 ]);
91+ if (args .length >= 5 ) a = Boolean .parseBoolean (args [4 ]);
92+ if (args .length >= 6 ) i = Boolean .parseBoolean (args [5 ]);
93+
94+ if (t == null ) continue ;
95+ p = new PotionEffect (t , d , s , h , a , i );
96+ potionEffects .add (p );
97+ }
98+
99+ }
100+
101+ public Set <PotionEffect > getPotionEffects () {
102+ return potionEffects ;
103+ }
47104
48105 public PotionEffectType getPotionType () {
49106 return type ;
@@ -56,25 +113,15 @@ public int getDuration() {
56113 @ Override
57114 public PostCastAction castSpell (LivingEntity caster , SpellCastState state , float power , String [] args ) {
58115 if (state == SpellCastState .NORMAL ) {
59- LivingEntity target = null ;
60- if (targeted ) {
61- TargetInfo <LivingEntity > targetInfo = getTargetedEntity (caster , power );
62- if (targetInfo != null ) {
63- target = targetInfo .getTarget ();
64- power = targetInfo .getPower ();
65- }
66- } else target = caster ;
67-
68- if (target == null ) return noTarget (caster );
69-
70- int dur = spellPowerAffectsDuration ? Math .round (duration * power ) : duration ;
71- int str = spellPowerAffectsStrength ? Math .round (strength * power ) : strength ;
72-
73- applyPotionEffect (caster , target , new PotionEffect (type , dur , str , ambient , !hidden ));
74- if (targeted ) playSpellEffects (caster , target );
75- else playSpellEffects (EffectPosition .CASTER , caster );
116+ TargetInfo <LivingEntity > targetInfo = getTargetedEntity (caster , power );
117+ if (targetInfo == null ) return noTarget (caster );
118+
119+ LivingEntity target = targetInfo .getTarget ();
76120
121+ handlePotionEffects (caster , target , power );
122+ playSpellEffects (caster , target );
77123 sendMessages (caster , target , args );
124+
78125 return PostCastAction .NO_MESSAGES ;
79126 }
80127 return PostCastAction .HANDLE_NORMALLY ;
@@ -83,37 +130,45 @@ public PostCastAction castSpell(LivingEntity caster, SpellCastState state, float
83130 @ Override
84131 public boolean castAtEntity (LivingEntity caster , LivingEntity target , float power ) {
85132 if (!validTargetList .canTarget (caster , target )) return false ;
86- int dur = spellPowerAffectsDuration ? Math .round (duration * power ) : duration ;
87- int str = spellPowerAffectsStrength ? Math .round (strength * power ) : strength ;
88- PotionEffect effect = new PotionEffect (type , dur , str , ambient , !hidden );
89- if (targeted ) {
90- applyPotionEffect (caster , target , effect );
91- playSpellEffects (caster , target );
92- } else {
93- applyPotionEffect (caster , caster , effect );
94- playSpellEffects (EffectPosition .CASTER , caster );
95- }
133+ handlePotionEffects (caster , target , power );
134+ playSpellEffects (caster , target );
96135 return true ;
97136 }
98137
99138 @ Override
100139 public boolean castAtEntity (LivingEntity target , float power ) {
101140 if (!validTargetList .canTarget (target )) return false ;
102- int dur = spellPowerAffectsDuration ? Math .round (duration * power ) : duration ;
103- int str = spellPowerAffectsStrength ? Math .round (strength * power ) : strength ;
104- PotionEffect effect = new PotionEffect (type , dur , str , ambient , !hidden );
105- applyPotionEffect (null , target , effect );
141+ handlePotionEffects (null , target , power );
106142 playSpellEffects (EffectPosition .TARGET , target );
107143 return true ;
108144 }
109145
110- private void applyPotionEffect (LivingEntity caster , LivingEntity target , PotionEffect effect ) {
146+ private void handlePotionEffects (LivingEntity caster , LivingEntity target , float power ) {
147+ if (potionEffects .isEmpty ()) {
148+ applyPotionEffect (caster , target , potionEffect , power );
149+ return ;
150+ }
151+
152+ for (PotionEffect effect : potionEffects ) {
153+ applyPotionEffect (caster , target , effect , power );
154+ }
155+ }
156+
157+ private void applyPotionEffect (LivingEntity caster , LivingEntity target , PotionEffect effect , float power ) {
158+ if (effect == null ) return ;
159+
111160 DamageCause cause = null ;
112161 if (effect .getType () == PotionEffectType .POISON ) cause = DamageCause .POISON ;
113162 else if (effect .getType () == PotionEffectType .WITHER ) cause = DamageCause .WITHER ;
114- if (cause != null ) EventUtil .call (new SpellApplyDamageEvent (this , caster , target , effect .getAmplifier (), cause , "" ));
163+
164+ int d = spellPowerAffectsDuration ? FastMath .round (effect .getDuration () * power ) : effect .getDuration ();
165+ int s = spellPowerAffectsStrength ? FastMath .round (effect .getAmplifier () * power ) : effect .getAmplifier ();
166+
167+ if (cause != null ) EventUtil .call (new SpellApplyDamageEvent (this , caster , target , s , cause , "" ));
168+
115169 if (override && target .hasPotionEffect (effect .getType ())) target .removePotionEffect (effect .getType ());
116- target .addPotionEffect (effect );
170+
171+ target .addPotionEffect (new PotionEffect (effect .getType (), d , s , effect .isAmbient (), effect .hasParticles (), effect .hasIcon ()));
117172 }
118173
119174}
0 commit comments