Add auto cleanup function

This commit is contained in:
GeorgH93 2015-06-08 01:07:33 +02:00
parent 361f33eccc
commit fc895b9b5e
7 changed files with 134 additions and 37 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.11</version>
<version>1.11.1</version>
<repositories>
<repository>
<id>in-project</id>

View File

@ -31,7 +31,7 @@ public class Config
{
private MinePacks MP;
private FileConfiguration config;
private static final int CONFIG_VERSION = 5;
private static final int CONFIG_VERSION = 6;
public Config(MinePacks mp)
{
@ -85,24 +85,26 @@ private void NewConfig(File file)
config.set("BackpackTitle", ChatColor.AQUA + "%s Backpack");
config.set("command_cooldown", -1);
config.set("drop_on_death", true);
config.set("Language","en");
config.set("Language", "en");
config.set("LanguageUpdateMode","Overwrite");
config.set("Database.Type","sqlite");
config.set("Database.UpdatePlayer", true);
config.set("Database.AutoCleanup.MaxInactiveDays", -1);
config.set("Database.UseUUIDs", Bukkit.getServer().getOnlineMode() && UUIDComp());
config.set("Database.UseUUIDSeparators", false);
config.set("Database.MySQL.Host", "localhost:3306");
config.set("Database.MySQL.Database", "minecraft");
config.set("Database.MySQL.User", "minecraft");
config.set("Database.MySQL.Password", "minecraft");
config.set("Database.Tables.User", "backpack_players");
config.set("Database.Tables.Backpack", "backpacks");
config.set("Database.Tables.Fields.User.Player_ID", "player_id");
config.set("Database.Tables.Fields.User.Name", "name");
config.set("Database.Tables.Fields.User.UUID", "uuid");
config.set("Database.Tables.Fields.Backpack.Owner_ID", "owner");
config.set("Database.Tables.Fields.Backpack.ItemStacks", "itemstacks");
config.set("Database.Tables.Fields.Backpack.Version", "version");
config.set("Database.MySQL.Host", "localhost:3306");
config.set("Database.MySQL.Database", "minecraft");
config.set("Database.MySQL.User", "minecraft");
config.set("Database.MySQL.Password", "minecraft");
config.set("Database.Tables.User", "backpack_players");
config.set("Database.Tables.Backpack", "backpacks");
config.set("Database.Tables.Fields.User.Player_ID", "player_id");
config.set("Database.Tables.Fields.User.Name", "name");
config.set("Database.Tables.Fields.User.UUID", "uuid");
config.set("Database.Tables.Fields.Backpack.Owner_ID", "owner");
config.set("Database.Tables.Fields.Backpack.ItemStacks", "itemstacks");
config.set("Database.Tables.Fields.Backpack.Version", "version");
config.set("Database.Tables.Fields.Backpack.LastUpdate", "lastupdate");
config.set("auto-update", true);
config.set("Version",CONFIG_VERSION);
@ -128,10 +130,16 @@ private boolean UpdateConfig(File file)
config.set("Database.Tables.Fields.Backpack.Owner_ID", "owner");
config.set("Database.Tables.Fields.Backpack.ItemStacks", "itemstacks");
config.set("Database.Tables.Fields.Backpack.Version", "version");
case 2: config.set("Database.UseUUIDSeparators", false);
case 3: config.set("auto-update", true);
case 4: config.set("command_cooldown", -1);
break;
case 2:
config.set("Database.UseUUIDSeparators", false);
case 3:
config.set("auto-update", true);
case 4:
config.set("command_cooldown", -1);
case 5:
config.set("Database.AutoCleanup.MaxInactiveDays", -1);
config.set("Database.Tables.Fields.Backpack.LastUpdate", "lastupdate");
break;
case CONFIG_VERSION: return false;
default: MP.log.info("Config File Version newer than expected!"); return false;
}
@ -159,6 +167,11 @@ public String GetLanguageUpdateMode()
return config.getString("LanguageUpdateMode");
}
public int GetAutoCleanupMaxInactiveDays()
{
return config.getInt("Database.AutoCleanup.MaxInactiveDays", -1);
}
public String GetDatabaseType()
{
return config.getString("Database.Type");

View File

@ -31,6 +31,7 @@ public class Database
protected MinePacks plugin;
protected boolean UseUUIDs, UseUUIDSeparators;
protected long maxAge;
private HashSet<Backpack> backpacks = new HashSet<Backpack>();
protected ItemStackSerializer itsSerializer = new ItemStackSerializer();
@ -38,8 +39,9 @@ public class Database
public Database(MinePacks mp)
{
plugin = mp;
UseUUIDSeparators = plugin.config.getUseUUIDSeparators();
UseUUIDs = plugin.config.UseUUIDs();
UseUUIDSeparators = plugin.config.getUseUUIDSeparators();
UseUUIDs = plugin.config.UseUUIDs();
maxAge = plugin.config.GetAutoCleanupMaxInactiveDays();
}
public void Close() { }

View File

@ -21,6 +21,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.Date;
import javax.swing.filechooser.FileFilter;
@ -37,6 +38,7 @@ public class Files extends Database
public Files(MinePacks mp)
{
super(mp);
maxAge *= 24 * 3600000L;
saveFolder = new File(plugin.getDataFolder(), "backpacks");
if(!saveFolder.exists())
{
@ -44,16 +46,21 @@ public Files(MinePacks mp)
}
else
{
CheckFileNames();
CheckFiles();
}
}
private void CheckFileNames()
private void CheckFiles()
{
File[] allFiles = saveFolder.listFiles(new BackpackFileFilter());
int len;
for (File file : allFiles)
{
if(maxAge > 0 && (new Date()).getTime() - file.lastModified() > maxAge) // Check if the file is older then x days
{
file.delete(); // Delete old files
continue; // We don't have to check if the file name is correct cause we have the delted the file
}
len = file.getName().length() - ext.length();
if(UseUUIDs) // Use UUID-based saving
{

View File

@ -46,6 +46,18 @@ public MySQL(MinePacks mp)
// Fire DB request every 10 minutes to keep database connection alive
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){ @Override public void run() { try { GetConnection().createStatement().execute("SELECT 1"); }
catch(Exception e) { } }}, 600*20, 600*20);
if(maxAge > 0)
{
try
{
GetConnection().createStatement().execute("DELETE FROM `" + Table_Backpacks + "` WHERE `" + Field_BPLastUpdate + "` + INTERVAL " + maxAge + " day < NOW()");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
protected void CheckUUIDs()
@ -143,7 +155,8 @@ protected void CheckDB()
{
Statement stmt = GetConnection().createStatement();
ResultSet res;
stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Players + "` (`" + Field_PlayerID + "` INT UNSIGNED NOT NULL AUTO_INCREMENT,`" + Field_Name + "` CHAR(16) NOT NULL UNIQUE" + ((UseUUIDs) ? ",`" + Field_UUID + "` CHAR(36) UNIQUE" : "") + ", PRIMARY KEY (`" + Field_PlayerID + "`));");
stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Players + "` (`" + Field_PlayerID + "` INT UNSIGNED NOT NULL AUTO_INCREMENT,`" + Field_Name + "` CHAR(16) NOT NULL UNIQUE"
+ ((UseUUIDs) ? ",`" + Field_UUID + "` CHAR(36) UNIQUE" : "") + ", PRIMARY KEY (`" + Field_PlayerID + "`));");
if(UseUUIDs)
{
res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + Table_Players + "' AND COLUMN_NAME = '" + Field_UUID + "';");
@ -162,12 +175,22 @@ protected void CheckDB()
}
res.close();
}
stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`" + Field_BPOwner + "` INT UNSIGNED NOT NULL, `" + Field_BPITS + "` BLOB, `" + Field_BPVersion + "` INT DEFAULT 0, PRIMARY KEY (`" + Field_BPOwner + "`));");
stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`" + Field_BPOwner + "` INT UNSIGNED NOT NULL, `" + Field_BPITS + "` BLOB, `"
+ Field_BPVersion + "` INT DEFAULT 0, " + ((maxAge > 0) ? "`" + Field_BPLastUpdate + "` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " : "") + "PRIMARY KEY (`" + Field_BPOwner + "`));");
res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + Table_Backpacks + "' AND COLUMN_NAME = '" + Field_BPVersion + "';");
if(!res.next())
{
stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `" + Field_BPVersion + "` INT DEFAULT 0;");
}
if(maxAge > 0)
{
res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + Table_Backpacks + "' AND COLUMN_NAME = '" + Field_BPLastUpdate + "';");
if(!res.next())
{
stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `" + Field_BPLastUpdate + "` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;");
}
res.close();
}
stmt.close();
}
catch (SQLException e)
@ -176,6 +199,12 @@ protected void CheckDB()
}
}
protected void AddDateFieldToQuery()
{
Query_InsertBP = ") VALUES (";
Query_UpdateBP = ",`" + Field_BPLastUpdate + "`=NOW()";
}
// Plugin Functions
public void UpdatePlayer(final Player player)
{

View File

@ -34,7 +34,7 @@ public class SQL extends Database
protected Connection conn = null;
protected String Table_Players, Table_Backpacks; // Table Names
protected String Field_Name, Field_PlayerID, Field_UUID, Field_BPOwner, Field_BPITS, Field_BPVersion; // Table Fields
protected String Field_Name, Field_PlayerID, Field_UUID, Field_BPOwner, Field_BPITS, Field_BPVersion, Field_BPLastUpdate; // Table Fields
protected String Query_UpdatePlayerGet, Query_UpdatePlayerUUID, Query_UpdatePlayerAdd, Query_GetPlayerID, Query_InsertBP, Query_UpdateBP, Query_GetBP; // DB Querys
protected boolean UpdatePlayer;
@ -42,15 +42,16 @@ public SQL(MinePacks mp)
{
super(mp);
// Load Settings
Table_Players = plugin.config.getUserTable();
Table_Backpacks = plugin.config.getBackpackTable();
Field_PlayerID = plugin.config.getDBFields("User.Player_ID");
Field_Name = plugin.config.getDBFields("User.Name");
Field_UUID = plugin.config.getDBFields("User.UUID");
Field_BPOwner = plugin.config.getDBFields("Backpack.Owner_ID");
Field_BPITS = plugin.config.getDBFields("Backpack.ItemStacks");
Field_BPVersion = plugin.config.getDBFields("Backpack.Version");
UpdatePlayer = plugin.config.getUpdatePlayer();
Table_Players = plugin.config.getUserTable();
Table_Backpacks = plugin.config.getBackpackTable();
Field_PlayerID = plugin.config.getDBFields("User.Player_ID");
Field_Name = plugin.config.getDBFields("User.Name");
Field_UUID = plugin.config.getDBFields("User.UUID");
Field_BPOwner = plugin.config.getDBFields("Backpack.Owner_ID");
Field_BPITS = plugin.config.getDBFields("Backpack.ItemStacks");
Field_BPVersion = plugin.config.getDBFields("Backpack.Version");
Field_BPLastUpdate = plugin.config.getDBFields("Backpack.LastUpdate");
UpdatePlayer = plugin.config.getUpdatePlayer();
}
public void Close()
@ -79,8 +80,24 @@ protected void BuildQuerys()
Query_GetPlayerID = "SELECT `" + Field_PlayerID + "` FROM `" + Table_Players + "` WHERE `" + Field_Name + "`=?;";
Query_GetBP = "SELECT `" + Field_BPOwner + "`,`" + Field_BPITS + "`,`" + Field_BPVersion + "` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `" + Table_Backpacks + "`.`" + Field_BPOwner + "`=`" + Table_Players + "`.`" + Field_PlayerID + "` WHERE `" + Field_Name + "`=?;";
}
Query_InsertBP = "INSERT INTO `" + Table_Backpacks + "` (`" + Field_BPOwner + "`, `" + Field_BPITS + "`, `" + Field_BPVersion + "`) VALUES (?,?,?);";
Query_UpdateBP = "UPDATE `" + Table_Backpacks + "` SET `" + Field_BPITS + "`=?,`" + Field_BPVersion + "`=? WHERE `" + Field_BPOwner + "`=?;";
Query_InsertBP = "INSERT INTO `" + Table_Backpacks + "` (`" + Field_BPOwner + "`, `" + Field_BPITS + "`, `" + Field_BPVersion + "`";
Query_UpdateBP = "UPDATE `" + Table_Backpacks + "` SET `" + Field_BPITS + "`=?,`" + Field_BPVersion + "`=?";
if(maxAge < 1)
{
Query_InsertBP += ") VALUES (";
}
else
{
AddDateFieldToQuery();
}
Query_InsertBP += "?,?,?);";
Query_UpdateBP += " WHERE `" + Field_BPOwner + "`=?;";
}
protected void AddDateFieldToQuery() // Will be overwriten by the used DB System
{
Query_InsertBP = ", `" + Field_BPLastUpdate + "`) VALUES (?,";
Query_UpdateBP = ",`" + Field_BPLastUpdate + "`=?";
}
protected void CheckUUIDs() { }

