Skip to content

Commit 110b359

Browse files
feat: DataSpell location elements (#1097)
* feat: Enhance DataSpell * Update core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java Co-authored-by: JasperLorelai <contact@jasperlorelai.eu> * Update core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java Co-authored-by: JasperLorelai <contact@jasperlorelai.eu> * Update core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java Co-authored-by: JasperLorelai <contact@jasperlorelai.eu> * Update core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java Co-authored-by: JasperLorelai <contact@jasperlorelai.eu> * Update core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java --------- Co-authored-by: JasperLorelai <contact@jasperlorelai.eu>
1 parent f92ff63 commit 110b359

2 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.function.Function;
44

5+
import org.bukkit.Location;
56
import org.bukkit.entity.Player;
67
import org.bukkit.entity.LivingEntity;
78

@@ -10,51 +11,62 @@
1011
import com.nisovin.magicspells.variables.Variable;
1112
import com.nisovin.magicspells.spells.TargetedSpell;
1213
import com.nisovin.magicspells.util.config.ConfigData;
13-
import com.nisovin.magicspells.spells.TargetedEntitySpell;
14+
import com.nisovin.magicspells.util.data.DataLocation;
1415
import com.nisovin.magicspells.util.data.DataLivingEntity;
16+
import com.nisovin.magicspells.spells.TargetedEntitySpell;
17+
import com.nisovin.magicspells.spells.TargetedLocationSpell;
1518
import com.nisovin.magicspells.variables.variabletypes.GlobalVariable;
1619
import com.nisovin.magicspells.variables.variabletypes.GlobalStringVariable;
1720

18-
public class DataSpell extends TargetedSpell implements TargetedEntitySpell {
21+
public class DataSpell extends TargetedSpell implements TargetedEntitySpell, TargetedLocationSpell {
1922

20-
private final ConfigData<Function<? super LivingEntity, String>> dataElement;
23+
private final ConfigData<String> dataElement;
2124
private final ConfigData<String> variableName;
2225

2326
public DataSpell(MagicConfig config, String spellName) {
2427
super(config, spellName);
2528

2629
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");
3531
}
3632

3733
@Override
3834
public CastResult cast(SpellData data) {
3935
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());
4139

42-
return castAtEntity(info.spellData());
40+
TargetInfo<Location> locationInfo = getTargetedBlockLocation(data);
41+
if (locationInfo.noTarget()) return noTarget(locationInfo);
42+
43+
return castAtLocation(locationInfo.spellData());
4344
}
4445

4546
@Override
4647
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) {
4763
Variable variable = MagicSpells.getVariableManager().getVariable(variableName.get(data));
4864
if (variable == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data);
4965

5066
Player caster = data.caster() instanceof Player player ? player : null;
5167
if (caster == null && !(variable instanceof GlobalVariable) && !(variable instanceof GlobalStringVariable))
5268
return new CastResult(PostCastAction.ALREADY_HANDLED, data);
5369

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());
5870
MagicSpells.getVariableManager().set(variable, caster == null ? null : caster.getName(), value);
5971

6072
playSpellEffects(data);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.nisovin.magicspells.util.data;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
import java.util.function.Function;
6+
7+
import org.bukkit.Location;
8+
import org.bukkit.block.Block;
9+
10+
public class DataLocation {
11+
12+
private static final Map<String, Function<Location, String>> dataElements = new HashMap<>();
13+
14+
static {
15+
dataElements.put("location.biome", location -> location.getBlock().getBiome().toString());
16+
dataElements.put("location.block.data", DataLocation::blockData);
17+
dataElements.put("location.block.type", location -> location.getBlock().getType().name());
18+
dataElements.put("location.elevation", location -> location.getWorld().getHighestBlockYAt(location) + "");
19+
dataElements.put("location.light", location -> location.getBlock().getLightLevel() + "");
20+
dataElements.put("location.light.blocks", location -> location.getBlock().getLightFromBlocks() + "");
21+
dataElements.put("location.light.sky", location -> location.getBlock().getLightFromSky() + "");
22+
dataElements.put("location", Location::toString);
23+
dataElements.put("location.blockx", location -> location.getBlockX() + "");
24+
dataElements.put("location.blocky", location -> location.getBlockY() + "");
25+
dataElements.put("location.blockz", location -> location.getBlockZ() + "");
26+
dataElements.put("location.pitch", location -> location.getPitch() + "");
27+
dataElements.put("location.x", location -> location.getX() + "");
28+
dataElements.put("location.y", location -> location.getY() + "");
29+
dataElements.put("location.yaw", location -> location.getYaw() + "");
30+
dataElements.put("location.z", location -> location.getZ() + "");
31+
dataElements.put("world", location -> location.getWorld().toString());
32+
dataElements.put("world.name", location -> location.getWorld().getName());
33+
}
34+
35+
private static String blockData(Location location) {
36+
Block block = location.getBlock();
37+
return block.getBlockData().getAsString();
38+
}
39+
40+
public static Function<Location, String> getDataFunction(String elementId) {
41+
return dataElements.get(elementId);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)