Skip to content

Commit

Permalink
Merge pull request #26
Browse files Browse the repository at this point in the history
Silent work 1.20.1
  • Loading branch information
AdamCalculator authored Mar 22, 2024
2 parents c9fe93b + 01c4de3 commit 74b5ebe
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 36 deletions.
6 changes: 5 additions & 1 deletion common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ public static void debugNetwork() {
if (isRelease()) return;

try {
Thread.sleep(13);
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public static boolean isDebugLogs() {
return false;
}
}
26 changes: 26 additions & 0 deletions common/src/main/java/com/adamcalculator/dynamicpack/pack/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Pack {
public static final String UNKNOWN_PACK_MCMETA = """
Expand All @@ -30,6 +33,8 @@ public class Pack {
private boolean isSyncing = false;
private final String remoteTypeStr;
private Exception latestException;
private final List<Consumer<Pack>> destroyListeners = new ArrayList<>();
private boolean destroyed = false;


public Pack(File location, JSONObject json) {
Expand Down Expand Up @@ -190,9 +195,30 @@ public Exception getLatestException() {

public void saveReScanData(Pack oldestPack) {
if (oldestPack == null) return;
oldestPack.markAsDestroyed(this);

if (this.latestException == null) {
this.latestException = oldestPack.latestException;
}
}

public void addDestroyListener(Consumer<Pack> runnable) {
destroyListeners.add(runnable);
}

public void removeDestroyListener(Consumer<Pack> runnable) {
destroyListeners.remove(runnable);
}

private void markAsDestroyed(Pack heirPack) {
for (Consumer<Pack> runnable : destroyListeners.toArray(new Consumer[0])) {
runnable.accept(heirPack);
}
destroyListeners.clear();
this.destroyed = true;
}

public boolean isDestroyed() {
return destroyed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
public class SyncingTask implements Runnable {
public static boolean isSyncing = false;
public static String syncingLog1 = "";
public static String syncingLog2 = "";
public static String syncingLog3 = "";

private final boolean manually; // skip checkUpdateAvailable().
private boolean reloadRequired = false;
Expand All @@ -30,6 +33,7 @@ public void run() {
}
isSyncing = true;
Out.println("SyncTask started!");
log("Syncing started...");
onSyncStart();
DynamicPackModBase.INSTANCE.rescanPacks();
DynamicPackModBase.INSTANCE.rescanPacksBlocked = true;
Expand All @@ -38,15 +42,18 @@ public void run() {
try {
pack.sync(createSyncProgressForPack(pack), manually);
onPackDoneSuccess(pack);
log(pack.getName() + " success");
} catch (Exception e) {
Out.error("Pack error: " + pack.getName(), e);
log(pack.getName() + " error: " + e);
onPackError(pack, e);
}
}
DynamicPackModBase.INSTANCE.rescanPacksBlocked = false;
onSyncDone(reloadRequired);
Out.println("SyncTask ended!");
isSyncing = false;
clearLog();
}

public void onPackDoneSuccess(Pack pack) {}
Expand Down Expand Up @@ -78,6 +85,7 @@ private void _packLog(String s) {

@Override
public void start() {
log("Sync started " + pack.getName());
_packLog("Sync started.");
}

Expand All @@ -101,20 +109,25 @@ public void done(boolean reloadRequired) {
@Override
public void textLog(String s) {
_packLog("[textLog] " + s);
log(s);
}

@Override
public void downloading(String name, float percentage) {
if (cachedDownloading == null) {
cachedDownloading = new StateDownloading(name);
log("Downloading " + name);
} else {
cachedDownloading.setName(name);
if (!cachedDownloading.getName().equals(name)) {
cachedDownloading.setName(name);
log("Downloading " + name);
}
}
if (percentage == 100f) {
setState(new StateDownloadDone());
return;
}

syncingLog3 = "Downloading " + name + " " + (Math.round(percentage * 100f) / 100f) + "%";
cachedDownloading.setPercentage(Math.round(percentage * 10f) / 10f);
setState(cachedDownloading);
}
Expand All @@ -125,4 +138,17 @@ public void stateChanged(SyncProgressState state) {
}
};
}

private void log(String s) {
Out.debug("log: " + s);
syncingLog1 = syncingLog2;
syncingLog2 = syncingLog3;
syncingLog3 = s;
}

private void clearLog() {
syncingLog3 = "";
syncingLog2 = "";
syncingLog1 = "";
}
}
39 changes: 16 additions & 23 deletions common/src/main/java/com/adamcalculator/dynamicpack/util/Out.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,74 @@
package com.adamcalculator.dynamicpack.util;

import com.adamcalculator.dynamicpack.Mod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class Out {
public static final Logger LOGGER = LoggerFactory.getLogger("dynamicpack");
public static boolean ENABLE = true;
public static boolean USE_SOUT = false;
public static final String PREFIX = "[DynamicPack] ";

public static void println(Object o) {
if (!ENABLE) return;
if (USE_SOUT) {
System.out.println(o);
System.out.println(PREFIX + o);
return;
}
LOGGER.info(o + "");
LOGGER.info(PREFIX + o);
}

public static void error(String s, Exception e) {
if (!ENABLE) return;
if (USE_SOUT) {
System.err.println(s);
System.err.println(PREFIX + s);
e.printStackTrace();
return;
}
LOGGER.error(s, e);
}

public static void downloading(String url, File file) {
if (!ENABLE) return;
if (USE_SOUT) {
System.out.println(file.getName() + " downloading from " + url);
return;
}
LOGGER.warn("Downloading " + file.getName() + " from " + url);
LOGGER.error(PREFIX + s, e);
}

public static void warn(String s) {
if (!ENABLE) return;
if (USE_SOUT) {
System.out.println("WARN: " + s);
System.out.println(PREFIX + "WARN: " + s);
return;
}
LOGGER.warn(s);
LOGGER.warn(PREFIX + s);
}

/**
* Always enable! Ignore enable/disable
*/
public static void securityWarning(String s) {
if (USE_SOUT) {
System.out.println("[dynamicpack] " + s);
System.out.println("[DynamicPack] " + s);
return;
}

try {
LOGGER.warn(s);
LOGGER.warn("[DynamicPack] " + s);
} catch (Exception ignored) {
System.out.println("[dynamicpack] " + s);
System.out.println("[DynamicPack] " + s);
}
}

public static void debug(String s) {
println("DEBUG: " + s);
if (Mod.isDebugLogs()) {
println("DEBUG: " + s);
}
}

/**
* Always enable! Ignore enable/disable
*/
public static void securityStackTrace() {
if (USE_SOUT) {
System.out.println("[dynamicpack] Stacktrace");
System.out.println("[DynamicPack] Stacktrace");
new Throwable("StackTrace printer").printStackTrace();
return;
}
LOGGER.error("No error. This is stacktrace printer", new Throwable("StackTrace printer"));
LOGGER.error("[DynamicPack] No error. This is stacktrace printer", new Throwable("StackTrace printer"));
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package com.adamcalculator.dynamicpack;

import com.adamcalculator.dynamicpack.include.modmenu.util.DrawingUtil;
import com.adamcalculator.dynamicpack.pack.Pack;
import com.adamcalculator.dynamicpack.sync.SyncingTask;
import com.terraformersmc.modmenu.util.DrawingUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.Date;
import java.util.function.Consumer;

public class DynamicPackScreen extends Screen {
private final Screen parent;
private final Pack pack;
private final Text screenDescText;
private Pack pack;
private final MutableText screenDescText;
private ButtonWidget syncButton;
private final Consumer<Pack> destroyListener = this::setPack;

public DynamicPackScreen(Screen parent, Pack pack) {
super(Text.literal(pack.getName()).formatted(Formatting.BOLD));
this.pack = pack;
this.client = MinecraftClient.getInstance();
this.parent = parent;
this.screenDescText = Text.translatable("dynamicpack.screen.pack.description");
setPack(pack);
}

private void setPack(Pack pack) {
if (this.pack != null) {
this.pack.removeDestroyListener(destroyListener);
}
this.pack = pack;
pack.addDestroyListener(destroyListener);
}

@Override
Expand All @@ -39,6 +51,13 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {

if (pack.getLatestException() != null) {
DrawingUtil.drawWrappedString(context, Text.translatable("dynamicpack.screen.pack.latestException", pack.getLatestException().getMessage()).asTruncatedString(9999), 20, 78 + h, 500, 99, 0xff2222);
h+=10;
}

if (SyncingTask.isSyncing) {
DrawingUtil.drawWrappedString(context, SyncingTask.syncingLog1, 20, 78+30 + h, 500, 99, 0xCCCCCC);
DrawingUtil.drawWrappedString(context, SyncingTask.syncingLog2, 20, 78+30+20 + h, 500, 99, 0xCCCCCC);
DrawingUtil.drawWrappedString(context, SyncingTask.syncingLog3, 20, 78+30+40 + h, 500, 99, 0xCCCCCC);
}

super.render(context, mouseX, mouseY, delta);
Expand All @@ -48,10 +67,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
protected void init() {
addDrawableChild(syncButton = Compat.createButton(
Text.translatable("dynamicpack.screen.pack.manually_sync"),
() -> {
DynamicPackModBase.INSTANCE.startManuallySync();
close();
},
() -> DynamicPackModBase.INSTANCE.startManuallySync(),
100, 20, width - 120, 10
));

Expand All @@ -61,5 +77,6 @@ protected void init() {
@Override
public void close() {
this.client.setScreen(parent);
pack.removeDestroyListener(destroyListener);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.adamcalculator.dynamicpack;

import com.adamcalculator.dynamicpack.pack.Pack;
import com.adamcalculator.dynamicpack.sync.SyncingTask;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.pack.PackListWidget;
Expand All @@ -16,7 +15,7 @@ public class PackMixinHelper {
public static void renderResourcePackEntry(Object resourcePackEntryMixin, DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci) {
PackListWidget.ResourcePackEntry entry = (PackListWidget.ResourcePackEntry) resourcePackEntryMixin;
Pack pack = DynamicPackModBase.INSTANCE.getDynamicPackByMinecraftName(entry.getName());
if (pack != null && !SyncingTask.isSyncing) {
if (pack != null) {
int i = mouseX - x;
int j = mouseY - y;
context.drawTexture(pack.getLatestException() != null ? BUTTON_WARNING_TEXTURE : BUTTON_TEXTURE, x + 174, y+16, 0.0F, ((i >= 174 && j >= 16 && hovered) ? 16f : 0f), 16, 16, 16, 32);
Expand All @@ -26,7 +25,7 @@ public static void renderResourcePackEntry(Object resourcePackEntryMixin, DrawCo
public static void mouseClicked(Object resourcePackEntryMixin, PackListWidget widget, double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) {
PackListWidget.ResourcePackEntry entry = (PackListWidget.ResourcePackEntry) resourcePackEntryMixin;
Pack pack = DynamicPackModBase.INSTANCE.getDynamicPackByMinecraftName(entry.getName());
if (pack != null && !SyncingTask.isSyncing) {
if (pack != null) {
double d = mouseX - (double)widget.getRowLeft();
double e = mouseY - (double)widget.getRowTop(widget.children().indexOf(entry));

Expand Down
Loading

0 comments on commit 74b5ebe

Please sign in to comment.