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
@@ -0,0 +1,57 @@
package com.nisovin.magicspells.spells.instant;

import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;

import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.CastResult;
import com.nisovin.magicspells.util.MagicConfig;
import com.nisovin.magicspells.spells.InstantSpell;
import com.nisovin.magicspells.variables.Variable;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.util.managers.VariableManager;
import com.nisovin.magicspells.variables.variabletypes.GlobalStringVariable;
import com.nisovin.magicspells.variables.variabletypes.PlayerStringVariable;

public class CommandStorageSpell extends InstantSpell {

private final ConfigData<String> tag;
private final ConfigData<String> variable;
private final ConfigData<NamespacedKey> containerId;

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

tag = getConfigDataString("tag", null);
variable = getConfigDataString("variable", null);

containerId = getConfigDataNamespacedKey("container-id", null);
}

@Override
public CastResult cast(SpellData data) {
if (!(data.caster() instanceof Player caster)) return new CastResult(PostCastAction.ALREADY_HANDLED, data);

VariableManager manager = MagicSpells.getVariableManager();

String tag = this.tag.get(data);
NamespacedKey containerId = this.containerId.get(data);
Variable variable = manager.getVariable(this.variable.get(data));

if (tag == null || containerId == null || variable == null)
return new CastResult(PostCastAction.ALREADY_HANDLED, data);

if (variable instanceof GlobalStringVariable || variable instanceof PlayerStringVariable) {
String value = MagicSpells.getVolatileCodeHandler().getCommandStorageString(containerId, tag);
if (value != null) manager.set(variable, caster.getName(), value);
} else {
Double value = MagicSpells.getVolatileCodeHandler().getCommandStorageDouble(containerId, tag);
if (value != null) manager.set(variable, caster.getName(), value);
}

playSpellEffects(data);
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import org.bukkit.util.Vector;
import org.bukkit.inventory.ItemStack;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;

import io.papermc.paper.advancement.AdvancementDisplay.Frame;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;

import com.nisovin.magicspells.util.glow.GlowManager;

import io.papermc.paper.advancement.AdvancementDisplay.Frame;

public class VolatileCodeDisabled extends VolatileCodeHandle {

public VolatileCodeDisabled() {
Expand Down Expand Up @@ -81,4 +84,14 @@ public long countEntitySchedulerTasks() {
return -1;
}

@Override
public @Nullable String getCommandStorageString(Key containerId, String tagKey) {
return null;
}

@Override
public @Nullable Double getCommandStorageDouble(Key containerId, String tagKey) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;

import io.papermc.paper.adventure.PaperAdventure;
Expand All @@ -45,10 +46,12 @@
import com.nisovin.magicspells.volatilecode.VolatileCodeHandle;
import com.nisovin.magicspells.volatilecode.VolatileCodeHelper;

import net.minecraft.nbt.Tag;
import net.minecraft.util.ARGB;
import net.minecraft.core.BlockPos;
import net.minecraft.advancements.*;
import net.minecraft.world.phys.Vec3;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.protocol.game.*;
Expand Down Expand Up @@ -321,4 +324,20 @@ public long countEntitySchedulerTasks() {
return null;
}

@Override
public @Nullable String getCommandStorageString(Key containerId, String tagKey) {
Tag tag = getCommandStorage(containerId).get(tagKey);
return tag == null ? null : tag.asString().orElse(tag.toString());
}

@Override
public @Nullable Double getCommandStorageDouble(Key containerId, String tagKey) {
return getCommandStorage(containerId).getDouble(tagKey).orElse(null);
}

private CompoundTag getCommandStorage(Key id) {
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
return server.getCommandStorage().get(PaperAdventure.asVanilla(id));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.jetbrains.annotations.Nullable;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;

import com.nisovin.magicspells.util.glow.GlowManager;
Expand Down Expand Up @@ -59,4 +60,10 @@ public Object getVolatileParticleData(
return null;
}

@Nullable
public abstract String getCommandStorageString(Key containerId, String tagKey);

@Nullable
public abstract Double getCommandStorageDouble(Key containerId, String tagKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.bukkit.craftbukkit.entity.CraftLivingEntity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;

import io.papermc.paper.adventure.PaperAdventure;
Expand All @@ -42,10 +44,12 @@
import com.nisovin.magicspells.volatilecode.VolatileCodeHandle;
import com.nisovin.magicspells.volatilecode.VolatileCodeHelper;

import net.minecraft.nbt.Tag;
import net.minecraft.util.ARGB;
import net.minecraft.core.BlockPos;
import net.minecraft.advancements.*;
import net.minecraft.world.phys.Vec3;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -291,4 +295,20 @@ public long countEntitySchedulerTasks() {
return count;
}

@Override
public @Nullable String getCommandStorageString(Key containerId, String tagKey) {
Tag tag = getCommandStorage(containerId).get(tagKey);
return tag == null ? null : tag.asString().orElse(tag.toString());
}

@Override
public @Nullable Double getCommandStorageDouble(Key containerId, String tagKey) {
return getCommandStorage(containerId).getDouble(tagKey).orElse(null);
}

private CompoundTag getCommandStorage(Key id) {
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
return server.getCommandStorage().get(PaperAdventure.asVanilla(id));
}

}
Loading