Skip to content

Commit b519469

Browse files
Added targeted.DataSpell
Has an option called variable-name which should be set to the name of a string variable to save data into. There is an option called data-element which defaults to "uuid" and accepts a string. This field determines what data will be saved to the string variable. Current valid options are "uuid", "name", "customname", and "entitytype".
1 parent 85d899d commit b519469

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.nisovin.magicspells.spells.targeted;
2+
3+
import com.nisovin.magicspells.MagicSpells;
4+
import com.nisovin.magicspells.spells.TargetedSpell;
5+
import com.nisovin.magicspells.util.EntityData;
6+
import com.nisovin.magicspells.util.MagicConfig;
7+
import com.nisovin.magicspells.util.TargetInfo;
8+
import com.nisovin.magicspells.util.data.DataEntity;
9+
import org.bukkit.entity.LivingEntity;
10+
import org.bukkit.entity.Player;
11+
12+
import java.util.function.Function;
13+
14+
public class DataSpell extends TargetedSpell {
15+
16+
17+
private String variableName;
18+
private Function<? super LivingEntity, String> dataElement;
19+
20+
public DataSpell(MagicConfig config, String spellName) {
21+
super(config, spellName);
22+
23+
this.variableName = getConfigString("variable-name", null);
24+
this.dataElement = DataEntity.getDataFunction(getConfigString("data-element", "uuid"));
25+
26+
}
27+
28+
@Override
29+
public void initialize() {
30+
if (variableName == null) {
31+
MagicSpells.error("variable-name is null for DataSpell");
32+
return;
33+
}
34+
35+
if (dataElement == null) MagicSpells.error("Invalid option defined for data-element");
36+
}
37+
38+
@Override
39+
public PostCastAction castSpell(Player player, SpellCastState state, float power, String[] args) {
40+
if (state == SpellCastState.NORMAL) {
41+
TargetInfo<LivingEntity> targetInfo = getTargetedEntity(player, power);
42+
if (targetInfo == null) return noTarget(player);
43+
LivingEntity target = targetInfo.getTarget();
44+
if (target == null) return noTarget(player);
45+
46+
String value = dataElement.apply(target);
47+
MagicSpells.getVariableManager().set(variableName, player, value);
48+
49+
playSpellEffects(player, target);
50+
}
51+
return PostCastAction.HANDLE_NORMALLY;
52+
}
53+
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.nisovin.magicspells.util.data;
2+
3+
import org.bukkit.entity.Entity;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
9+
public class DataEntity {
10+
11+
private static Map<String, Function<Entity, String>> dataElements = new HashMap<>();
12+
13+
static {
14+
dataElements.put("uuid", entity -> entity.getUniqueId().toString());
15+
dataElements.put("name", Entity::getName);
16+
dataElements.put("customname", Entity::getCustomName);
17+
dataElements.put("entitytype", entity -> entity.getType().name());
18+
}
19+
20+
public static Function<Entity, String> getDataFunction(String elementId) {
21+
return dataElements.get(elementId);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)