diff --git a/core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java b/core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java index 33f9cb2bb..d9e47651a 100644 --- a/core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java +++ b/core/src/main/java/com/nisovin/magicspells/spells/targeted/DataSpell.java @@ -2,6 +2,7 @@ import java.util.function.Function; +import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.entity.LivingEntity; @@ -10,40 +11,55 @@ import com.nisovin.magicspells.variables.Variable; import com.nisovin.magicspells.spells.TargetedSpell; import com.nisovin.magicspells.util.config.ConfigData; -import com.nisovin.magicspells.spells.TargetedEntitySpell; +import com.nisovin.magicspells.util.data.DataLocation; import com.nisovin.magicspells.util.data.DataLivingEntity; +import com.nisovin.magicspells.spells.TargetedEntitySpell; +import com.nisovin.magicspells.spells.TargetedLocationSpell; import com.nisovin.magicspells.variables.variabletypes.GlobalVariable; import com.nisovin.magicspells.variables.variabletypes.GlobalStringVariable; -public class DataSpell extends TargetedSpell implements TargetedEntitySpell { +public class DataSpell extends TargetedSpell implements TargetedEntitySpell, TargetedLocationSpell { - private final ConfigData> dataElement; + private final ConfigData dataElement; private final ConfigData variableName; public DataSpell(MagicConfig config, String spellName) { super(config, spellName); variableName = getConfigDataString("variable-name", ""); - - ConfigData supplier = getConfigDataString("data-element", "uuid"); - if (supplier.isConstant()) { - Function function = DataLivingEntity.getDataFunction(supplier.get()); - dataElement = data -> function; - } else { - dataElement = data -> DataLivingEntity.getDataFunction(supplier.get(data)); - } + dataElement = getConfigDataString("data-element", "uuid"); } @Override public CastResult cast(SpellData data) { TargetInfo info = getTargetedEntity(data); - if (info.noTarget()) return noTarget(info); + if (info.cancelled()) return noTarget(info); + + if (!info.empty()) return castAtEntity(info.spellData()); - return castAtEntity(info.spellData()); + TargetInfo locationInfo = getTargetedBlockLocation(data); + if (locationInfo.noTarget()) return noTarget(locationInfo); + + return castAtLocation(locationInfo.spellData()); } @Override public CastResult castAtEntity(SpellData data) { + Function dataElement = DataLivingEntity.getDataFunction(this.dataElement.get(data)); + if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); + + return applyValue(data, dataElement.apply(data.target())); + } + + @Override + public CastResult castAtLocation(SpellData data) { + Function dataElement = DataLocation.getDataFunction(this.dataElement.get(data)); + if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); + + return applyValue(data, dataElement.apply(data.location())); + } + + private CastResult applyValue(SpellData data, String value) { Variable variable = MagicSpells.getVariableManager().getVariable(variableName.get(data)); if (variable == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); @@ -51,10 +67,6 @@ public CastResult castAtEntity(SpellData data) { if (caster == null && !(variable instanceof GlobalVariable) && !(variable instanceof GlobalStringVariable)) return new CastResult(PostCastAction.ALREADY_HANDLED, data); - Function dataElement = this.dataElement.get(data); - if (dataElement == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data); - - String value = dataElement.apply(data.target()); MagicSpells.getVariableManager().set(variable, caster == null ? null : caster.getName(), value); playSpellEffects(data); diff --git a/core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java b/core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java new file mode 100644 index 000000000..2ca323f09 --- /dev/null +++ b/core/src/main/java/com/nisovin/magicspells/util/data/DataLocation.java @@ -0,0 +1,44 @@ +package com.nisovin.magicspells.util.data; + +import java.util.Map; +import java.util.HashMap; +import java.util.function.Function; + +import org.bukkit.Location; +import org.bukkit.block.Block; + +public class DataLocation { + + private static final Map> dataElements = new HashMap<>(); + + static { + dataElements.put("location.biome", location -> location.getBlock().getBiome().toString()); + dataElements.put("location.block.data", DataLocation::blockData); + dataElements.put("location.block.type", location -> location.getBlock().getType().name()); + dataElements.put("location.elevation", location -> location.getWorld().getHighestBlockYAt(location) + ""); + dataElements.put("location.light", location -> location.getBlock().getLightLevel() + ""); + dataElements.put("location.light.blocks", location -> location.getBlock().getLightFromBlocks() + ""); + dataElements.put("location.light.sky", location -> location.getBlock().getLightFromSky() + ""); + dataElements.put("location", Location::toString); + dataElements.put("location.blockx", location -> location.getBlockX() + ""); + dataElements.put("location.blocky", location -> location.getBlockY() + ""); + dataElements.put("location.blockz", location -> location.getBlockZ() + ""); + dataElements.put("location.pitch", location -> location.getPitch() + ""); + dataElements.put("location.x", location -> location.getX() + ""); + dataElements.put("location.y", location -> location.getY() + ""); + dataElements.put("location.yaw", location -> location.getYaw() + ""); + dataElements.put("location.z", location -> location.getZ() + ""); + dataElements.put("world", location -> location.getWorld().toString()); + dataElements.put("world.name", location -> location.getWorld().getName()); + } + + private static String blockData(Location location) { + Block block = location.getBlock(); + return block.getBlockData().getAsString(); + } + + public static Function getDataFunction(String elementId) { + return dataElements.get(elementId); + } + +}