Skip to content

Commit

Permalink
Backup config before modification
Browse files Browse the repository at this point in the history
  • Loading branch information
weaves7 committed Mar 25, 2020
1 parent 9e199fd commit 5421107
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 138 deletions.
24 changes: 23 additions & 1 deletion src/main/java/org/wargamer2010/signshop/SignShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.wargamer2010.signshop.blocks.SignShopBooks;
import org.wargamer2010.signshop.blocks.SignShopItemMeta;
import org.wargamer2010.signshop.commands.*;
import org.wargamer2010.signshop.configuration.ColorUtil;
import org.wargamer2010.signshop.configuration.SignShopConfig;
import org.wargamer2010.signshop.configuration.Storage;
import org.wargamer2010.signshop.configuration.configUtil;
import org.wargamer2010.signshop.listeners.SignShopBlockListener;
import org.wargamer2010.signshop.listeners.SignShopLoginListener;
import org.wargamer2010.signshop.listeners.SignShopPlayerListener;
Expand All @@ -25,6 +28,7 @@
import org.wargamer2010.signshop.player.PlayerMetadata;
import org.wargamer2010.signshop.timing.TimeManager;
import org.wargamer2010.signshop.util.DataConverter;
import org.wargamer2010.signshop.util.SSTimeUtil;
import org.wargamer2010.signshop.util.commandUtil;
import org.wargamer2010.signshop.worth.CMIWorthHandler;
import org.wargamer2010.signshop.worth.EssentialsWorthHandler;
Expand All @@ -38,14 +42,14 @@
import java.util.logging.*;

public class SignShop extends JavaPlugin {
public static final int DATA_VERSION = 3;
private static final Logger logger = Logger.getLogger("Minecraft");
private static final Logger transactionlogger = Logger.getLogger("SignShop_Transactions");
public static WorthHandler worthHandler;
private static SignShop instance;
//Statics
private static Storage store;
private static TimeManager manager = null;
public static final int DATA_VERSION = 3;
//Permissions
private static boolean USE_PERMISSIONS = false;
// Commands
Expand Down Expand Up @@ -117,6 +121,9 @@ public void onEnable() {

setupCommands();

// Backup config if it is an old version
backupOldConfig();

SignShopConfig.init();
SignShopBooks.init();
PlayerMetadata.init();
Expand Down Expand Up @@ -278,6 +285,21 @@ private void registerSSListeners() {
pm.registerEvents(new SharedMoneyTransaction(), this);
}

private void backupOldConfig() {
FileConfiguration ymlThing = configUtil.loadYMLFromPluginFolder(SignShopConfig.configFilename);
File configFile = new File(SignShop.getInstance().getDataFolder(), SignShopConfig.configFilename);

if ((ymlThing != null && configFile.exists())
&& (!ymlThing.isSet("ConfigVersionDoNotTouch") || ymlThing.getInt("ConfigVersionDoNotTouch") != SignShopConfig.getConfigVersionDoNotTouch())) {

SignShop.log("Old config detected, backing it up before modifiying it.", Level.INFO);
File configBackup = new File(SignShop.getInstance().getDataFolder(), "configBackup" + SSTimeUtil.getDateTimeStamp() + ".yml");
FileUtil.copy(configFile, configBackup);
ymlThing.set("ConfigVersionDoNotTouch", SignShopConfig.getConfigVersionDoNotTouch());
SignShopConfig.saveConfig(ymlThing, configFile);
}
}

private static class TransferFormatter extends Formatter {
private final DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");

Expand Down
Loading

0 comments on commit 5421107

Please sign in to comment.