Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
@Getter
public class ModContainer {
private ModVersion modVersion;
private ModEnvType envType;

private String originalStoragePath;
private String levelId = "";

private CommandDispatcher<CommandSourceStack> dispatcher;
private Path configPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.skydynamic.quickbakcupmulti;

public enum ModEnvType {
CLIENT,
SERVER
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.skydynamic.increment.storage.lib.utils.StorageManager;
import io.github.skydynamic.quickbakcupmulti.command.ModCommand;
import io.github.skydynamic.quickbakcupmulti.config.ModConfig;
import io.github.skydynamic.quickbakcupmulti.database.DatabaseManager;
import io.github.skydynamic.quickbakcupmulti.schedule.quartz.DisableQuartzInfoLogger;
import io.github.skydynamic.quickbakcupmulti.translate.Translate;
import io.github.skydynamic.quickbakcupmulti.utils.UpdateChecker;
Expand All @@ -15,6 +16,7 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.UUID;

public final class QuickbakcupmultiReforged {
public static final String MOD_ID = "quickbakcupmulti_reforged";
Expand All @@ -40,7 +42,9 @@ public static void init(ModContainer container) {
modConfig.save();
modContainer.setPermissionManager(new PermissionManager());

new UpdateChecker().start();
if (modConfig.isCheckUpdate()) {
new UpdateChecker().start();
}

// Initialize Translate
Translate.handleResourceReload(modConfig.getLang());
Expand All @@ -64,4 +68,21 @@ public static String formatTimestamp(long timestamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(timestamp);
}

public static void setNewDataBase(String collectionName) {
QuickbakcupmultiReforged.getModContainer().setOriginalStoragePath(QuickbakcupmultiReforged.getModConfig().getStoragePath());

String appendFolder = (QuickbakcupmultiReforged.getModContainer().getEnvType() == ModEnvType.CLIENT) ? "/" + collectionName : "";
DatabaseManager databaseManager = new DatabaseManager(
"QuickBakcupMulti",
QuickbakcupmultiReforged.getModConfig().getStoragePath(),
UUID.nameUUIDFromBytes(collectionName.getBytes())
);

ModConfig modTempConfig = QuickbakcupmultiReforged.getModConfig().copy();

modTempConfig.setStoragePath(QuickbakcupmultiReforged.getModConfig().getStoragePath() + appendFolder);
QuickbakcupmultiReforged.setDatabase(new Database(databaseManager));
QuickbakcupmultiReforged.setManager(new StorageManager(QuickbakcupmultiReforged.getDatabase(), modTempConfig));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.skydynamic.quickbakcupmulti.client.screen;

import io.github.skydynamic.quickbakcupmulti.translate.Translate;
import lombok.Setter;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

public class RestoreScreen extends Screen {
private final Button cancelButton;
@Setter
private String state = Translate.tr("quickbackupmulti.screen.restore_screen.title");
@Setter
private String progress = "0%";

public RestoreScreen(Button.OnPress onCancelButtonPress) {
super(Component.nullToEmpty(Translate.tr("quickbackupmulti.screen.restore_screen.title")));
cancelButton = Button.builder(Component.nullToEmpty(Translate.tr("quickbackupmulti.screen.restore_screen.cancel_button")), onCancelButtonPress).build();
}

@Override
protected void init() {
cancelButton.setPosition(this.width / 2 - cancelButton.getWidth() / 2, this.height / 2 + 40);

this.addRenderableWidget(this.cancelButton);
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
guiGraphics.drawCenteredString(font, Component.nullToEmpty(this.state), this.width / 2, this.height / 2, 0xFFFFFF);
guiGraphics.drawCenteredString(
font,
Component.nullToEmpty(Translate.tr("quickbackupmulti.screen.restore_screen.progress") + this.progress),
this.width / 2,
this.height / 2 + 20,
0xFFFFFF
);
}

@Override
public boolean shouldCloseOnEsc() {
return false;
}

@Override
protected boolean shouldNarrateNavigation() {
return false;
}

@Override
public void renderBackground(GuiGraphics guiGraphics, int i, int j, float f) {
this.renderPanorama(guiGraphics, f);
this.renderBlurredBackground(f);
this.renderMenuBackground(guiGraphics);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.skydynamic.quickbakcupmulti.command;

import com.mojang.brigadier.CommandDispatcher;
import io.github.skydynamic.quickbakcupmulti.ModEnvType;
import io.github.skydynamic.quickbakcupmulti.QuickbakcupmultiReforged;
import io.github.skydynamic.quickbakcupmulti.command.settings.SettingCommand;
import lombok.Getter;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -49,4 +51,12 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(ShowCommand.cmd)
);
}

public static boolean serverOnly(CommandSourceStack stack) {
return QuickbakcupmultiReforged.getModContainer().getEnvType() == ModEnvType.SERVER;
}

public static boolean clientOnly(CommandSourceStack stack) {
return QuickbakcupmultiReforged.getModContainer().getEnvType() == ModEnvType.CLIENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.skydynamic.quickbakcupmulti.QuickbakcupmultiReforged;
import io.github.skydynamic.quickbakcupmulti.restore.RestoreTimer;
import io.github.skydynamic.quickbakcupmulti.utils.permission.PermissionManager;
import io.github.skydynamic.quickbakcupmulti.utils.permission.PermissionType;
import lombok.Getter;
Expand All @@ -17,7 +18,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -50,20 +50,6 @@ public class RestoreCommand {
.requires(it -> PermissionManager.hasPermission(it, 4, PermissionType.ADMIN))
.executes(it -> cancelRestore(it.getSource()));

private static class RestoreThread extends TimerTask {
private final Runnable executor;

public RestoreThread(Runnable executor) {
this.executor = executor;
}

@Override
public void run() {
ModCommand.getLogger().info("Restore thread started...");
executor.run();
}
}

@Getter
private static final ConcurrentHashMap<String, ConcurrentHashMap<String, Object>> restoreDataMap = new ConcurrentHashMap<>();

Expand All @@ -79,8 +65,8 @@ private static int restoreBackup(CommandSourceStack commandSource, String name)
synchronized (restoreDataMap) {
restoreDataMap.put("QBM", restoreMap);
commandSource.sendSystemMessage(Component.nullToEmpty(tr("quickbackupmulti.restore.confirm_hint")));
return 1;
}
return 1;
}

private static void executeRestore(CommandSourceStack commandSource) {
Expand Down Expand Up @@ -122,14 +108,7 @@ private static void executeRestore(CommandSourceStack commandSource) {
countdown.shutdown();
}
}, 0, 1, TimeUnit.SECONDS);
timer.schedule(new RestoreThread(() -> {
restoreDataMap.clear();
for (ServerPlayer player : players) {
player.connection.disconnect(Component.literal("Server restore backup"));
}
QuickbakcupmultiReforged.getModContainer().setRestoringBackup(true);
QuickbakcupmultiReforged.getServerManager().stopServer();
}), 10000);
timer.schedule(new RestoreTimer(QuickbakcupmultiReforged.getModContainer().getEnvType(), players), 10000);
} else {
commandSource.sendSystemMessage(Component.nullToEmpty(tr("quickbackupmulti.confirm_restore.nothing_to_confirm")));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.skydynamic.quickbakcupmulti.command.settings;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.skydynamic.quickbakcupmulti.QuickbakcupmultiReforged;
import io.github.skydynamic.quickbakcupmulti.command.ModCommand;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;

import static io.github.skydynamic.quickbakcupmulti.translate.Translate.tr;

public class AutoReJoinSettingCommand {
public static final LiteralArgumentBuilder<CommandSourceStack> cmd = Commands.literal("auto-rejoin")
.requires(ModCommand::clientOnly)
.executes(it -> execute(it.getSource()));

private static int execute(CommandSourceStack source) {
QuickbakcupmultiReforged.getModConfig().setClientAutoReJoinWorld(!QuickbakcupmultiReforged.getModConfig().isClientAutoReJoinWorld());
source.sendSystemMessage(
Component.nullToEmpty(tr("quickbackupmulti.rejoin.switch", QuickbakcupmultiReforged.getModConfig().isClientAutoReJoinWorld()))
);
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.skydynamic.quickbakcupmulti.QuickbakcupmultiReforged;
import io.github.skydynamic.quickbakcupmulti.command.ModCommand;
import io.github.skydynamic.quickbakcupmulti.config.ModConfig;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand All @@ -11,6 +12,7 @@

public class RestartModeSettingCommand {
public static final LiteralArgumentBuilder<CommandSourceStack> cmd = Commands.literal("auto-restart-mode")
.requires(ModCommand::serverOnly)
.then(getCmdTree());

public static LiteralArgumentBuilder<CommandSourceStack> getCmdTree() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class SettingCommand {
public static final LiteralArgumentBuilder<CommandSourceStack> cmd = Commands.literal("setting")
.requires(it -> PermissionManager.hasPermission(it, 4, PermissionType.ADMIN))
.then(LangSettingCommand.cmd)
.then(RestartModeSettingCommand.cmd);
.then(RestartModeSettingCommand.cmd)
.then(AutoReJoinSettingCommand.cmd);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
@SuppressWarnings("FieldMayBeFinal")
@Getter
public class DatabaseConfig {
private ScheduleConfig backup = new ScheduleConfig();

public ScheduleConfig backup = new ScheduleConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public boolean save() {
}

public boolean load() {
if (path == null) {
logger.error("Config Path is null");
return false;
}

if (!Files.exists(path)) {
return save();
}
Expand All @@ -66,6 +71,57 @@ public boolean load() {
return true;
}

public ModConfig copy() {
ModConfig newConfig = new ModConfig(this.path);
ConfigStorage newStorage = new ConfigStorage();

newStorage.checkUpdate = this.config.checkUpdate;
newStorage.lang = this.config.lang;
newStorage.maxScheduleBackup = this.config.maxScheduleBackup;
newStorage.autoRestartMode = this.config.autoRestartMode;
newStorage.clientAutoReJoinWorld = this.config.clientAutoReJoinWorld;
newStorage.storagePath = this.config.storagePath;
newStorage.cacheDatabase = this.config.cacheDatabase;

newStorage.ignoredFiles = new ArrayList<>(this.config.ignoredFiles);
newStorage.ignoredFolders = new ArrayList<>(this.config.ignoredFolders);

newStorage.scheduleBackup = new ScheduleBackupConfig();
newStorage.scheduleBackup.enabled = this.config.scheduleBackup.enabled;
newStorage.scheduleBackup.interval = this.config.scheduleBackup.interval;
newStorage.scheduleBackup.crontab = this.config.scheduleBackup.crontab;
newStorage.scheduleBackup.resetTimerOnBackup = this.config.scheduleBackup.resetTimerOnBackup;
newStorage.scheduleBackup.requireOnlinePlayers = this.config.scheduleBackup.requireOnlinePlayers;
newStorage.scheduleBackup.requireOnlinePlayersIgnoreCarpetFakePlayer = this.config.scheduleBackup.requireOnlinePlayersIgnoreCarpetFakePlayer;
newStorage.scheduleBackup.requireOnlinePlayersBlacklist = this.config.scheduleBackup.requireOnlinePlayersBlacklist;

newStorage.prune = new PruneScheduleConfig();
newStorage.prune.enabled = this.config.prune.enabled;
newStorage.prune.interval = this.config.prune.interval;
newStorage.prune.crontab = this.config.prune.crontab;
newStorage.prune.timezoneOverride = this.config.prune.timezoneOverride;

newStorage.prune.regularBackup = new PbsConfig();
newStorage.prune.regularBackup.enabled = this.config.prune.regularBackup.enabled;
newStorage.prune.regularBackup.maxAmount = this.config.prune.regularBackup.maxAmount;
newStorage.prune.regularBackup.maxLifeTime = this.config.prune.regularBackup.maxLifeTime;
newStorage.prune.regularBackup.last = this.config.prune.regularBackup.last;
newStorage.prune.regularBackup.hour = this.config.prune.regularBackup.hour;
newStorage.prune.regularBackup.day = this.config.prune.regularBackup.day;
newStorage.prune.regularBackup.week = this.config.prune.regularBackup.week;
newStorage.prune.regularBackup.month = this.config.prune.regularBackup.month;
newStorage.prune.regularBackup.year = this.config.prune.regularBackup.year;

newStorage.database = new DatabaseConfig();
newStorage.database.backup = new ScheduleConfig();
newStorage.database.backup.enabled = this.config.database.backup.enabled;
newStorage.database.backup.interval = this.config.database.backup.interval;
newStorage.database.backup.crontab = this.config.database.backup.crontab;

newConfig.setConfig(newStorage);
return newConfig;
}

public boolean isCheckUpdate() {
return config.checkUpdate;
}
Expand All @@ -88,6 +144,7 @@ public String getLang() {

public void setLang(String lang) {
config.lang = lang;
save();
}

public int getMaxScheduleBackup() {
Expand All @@ -100,6 +157,16 @@ public AutoRestartMode getAutoRestartMode() {

public void setAutoRestartMode(AutoRestartMode autoRestartMode) {
config.autoRestartMode = autoRestartMode;
save();
}

public boolean isClientAutoReJoinWorld() {
return config.clientAutoReJoinWorld;
}

public void setClientAutoReJoinWorld(boolean clientAutoReJoinWorld) {
config.clientAutoReJoinWorld = clientAutoReJoinWorld;
save();
}

public boolean isCacheDatabase() {
Expand All @@ -123,6 +190,10 @@ public DatabaseConfig getDatabaseConfig() {
return config.storagePath;
}

public void setStoragePath(String storagePath) {
config.storagePath = storagePath;
}

public enum AutoRestartMode {
DISABLE,
DEFAULT,
Expand All @@ -140,6 +211,7 @@ public static class ConfigStorage {
private int maxScheduleBackup = 10;

private AutoRestartMode autoRestartMode = AutoRestartMode.DEFAULT;
private boolean clientAutoReJoinWorld = true;

private String storagePath = "./QuickBackupMulti";
private boolean cacheDatabase = false;
Expand All @@ -159,6 +231,7 @@ public String toString() {
", lang='" + lang + '\'' +
", maxScheduleBackup=" + maxScheduleBackup +
", autoRestartMode=" + autoRestartMode +
", clientAutoReJoinWorld=" + clientAutoReJoinWorld +
", storagePath='" + storagePath + '\'' +
", cacheDatabase=" + cacheDatabase +
", scheduleBackup=" + scheduleBackup +
Expand Down
Loading