Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MehradN committed Apr 24, 2023
1 parent b1058d8 commit d5bc4ee
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import ir.mehradn.rollback.Rollback;
import ir.mehradn.rollback.config.RollbackConfig;
import ir.mehradn.rollback.util.backup.BackupManager;
import ir.mehradn.rollback.util.backup.RollbackBackup;
import ir.mehradn.rollback.util.backup.RollbackWorld;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ir/mehradn/rollback/gui/RollbackScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.LevelSummary;

import java.io.IOException;
import java.nio.file.Path;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import net.minecraft.world.level.storage.LevelSummary;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Environment(EnvType.CLIENT)
Expand All @@ -39,7 +37,8 @@ public final class RollbackSelectionList extends ObjectSelectionList<RollbackSel
private final CurrentSaveEntry currentSaveEntry;
private boolean shouldReloadEntries;

public RollbackSelectionList(RollbackScreen screen, BackupManager backupManager, RollbackWorld rollbackWorld, LevelSummary levelSummary, Minecraft minecraftClient,
public RollbackSelectionList(RollbackScreen screen, BackupManager backupManager, RollbackWorld rollbackWorld,
LevelSummary levelSummary, Minecraft minecraftClient,
int width, int height, int top, int bottom, int itemHeight) {
super(minecraftClient, width, height, top, bottom, itemHeight);
this.screen = screen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.world.level.storage.LevelStorageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Optional;

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Consumer;

@Environment(EnvType.CLIENT)
@Mixin(EditWorldScreen.class)
public abstract class EditWorldScreenMixin extends Screen implements EditWorldScreenExpanded {
@Shadow @Final private BooleanConsumer callback;

private Consumer<WorldSelectionListCallbackAction> callbackActionConsumer = null;
private int[] buttonPos1;
private int[] buttonPos2;
Expand All @@ -43,10 +41,10 @@ public void setCallbackAction(Consumer<WorldSelectionListCallbackAction> consume
@ModifyExpressionValue(method = "init", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/Button$Builder;build()Lnet/minecraft/client/gui/components/Button;"))
private Button hideButtons(Button btn) {
if (Component.translatable("selectWorld.edit.backup").equals(btn.getMessage())) {
this.buttonPos1 = new int[]{btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight()};
this.buttonPos1 = new int[]{ btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight() };
btn.setY(-99999);
} else if (Component.translatable("selectWorld.edit.backupFolder").equals(btn.getMessage())) {
this.buttonPos2 = new int[]{btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight()};
this.buttonPos2 = new int[]{ btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight() };
btn.setY(-99999);
}
return btn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.client.renderer.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.nio.file.Path;

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.world.level.storage.LevelStorageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.nio.file.Path;

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

@Environment(EnvType.CLIENT)
@Mixin(MinecraftServer.class)
public abstract class MinecraftServerMixin extends ReentrantBlockableEventLoop<TickTask> implements CommandSource, AutoCloseable, MinecraftServerExpanded {
public abstract class MinecraftServerMixin extends ReentrantBlockableEventLoop<TickTask>
implements CommandSource, AutoCloseable, MinecraftServerExpanded {
@Shadow @Final protected LevelStorageSource.LevelStorageAccess storageSource;

private BackupManager backupManager;

public MinecraftServerMixin(String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@Mixin(SelectWorldScreen.class)
public abstract class SelectWorldScreenMixin extends Screen {
@Shadow private WorldSelectionList list;

private Button rollbackButton;
private int[] buttonPos;

Expand All @@ -31,7 +30,7 @@ protected SelectWorldScreenMixin(Component component) {
@ModifyExpressionValue(method = "init", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/Button$Builder;build()Lnet/minecraft/client/gui/components/Button;"))
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()};
this.buttonPos = new int[]{ btn.getX(), btn.getY(), btn.getWidth(), btn.getHeight() };
if (RollbackConfig.replaceReCreateButton)
btn.setY(-99999);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public abstract class WorldListEntryMixin extends WorldSelectionList.Entry imple

@Shadow public abstract void recreateWorld();

@Shadow protected abstract void queueLoadScreen();

@Shadow protected abstract void loadWorld();

public void rollbackWorld() {
Rollback.LOGGER.debug("Opening rollback screen...");
queueLoadScreen();
Expand All @@ -58,6 +54,10 @@ public void rollbackWorld() {
}));
}

@Shadow protected abstract void queueLoadScreen();

@Shadow protected abstract void loadWorld();

@ModifyExpressionValue(method = "editWorld", at = @At(value = "NEW", target = "net/minecraft/client/gui/screens/worldselection/EditWorldScreen"))
private EditWorldScreen improveEditWorldScreen(EditWorldScreen screen) {
((EditWorldScreenExpanded)screen).setCallbackAction((action) -> {
Expand Down
55 changes: 27 additions & 28 deletions src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.LevelSummary;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -53,6 +52,33 @@ private BackupManager() {
this.iconsDirectory = this.rollbackDirectory.resolve("icons");
}

public static BackupManager loadMetadata() {
Rollback.LOGGER.info("Loading metadata file...");
Path metadataFilePath = Minecraft.getInstance().getLevelSource().getBackupPath().resolve("rollbacks/rollbacks.json");
BackupManager backupManager;
boolean save = false;

try (FileReader reader = new FileReader(metadataFilePath.toFile())) {
JsonObject metadata = JsonParser.parseReader(reader).getAsJsonObject();
MetadataUpdater updater = new MetadataUpdater(metadata);
if (updater.getVersion().isOutdated()) {
metadata = updater.update();
save = true;
}
backupManager = gson.fromJson(metadata, BackupManager.class);
} catch (FileNotFoundException e) {
Rollback.LOGGER.warn("Metadata file not found! Creating a new one...");
backupManager = new BackupManager();
save = true;
} catch (IOException e) {
throw new RuntimeException(e);
}

if (save)
backupManager.saveMetadata();
return backupManager;
}

public RollbackWorld getWorld(String worldName) {
if (!this.worlds.containsKey(worldName))
this.worlds.put(worldName, new RollbackWorld());
Expand Down Expand Up @@ -252,31 +278,4 @@ private void deleteGhostIcons(String worldName) {
if (backup.iconPath != null && !Files.isRegularFile(this.rollbackDirectory.resolve(backup.iconPath)))
backup.iconPath = null;
}

public static BackupManager loadMetadata() {
Rollback.LOGGER.info("Loading metadata file...");
Path metadataFilePath = Minecraft.getInstance().getLevelSource().getBackupPath().resolve("rollbacks/rollbacks.json");
BackupManager backupManager;
boolean save = false;

try (FileReader reader = new FileReader(metadataFilePath.toFile())) {
JsonObject metadata = JsonParser.parseReader(reader).getAsJsonObject();
MetadataUpdater updater = new MetadataUpdater(metadata);
if (updater.getVersion().isOutdated()) {
metadata = updater.update();
save = true;
}
backupManager = gson.fromJson(metadata, BackupManager.class);
} catch (FileNotFoundException e) {
Rollback.LOGGER.warn("Metadata file not found! Creating a new one...");
backupManager = new BackupManager();
save = true;
} catch (IOException e) {
throw new RuntimeException(e);
}

if (save)
backupManager.saveMetadata();
return backupManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public Version(int major, int minor) {
this.minor = minor;
}

public static Version fromString(String version) {
String[] versionParts = version.split("\\.");
return new Version(
Integer.parseInt(versionParts[0]),
Integer.parseInt(versionParts[1])
);
}

public boolean isLessThan(int major, int minor) {
return (this.major < major || (this.major == major && this.minor < minor));
}
Expand All @@ -69,13 +77,5 @@ public boolean isOutdated() {
public String toString() {
return this.major + "." + this.minor;
}

public static Version fromString(String version) {
String[] versionParts = version.split("\\.");
return new Version(
Integer.parseInt(versionParts[0]),
Integer.parseInt(versionParts[1])
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.annotations.SerializedName;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.annotations.SerializedName;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import ir.mehradn.rollback.util.backup.MetadataUpdater;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.lang.reflect.Type;

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.lang.reflect.Type;
import java.nio.file.Path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.util.function.Consumer;

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import java.nio.file.Path;

@Environment(EnvType.CLIENT)
Expand Down

0 comments on commit d5bc4ee

Please sign in to comment.