improve handling of errors in the wd scheme

also config for join message
This commit is contained in:
GiantLuigi4 2024-10-18 00:11:58 -04:00
parent 6e712fc8da
commit cc18c6f793
6 changed files with 67 additions and 47 deletions

View File

@ -43,13 +43,13 @@ import net.montoyo.wd.config.ClientConfig;
import net.montoyo.wd.config.CommonConfig; import net.montoyo.wd.config.CommonConfig;
import net.montoyo.wd.controls.ScreenControlRegistry; import net.montoyo.wd.controls.ScreenControlRegistry;
import net.montoyo.wd.core.*; 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.miniserv.server.Server;
import net.montoyo.wd.net.WDNetworkRegistry; import net.montoyo.wd.net.WDNetworkRegistry;
import net.montoyo.wd.net.client_bound.S2CMessageServerInfo; 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.DistSafety;
import net.montoyo.wd.utilities.Log; import net.montoyo.wd.utilities.Log;
import net.montoyo.wd.utilities.serialization.Util; import net.montoyo.wd.utilities.serialization.Util;
@ -291,6 +291,10 @@ public class WebDisplays {
@SubscribeEvent @SubscribeEvent
public void onLogIn(PlayerEvent.PlayerLoggedInEvent ev) { public void onLogIn(PlayerEvent.PlayerLoggedInEvent ev) {
if (!CommonConfig.joinMessage) {
return;
}
if(!ev.getEntity().level().isClientSide && ev.getEntity() instanceof ServerPlayer) { if(!ev.getEntity().level().isClientSide && ev.getEntity() instanceof ServerPlayer) {
IWDDCapability cap = ev.getEntity().getCapability(WDDCapability.Provider.cap, null).orElseThrow(RuntimeException::new); IWDDCapability cap = ev.getEntity().getCapability(WDDCapability.Provider.cap, null).orElseThrow(RuntimeException::new);

View File

@ -27,6 +27,7 @@ public class WDScheme implements CefResourceHandler {
private boolean isErrorPage; private boolean isErrorPage;
String url; String url;
boolean onlyError = false;
public WDScheme(String url) { public WDScheme(String url) {
this.url = url; this.url = url;
@ -47,14 +48,21 @@ public class WDScheme implements CefResourceHandler {
fileStr = URLDecoder.decode(fileStr, StandardCharsets.UTF_8); fileStr = URLDecoder.decode(fileStr, StandardCharsets.UTF_8);
if (uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr)) if (uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr)) {
return false; // invalid URL or no UUID
onlyError = true;
cefCallback.Continue();
return true;
}
UUID uuid; UUID uuid;
try { try {
uuid = UUID.fromString(uuidStr); uuid = UUID.fromString(uuidStr);
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
return false; //Invalid UUID // invalid UUID
onlyError = true;
cefCallback.Continue();
return true;
} }
task = new ClientTaskGetFile(uuid, fileStr); task = new ClientTaskGetFile(uuid, fileStr);
@ -65,24 +73,29 @@ public class WDScheme implements CefResourceHandler {
@Override @Override
public void getResponseHeaders(CefResponse cefResponse, IntRef contentLength, StringRef redir) { public void getResponseHeaders(CefResponse cefResponse, IntRef contentLength, StringRef redir) {
Log.info("Waiting for response..."); int status;
int status = task.waitForResponse(); if (onlyError) {
Log.info("Got response %d", status); status = Constants.GETF_STATUS_BAD_NAME;
} else {
Log.info("Waiting for response...");
status = task.waitForResponse();
Log.info("Got response %d", status);
if (status == 0) { if (status == 0) {
//OK //OK
int extPos = task.getFileName().lastIndexOf('.'); int extPos = task.getFileName().lastIndexOf('.');
if (extPos >= 0) { if (extPos >= 0) {
String mime = mapMime(task.getFileName().substring(extPos + 1)); String mime = mapMime(task.getFileName().substring(extPos + 1));
if (mime != null) if (mime != null)
cefResponse.setMimeType(mime); 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; int errCode;
@ -102,14 +115,16 @@ public class WDScheme implements CefResourceHandler {
errStr = "Internal Server Error"; errStr = "Internal Server Error";
} }
cefResponse.setStatus(errCode); // reporting the actual status and text makes CEF not display the page
cefResponse.setStatusText(errStr); cefResponse.setStatus(200);
cefResponse.setStatusText("OK");
cefResponse.setMimeType("text/html");
dataToWrite = String.format(ERROR_PAGE, errCode, errStr).getBytes(StandardCharsets.UTF_8); dataToWrite = String.format(ERROR_PAGE, errCode, errStr).getBytes(StandardCharsets.UTF_8);
dataOffset = 0; dataOffset = 0;
amountToWrite = dataToWrite.length; amountToWrite = dataToWrite.length;
isErrorPage = true; isErrorPage = true;
contentLength.set(amountToWrite); contentLength.set(0);
} }
private byte[] dataToWrite; private byte[] dataToWrite;
@ -120,9 +135,8 @@ public class WDScheme implements CefResourceHandler {
public boolean readResponse(byte[] output, int bytesToRead, IntRef bytesRead, CefCallback cefCallback) { public boolean readResponse(byte[] output, int bytesToRead, IntRef bytesRead, CefCallback cefCallback) {
if (dataToWrite == null) { if (dataToWrite == null) {
if (isErrorPage) { if (isErrorPage) {
dataToWrite = null;
bytesRead.set(0); bytesRead.set(0);
return true; return false;
} }
dataToWrite = task.waitForData(); dataToWrite = task.waitForData();
@ -132,7 +146,7 @@ public class WDScheme implements CefResourceHandler {
if (amountToWrite <= 0) { if (amountToWrite <= 0) {
dataToWrite = null; dataToWrite = null;
bytesRead.set(0); bytesRead.set(0);
return true; return false;
} }
} }
@ -158,8 +172,9 @@ public class WDScheme implements CefResourceHandler {
@Override @Override
public void cancel() { public void cancel() {
System.out.println("Scheme query canceled."); Log.info("Scheme query canceled or finished.");
task.cancel(); if (!onlyError)
task.cancel();
} }
public static String mapMime(String ext) { public static String mapMime(String ext) {

View File

@ -18,13 +18,18 @@ public class CommonConfig {
public static void init() { public static void init() {
// loads the class // loads the class
} }
@Name("hard_recipes") @Name("hard_recipes")
@Comment("If true, breaking the minePad is required to craft upgrades.") @Comment("If true, breaking the minePad is required to craft upgrades.")
@Translation("config.webdisplays.hard_recipes") @Translation("config.webdisplays.hard_recipes")
@IntRange(minV = 0, maxV = Integer.MAX_VALUE)
@Default(valueBoolean = true) @Default(valueBoolean = true)
public static boolean hardRecipes = 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") @Name("disable_ownership_thief")
@Comment("If true, the ownership thief item will be disabled") @Comment("If true, the ownership thief item will be disabled")

View File

@ -250,7 +250,7 @@ public class ScreenBlockEntity extends BlockEntity {
} }
public static String url(String url) throws IOException { public static String url(String url) throws IOException {
System.out.println("URL received: " + url); Log.info("URL received: " + url);
if (!(WebDisplays.PROXY instanceof ClientProxy)) { if (!(WebDisplays.PROXY instanceof ClientProxy)) {
List<ServerPlayer> serverPlayers = WebDisplays.PROXY.getServer().getPlayerList().getPlayers(); List<ServerPlayer> serverPlayers = WebDisplays.PROXY.getServer().getPlayerList().getPlayers();
SyncPlugin.syncPlayers(serverPlayers); SyncPlugin.syncPlayers(serverPlayers);

View File

@ -4,31 +4,33 @@
package net.montoyo.wd.utilities; package net.montoyo.wd.utilities;
import org.apache.logging.log4j.Level; import com.mojang.logging.LogUtils;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
public abstract class Log { public abstract class Log {
private static final Logger logger = LogUtils.getLogger();
public static void info(String what, Object... data) { 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) { 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) { 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) { 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) { 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) { 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);
} }
} }

View File

@ -67,14 +67,8 @@
padding: 10px; padding: 10px;
} }
@font-face {
font-family: 'CustomFont';
src: url('https://github.com/IdreesInc/Minecraft-Font/blob/main/Minecraft.otf') format('opentype');
}
.no_margin { .no_margin {
margin: 0px; margin: 0px;
font-family: 'CustomFont', Arial, sans-serif;
color: #5555ff; color: #5555ff;
} }
</style> </style>