Skip to content

Commit

Permalink
legacy save data auto-updater
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 5, 2020
1 parent 4643838 commit 7539054
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 8 deletions.
3 changes: 3 additions & 0 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -532,6 +532,9 @@ public void reloadSaves() {
else {
serverFlagMap = new MapTagFlagTracker();
}
if (new File(getDataFolder(), "saves.yml").exists()) {
LegacySavesUpdater.updateLegacySaves();
}
Bukkit.getServer().getPluginManager().callEvent(new SavesReloadEvent());
}

Expand Down
Expand Up @@ -554,6 +554,10 @@ public LocationTag getBlockLocation() {

@Override
public AbstractFlagTracker getFlagTracker() {
if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) {
Debug.echoError("Location flags are only available in 1.16+");
return null;
}
MapTag map = (MapTag) DataPersistenceHelper.getDenizenKey(getChunk(), "flag_tracker_" + getBlockX() + "_" + getBlockY() + "_" + getBlockZ());
if (map == null) {
map = new MapTag();
Expand All @@ -563,6 +567,10 @@ public AbstractFlagTracker getFlagTracker() {

@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) {
Debug.echoError("Location flags are only available in 1.16+");
return;
}
DataPersistenceHelper.setDenizenKey(getChunk(), "flag_tracker_" + getBlockX() + "_" + getBlockY() + "_" + getBlockZ(), ((MapTagFlagTracker) tracker).map);
}

Expand Down
Expand Up @@ -4,7 +4,6 @@
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.inventory.SlotHelper;
import com.denizenscript.denizen.utilities.nbt.CustomNBT;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
Expand Down
@@ -0,0 +1,120 @@
package com.denizenscript.denizen.utilities;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.UUID;

public class LegacySavesUpdater {

public static void updateLegacySaves() {
Debug.log("==== UPDATING LEGACY SAVES TO NEW FLAG ENGINE ====");
File savesFile = new File(Denizen.getInstance().getDataFolder(), "saves.yml");
FileConfiguration saveSection = YamlConfiguration.loadConfiguration(savesFile);
savesFile.renameTo(new File(Denizen.getInstance().getDataFolder(), "saves.yml.bak"));
if (saveSection.contains("Global")) {
Debug.log("==== Update global data ====");
ConfigurationSection globalSection = saveSection.getConfigurationSection("Global");
if (globalSection.contains("Flags")) {
applyFlags(Denizen.getInstance().serverFlagMap, globalSection.getConfigurationSection("Flags"));
}
if (globalSection.contains("Scripts")) {
ConfigurationSection scriptsSection = globalSection.getConfigurationSection("Scripts");
for (String script : scriptsSection.getKeys(false)) {
ConfigurationSection scriptSection = scriptsSection.getConfigurationSection(script);
if (scriptSection.contains("Cooldown Time")) {
long time = scriptSection.getLong("Cooldown Time");
TimeTag cooldown = new TimeTag(time);
Denizen.getInstance().serverFlagMap.setFlag("__interact_cooldown." + script, cooldown, cooldown);
}
}
}
}
if (saveSection.contains("Players")) {
Debug.log("==== Update player data ====");
ConfigurationSection playerSection = saveSection.getConfigurationSection("Players");
for (String plPrefix : playerSection.getKeys(false)) {
ConfigurationSection subSection = playerSection.getConfigurationSection(plPrefix);
for (String uuidString : subSection.getKeys(false)) {
UUID id = UUID.fromString(uuidString.substring(0, 8) + "-" + uuidString.substring(8, 12) + "-" + uuidString.substring(12, 16) + "-" + uuidString.substring(16, 20) + "-" + uuidString.substring(20, 32));
PlayerTag player = PlayerTag.valueOf(id.toString(), CoreUtilities.errorButNoDebugContext);
if (player == null) {
Debug.echoError("Cannot update data for player with id: " + uuidString);
continue;
}
ConfigurationSection actual = subSection.getConfigurationSection(uuidString);
AbstractFlagTracker tracker = player.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(tracker, actual.getConfigurationSection("Flags"));
}
if (actual.contains("Scripts")) {
ConfigurationSection scriptsSection = actual.getConfigurationSection("Scripts");
for (String script : scriptsSection.getKeys(false)) {
ConfigurationSection scriptSection = scriptsSection.getConfigurationSection(script);
if (scriptSection.contains("Current Step")) {
tracker.setFlag("__interact_step." + script, new ElementTag(scriptSection.getString("Current Step")), null);
}
if (scriptSection.contains("Cooldown Time")) {
long time = scriptSection.getLong("Cooldown Time");
TimeTag cooldown = new TimeTag(time);
tracker.setFlag("__interact_cooldown." + script, cooldown, cooldown);
}
}
}
player.reapplyTracker(tracker);
}
}
}
if (saveSection.contains("NPCs")) {
Debug.log("==== Update NPC data ====");
ConfigurationSection npcsSection = saveSection.getConfigurationSection("NPCs");
for (String npcId : npcsSection.getKeys(false)) {
ConfigurationSection actual = npcsSection.getConfigurationSection(npcId);
NPCTag npc = NPCTag.valueOf(npcId, CoreUtilities.errorButNoDebugContext);
if (npc == null) {
Debug.echoError("Cannot update data for NPC with id: " + npcId);
continue;
}
AbstractFlagTracker tracker = npc.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(tracker, actual.getConfigurationSection("Flags"));
}
npc.reapplyTracker(tracker);
}
}
Denizen.getInstance().saveSaves();
Debug.log("==== Done updating legacy saves ====");
}

