Files
fabric-carpet/src/main/java/carpet/logging/LoggerRegistry.java
T
Fx Morin 0714098347 Ported rule updateSuppressionCrashFix from Carpet-Extra + More (#1133)
* Posted rule `updateSuppressionCrashFix` from Carpet-Extra
Also added catch for update suppression in neighbour update during ServerPlayerEntity tick.
Added Logger `updateSuppressedCrashes` so not everyone sees the crash messages
Added Coords to crash message so we now know where it originates from

* Make it easier to read

* that `s` has been there for a long time

* Made `updateSuppressionCrashFix` compatible with carpet-extra
2021-10-20 23:08:23 -03:00

178 lines
6.2 KiB
Java

package carpet.logging;
import carpet.CarpetServer;
import carpet.CarpetSettings;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.DyeColor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class LoggerRegistry
{
// Map from logger names to loggers.
private static Map<String, Logger> loggerRegistry = new HashMap<>();
// Map from player names to the set of names of the logs that player is subscribed to.
private static Map<String, Map<String, String>> playerSubscriptions = new HashMap<>();
//statics to quickly asses if its worth even to call each one
public static boolean __tnt;
public static boolean __projectiles;
public static boolean __fallingBlocks;
public static boolean __tps;
public static boolean __counter;
public static boolean __mobcaps;
public static boolean __packets;
public static boolean __pathfinding;
public static boolean __explosions;
public static boolean __updateSuppressedCrashes;
public static void initLoggers()
{
stopLoggers();
registerLoggers();
CarpetServer.registerExtensionLoggers();
}
public static void registerLoggers()
{
registerLogger("tnt", Logger.stardardLogger( "tnt", "brief", new String[]{"brief", "full"}));
registerLogger("projectiles", Logger.stardardLogger("projectiles", "brief", new String[]{"brief", "full"}));
registerLogger("fallingBlocks",Logger.stardardLogger("fallingBlocks", "brief", new String[]{"brief", "full"}));
registerLogger("pathfinding", Logger.stardardLogger("pathfinding", "20", new String[]{"2", "5", "10"}));
registerLogger("tps", HUDLogger.stardardHUDLogger("tps", null, null));
registerLogger("packets", HUDLogger.stardardHUDLogger("packets", null, null));
registerLogger("counter",HUDLogger.stardardHUDLogger("counter","white", Arrays.stream(DyeColor.values()).map(Object::toString).toArray(String[]::new)));
registerLogger("mobcaps", HUDLogger.stardardHUDLogger("mobcaps", "dynamic",new String[]{"dynamic", "overworld", "nether","end"}));
registerLogger("explosions", HUDLogger.stardardLogger("explosions", "brief",new String[]{"brief", "full"}));
registerLogger("updateSuppressedCrashes", Logger.stardardLogger("updateSuppressedCrashes", null,null));
}
/**
* Gets the logger with the given name. Returns null if no such logger exists.
*/
public static Logger getLogger(String name) { return loggerRegistry.get(name); }
/**
* Gets the set of logger names.
*/
public static Set<String> getLoggerNames() { return loggerRegistry.keySet(); }
/**
* Subscribes the player with name playerName to the log with name logName.
*/
public static void subscribePlayer(String playerName, String logName, String option)
{
if (!playerSubscriptions.containsKey(playerName)) playerSubscriptions.put(playerName, new HashMap<>());
Logger log = loggerRegistry.get(logName);
if (option == null) option = log.getDefault();
playerSubscriptions.get(playerName).put(logName,option);
log.addPlayer(playerName, option);
}
/**
* Unsubscribes the player with name playerName from the log with name logName.
*/
public static void unsubscribePlayer(String playerName, String logName)
{
if (playerSubscriptions.containsKey(playerName))
{
Map<String,String> subscriptions = playerSubscriptions.get(playerName);
subscriptions.remove(logName);
loggerRegistry.get(logName).removePlayer(playerName);
if (subscriptions.size() == 0) playerSubscriptions.remove(playerName);
}
}
/**
* If the player is not subscribed to the log, then subscribe them. Otherwise, unsubscribe them.
*/
public static boolean togglePlayerSubscription(String playerName, String logName)
{
if (playerSubscriptions.containsKey(playerName) && playerSubscriptions.get(playerName).containsKey(logName))
{
unsubscribePlayer(playerName, logName);
return false;
}
else
{
subscribePlayer(playerName, logName, null);
return true;
}
}
/**
* Get the set of logs the current player is subscribed to.
*/
public static Map<String,String> getPlayerSubscriptions(String playerName)
{
if (playerSubscriptions.containsKey(playerName))
{
return playerSubscriptions.get(playerName);
}
return null;
}
protected static void setAccess(Logger logger)
{
String name = logger.getLogName();
boolean value = logger.hasOnlineSubscribers();
try
{
Field f = logger.getField();
f.setBoolean(null, value);
}
catch (IllegalAccessException e)
{
CarpetSettings.LOG.error("Cannot change logger quick access field");
}
}
/**
* Called when the server starts. Creates the logs used by Carpet mod.
*/
public static void registerLogger(String name, Logger logger)
{
loggerRegistry.put(name, logger);
setAccess(logger);
}
private static Set<String> seenPlayers = new HashSet<>();
public static void stopLoggers()
{
for(Logger log: loggerRegistry.values() )
{
log.serverStopped();
}
seenPlayers.clear();
loggerRegistry.clear();
playerSubscriptions.clear();
}
public static void playerConnected(PlayerEntity player)
{
boolean firstTime = false;
if (!seenPlayers.contains(player.getName().getString()))
{
seenPlayers.add(player.getName().getString());
firstTime = true;
//subscribe them to the defualt loggers
}
for(Logger log: loggerRegistry.values() )
{
log.onPlayerConnect(player, firstTime);
}
}
public static void playerDisconnected(PlayerEntity player)
{
for(Logger log: loggerRegistry.values() )
{
log.onPlayerDisconnect(player);
}
}
}