View File

@ -40,6 +40,7 @@ public SQLite(MinePacks mp)
Field_BPOwner = "owner";
Field_BPITS = "itemstacks";
Field_BPVersion = "version";
Field_BPLastUpdate = "lastupdate";
Table_Players = "backpack_players";
Table_Backpacks = "backpacks";
@ -52,6 +53,18 @@ public SQLite(MinePacks mp)
{
CheckUUIDs(); // Check if there are user accounts without UUID
}
if(maxAge > 0)
{
try
{
GetConnection().createStatement().execute("DELETE FROM `" + Table_Backpacks + "` WHERE `" + Field_BPLastUpdate + "` < DATE('now', '-" + maxAge + " days')");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
protected Connection GetConnection()
@ -98,6 +111,16 @@ protected void CheckDB()
stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `version` INT DEFAULT 0;");
}
catch(SQLException e) { }
if(maxAge > 0)
{
try
{
ResultSet rs = stmt.executeQuery("SELECT DATE('now');");
rs.next();
stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';");
}
catch(SQLException e) { }
}
stmt.close();
}
catch (SQLException e)
@ -135,4 +158,10 @@ protected void CheckUUIDs()
e.printStackTrace();
}
}
protected void AddDateFieldToQuery()
{
Query_InsertBP = ") VALUES (";
Query_UpdateBP = ",`" + Field_BPLastUpdate + "`=DATE('now')";
}
}