Skip to content

Commit fbe9cd0

Browse files
Add GlideSpell.
The GlideSpell is a targeted spell which implements TargetedEntitySpell. There is currently only one field unique to this spell beyond those of the targeted spell, which is target-state. target-state may be set to one of the following: - on - yes - true - enable - enabled - off - no - false - disable - disabled - toggle - switch The "yes" options set the gliding state to be true. The "no" options set the gliding state to be false. The "toggle" options set the gliding state to be whatever it currently isn't. The default value is "toggle"
1 parent 3a79ba1 commit fbe9cd0

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.nisovin.magicspells.spells.targeted;
2+
3+
import com.nisovin.magicspells.spells.TargetedEntitySpell;
4+
import com.nisovin.magicspells.spells.TargetedSpell;
5+
import com.nisovin.magicspells.util.MagicConfig;
6+
import com.nisovin.magicspells.util.TargetInfo;
7+
import org.bukkit.entity.LivingEntity;
8+
import org.bukkit.entity.Player;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
public class GlideSpell extends TargetedSpell implements TargetedEntitySpell{
14+
15+
private TargetBooleanState targetState;
16+
17+
public GlideSpell(MagicConfig config, String spellName) {
18+
super(config, spellName);
19+
20+
targetState = TargetBooleanState.getFromName(getConfigString("target-state", "toggle"));
21+
22+
}
23+
24+
@Override
25+
public PostCastAction castSpell(Player player, SpellCastState state, float power, String[] args) {
26+
if (state == SpellCastState.NORMAL) {
27+
TargetInfo<LivingEntity> targetInfo = getTargetedEntity(player, power);
28+
if (targetInfo == null) return noTarget(player);
29+
LivingEntity target = targetInfo.getTarget();
30+
if (target == null) return noTarget(player);
31+
target.setGliding(targetState.getBooleanState(target.isGliding()));
32+
}
33+
return PostCastAction.HANDLE_NORMALLY;
34+
}
35+
36+
@Override
37+
public boolean castAtEntity(Player caster, LivingEntity target, float power) {
38+
target.setGliding(targetState.getBooleanState(target.isGliding()));
39+
return true;
40+
}
41+
42+
@Override
43+
public boolean castAtEntity(LivingEntity target, float power) {
44+
return castAtEntity(null, target, power);
45+
}
46+
47+
48+
enum TargetBooleanState {
49+
50+
ON("on", "yes", "true", "enable", "enabled") {
51+
52+
@Override
53+
public boolean getBooleanState(boolean current) {
54+
return true;
55+
}
56+
57+
},
58+
59+
60+
OFF("off", "no", "false", "disable", "disabled") {
61+
62+
@Override
63+
public boolean getBooleanState(boolean current) {
64+
return false;
65+
}
66+
67+
},
68+
69+
70+
TOGGLE("toggle", "switch") {
71+
72+
@Override
73+
public boolean getBooleanState(boolean current) {
74+
return !current;
75+
}
76+
77+
}
78+
79+
;
80+
81+
private final String[] names;
82+
83+
TargetBooleanState(String... names) {
84+
this.names = names;
85+
}
86+
87+
public abstract boolean getBooleanState(boolean current);
88+
89+
private static Map<String, TargetBooleanState> nameToState = new HashMap<>();
90+
static {
91+
for (TargetBooleanState value : TargetBooleanState.values()) {
92+
for (String name : value.names) {
93+
nameToState.put(name, value);
94+
}
95+
}
96+
}
97+
98+
public static TargetBooleanState getFromName(String name) {
99+
TargetBooleanState ret = nameToState.get(name.toLowerCase());
100+
if (ret == null) ret = TargetBooleanState.TOGGLE;
101+
return ret;
102+
}
103+
104+
}
105+
106+
}

0 commit comments

Comments
 (0)