Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.function.Function;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.entity.LivingEntity;

Expand All @@ -10,51 +11,62 @@
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<Function<? super LivingEntity, String>> dataElement;
private final ConfigData<String> dataElement;
private final ConfigData<String> variableName;

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

variableName = getConfigDataString("variable-name", "");

ConfigData<String> supplier = getConfigDataString("data-element", "uuid");
if (supplier.isConstant()) {
Function<? super LivingEntity, String> 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<LivingEntity> info = getTargetedEntity(data);
if (info.noTarget()) return noTarget(info);
if (info.cancelled()) return noTarget(info);

if (!info.empty()) return castAtEntity(info.spellData());
Comment thread
DragonsAscent marked this conversation as resolved.

return castAtEntity(info.spellData());
TargetInfo<Location> locationInfo = getTargetedBlockLocation(data);
if (locationInfo.noTarget()) return noTarget(locationInfo);

return castAtLocation(locationInfo.spellData());
}

@Override
public CastResult castAtEntity(SpellData data) {
Function<? super LivingEntity, String> 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<Location, String> 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);

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

Function<? super LivingEntity, String> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Function<Location, String>> 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<Location, String> getDataFunction(String elementId) {
return dataElements.get(elementId);
}

}
Loading