public static void applyFlags(AbstractFlagTracker tracker, ConfigurationSection section) {
try {
for (String flagName : section.getKeys(false)) {
if (flagName.endsWith("-expiration")) {
continue;
}
TimeTag expireAt = null;
if (section.contains(flagName + "-expiration")) {
long expireTime = section.getLong(flagName + "-expiration");
expireAt = new TimeTag(expireTime);
}
Object value = section.get(flagName);
ObjectTag setAs = CoreUtilities.objectToTagForm(value, CoreUtilities.errorButNoDebugContext);
tracker.setFlag(flagName, setAs, expireAt);
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}
Expand Up @@ -273,7 +273,7 @@ public void save(CommandContext args, CommandSender sender) throws CommandExcept
/*
* DENIZEN RELOAD
*/
@Command(aliases = {"denizen"}, usage = "reload (saves|notables|config|scripts) (-a)",
@Command(aliases = {"denizen"}, usage = "reload (saves|notes|config|scripts) (-a)",
desc = "Reloads various Denizen files from disk to memory.", modifiers = {"reload"},
min = 1, max = 3, permission = "denizen.basic", flags = "a")
public void reload(CommandContext args, CommandSender sender) throws CommandException {
Expand All @@ -284,7 +284,7 @@ public void reload(CommandContext args, CommandSender sender) throws CommandExce
DenizenCore.reloadScripts();
denizen.notableManager().reloadNotables();
denizen.reloadSaves();
Messaging.send(sender, "Denizen/saves.yml, Denizen/notables.yml, Denizen/config.yml, and Denizen/scripts/... reloaded from disk to memory.");
Messaging.send(sender, "Denizen save data, config, and scripts reloaded from disk to memory.");
if (ScriptHelper.hadError()) {
Messaging.sendError(sender, "There was an error loading your scripts, check the console for details!");
}
Expand All @@ -294,17 +294,17 @@ public void reload(CommandContext args, CommandSender sender) throws CommandExce
if (args.length() > 2) {
if (args.getString(1).equalsIgnoreCase("saves")) {
denizen.reloadSaves();
Messaging.send(sender, "Denizen/saves.yml reloaded from disk to memory.");
Messaging.send(sender, "Denizen save data reloaded from disk to memory.");
return;
}
else if (args.getString(1).equalsIgnoreCase("notables")) {
else if (args.getString(1).equalsIgnoreCase("notes")) {
denizen.notableManager().reloadNotables();
Messaging.send(sender, "Denizen/notables.yml reloaded from disk to memory.");
Messaging.send(sender, "Denizen note data reloaded from disk to memory.");
return;
}
else if (args.getString(1).equalsIgnoreCase("config")) {
denizen.reloadConfig();
Messaging.send(sender, "Denizen/config.yml reloaded from disk to memory.");
Messaging.send(sender, "Denizen config file reloaded from disk to memory.");
return;
}
else if (args.getString(1).equalsIgnoreCase("scripts")) {
Expand All @@ -319,7 +319,7 @@ else if (args.getString(1).equalsIgnoreCase("scripts")) {
}

Messaging.send(sender, "");
Messaging.send(sender, "<f>Specify which parts to reload. Valid options are: SAVES, NOTABLES, CONFIG, SCRIPTS");
Messaging.send(sender, "<f>Specify which parts to reload. Valid options are: SAVES, NOTES, CONFIG, SCRIPTS");
Messaging.send(sender, "<b>Example: /denizen reload scripts");
Messaging.send(sender, "<f>Use '-a' to reload all parts.");
Messaging.send(sender, "");
Expand Down

0 comments on commit 7539054

Please sign in to comment.