Skip to content

Commit

Permalink
Changed the RollbackConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
MehradN committed Apr 23, 2023
1 parent 11bb13e commit 7dcad60
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 79 deletions.
71 changes: 11 additions & 60 deletions src/main/java/ir/mehradn/rollback/config/RollbackConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()); }
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/ir/mehradn/rollback/event/RollbackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<CommandSourceStack> context, String name) {
if (isNotServerHost(context))
return 0;
return -1;

Rollback.LOGGER.info("Executing the \"backup new\" command...");
MinecraftServer server = context.getSource().getServer();
Expand All @@ -64,7 +64,7 @@ public static int createBackup(CommandContext<CommandSourceStack> 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);
Expand All @@ -79,7 +79,7 @@ public static int createBackup(CommandContext<CommandSourceStack> context, Strin

public static int deleteBackup(CommandContext<CommandSourceStack> context, int position) {
if (isNotServerHost(context))
return 0;
return -1;

Rollback.LOGGER.info("Executing the \"backup delete\" command...");
MinecraftServer server = context.getSource().getServer();
Expand All @@ -106,7 +106,7 @@ public static int deleteBackup(CommandContext<CommandSourceStack> context, int p

public static int listBackups(CommandContext<CommandSourceStack> context) {
if (isNotServerHost(context))
return 0;
return -1;

MinecraftServer server = context.getSource().getServer();
String worldName = ((MinecraftServerExpanded)server).getLevelAccess().getLevelId();
Expand All @@ -115,7 +115,7 @@ public static int listBackups(CommandContext<CommandSourceStack> 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 2 additions & 5 deletions src/main/resources/assets/rollback/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 7dcad60

Please sign in to comment.