From cc18c6f793147808a8e8b8c4f43724d54d12c5aa Mon Sep 17 00:00:00 2001 From: GiantLuigi4 <49770992+GiantLuigi4@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:11:58 -0400 Subject: [PATCH] improve handling of errors in the wd scheme also config for join message --- src/main/java/net/montoyo/wd/WebDisplays.java | 12 ++-- .../java/net/montoyo/wd/client/WDScheme.java | 67 ++++++++++++------- .../net/montoyo/wd/config/CommonConfig.java | 9 ++- .../montoyo/wd/entity/ScreenBlockEntity.java | 2 +- .../java/net/montoyo/wd/utilities/Log.java | 18 ++--- .../assets/webdisplays/html/main.html | 6 -- 6 files changed, 67 insertions(+), 47 deletions(-) diff --git a/src/main/java/net/montoyo/wd/WebDisplays.java b/src/main/java/net/montoyo/wd/WebDisplays.java index 564a8c0..52e0e62 100644 --- a/src/main/java/net/montoyo/wd/WebDisplays.java +++ b/src/main/java/net/montoyo/wd/WebDisplays.java @@ -43,13 +43,13 @@ import net.montoyo.wd.config.ClientConfig; import net.montoyo.wd.config.CommonConfig; import net.montoyo.wd.controls.ScreenControlRegistry; import net.montoyo.wd.core.*; -import net.montoyo.wd.registry.BlockRegistry; -import net.montoyo.wd.registry.ItemRegistry; -import net.montoyo.wd.registry.WDTabs; -import net.montoyo.wd.registry.TileRegistry; import net.montoyo.wd.miniserv.server.Server; import net.montoyo.wd.net.WDNetworkRegistry; import net.montoyo.wd.net.client_bound.S2CMessageServerInfo; +import net.montoyo.wd.registry.BlockRegistry; +import net.montoyo.wd.registry.ItemRegistry; +import net.montoyo.wd.registry.TileRegistry; +import net.montoyo.wd.registry.WDTabs; import net.montoyo.wd.utilities.DistSafety; import net.montoyo.wd.utilities.Log; import net.montoyo.wd.utilities.serialization.Util; @@ -291,6 +291,10 @@ public class WebDisplays { @SubscribeEvent public void onLogIn(PlayerEvent.PlayerLoggedInEvent ev) { + if (!CommonConfig.joinMessage) { + return; + } + if(!ev.getEntity().level().isClientSide && ev.getEntity() instanceof ServerPlayer) { IWDDCapability cap = ev.getEntity().getCapability(WDDCapability.Provider.cap, null).orElseThrow(RuntimeException::new); diff --git a/src/main/java/net/montoyo/wd/client/WDScheme.java b/src/main/java/net/montoyo/wd/client/WDScheme.java index 8b9a814..0101b40 100644 --- a/src/main/java/net/montoyo/wd/client/WDScheme.java +++ b/src/main/java/net/montoyo/wd/client/WDScheme.java @@ -27,6 +27,7 @@ public class WDScheme implements CefResourceHandler { private boolean isErrorPage; String url; + boolean onlyError = false; public WDScheme(String url) { this.url = url; @@ -47,14 +48,21 @@ public class WDScheme implements CefResourceHandler { fileStr = URLDecoder.decode(fileStr, StandardCharsets.UTF_8); - if (uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr)) - return false; + if (uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr)) { + // invalid URL or no UUID + onlyError = true; + cefCallback.Continue(); + return true; + } UUID uuid; try { uuid = UUID.fromString(uuidStr); } catch (IllegalArgumentException ex) { - return false; //Invalid UUID + // invalid UUID + onlyError = true; + cefCallback.Continue(); + return true; } task = new ClientTaskGetFile(uuid, fileStr); @@ -65,24 +73,29 @@ public class WDScheme implements CefResourceHandler { @Override public void getResponseHeaders(CefResponse cefResponse, IntRef contentLength, StringRef redir) { - Log.info("Waiting for response..."); - int status = task.waitForResponse(); - Log.info("Got response %d", status); + int status; + if (onlyError) { + status = Constants.GETF_STATUS_BAD_NAME; + } else { + Log.info("Waiting for response..."); + status = task.waitForResponse(); + Log.info("Got response %d", status); - if (status == 0) { - //OK - int extPos = task.getFileName().lastIndexOf('.'); - if (extPos >= 0) { - String mime = mapMime(task.getFileName().substring(extPos + 1)); + if (status == 0) { + //OK + int extPos = task.getFileName().lastIndexOf('.'); + if (extPos >= 0) { + String mime = mapMime(task.getFileName().substring(extPos + 1)); - if (mime != null) - cefResponse.setMimeType(mime); + if (mime != null) + cefResponse.setMimeType(mime); + } + + cefResponse.setStatus(200); + cefResponse.setStatusText("OK"); + contentLength.set(0); + return; } - - cefResponse.setStatus(200); - cefResponse.setStatusText("OK"); - contentLength.set(0); - return; } int errCode; @@ -102,14 +115,16 @@ public class WDScheme implements CefResourceHandler { errStr = "Internal Server Error"; } - cefResponse.setStatus(errCode); - cefResponse.setStatusText(errStr); + // reporting the actual status and text makes CEF not display the page + cefResponse.setStatus(200); + cefResponse.setStatusText("OK"); + cefResponse.setMimeType("text/html"); dataToWrite = String.format(ERROR_PAGE, errCode, errStr).getBytes(StandardCharsets.UTF_8); dataOffset = 0; amountToWrite = dataToWrite.length; isErrorPage = true; - contentLength.set(amountToWrite); + contentLength.set(0); } private byte[] dataToWrite; @@ -120,9 +135,8 @@ public class WDScheme implements CefResourceHandler { public boolean readResponse(byte[] output, int bytesToRead, IntRef bytesRead, CefCallback cefCallback) { if (dataToWrite == null) { if (isErrorPage) { - dataToWrite = null; bytesRead.set(0); - return true; + return false; } dataToWrite = task.waitForData(); @@ -132,7 +146,7 @@ public class WDScheme implements CefResourceHandler { if (amountToWrite <= 0) { dataToWrite = null; bytesRead.set(0); - return true; + return false; } } @@ -158,8 +172,9 @@ public class WDScheme implements CefResourceHandler { @Override public void cancel() { - System.out.println("Scheme query canceled."); - task.cancel(); + Log.info("Scheme query canceled or finished."); + if (!onlyError) + task.cancel(); } public static String mapMime(String ext) { diff --git a/src/main/java/net/montoyo/wd/config/CommonConfig.java b/src/main/java/net/montoyo/wd/config/CommonConfig.java index 37d0abd..c993d89 100644 --- a/src/main/java/net/montoyo/wd/config/CommonConfig.java +++ b/src/main/java/net/montoyo/wd/config/CommonConfig.java @@ -18,13 +18,18 @@ public class CommonConfig { public static void init() { // loads the class } - + @Name("hard_recipes") @Comment("If true, breaking the minePad is required to craft upgrades.") @Translation("config.webdisplays.hard_recipes") - @IntRange(minV = 0, maxV = Integer.MAX_VALUE) @Default(valueBoolean = true) public static boolean hardRecipes = true; + + @Name("join_message") + @Comment("Whether or not webdisplays should thank the user for using the mod") + @Translation("config.webdisplays.join_message") + @Default(valueBoolean = true) + public static boolean joinMessage = true; @Name("disable_ownership_thief") @Comment("If true, the ownership thief item will be disabled") diff --git a/src/main/java/net/montoyo/wd/entity/ScreenBlockEntity.java b/src/main/java/net/montoyo/wd/entity/ScreenBlockEntity.java index d7fca06..0e5ef47 100644 --- a/src/main/java/net/montoyo/wd/entity/ScreenBlockEntity.java +++ b/src/main/java/net/montoyo/wd/entity/ScreenBlockEntity.java @@ -250,7 +250,7 @@ public class ScreenBlockEntity extends BlockEntity { } public static String url(String url) throws IOException { - System.out.println("URL received: " + url); + Log.info("URL received: " + url); if (!(WebDisplays.PROXY instanceof ClientProxy)) { List serverPlayers = WebDisplays.PROXY.getServer().getPlayerList().getPlayers(); SyncPlugin.syncPlayers(serverPlayers); diff --git a/src/main/java/net/montoyo/wd/utilities/Log.java b/src/main/java/net/montoyo/wd/utilities/Log.java index fc1003f..0515826 100644 --- a/src/main/java/net/montoyo/wd/utilities/Log.java +++ b/src/main/java/net/montoyo/wd/utilities/Log.java @@ -4,31 +4,33 @@ package net.montoyo.wd.utilities; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; +import com.mojang.logging.LogUtils; +import org.slf4j.Logger; public abstract class Log { + private static final Logger logger = LogUtils.getLogger(); + public static void info(String what, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.INFO, String.format(what, data)); + logger.info(String.format(what, data)); } public static void warning(String what, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.WARN, String.format(what, data)); + logger.warn(String.format(what, data)); } public static void error(String what, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.ERROR, String.format(what, data)); + logger.error(String.format(what, data)); } public static void infoEx(String what, Throwable e, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.INFO, String.format(what, data), e); + logger.info(String.format(what, data), e); } public static void warningEx(String what, Throwable e, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.WARN, String.format(what, data), e); + logger.warn(String.format(what, data), e); } public static void errorEx(String what, Throwable e, Object... data) { - LogManager.getLogger("WebDisplays").log(Level.ERROR, String.format(what, data), e); + logger.error(String.format(what, data), e); } } diff --git a/src/main/resources/assets/webdisplays/html/main.html b/src/main/resources/assets/webdisplays/html/main.html index 937d998..ecf6ad4 100644 --- a/src/main/resources/assets/webdisplays/html/main.html +++ b/src/main/resources/assets/webdisplays/html/main.html @@ -67,14 +67,8 @@ padding: 10px; } - @font-face { - font-family: 'CustomFont'; - src: url('https://github.com/IdreesInc/Minecraft-Font/blob/main/Minecraft.otf') format('opentype'); - } - .no_margin { margin: 0px; - font-family: 'CustomFont', Arial, sans-serif; color: #5555ff; }