Skip to content

Commit 2788da6

Browse files
committed
add backups to filebase storage
1 parent 10a6f31 commit 2788da6

3 files changed

Lines changed: 405 additions & 41 deletions

File tree

src/main/java/dev/noah/perplayerkit/PerPlayerKit.java

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import dev.noah.perplayerkit.storage.StorageSelector;
3232
import dev.noah.perplayerkit.storage.exceptions.StorageConnectionException;
3333
import dev.noah.perplayerkit.storage.exceptions.StorageOperationException;
34+
import dev.noah.perplayerkit.util.BackupManager;
3435
import dev.noah.perplayerkit.util.BroadcastManager;
3536
import org.bstats.bukkit.Metrics;
3637
import org.bukkit.Bukkit;
@@ -40,11 +41,11 @@
4041
import org.bukkit.plugin.java.JavaPlugin;
4142
import org.ipvp.canvas.MenuFunctionListener;
4243

43-
4444
public final class PerPlayerKit extends JavaPlugin {
4545

4646
public static Plugin plugin;
4747
public static StorageManager storageManager;
48+
private BackupManager backupManager;
4849

4950
public static Plugin getPlugin() {
5051
return plugin;
@@ -98,6 +99,18 @@ public void onEnable() {
9899
return;
99100
}
100101

102+
// Initialize backup system for file-based storage methods
103+
if (isFileBasedStorage(dbType)) {
104+
backupManager = new BackupManager(this);
105+
if (backupManager.isEnabled()) {
106+
getLogger().info("Backup system initialized for file-based storage");
107+
} else {
108+
getLogger().info("Backup system disabled in configuration");
109+
}
110+
} else {
111+
getLogger().info("Backup system not needed for non-file-based storage: " + dbType);
112+
}
113+
101114
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
102115

103116
if (storageManager.isConnected()) {
@@ -111,14 +124,13 @@ public void onEnable() {
111124
attemptDatabaseConnection(false);
112125
}
113126

114-
}, 30 * 20, 30 * 20); //runs every 30 seconds
127+
}, 30 * 20, 30 * 20); // runs every 30 seconds
115128

116129
loadDatabaseData();
117130
getLogger().info("Database data loaded");
118131

119132
UpdateChecker updateChecker = new UpdateChecker(this);
120133

