From afa051b1b2b18e8fa75e417c03cf13ce44d7aab6 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Wed, 27 May 2015 12:26:31 -0500 Subject: [PATCH] Move Data After Storage Changed, Closes #75 After someone reloads the plugin and they've switched the storage system then put the data, if any exists, into the new storage system. This means a player can go from flatfile to mysql or from mysql to flatfile without having to do any manual work. --- .../java/com/graywolf336/jail/JailIO.java | 55 ++++++++++++------- .../java/com/graywolf336/jail/beans/Jail.java | 2 +- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index 5a30e62..b50aae9 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -37,25 +37,12 @@ public class JailIO { private JailMain pl; private FileConfiguration flat, records; private Connection con; - private int storage; //0 = flatfile, 1 = sqlite, 2 = mysql + private int storage = -1; //0 = flatfile, 1 = sqlite, 2 = mysql private String prefix; + private boolean changed = false; protected JailIO(JailMain plugin) { this.pl = plugin; - - String st = pl.getConfig().getString("storage.type", "flatfile"); - if(st.equalsIgnoreCase("sqlite")) { - storage = 1; - prefix = pl.getConfig().getString("storage.mysql.prefix"); - }else if(st.equalsIgnoreCase("mysql")) { - storage = 2; - prefix = pl.getConfig().getString("storage.mysql.prefix"); - }else { - storage = 0; - } - - pl.debug("The storage type " + st + " with the type being " + storage + "."); - if(!pl.inDebug()) pl.getLogger().info("Storage type selected: " + st); } /** Loads the language file from disk, if there is none then we save the default one. */ @@ -98,6 +85,30 @@ public class JailIO { /** Prepares the storage engine to be used, returns true if everything went good. */ protected boolean prepareStorage(boolean doInitialCreations) { + int inital = storage == -1 ? -1 : storage; + + String st = pl.getConfig().getString("storage.type", "flatfile"); + if(st.equalsIgnoreCase("sqlite")) { + storage = 1; + prefix = pl.getConfig().getString("storage.mysql.prefix"); + }else if(st.equalsIgnoreCase("mysql")) { + storage = 2; + prefix = pl.getConfig().getString("storage.mysql.prefix"); + }else { + storage = 0; + } + + //Determine if we changed storage types midstream + //this way we can know whether to save EVERYTHING + //or not afterwards + if(inital != -1 && inital != storage) { + pl.debug("We changed storage types! We used to be " + inital + " and changed to " + storage + "."); + changed = true; + } + + pl.debug("The storage type " + st + " with the type being " + storage + "."); + if(!pl.inDebug()) pl.getLogger().info("Storage type selected: " + st); + switch(storage) { case 1: try { @@ -152,6 +163,11 @@ public class JailIO { records = YamlConfiguration.loadConfiguration(new File(pl.getDataFolder(), "records.yml")); break; } + + if(changed) { + changed = false; + this.saveEverything(); + } return true; } @@ -711,7 +727,7 @@ public class JailIO { } /** Saves everything about a jail, don't usually call this. */ - public void saveEverything() { + protected void saveEverything() { long st = System.currentTimeMillis(); for(Jail j : pl.getJailManager().getJails()) { @@ -721,7 +737,7 @@ public class JailIO { //when we are not using the flatfile storage if(storage != 0) { for(Cell c : j.getCells()) { - saveCell(j, c); + saveCell(j, c, true); } } } @@ -953,9 +969,10 @@ public class JailIO { } } - public void saveCell(Jail j, Cell c) { + public void saveCell(Jail j, Cell c, boolean force) { //if the cell hasn't changed, no need to save it again - if(!c.hasChanged()) return; + //unless they're forcing the save + if(!c.hasChanged() && !force) return; switch(storage) { case 1: diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index f778420..6a9787a 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -161,7 +161,7 @@ public class Jail { /** Adds a cell to the Jail. */ public boolean addCell(Cell cell, boolean save) { - if(save) plugin.getJailIO().saveCell(this, cell); + if(save) plugin.getJailIO().saveCell(this, cell, false); //Check if it already exists or not if(this.cells.containsKey(cell.getName())) return false;