From 7dcad608a661487fdbcada2ec174ceabad1c8380 Mon Sep 17 00:00:00 2001 From: Mehrad Nayyeri Date: Sun, 23 Apr 2023 12:16:01 +0330 Subject: [PATCH] Changed the RollbackConfig --- .../rollback/config/RollbackConfig.java | 71 +++---------------- .../rollback/event/AutomatedBackup.java | 2 +- .../rollback/event/RollbackCommand.java | 18 ++--- .../mixin/SelectWorldScreenMixin.java | 6 +- .../rollback/mixin/WorldListEntryMixin.java | 2 +- .../resources/assets/rollback/lang/en_us.json | 7 +- 6 files changed, 27 insertions(+), 79 deletions(-) diff --git a/src/main/java/ir/mehradn/rollback/config/RollbackConfig.java b/src/main/java/ir/mehradn/rollback/config/RollbackConfig.java index 64d3fe3..daba4c6 100644 --- a/src/main/java/ir/mehradn/rollback/config/RollbackConfig.java +++ b/src/main/java/ir/mehradn/rollback/config/RollbackConfig.java @@ -5,80 +5,31 @@ import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) -public final class RollbackConfig { +public final class RollbackConfig extends MidnightConfig { + @Entry(min = 1, max = 10, isSlider = true) public static int backupsPerWorld = 5; + @Entry(min = 1, max = 15, isSlider = true) public static int backupFrequency = 1; + @Entry public static TimerMode timerMode = TimerMode.DAYLIGHT_CYCLE; + @Entry public static boolean replaceReCreateButton = true; + @Entry public static boolean promptEnabled = true; + public static void register() { - MidnightConfig.init("rollback", _RollbackConfig.class); + MidnightConfig.init("rollback", RollbackConfig.class); } public static int maxBackupsPerWorld() { - return _RollbackConfig.backupsPerWorld; + return backupsPerWorld; } public static int daysPerBackup() { - return _RollbackConfig.backupFrequency; + return backupFrequency; } public static int ticksPerBackup() { - return _RollbackConfig.backupFrequency * 24000; - } - - public static TimerMode timerMode() { - switch (_RollbackConfig.timerMode) { - case DAYLIGHT_CYCLE -> { return TimerMode.DAYLIGHT_CYCLE; } - case IN_GAME_TIME -> { return TimerMode.IN_GAME_TIME; } - } - return TimerMode.IN_GAME_TIME; - } - - public static CommandAccess commandAccess() { - switch (_RollbackConfig.commandAccess) { - case ON_CHEATS -> { return CommandAccess.ON_CHEATS; } - case ALWAYS -> { return CommandAccess.ALWAYS; } - } - return CommandAccess.ON_CHEATS; - } - - public static boolean replaceReCreateButton() { - return _RollbackConfig.replaceReCreateButton; - } - - public static boolean promptDisabled() { - return !_RollbackConfig.promptEnabled; + return backupFrequency * 24000; } public enum TimerMode { DAYLIGHT_CYCLE, IN_GAME_TIME } - - public enum CommandAccess { - ON_CHEATS, - ALWAYS - } - - // DO NOT USE OUTSIDE OF THIS CLASS - // I am forced to keep it public. - public static final class _RollbackConfig extends MidnightConfig { - @Entry(min = 1, max = 10, isSlider = true) - public static int backupsPerWorld = 5; - @Entry(min = 1, max = 15, isSlider = true) - public static int backupFrequency = 1; - @Entry - public static _TimerMode timerMode = _TimerMode.DAYLIGHT_CYCLE; - @Entry - public static _CommandAccess commandAccess = _CommandAccess.ON_CHEATS; - @Entry - public static boolean replaceReCreateButton = true; - @Entry - public static boolean promptEnabled = true; - - public enum _TimerMode { - DAYLIGHT_CYCLE, - IN_GAME_TIME - } - public enum _CommandAccess { - ON_CHEATS, - ALWAYS - } - } } diff --git a/src/main/java/ir/mehradn/rollback/event/AutomatedBackup.java b/src/main/java/ir/mehradn/rollback/event/AutomatedBackup.java index 0b8cbee..fea37c3 100644 --- a/src/main/java/ir/mehradn/rollback/event/AutomatedBackup.java +++ b/src/main/java/ir/mehradn/rollback/event/AutomatedBackup.java @@ -79,7 +79,7 @@ private static boolean shouldUpdate(int serverTick, int worldTick) { private static boolean shouldCreateBackup(int worldTick) { if (rollbackWorld.automatedBackups) { - switch (RollbackConfig.timerMode()) { + switch (RollbackConfig.timerMode) { case DAYLIGHT_CYCLE -> { return (isMorning(worldTick) && rollbackWorld.daysSinceLastBackup >= RollbackConfig.daysPerBackup()); } case IN_GAME_TIME -> { return (rollbackWorld.ticksSinceLastBackup >= RollbackConfig.ticksPerBackup()); } } diff --git a/src/main/java/ir/mehradn/rollback/event/RollbackCommand.java b/src/main/java/ir/mehradn/rollback/event/RollbackCommand.java index e4876c0..de423ca 100644 --- a/src/main/java/ir/mehradn/rollback/event/RollbackCommand.java +++ b/src/main/java/ir/mehradn/rollback/event/RollbackCommand.java @@ -27,7 +27,7 @@ public final class RollbackCommand { public static final int MAX_NAME_LENGTH = 32; public static void register() { - CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> { + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { Rollback.LOGGER.debug("Registering the rollback command..."); if (environment.includeIntegrated) dispatcher.register(Commands.literal("rollback") @@ -41,20 +41,20 @@ public static void register() { .executes((context) -> deleteBackup(context, 0))) .then(Commands.literal("latest") .executes((context) -> deleteBackup(context, 1))) - .then(Commands.argument("number", IntegerArgumentType.integer(1, RollbackConfig.maxBackupsPerWorld())) + .then(Commands.argument("number", IntegerArgumentType.integer(1, 10)) .executes((context) -> deleteBackup(context, 2)))) .then(Commands.literal("list") .executes(RollbackCommand::listBackups))); - })); + }); } public static boolean hasAccessToCommand(CommandSourceStack source) { - return ((RollbackConfig.commandAccess() == RollbackConfig.CommandAccess.ALWAYS) || source.hasPermission(4)); + return source.hasPermission(4); } public static int createBackup(CommandContext context, String name) { if (isNotServerHost(context)) - return 0; + return -1; Rollback.LOGGER.info("Executing the \"backup new\" command..."); MinecraftServer server = context.getSource().getServer(); @@ -64,7 +64,7 @@ public static int createBackup(CommandContext context, Strin name = null; if (name != null && name.length() > MAX_NAME_LENGTH) { context.getSource().sendFailure(Component.translatable("rollback.command.create.nameTooLong")); - return 0; + return -2; } boolean f = backupManager.createRollbackBackup(server, name); @@ -79,7 +79,7 @@ public static int createBackup(CommandContext context, Strin public static int deleteBackup(CommandContext context, int position) { if (isNotServerHost(context)) - return 0; + return -1; Rollback.LOGGER.info("Executing the \"backup delete\" command..."); MinecraftServer server = context.getSource().getServer(); @@ -106,7 +106,7 @@ public static int deleteBackup(CommandContext context, int p public static int listBackups(CommandContext context) { if (isNotServerHost(context)) - return 0; + return -1; MinecraftServer server = context.getSource().getServer(); String worldName = ((MinecraftServerExpanded)server).getLevelAccess().getLevelId(); @@ -115,7 +115,7 @@ public static int listBackups(CommandContext context) { if (world.backups.isEmpty()) { context.getSource().sendSystemMessage(Component.translatable("rollback.command.list.noBackups")); - return 1; + return 0; } context.getSource().sendSystemMessage(Component.translatable("rollback.command.list.title")); diff --git a/src/main/java/ir/mehradn/rollback/mixin/SelectWorldScreenMixin.java b/src/main/java/ir/mehradn/rollback/mixin/SelectWorldScreenMixin.java index 2146fa6..c121618 100644 --- a/src/main/java/ir/mehradn/rollback/mixin/SelectWorldScreenMixin.java +++ b/src/main/java/ir/mehradn/rollback/mixin/SelectWorldScreenMixin.java @@ -32,7 +32,7 @@ protected SelectWorldScreenMixin(Component component) { private Button hideButton(Button btn) { if (Component.translatable("selectWorld.recreate").equals(btn.getMessage())) { this.buttonPos = new int[]{btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight()}; - if (RollbackConfig.replaceReCreateButton()) + if (RollbackConfig.replaceReCreateButton) btn.setY(-99999); else this.buttonPos[1] = -99999; @@ -42,7 +42,7 @@ private Button hideButton(Button btn) { @Inject(method = "init", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/client/gui/screens/worldselection/SelectWorldScreen;updateButtonStatus(ZZ)V")) private void addButton(CallbackInfo ci) { - if (RollbackConfig.replaceReCreateButton()) { + if (RollbackConfig.replaceReCreateButton) { this.rollbackButton = addRenderableWidget(Button.builder( Component.translatable("rollback.button"), (button) -> this.list.getSelectedOpt().ifPresent((worldEntry) -> ((WorldListEntryExpanded)(Object)worldEntry).rollbackWorld()) @@ -52,7 +52,7 @@ private void addButton(CallbackInfo ci) { @Inject(method = "updateButtonStatus", at = @At("RETURN")) private void onUpdateButtonStatus(boolean isPlayable, boolean isSelectable, CallbackInfo ci) { - if (RollbackConfig.replaceReCreateButton()) + if (RollbackConfig.replaceReCreateButton) this.rollbackButton.active = isPlayable; } } diff --git a/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java b/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java index 35e9094..00bbc58 100644 --- a/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java +++ b/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java @@ -79,7 +79,7 @@ private void deleteBackups(CallbackInfo ci) { @Inject(method = "loadWorld", cancellable = true, at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/client/gui/screens/worldselection/WorldSelectionList$WorldListEntry;queueLoadScreen()V")) private void promptFeature(CallbackInfo ci) { - if (RollbackConfig.promptDisabled()) + if (!RollbackConfig.promptEnabled) return; this.queueLoadScreen(); diff --git a/src/main/resources/assets/rollback/lang/en_us.json b/src/main/resources/assets/rollback/lang/en_us.json index 3e3b66a..7f48943 100644 --- a/src/main/resources/assets/rollback/lang/en_us.json +++ b/src/main/resources/assets/rollback/lang/en_us.json @@ -32,11 +32,8 @@ "rollback.midnightconfig.backupsPerWorld": "Maximum Backups Per World:", "rollback.midnightconfig.backupFrequency": "Minecraft Days Per Backup:", "rollback.midnightconfig.timerMode": "Backup Timer Is Based On:", - "rollback.midnightconfig.commandAccess": "Rollback-Command Access:", "rollback.midnightconfig.replaceReCreateButton": "Replace Re-Create Button:", "rollback.midnightconfig.promptEnabled": "Suggest Enabling Automated Backups:", - "rollback.midnightconfig.enum._TimerMode.DAYLIGHT_CYCLE": "Daylight Cycle", - "rollback.midnightconfig.enum._TimerMode.IN_GAME_TIME": "In-Game Time", - "rollback.midnightconfig.enum._CommandAccess.ON_CHEATS": "On Cheats", - "rollback.midnightconfig.enum._CommandAccess.ALWAYS": "Always" + "rollback.midnightconfig.enum.TimerMode.DAYLIGHT_CYCLE": "Daylight Cycle", + "rollback.midnightconfig.enum.TimerMode.IN_GAME_TIME": "In-Game Time" } \ No newline at end of file