Skip to content

Commit

Permalink
1.0.9 commit2
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 18, 2024
1 parent 6e7ddc5 commit daae5b1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,8 @@ private boolean checkMinecraftMetaIsValid(String s) {
public boolean isContentActive(String id) {
return true; // todo
}

public String getName() {
return location.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand All @@ -19,28 +41,40 @@ 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();
}

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"));
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/assets/dynamicpack/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -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..."
}
16 changes: 14 additions & 2 deletions src/main/resources/assets/dynamicpack/lang/ru_ru.json
Original file line number Diff line number Diff line change
@@ -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": "Обработка..."
}

0 comments on commit daae5b1

Please sign in to comment.