From 3b0cb8da583f40cf4c1e94da8d7c220d288770cf Mon Sep 17 00:00:00 2001 From: JasperLorelai Date: Sat, 18 Jul 2026 23:54:54 +0200 Subject: [PATCH] feat: Command Storage retrieval --- .../spells/instant/CommandStorageSpell.java | 57 +++++++++++++++++++ .../volatilecode/VolatileCodeDisabled.java | 17 +++++- .../latest/VolatileCodeLatest.java | 19 +++++++ .../volatilecode/VolatileCodeHandle.java | 7 +++ .../v26_1_2/VolatileCode_v26_1_2.java | 20 +++++++ 5 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/com/nisovin/magicspells/spells/instant/CommandStorageSpell.java diff --git a/core/src/main/java/com/nisovin/magicspells/spells/instant/CommandStorageSpell.java b/core/src/main/java/com/nisovin/magicspells/spells/instant/CommandStorageSpell.java new file mode 100644 index 000000000..a49a7f8b7 --- /dev/null +++ b/core/src/main/java/com/nisovin/magicspells/spells/instant/CommandStorageSpell.java @@ -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 tag; + private final ConfigData variable; + private final ConfigData 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); + } + +} diff --git a/core/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeDisabled.java b/core/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeDisabled.java index 913c897f1..5818c4cda 100644 --- a/core/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeDisabled.java +++ b/core/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeDisabled.java @@ -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() { @@ -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; + } + } diff --git a/nms/latest/src/main/java/com/nisovin/magicspells/volatilecode/latest/VolatileCodeLatest.java b/nms/latest/src/main/java/com/nisovin/magicspells/volatilecode/latest/VolatileCodeLatest.java index 1cf9c2fb8..d1391bbe7 100644 --- a/nms/latest/src/main/java/com/nisovin/magicspells/volatilecode/latest/VolatileCodeLatest.java +++ b/nms/latest/src/main/java/com/nisovin/magicspells/volatilecode/latest/VolatileCodeLatest.java @@ -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; @@ -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.*; @@ -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)); + } + } diff --git a/nms/shared/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeHandle.java b/nms/shared/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeHandle.java index d13bebd53..16fcd293d 100644 --- a/nms/shared/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeHandle.java +++ b/nms/shared/src/main/java/com/nisovin/magicspells/volatilecode/VolatileCodeHandle.java @@ -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; @@ -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); + } diff --git a/nms/v26_1_2/src/main/java/com/nisovin/magicspells/volatilecode/v26_1_2/VolatileCode_v26_1_2.java b/nms/v26_1_2/src/main/java/com/nisovin/magicspells/volatilecode/v26_1_2/VolatileCode_v26_1_2.java index a877d444e..1dd43237f 100644 --- a/nms/v26_1_2/src/main/java/com/nisovin/magicspells/volatilecode/v26_1_2/VolatileCode_v26_1_2.java +++ b/nms/v26_1_2/src/main/java/com/nisovin/magicspells/volatilecode/v26_1_2/VolatileCode_v26_1_2.java @@ -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; @@ -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; @@ -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)); + } + }