121-
122134
// REGISTER THINGS START
123135
KitSlotTabCompleter kitSlotTabCompleter = new KitSlotTabCompleter();
124136
ECSlotTabCompleter ecSlotTabCompleter = new ECSlotTabCompleter();
@@ -144,50 +156,43 @@ public void onEnable() {
144156
this.getCommand("deletekit").setTabCompleter(kitSlotTabCompleter);
145157

146158
this.getCommand("inspectkit").setExecutor(new InspectKitCommand(plugin));
147-
this.getCommand("inspectkit").setTabCompleter(new InspectKitCommand(plugin));
159+
this.getCommand("inspectkit").setTabCompleter(kitSlotTabCompleter);
148160

149161
this.getCommand("inspectec").setExecutor(new InspectEcCommand(plugin));
150-
this.getCommand("inspectec").setTabCompleter(new InspectEcCommand(plugin));
162+
this.getCommand("inspectec").setTabCompleter(ecSlotTabCompleter);
151163

152164
this.getCommand("enderchest").setExecutor(new EnderchestCommand());
165+
this.getCommand("enderchest").setTabCompleter(ecSlotTabCompleter);
153166

167+
this.getCommand("regear").setExecutor(new RegearCommand(this));
154168

155-
SavePublicKitCommand savePublicKitCommand = new SavePublicKitCommand();
156-
this.getCommand("savepublickit").setExecutor(savePublicKitCommand);
157-
this.getCommand("savepublickit").setTabCompleter(savePublicKitCommand);
158-
159-
PublicKitCommand publicKitCommand = new PublicKitCommand(plugin);
160-
this.getCommand("publickit").setExecutor(publicKitCommand);
161-
this.getCommand("publickit").setTabCompleter(publicKitCommand);
169+
this.getCommand("publickit").setExecutor(new PublicKitCommand(plugin));
162170

171+
this.getCommand("savepublickit").setExecutor(new SavePublicKitCommand());
172+
this.getCommand("savepublickit").setTabCompleter(kitSlotTabCompleter);
163173

164-
for (int i = 1; i <= 9; i++) {
165-
this.getCommand("k" + i).setExecutor(new ShortKitCommand());
166-
}
167-
168-
for (int i = 1; i <= 9; i++) {
169-
this.getCommand("ec" + i).setExecutor(new ShortECCommand());
170-
}
171-
172-
RegearCommand regearCommand = new RegearCommand(this);
173-
this.getCommand("regear").setExecutor(regearCommand);
174+
this.getCommand("perplayerkit").setExecutor(new PerPlayerKitCommand(plugin));
174175

175-
this.getCommand("heal").setExecutor(new HealCommand());
176176
this.getCommand("repair").setExecutor(new RepairCommand());
177-
this.getCommand("perplayerkit").setExecutor(new PerPlayerKitCommand(this));
177+
this.getCommand("heal").setExecutor(new HealCommand());
178178

179+
this.getCommand("shortkit").setExecutor(new ShortKitCommand());
180+
this.getCommand("shortkit").setTabCompleter(kitSlotTabCompleter);
181+
182+
this.getCommand("shortec").setExecutor(new ShortECCommand());
183+
this.getCommand("shortec").setTabCompleter(ecSlotTabCompleter);
179184

180-
Bukkit.getPluginManager().registerEvents(regearCommand, this);
181-
Bukkit.getPluginManager().registerEvents(new JoinListener(this, updateChecker), this);
182-
Bukkit.getPluginManager().registerEvents(new QuitListener(this), this);
183185
Bukkit.getPluginManager().registerEvents(new MenuFunctionListener(), this);
186+
Bukkit.getPluginManager().registerEvents(new AutoRekitListener(this), this);
187+
Bukkit.getPluginManager().registerEvents(new JoinListener(plugin, updateChecker), this);
188+
Bukkit.getPluginManager().registerEvents(new QuitListener(this), this);
184189
Bukkit.getPluginManager().registerEvents(new KitMenuCloseListener(), this);
185190
Bukkit.getPluginManager().registerEvents(new KitRoomSaveListener(), this);
186-
Bukkit.getPluginManager().registerEvents(new AutoRekitListener(this), this);
191+
187192
Bukkit.getPluginManager().registerEvents(new AboutCommandListener(), this);
188193

189-
// features
190-
if(getConfig().getBoolean("feature.old-death-drops", false)) {
194+
// features
195+
if (getConfig().getBoolean("feature.old-death-drops", false)) {
191196
Bukkit.getPluginManager().registerEvents(new OldDeathDropListener(), this);
192197
}
193198

@@ -199,10 +204,8 @@ public void onEnable() {
199204
Bukkit.getPluginManager().registerEvents(new ShulkerDropItemsListener(), this);
200205
}
201206

202-
203207
// REGISTER THINGS END
204208

205-
206209
BroadcastManager.get().startScheduledBroadcast();
207210
updateChecker.printStartupStatus();
208211

@@ -211,8 +214,24 @@ public void onEnable() {
211214
@Override
212215
public void onDisable() {
213216
closeDatabaseConnection();
217+
218+
// Shutdown backup manager if it exists
219+
if (backupManager != null) {
220+
backupManager.shutdown();
221+
}
214222
}
215223

224+
/**
225+
* Check if the storage type is file-based (requires backups)
226+
*
227+
* @param storageType The storage type from configuration
228+
* @return true if file-based storage, false otherwise
229+
*/
230+
private boolean isFileBasedStorage(String storageType) {
231+
return storageType.equalsIgnoreCase("sqlite") ||
232+
storageType.equalsIgnoreCase("yml") ||
233+
storageType.equalsIgnoreCase("yaml");
234+
}
216235

217236
private void loadPublicKitsIdsFromConfig() {
218237
// generate list of public kits from the config
@@ -238,7 +257,6 @@ private void loadDatabaseData() {
238257

239258
}
240259

241-
242260
private void attemptDatabaseConnection(boolean disableOnFail) {
243261
try {
244262
storageManager.connect();
@@ -260,7 +278,7 @@ private void closeDatabaseConnection() {
260278
try {
261279
storageManager.close();
262280
} catch (StorageConnectionException e) {
263-
// retry once
281+
// retry once
264282
try {
265283
storageManager.close();
266284
} catch (StorageConnectionException ex) {
@@ -269,8 +287,7 @@ private void closeDatabaseConnection() {
269287
}
270288
}
271289

272-
273-
private void notice(){
290+
private void notice() {
274291
String notice = """
275292
* PerPlayerKit is free software: you can redistribute it and/or modify it under
276293
* the terms of the GNU Affero General Public License as published by the
@@ -285,19 +302,14 @@ private void notice(){
285302
* You should have received a copy of the GNU Affero General Public License
286303
* along with PerPlayerKit. If not, see <https://www.gnu.org/licenses/>.""";
287304

288-
289305
String otherInfo = """
290306
* All users must be provided with the source code of the software, as per the AGPL-3.0 license.
291307
* If you are using a modified version of PerPlayerKit, you must make the source code of your
292308
* modified version available to all users, as per the AGPL-3.0 license.
293309
* Consider modifying the /aboutperplayerkit command to include a link to your modified source code.
294310
""";
295311

296-
297-
298312
getLogger().info(notice);
299313
getLogger().info(otherInfo);
300314
}
301-
302-
303315
}

0 commit comments

Comments
 (0)