|
2 | 2 |
|
3 | 3 | import java.util.function.Function; |
4 | 4 |
|
| 5 | +import org.bukkit.Location; |
5 | 6 | import org.bukkit.entity.Player; |
6 | 7 | import org.bukkit.entity.LivingEntity; |
7 | 8 |
|
|
10 | 11 | import com.nisovin.magicspells.variables.Variable; |
11 | 12 | import com.nisovin.magicspells.spells.TargetedSpell; |
12 | 13 | import com.nisovin.magicspells.util.config.ConfigData; |
13 | | -import com.nisovin.magicspells.spells.TargetedEntitySpell; |
| 14 | +import com.nisovin.magicspells.util.data.DataLocation; |
14 | 15 | import com.nisovin.magicspells.util.data.DataLivingEntity; |
| 16 | +import com.nisovin.magicspells.spells.TargetedEntitySpell; |
| 17 | +import com.nisovin.magicspells.spells.TargetedLocationSpell; |
15 | 18 | import com.nisovin.magicspells.variables.variabletypes.GlobalVariable; |
16 | 19 | import com.nisovin.magicspells.variables.variabletypes.GlobalStringVariable; |
17 | 20 |
|
18 | | -public class DataSpell extends TargetedSpell implements TargetedEntitySpell { |
| 21 | +public class DataSpell extends TargetedSpell implements TargetedEntitySpell, TargetedLocationSpell { |
19 | 22 |
|
20 | | - private final ConfigData<Function<? super LivingEntity, String>> dataElement; |
| 23 | + private final ConfigData<String> dataElement; |
21 | 24 | private final ConfigData<String> variableName; |
22 | 25 |
|
23 | 26 | public DataSpell(MagicConfig config, String spellName) { |
24 | 27 | super(config, spellName); |
25 | 28 |
|
26 | 29 | variableName = getConfigDataString("variable-name", ""); |
27 | | - |
28 | | - ConfigData<String> supplier = getConfigDataString("data-element", "uuid"); |
29 | | - if (supplier.isConstant()) { |
30 | | - Function<? super LivingEntity, String> function = DataLivingEntity.getDataFunction(supplier.get()); |
31 | | - dataElement = data -> function; |
32 | | - } else { |
33 | | - dataElement = data -> DataLivingEntity.getDataFunction(supplier.get(data)); |
34 | | - } |
| 30 | + dataElement = getConfigDataString("data-element", "uuid"); |
35 | 31 | } |
36 | 32 |
|
37 | 33 | @Override |
38 | 34 | public CastResult cast(SpellData data) { |
39 | 35 | TargetInfo<LivingEntity> info = getTargetedEntity(data); |
40 | | - if (info.noTarget()) return noTarget(info); |
| 36 | + if (info.cancelled()) return noTarget(info); |
| 37 | + |
| 38 | + if (!info.empty()) return castAtEntity(info.spellData()); |
41 | 39 |
|
42 | | - return castAtEntity(info.spellData()); |
| 40 | + TargetInfo<Location> locationInfo = getTargetedBlockLocation(data); |
| 41 | + if (locationInfo.noTarget()) return noTarget(locationInfo); |
| 42 | + |
| 43 | + return castAtLocation(locationInfo.spellData()); |
43 | 44 | } |
44 | 45 |
|
45 | 46 | @Override |
46 | 47 | public CastResult castAtEntity(SpellData data) { |
| 48 | + Function<? super LivingEntity, String> dataElement = DataLivingEntity.getDataFunction(this.dataElement.get(data)); |
| 49 | + if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); |
| 50 | + |
| 51 | + return applyValue(data, dataElement.apply(data.target())); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public CastResult castAtLocation(SpellData data) { |
| 56 | + Function<Location, String> dataElement = DataLocation.getDataFunction(this.dataElement.get(data)); |
| 57 | + if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); |
| 58 | + |
| 59 | + return applyValue(data, dataElement.apply(data.location())); |
| 60 | + } |
| 61 | + |
| 62 | + private CastResult applyValue(SpellData data, String value) { |
47 | 63 | Variable variable = MagicSpells.getVariableManager().getVariable(variableName.get(data)); |
48 | 64 | if (variable == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); |
49 | 65 |
|
50 | 66 | Player caster = data.caster() instanceof Player player ? player : null; |
51 | 67 | if (caster == null && !(variable instanceof GlobalVariable) && !(variable instanceof GlobalStringVariable)) |
52 | 68 | return new CastResult(PostCastAction.ALREADY_HANDLED, data); |
53 | 69 |
|
54 | | - Function<? super LivingEntity, String> dataElement = this.dataElement.get(data); |
55 | | - if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); |
56 | | - |
57 | | - String value = dataElement.apply(data.target()); |
58 | 70 | MagicSpells.getVariableManager().set(variable, caster == null ? null : caster.getName(), value); |
59 | 71 |
|
60 | 72 | playSpellEffects(data); |
|
0 commit comments