Skip to content

Commit

Permalink
feat: gui
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Dec 3, 2024
1 parent 0c88a0d commit 259a1b9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions common/src/main/resources/assets/x-backup/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

"xb.gui.backups.title" : "World Backups",
"xb.gui.backups.restored" : "Backup restored successfully!",
"xb.gui.no_polylib": "PolyLib is not installed, please install it to use the GUI.",
"xb.button.backups" : "Backups",
"xb.button.restore_backup" : "Restore Backup",
"xb.button.delete_backup" : "Delete Backup",
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/assets/x-backup/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"xb.gui.backups.title" : "备份列表",
"xb.gui.backups.restored" : "成功还原备份",
"xb.button.backups" : "世界备份",
"xb.gui.no_polylib": "未安装 PolyLib,请安装后使用 GUI 功能。",
"xb.button.restore_backup" : "还原备份",
"xb.button.delete_backup" : "删除备份",
"xb.button.backups_entry" : "管理备份",
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/github/zly2006/xbackup/gui/BackupsGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import net.minecraft.client.texture.NativeImageBackedTexture
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import java.nio.file.Path
import java.text.SimpleDateFormat

class BackupsGui(private val service: BackupDatabaseService) : GuiProvider {
class BackupsGui(private val service: BackupDatabaseService, val worldRoot: Path) : GuiProvider {
private var backupList: GuiList<BackupDatabaseService.Backup?>? = null
private var selected: BackupDatabaseService.Backup? = null

Expand Down Expand Up @@ -111,7 +112,7 @@ class BackupsGui(private val service: BackupDatabaseService) : GuiProvider {
private fun deleteSelected(gui: ModularGui?) {
if (selected == null) return
runBlocking {
service!!.deleteBackup(selected!!)
service.deleteBackup(selected!!)
}
selected = null
updateList()
Expand All @@ -120,7 +121,7 @@ class BackupsGui(private val service: BackupDatabaseService) : GuiProvider {
private fun restoreSelected(gui: ModularGui) {
if (selected == null) return
runBlocking {
// service!!.restore(selected!!.id, )
service.restore(selected!!.id, worldRoot) { false }
}
selected = null
OptionDialog.simpleInfoDialog(
Expand All @@ -131,7 +132,7 @@ class BackupsGui(private val service: BackupDatabaseService) : GuiProvider {
}

private fun updateList() {
val backups = service!!.listBackups(0, Int.MAX_VALUE)
val backups = service.listBackups(0, Int.MAX_VALUE)
backups.sortedWith(Comparator.comparingLong(BackupDatabaseService.Backup::created).reversed())
backupList!!.list.clear()
backupList!!.list.addAll(backups)
Expand Down Expand Up @@ -226,8 +227,8 @@ class BackupsGui(private val service: BackupDatabaseService) : GuiProvider {

companion object {
private val DATE_TIME_FORMAT = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss")
fun open(service: BackupDatabaseService) {
MinecraftClient.getInstance().setScreen(ModularGuiScreen(BackupsGui(service)))
fun open(service: BackupDatabaseService, worldRoot: Path) {
MinecraftClient.getInstance().setScreen(ModularGuiScreen(BackupsGui(service, worldRoot)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.github.zly2006.xbackup.BackupDatabaseService;
import com.github.zly2006.xbackup.XBackup;
import com.github.zly2006.xbackup.gui.BackupsGui;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.world.SelectWorldScreen;
import net.minecraft.client.gui.screen.world.WorldListWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -25,7 +27,8 @@ protected MixinSelectWorldScreen(Text title) {
}

//? if poly_lib {
@Unique ButtonWidget buttonWidget;
@Unique
ButtonWidget buttonWidget;

@Shadow
private WorldListWidget levelList;
Expand All @@ -44,7 +47,7 @@ private void postInit(CallbackInfo ci) {
Path.of("").toAbsolutePath().resolve(XBackup.config.getBlobPath()).normalize(),
XBackup.config
);
BackupsGui.Companion.open(service);
BackupsGui.Companion.open(service, Path.of("saves", name));
}
}).dimensions(this.width / 2 + 160, this.height - 28, 20, 20).build();
buttonWidget.active = levelList.getSelectedAsOptional().isPresent();
Expand All @@ -67,7 +70,11 @@ private void worldSelected(CallbackInfo ci) {
)
private void render(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (buttonWidget != null && buttonWidget.isHovered()) {
setTooltip(Text.translatable("xb.button.backups"));
if (FabricLoader.getInstance().isModLoaded("polylib")) {
setTooltip(Text.translatable("xb.button.backups"));
} else {
setTooltip(Text.translatable("xb.gui.no_polylib").formatted(Formatting.RED));
}
}
}
//?}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.sqlite.SQLiteDataSource
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.coroutines.CoroutineContext
import kotlin.io.path.*

Expand Down Expand Up @@ -225,7 +226,8 @@ object XBackup : ModInitializer {
Path("xb.backups")
.resolve(backId.toString())
.resolve("x_backup.db")
.createParentDirectories()
.createParentDirectories(),
StandardCopyOption.REPLACE_EXISTING
)
server.broadcast(
Utils.translate(
Expand Down
File renamed without changes.

0 comments on commit 259a1b9

Please sign in to comment.