diff options
author | Keuin <[email protected]> | 2020-04-23 20:13:35 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2020-04-23 20:13:35 +0800 |
commit | ba19ab350516e26a8e52cc217878bde63c0b7eee (patch) | |
tree | 482a4ad4b4b8c4d630833f93863195f56ef34244 /src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java | |
parent | ffd446e4decbe487b8e56b6726be0ecc81f8428d (diff) |
Refactored code.
Added startup message on the first start after restoring a backup.
Adjusted text color.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java new file mode 100644 index 0000000..4f0f1ff --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -0,0 +1,65 @@ +package com.keuin.kbackupfabric; + +import com.keuin.kbackupfabric.data.BackupMetadata; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.server.ServerStartCallback; +import net.fabricmc.fabric.api.registry.CommandRegistry; +import net.minecraft.server.MinecraftServer; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static com.keuin.kbackupfabric.util.PrintUtil.debug; +import static com.keuin.kbackupfabric.util.PrintUtil.info; +import static org.apache.commons.io.FileUtils.forceDelete; + +/** + * This is the Main file of this plugin. + * It contains all events, including the init event. + */ +public final class KBPluginEvents implements ModInitializer, ServerStartCallback { + @Override + public void onInitialize() { + System.out.println("Initializing KBackup..."); + CommandRegistry.INSTANCE.register(false, KBRegister::registerCommands); + ServerStartCallback.EVENT.register(this); + } + + @Override + public void onStartServer(MinecraftServer server) { + // When the server starts, we check if we have just recovered from a backup. + // If so, then we print some message. + debug("KBackup onStartServer"); + // TODO: + // Check + try { + File levelDirectory = new File(server.getRunDirectory(), server.getLevelName()); + File metadataFile = new File(levelDirectory, BackupMetadata.metadataFileName); + if (metadataFile.exists()) { + // Metadata exists. Deserialize. + BackupMetadata metadata = null; + FileInputStream fileInputStream = new FileInputStream(metadataFile); + ObjectInputStream in = new ObjectInputStream(fileInputStream); + metadata = (BackupMetadata) in.readObject(); + in.close(); + fileInputStream.close(); + + // Print metadata + info("Recovered from previous backup:"); + info("Backup Name: " + metadata.getBackupName()); + info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime()))); + + // Delete metadata file + if (!metadataFile.delete()) { + System.gc(); + forceDelete(metadataFile); + } + } + } catch (IOException | ClassNotFoundException ignored) { + } + } +} |