From daae5b17c580bd91759e8562f3f553145e5fb7af Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 18 Mar 2024 23:17:56 +0700 Subject: [PATCH] 1.0.9 commit2 --- .../adamcalculator/dynamicpack/pack/Pack.java | 4 ++ .../dynamicpack/sync/SyncingTask.java | 8 ++- .../dynamicpack/FabricDynamicMod.java | 50 ++++++++++++++++--- .../assets/dynamicpack/lang/en_us.json | 14 +++++- .../assets/dynamicpack/lang/ru_ru.json | 16 +++++- 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java index 1799b4d..e6cede5 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java @@ -153,4 +153,8 @@ private boolean checkMinecraftMetaIsValid(String s) { public boolean isContentActive(String id) { return true; // todo } + + public String getName() { + return location.getName(); + } } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java index abe5878..8f3beff 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/sync/SyncingTask.java @@ -12,6 +12,8 @@ * Re-check all packs and update packs with update available */ public class SyncingTask implements Runnable { + public static long threadCounter = 0; + private final boolean manually; // skip checkUpdateAvailable(). private boolean reloadRequired = false; private Pack currentPack; @@ -48,7 +50,11 @@ public void onStateChanged(Pack pack, SyncProgressState state) {} private void setState(SyncProgressState state) { - onStateChanged(currentPack, state); + try { + onStateChanged(currentPack, state); + } catch (Exception e) { + Out.error("onStateChanged exception!!!", e); + } } public PackSyncProgress createSyncProgressForPack(Pack pack) { diff --git a/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java b/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java index 4148def..1d27cc9 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java +++ b/src/client/java/com/adamcalculator/dynamicpack/FabricDynamicMod.java @@ -1,5 +1,10 @@ package com.adamcalculator.dynamicpack; +import com.adamcalculator.dynamicpack.pack.Pack; +import com.adamcalculator.dynamicpack.sync.SyncingTask; +import com.adamcalculator.dynamicpack.sync.state.StateDownloading; +import com.adamcalculator.dynamicpack.sync.state.StateFileDeleted; +import com.adamcalculator.dynamicpack.sync.state.SyncProgressState; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; @@ -8,6 +13,23 @@ import net.minecraft.text.Text; public class FabricDynamicMod extends DynamicPackModBase implements ClientModInitializer { + private SystemToast toast = null; + private long toastUpdated = 0; + + public void setToastContent(Text title, Text text) { + if (!isMinecraftInitialized()) { + return; + } + + if (toast == null || (System.currentTimeMillis() - toastUpdated > 1000*5)) { + ToastManager toastManager = MinecraftClient.getInstance().getToastManager(); + toastManager.add(toast = new SystemToast(SystemToast.Type.NARRATOR_TOGGLE, title, text)); + } else { + toast.setContent(title, text); + } + toastUpdated = System.currentTimeMillis(); + } + @Override public void onInitializeClient() { @@ -19,14 +41,28 @@ public void onInitializeClient() { public void startSyncThread() { SyncingTask syncingTask = new SyncingTask(false) { @Override - public void syncDone(boolean reloadRequired) { + public void onSyncDone(boolean reloadRequired) { if (reloadRequired) { + setToastContent(Text.literal("DynamicPack"), Text.translatable("dynamicpack.toast.done")); tryToReloadResources(); } } + + @Override + public void onStateChanged(Pack pack, SyncProgressState state) { + if (state instanceof StateDownloading downloading) { + setToastContent(Text.translatable("dynamicpack.toast.pack.state.downloading.title", pack.getName()), Text.translatable("dynamicpack.toast.pack.state.downloading.description", downloading.getPercentage(), downloading.getName())); + + } else if (state instanceof StateFileDeleted deleted) { + setToastContent(Text.translatable("dynamicpack.toast.pack.state.deleting.title", pack.getName()), Text.translatable("dynamicpack.toast.pack.state.deleting.description", deleted.getPath().getFileName().toString())); + + } else { + setToastContent(Text.translatable("dynamicpack.toast.pack.state.unknown.title"), Text.translatable("dynamicpack.toast.pack.state.unknown.description")); + } + } }; Thread thread = new Thread(syncingTask); - thread.setName("DynamicPack-SyncTask"); + thread.setName("DynamicPack-SyncTask" + (SyncingTask.threadCounter++)); thread.start(); } @@ -34,13 +70,11 @@ private void tryToReloadResources() { MinecraftClient client = MinecraftClient.getInstance(); if (client != null) { if (client.world == null) { - client.reloadResources(); + client.send(client::reloadResources); + } else { - ToastManager toastManager = client.getToastManager(); - if (toastManager != null) { - toastManager.add(SystemToast.create(client, SystemToast.Type.PACK_LOAD_FAILURE, - Text.translatable("dynamicpack.toast.needReload"), Text.translatable("dynamicpack.toast.needReload.desc"))); - } + setToastContent(Text.translatable("dynamicpack.toast.needReload"), + Text.translatable("dynamicpack.toast.needReload.description")); } } } diff --git a/src/main/resources/assets/dynamicpack/lang/en_us.json b/src/main/resources/assets/dynamicpack/lang/en_us.json index 38195e8..8e02ff5 100644 --- a/src/main/resources/assets/dynamicpack/lang/en_us.json +++ b/src/main/resources/assets/dynamicpack/lang/en_us.json @@ -1,4 +1,16 @@ { "dynamicpack.toast.needReload": "Packs updated!", - "dynamicpack.toast.needReload.desc": "For update: F3 + T" + "dynamicpack.toast.needReload.description": "For update: F3 + T", + "dynamicpack.toast.syncStarted": "Sync started", + "dynamicpack.toast.done": "Sync done", + "dynamicpack.toast.pack.done.success.title": "Pack %s", + "dynamicpack.toast.pack.done.success.description": "Done!", + "dynamicpack.toast.pack.error.title": "Pack %s", + "dynamicpack.toast.pack.error.description": "Error while sync...", + "dynamicpack.toast.pack.state.downloading.title": "Pack %s", + "dynamicpack.toast.pack.state.downloading.description": "[%s%%] %s", + "dynamicpack.toast.pack.state.deleting.title": "Pack %s", + "dynamicpack.toast.pack.state.deleting.description": "Delete %s", + "dynamicpack.toast.pack.state.unknown.title": "Pack %s", + "dynamicpack.toast.pack.state.unknown.description": "Processing..." } \ No newline at end of file diff --git a/src/main/resources/assets/dynamicpack/lang/ru_ru.json b/src/main/resources/assets/dynamicpack/lang/ru_ru.json index 65acf31..d489c2a 100644 --- a/src/main/resources/assets/dynamicpack/lang/ru_ru.json +++ b/src/main/resources/assets/dynamicpack/lang/ru_ru.json @@ -1,4 +1,16 @@ { - "dynamicpack.toast.needReload": "Обновлено", - "dynamicpack.toast.needReload.desc": "Чтобы обновить: F3 + T" + "dynamicpack.toast.needReload": "Паки обновлены!", + "dynamicpack.toast.needReload.description": "Чтобы обновить: F3 + T", + "dynamicpack.toast.syncStarted": "Процесс начат!", + "dynamicpack.toast.done": "Успешно!", + "dynamicpack.toast.pack.done.success.title": "Пак %s", + "dynamicpack.toast.pack.done.success.description": "Успешно!", + "dynamicpack.toast.pack.error.title": "Пак %s", + "dynamicpack.toast.pack.error.description": "Ошибка...", + "dynamicpack.toast.pack.state.downloading.title": "Пак %s", + "dynamicpack.toast.pack.state.downloading.description": "[%s%%] %s", + "dynamicpack.toast.pack.state.deleting.title": "Пак %s", + "dynamicpack.toast.pack.state.deleting.description": "Удаление %s", + "dynamicpack.toast.pack.state.unknown.title": "Пак %s", + "dynamicpack.toast.pack.state.unknown.description": "Обработка..." } \ No newline at end of file