Cleans up message logging quite a bit

Adds methods to Stargate for easier logging and less redundancy
Loads the language loader in two parts to make it available while loading
Adds a translated string to the reload-message
Uses String.format to make long messages more readable
Makes it possible to get strings directly from the backup language to make debugging easier
This commit is contained in:
2021-10-26 15:05:05 +02:00
parent eaf7596014
commit 1c906528f2
22 changed files with 154 additions and 118 deletions

View File

@ -80,8 +80,7 @@ public final class BungeeHelper {
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") + "Error sending BungeeCord " +
"teleport packet");
Stargate.logSevere("Error sending BungeeCord teleport packet");
ex.printStackTrace();
return false;
}
@ -107,8 +106,7 @@ public final class BungeeHelper {
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") +
"Error sending BungeeCord connect packet");
Stargate.logSevere("Error sending BungeeCord connect packet");
ex.printStackTrace();
return false;
}
@ -122,7 +120,6 @@ public final class BungeeHelper {
* @return <p>The message contained in the byte array, or null on failure</p>
*/
public static String readPluginMessage(byte[] message) {
// Get data from message
byte[] data;
try {
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(message));
@ -139,8 +136,7 @@ public final class BungeeHelper {
//Read the message to the prepared array
dataInputStream.readFully(data);
} catch (IOException ex) {
Stargate.getConsoleLogger().severe(Stargate.getString("prefix") +
"Error receiving BungeeCord message");
Stargate.logSevere("Error receiving BungeeCord message");
ex.printStackTrace();
return null;
}
@ -166,8 +162,7 @@ public final class BungeeHelper {
Portal destinationPortal = PortalHandler.getBungeePortal(destination);
//If teleporting to an invalid portal, let the server decide where the player arrives
if (destinationPortal == null) {
Stargate.getConsoleLogger().info(Stargate.getString("prefix") + "Bungee gate " +
destination + " does not exist");
Stargate.logInfo(String.format("Bungee portal %s does not exist", destination));
return;
}
new PlayerTeleporter(destinationPortal, player).teleport(destinationPortal, null);

View File

@ -28,9 +28,13 @@ public final class EconomyHelper {
boolean success;
//Try to charge the player. Paying the portal owner is only possible if a UUID is available
if (entrancePortal.getGate().getToOwner()) {
UUID ownerUUID = entrancePortal.getOwner().getUUID();
success = ownerUUID != null && Stargate.getEconomyConfig().chargePlayerIfNecessary(player, ownerUUID, cost);
UUID ownerUUID = entrancePortal.getOwner().getUUID();
if (ownerUUID == null) {
Stargate.logWarning(String.format("The owner of the portal %s does not have a UUID and payment to owner " +
"was therefore not possible. Make the owner re-create the portal to fix this.", entrancePortal));
}
if (entrancePortal.getGate().getToOwner() && ownerUUID != null) {
success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, ownerUUID, cost);
} else {
success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost);
}

View File

@ -8,7 +8,6 @@ import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
/**
* Helper class for reading gate files
@ -54,8 +53,8 @@ public final class GateReader {
}
}
}
} catch (Exception ex) {
Stargate.getConsoleLogger().log(Level.SEVERE, "Could not load Gate " + fileName + " - " + ex.getMessage());
} catch (Exception exception) {
Stargate.logSevere(String.format("Could not load Gate %s - %s", fileName, exception.getMessage()));
return -1;
} finally {
if (scanner != null) {
@ -90,8 +89,8 @@ public final class GateReader {
for (Character symbol : line.toCharArray()) {
//Refuse read gate designs with unknown characters
if (symbol.equals('?') || (!characterMaterialMap.containsKey(symbol))) {
Stargate.getConsoleLogger().log(Level.SEVERE, "Could not load Gate " + fileName +
" - Unknown symbol '" + symbol + "' in diagram");
Stargate.logSevere(String.format("Could not load Gate %s - Unknown symbol '%s' in diagram", fileName,
symbol));
return -1;
}
//Add the read character to the row
@ -148,8 +147,8 @@ public final class GateReader {
try {
return Integer.parseInt(config.get(key));
} catch (NumberFormatException ex) {
Stargate.getConsoleLogger().log(Level.WARNING, String.format("%s reading %s: %s is not numeric",
ex.getClass().getName(), fileName, key));
Stargate.logWarning(String.format("%s reading %s: %s is not numeric", ex.getClass().getName(),
fileName, key));
}
}
@ -172,8 +171,7 @@ public final class GateReader {
if (material != null) {
return material;
} else {
Stargate.getConsoleLogger().log(Level.WARNING, String.format("Error reading %s: %s is not a material",
fileName, key));
Stargate.logWarning(String.format("Error reading %s: %s is not a material", fileName, key));
}
}
return defaultMaterial;

View File

@ -19,7 +19,6 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
/**
* Helper class for saving and loading portal save files
@ -54,8 +53,7 @@ public final class PortalFileHelper {
bufferedWriter.close();
} catch (Exception e) {
Stargate.getConsoleLogger().log(Level.SEVERE, "Exception while writing stargates to " +
saveFileLocation + ": " + e);
Stargate.logSevere(String.format("Exception while writing stargates to %s: %s", saveFileLocation, e));
}
}
@ -133,8 +131,7 @@ public final class PortalFileHelper {
if (database.exists()) {
return loadPortals(world, database);
} else {
Stargate.getConsoleLogger().info(Stargate.getString("prefix") + "{" + world.getName() +
"} No stargates for world ");
Stargate.logInfo(String.format("{%s} No stargates for world ", world.getName()));
}
return false;
}
@ -160,8 +157,8 @@ public final class PortalFileHelper {
doPostLoadTasks(world);
return true;
} catch (Exception e) {
Stargate.getConsoleLogger().log(Level.SEVERE, "Exception while reading stargates from " +
database.getName() + ": " + lineIndex);
Stargate.logSevere(String.format("Exception while reading stargates from %s: %d", database.getName(),
lineIndex));
e.printStackTrace();
}
return false;
@ -185,7 +182,7 @@ public final class PortalFileHelper {
//Check if the min. required portal data is present
String[] portalData = line.split(":");
if (portalData.length < 8) {
Stargate.getConsoleLogger().info(Stargate.getString("prefix") + "Invalid line - " + lineIndex);
Stargate.logInfo(String.format("Invalid line - %s", lineIndex));
return;
}
@ -208,8 +205,8 @@ public final class PortalFileHelper {
int openCount = PortalHandler.openAlwaysOpenPortals();
//Print info about loaded stargates so that admins can see if all stargates loaded
Stargate.getConsoleLogger().info(String.format("%s{%s} Loaded %d stargates with %d set as always-on",
Stargate.getString("prefix"), world.getName(), portalCount, openCount));
Stargate.logInfo(String.format("{%s} Loaded %d stargates with %d set as always-on", world.getName(),
portalCount, openCount));
//Re-draw the signs in case a bug in the config prevented the portal from loading and has been fixed since
for (Portal portal : PortalRegistry.getAllPortals()) {
@ -272,8 +269,7 @@ public final class PortalFileHelper {
sign.setLine(3, ChatColor.DARK_RED + Stargate.getString("signInvalidGate"));
sign.update();
Stargate.getConsoleLogger().info(Stargate.getString("prefix") + "Gate layout on line " + lineIndex +
" does not exist [" + gateName + "]");
Stargate.logInfo(String.format("Gate layout on line %d does not exist [%s]", lineIndex, gateName));
}
}