diff --git a/pom.xml b/pom.xml
index c9b48a7..dda4599 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,7 +57,7 @@
at.pcgamingfreaks
PluginLib
- 1.0.1-SNAPSHOT
+ 1.0.2-SNAPSHOT
diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java
index 8695d99..5b30870 100644
--- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java
+++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016-2017 GeorgH93
+ * Copyright (C) 2016-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -33,7 +33,7 @@
public class Files extends Database
{
- private static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
+ protected static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
private final File saveFolder;
@@ -199,10 +199,7 @@ public boolean accept(File file)
if (!file.isDirectory())
{
String path = file.getAbsolutePath().toLowerCase();
- if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.'))
- {
- return true;
- }
+ return (path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.');
}
return false;
}
diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java
index cc24482..6fb7683 100644
--- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java
+++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java
@@ -33,12 +33,13 @@
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
+import java.io.File;
+import java.io.FileOutputStream;
import java.sql.*;
import java.util.*;
public abstract class SQL extends Database
-{
- //TODO load cooldown
+{ //TODO load cooldown
private HikariDataSource dataSource;
protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names
@@ -46,6 +47,7 @@ public abstract class SQL extends Database
@Language("SQL") protected String queryUpdatePlayerAdd, queryGetPlayerID, queryInsertBp, queryUpdateBp, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs; // DB Querys
@Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown, queryGetCooldown; // DB Querys
protected boolean updatePlayer, syncCooldown;
+ private final File backupFolder;
public SQL(Minepacks mp)
{
@@ -91,6 +93,8 @@ public SQL(Minepacks mp)
e.printStackTrace();
}
}
+
+ backupFolder = new File(this.plugin.getDataFolder(), "backups");
}
protected abstract @Nullable HikariConfig getPoolConfig();
@@ -321,6 +325,7 @@ public void saveBackpack(final Backpack backpack)
else
{
plugin.getLogger().warning("Failed saving backpack for: " + name + " (Unable to get players ID from database)");
+ writeBackup(nameOrUUID, usedSerializer, data);
}
}
}
@@ -332,11 +337,28 @@ public void saveBackpack(final Backpack backpack)
}
catch(SQLException e)
{
+ plugin.getLogger().warning("Failed to save backpack in database! Error: " + e.getMessage());
e.printStackTrace();
+ writeBackup(nameOrUUID, usedSerializer, data);
}
});
}
+ private void writeBackup(final String userIdentifier, final int usedSerializer, final byte[] data)
+ {
+ File save = new File(backupFolder, userIdentifier + "_" + System.currentTimeMillis() + Files.EXT);
+ try(FileOutputStream fos = new FileOutputStream(save))
+ {
+ fos.write(usedSerializer);
+ fos.write(data);
+ plugin.getLogger().info("Backup of the backpack has been created: " + save.getAbsolutePath());
+ }
+ catch(Exception e2)
+ {
+ plugin.getLogger().warning("Failed to write backup! Error: " + e2.getMessage());
+ }
+ }
+
@Override
protected void loadBackpack(final OfflinePlayer player, final Callback callback)
{