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.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);

View File

@ -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,8 +73,12 @@ public class WDScheme implements CefResourceHandler {
@Override
public void getResponseHeaders(CefResponse cefResponse, IntRef contentLength, StringRef redir) {
int status;
if (onlyError) {
status = Constants.GETF_STATUS_BAD_NAME;
} else {
Log.info("Waiting for response...");
int status = task.waitForResponse();
status = task.waitForResponse();
Log.info("Got response %d", status);
if (status == 0) {
@ -84,6 +96,7 @@ public class WDScheme implements CefResourceHandler {
contentLength.set(0);
return;
}
}
int errCode;
String errStr;
@ -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,7 +172,8 @@ public class WDScheme implements CefResourceHandler {
@Override
public void cancel() {
System.out.println("Scheme query canceled.");
Log.info("Scheme query canceled or finished.");
if (!onlyError)
task.cancel();
}

View File

@ -22,10 +22,15 @@ public class CommonConfig {
@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")
@Translation("config.webdisplays.disable_thief")

View File

@ -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<ServerPlayer> serverPlayers = WebDisplays.PROXY.getServer().getPlayerList().getPlayers();
SyncPlugin.syncPlayers(serverPlayers);

View File

@ -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);
}
}

View File

@ -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;
}
</style>