implement proper logger

This commit is contained in:
glowredman committed 2022-07-09 21:22:26 +02:00
1 parent 770121a21f
commit 2c8bce171e
7 files changed
+69 -85

No files matched your search

+2 -4
View File
@@ -25,8 +25,7 @@ public class Config {
return true;
} catch (FileNotFoundException ignored) {
} catch (IOException e) {
Logger.Log("Failed to read the config file: " + fileName);
e.printStackTrace();
Constants.LOGGER.error("Failed to read the config file: " + fileName, e);
}
return false;
@@ -45,8 +44,7 @@ public class Config {
return true;
} catch (IOException e) {
Logger.Log("Failed to write the config file: " + fileName);
e.printStackTrace();
Constants.LOGGER.error("Failed to write the config file: " + fileName, e);
}
return false;
@@ -1,9 +1,14 @@
package yalter.mousetweaks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Constants {
public static final String MOD_NAME = "@MODNAME@";
public static final String MOD_ID = "@MODID@";
public static final String MOD_VERSION = "@VERSION@";
public static final Logger LOGGER = LogManager.getLogger(MOD_NAME);
// Obfuscated names
public static final String ISMOUSEOVERSLOT_NAME = "a";
+2 -1
View File
@@ -2,8 +2,9 @@ package yalter.mousetweaks;
public class Logger {
@Deprecated
public static void Log(String textToLog) {
System.out.println("[Mouse Tweaks] " + textToLog);
Constants.LOGGER.info(textToLog);
}
}
+16 -21
View File
@@ -32,6 +32,7 @@ public class Main extends DeobfuscationLayer {
public enum OnTickMethod {PROFILER, FORGE, LITELOADER}
public static List<OnTickMethod> onTickMethodOrder = new LinkedList<OnTickMethod>();
@Deprecated
public static int Debug = 0;
public static Config mainConfig;
@@ -55,7 +56,7 @@ public class Main extends DeobfuscationLayer {
}
public static boolean initialise(Constants.EntryPoint entryPoint) {
Logger.Log("A call to initialise, entry point: " + entryPoint.toString() + ".");
Constants.LOGGER.info("A call to initialise, entry point: " + entryPoint.toString() + ".");
if (disabled)
return false;
@@ -77,17 +78,17 @@ public class Main extends DeobfuscationLayer {
minecraftForge = Reflection.doesClassExist("net.minecraftforge.client.MinecraftForgeClient");
if (minecraftForge) {
Logger.Log("Minecraft Forge is installed.");
Constants.LOGGER.info("Minecraft Forge is installed.");
} else {
Logger.Log("Minecraft Forge is not installed.");
Constants.LOGGER.info("Minecraft Forge is not installed.");
}
liteLoader = ((entryPoint == Constants.EntryPoint.LITELOADER) // If we're initialising from LiteModMouseTweaks, the check is not necessary.
|| Reflection.doesClassExist("com.mumfrey.liteloader.core.LiteLoader"));
if (liteLoader) {
Logger.Log("LiteLoader is installed.");
Constants.LOGGER.info("LiteLoader is installed.");
} else {
Logger.Log("LiteLoader is not installed.");
Constants.LOGGER.info("LiteLoader is not installed.");
}
// Find the first suitable OnTick method.
@@ -97,7 +98,7 @@ public class Main extends DeobfuscationLayer {
case FORGE:
if (minecraftForge) {
useForge = true;
Logger.Log("Using Forge for the mod operation.");
Constants.LOGGER.info("Using Forge for the mod operation.");
break methodLoop;
}
break;
@@ -107,7 +108,7 @@ public class Main extends DeobfuscationLayer {
useLiteLoader = ((entryPoint == Constants.EntryPoint.LITELOADER)
|| Reflection.registerLiteModListenerInLiteLoader());
if (useLiteLoader) {
Logger.Log("Using LiteLoader for the mod operation.");
Constants.LOGGER.info("Using LiteLoader for the mod operation.");
break methodLoop;
}
}
@@ -116,7 +117,7 @@ public class Main extends DeobfuscationLayer {
case PROFILER:
if (Reflection.replaceProfiler()) {
useProfiler = true;
Logger.Log("Using the Minecraft profiler for the mod operation.");
Constants.LOGGER.info("Using the Minecraft profiler for the mod operation.");
optifine = Reflection.reflectOptifine();
@@ -133,7 +134,7 @@ public class Main extends DeobfuscationLayer {
}
ModCompatibility.initialize();
Logger.Log("Mouse Tweaks has been initialised.");
Constants.LOGGER.info("Mouse Tweaks has been initialised.");
return true;
}
@@ -146,7 +147,7 @@ public class Main extends DeobfuscationLayer {
LMBTweakWithoutItem = mainConfig.getOrCreateIntProperty("LMBTweakWithoutItem", 1);
WheelTweak = mainConfig.getOrCreateIntProperty("WheelTweak", 1);
WheelSearchOrder = mainConfig.getOrCreateIntProperty("WheelSearchOrder", 1);
Debug = mainConfig.getOrCreateIntProperty("Debug", 0);
// Debug = mainConfig.getOrCreateIntProperty("Debug", 0);
// Load the OnTick method order.
String onTickMethodString = mainConfig.getOrCreateProperty("OnTickMethodOrder",
@@ -176,7 +177,7 @@ public class Main extends DeobfuscationLayer {
boolean savedConfig = saveConfigFile();
if (savedConfig && !loadedConfig)
Logger.Log("Mouse Tweaks config file was created.");
Constants.LOGGER.info("Mouse Tweaks config file was created.");
}
public static boolean saveConfigFile() {
@@ -185,7 +186,7 @@ public class Main extends DeobfuscationLayer {
mainConfig.setIntProperty("LMBTweakWithoutItem", LMBTweakWithoutItem);
mainConfig.setIntProperty("WheelTweak", WheelTweak);
mainConfig.setIntProperty("WheelSearchOrder", WheelSearchOrder);
mainConfig.setIntProperty("Debug", Debug);
// mainConfig.setIntProperty("Debug", Debug);
String onTickMethodString = "";
for (OnTickMethod method : onTickMethodOrder) {
@@ -254,9 +255,7 @@ public class Main extends DeobfuscationLayer {
container = getContainerWithID(currentScreen);
disableForThisContainer = isDisabledForThisContainer(currentScreen);
if (Debug == 1) {
Logger.Log(new StringBuilder().append("You have just opened a ").append(getGuiContainerNameFromID(currentScreen)).append(" container (").append(currentScreen.getClass().getSimpleName()).append((container == null) ? "" : "; ").append((container == null) ? "" : container.getClass().getSimpleName()).append("), which has ").append(getSlotCountWithID(currentScreen)).append(" slots!").toString());
}
Constants.LOGGER.debug(new StringBuilder().append("You have just opened a ").append(getGuiContainerNameFromID(currentScreen)).append(" container (").append(currentScreen.getClass().getSimpleName()).append((container == null) ? "" : "; ").append((container == null) ? "" : container.getClass().getSimpleName()).append("), which has ").append(getSlotCountWithID(currentScreen)).append(" slots!").toString());
disableWheelForThisContainer = isWheelDisabledForThisContainer(currentScreen);
}
@@ -321,9 +320,7 @@ public class Main extends DeobfuscationLayer {
return;
}
if (Debug == 1) {
Logger.Log(new StringBuilder().append("You have selected a new slot, it's slot number is ").append(getSlotNumber(selectedSlot)).toString());
}
Constants.LOGGER.debug(new StringBuilder().append("You have selected a new slot, it's slot number is ").append(getSlotNumber(selectedSlot)).toString());
boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
@@ -380,9 +377,7 @@ public class Main extends DeobfuscationLayer {
if ((wheel != 0) && (selectedSlot != null)) {
int numItemsToMove = Math.abs(wheel);
if (Debug == 1) {
Logger.Log("numItemsToMove: " + numItemsToMove);
}
Constants.LOGGER.debug("numItemsToMove: " + numItemsToMove);
if (slotCount > Constants.INVENTORY_SIZE) {
ItemStack originalStack = getSlotStack(selectedSlot);
@@ -17,17 +17,17 @@ public class ModCompatibility extends DeobfuscationLayer {
forestryInstalled = Reflection.reflectForestry();
if (forestryInstalled) {
Logger.Log("Successfully reflected Forestry!");
Constants.LOGGER.info("Successfully reflected Forestry!");
}
codechickencoreInstalled = Reflection.reflectCodeChickenCore();
if (codechickencoreInstalled) {
Logger.Log("Successfully reflected CodeChickenCore!");
Constants.LOGGER.info("Successfully reflected CodeChickenCore!");
}
NEIInstalled = Reflection.reflectNEI();
if (NEIInstalled) {
Logger.Log("Successfully reflected NEI!");
Constants.LOGGER.info("Successfully reflected NEI!");
}
initialized = true;
@@ -33,7 +33,7 @@ public class Reflection {
int.class);
if (isMouseOverSlot == null) {
Logger.Log("Failed to get isMouseOverSlot method, quitting");
Constants.LOGGER.error("Failed to get isMouseOverSlot method, quitting");
return false;
}
@@ -44,7 +44,7 @@ public class Reflection {
getObfuscatedName(Constants.FIELDE_MCP_NAME, Constants.FIELDE_FORGE_NAME, Constants.FIELDE_NAME));
if (field == null) {
Logger.Log("Failed to retrieve the E field, disabling RMBTweak");
Constants.LOGGER.error("Failed to retrieve the E field, disabling RMBTweak");
Main.DisableRMBTweak = true;
} else {
guiContainerClass.storeField(Constants.FIELDE_FORGE_NAME, field);
@@ -53,7 +53,7 @@ public class Reflection {
getObfuscatedName(Constants.FIELDq_MCP_NAME, Constants.FIELDq_FORGE_NAME, Constants.FIELDq_NAME));
if (field == null) {
Logger.Log("Failed to retreive the q field, disabling RMBTweak");
Constants.LOGGER.error("Failed to retreive the q field, disabling RMBTweak");
Main.DisableRMBTweak = true;
} else {
guiContainerClass.storeField(Constants.FIELDq_FORGE_NAME, field);
@@ -66,13 +66,13 @@ public class Reflection {
public static boolean registerLiteModListenerInLiteLoader() {
liteLoaderClass = new ReflectionCache();
Class lL = getClass("com.mumfrey.liteloader.core.LiteLoader");
Class<?> lL = getClass("com.mumfrey.liteloader.core.LiteLoader");
if (lL != null) {
liteLoaderClass.storeClass("LiteLoader", lL);
liteLoaderClass.available = true;
Class llEvents = getClass("com.mumfrey.liteloader.core.Events");
Class<?> llEvents = getClass("com.mumfrey.liteloader.core.Events");
if (llEvents != null) {
liteLoaderClass.storeClass("Events", llEvents);
@@ -80,7 +80,7 @@ public class Reflection {
if (llEventsField != null) {
liteLoaderClass.storeField("events", llEventsField);
Class lM = getClass("com.mumfrey.liteloader.LiteMod");
Class<?> lM = getClass("com.mumfrey.liteloader.LiteMod");
if (lM != null) {
liteLoaderClass.storeClass("LiteMod", lM);
@@ -96,7 +96,7 @@ public class Reflection {
if (liteLoaderInstance != null) {
Object llEventsInstance = liteLoaderClass.getFieldValue("events", liteLoaderInstance);
if (llEventsInstance != null) {
Class mouseTweaks = getClass("yalter.mousetweaks.loaders.LiteModMouseTweaks");
Class<?> mouseTweaks = getClass("yalter.mousetweaks.loaders.LiteModMouseTweaks");
if (mouseTweaks != null) {
liteModMouseTweaks = new ReflectionCache();
liteModMouseTweaks.storeClass("LiteModMouseTweaks", mouseTweaks);
@@ -110,21 +110,21 @@ public class Reflection {
liteLoaderClass.invokeMethod(llEventsInstance, "addListener", LMMTInstance);
liteLoaderClass.compatible = true;
Logger.Log("Registered the LiteModMouseTweaks' listener in LiteLoader.");
Constants.LOGGER.info("Registered the LiteModMouseTweaks' listener in LiteLoader.");
return true;
} else {
Logger.Log("Could not get the instance from the getInstance method of LiteModMouseTweaks! Have you modified the class?");
Constants.LOGGER.error("Could not get the instance from the getInstance method of LiteModMouseTweaks! Have you modified the class?");
}
} else {
Logger.Log("Could not get the getInstance method from LiteModMouseTweaks! Have you modified the class?");
Constants.LOGGER.error("Could not get the getInstance method from LiteModMouseTweaks! Have you modified the class?");
}
} else {
Logger.Log("Could not get the LiteModMouseTweaks class! Have you deleted it accidentally?");
Constants.LOGGER.error("Could not get the LiteModMouseTweaks class! Have you deleted it accidentally?");
}
}
} else {
Logger.Log("Failed to retrieve the LiteLoader instance!");
Constants.LOGGER.error("Failed to retrieve the LiteLoader instance!");
}
}
}
@@ -144,11 +144,11 @@ public class Reflection {
if (optifineProfilerEnabled != null) {
gameSettings.storeField("ofProfiler", optifineProfilerEnabled);
Logger.Log("Optifine is installed.");
Constants.LOGGER.info("Optifine is installed.");
return true;
}
Logger.Log("Optifine is not installed.");
Constants.LOGGER.info("Optifine is not installed.");
return false;
}
@@ -162,14 +162,14 @@ public class Reflection {
return minecraft.setFieldValue(Minecraft.getMinecraft(), "mcProfiler", new ProfilerCustom());
}
Logger.Log("Failed to get the Minecraft profiler field!");
Constants.LOGGER.error("Failed to get the Minecraft profiler field!");
return false;
}
public static boolean reflectForestry() {
forestry = new ReflectionCache();
Class guiForestryClass = getClass("forestry.core.gui.GuiForestry");
Class<?> guiForestryClass = getClass("forestry.core.gui.GuiForestry");
if (guiForestryClass != null) {
forestry.storeClass("GuiForestry", guiForestryClass);
forestry.available = true;
@@ -210,7 +210,7 @@ public class Reflection {
public static boolean reflectCodeChickenCore() {
codechickencore = new ReflectionCache();
Class guiContainerWidgetClass = getClass("codechicken.core.inventory.GuiContainerWidget");
Class<?> guiContainerWidgetClass = getClass("codechicken.core.inventory.GuiContainerWidget");
if (guiContainerWidgetClass != null) {
codechickencore.storeClass("GuiContainerWidget", guiContainerWidgetClass);
@@ -224,12 +224,12 @@ public class Reflection {
public static boolean reflectNEI() {
NEI = new ReflectionCache();
Class guiRecipeClass = getClass("codechicken.nei.recipe.GuiRecipe");
Class<?> guiRecipeClass = getClass("codechicken.nei.recipe.GuiRecipe");
if (guiRecipeClass != null) {
NEI.storeClass("GuiRecipe", guiRecipeClass);
NEI.available = true;
Class guiEnchantmentModifierClass = getClass("codechicken.nei.GuiEnchantmentModifier");
Class<?> guiEnchantmentModifierClass = getClass("codechicken.nei.GuiEnchantmentModifier");
if (guiEnchantmentModifierClass != null) {
NEI.storeClass("GuiEnchantmentModifier", guiEnchantmentModifierClass);
@@ -245,11 +245,11 @@ public class Reflection {
}
public static boolean doesClassExist(String name) {
Class clazz = getClass(name);
Class<?> clazz = getClass(name);
return clazz != null;
}
public static Class getClass(String name) {
public static Class<?> getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
@@ -259,7 +259,7 @@ public class Reflection {
return null;
}
public static Field getField(Class clazz, String name) {
public static Field getField(Class<?> clazz, String name) {
try {
Field field = null;
@@ -277,19 +277,14 @@ public class Reflection {
return field;
} catch (Exception e) {
if (name != "ofProfiler") {
Logger.Log("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName()
+ "\"");
if (Main.Debug == 1) {
e.printStackTrace();
}
Constants.LOGGER.error("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() + "\"", e);
}
}
return null;
}
public static Field getFinalField(Class clazz, String name) {
public static Field getFinalField(Class<?> clazz, String name) {
try {
Field field = null;
@@ -311,18 +306,13 @@ public class Reflection {
field.setAccessible(true);
return field;
} catch (Exception e) {
Logger.Log("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName()
+ "\"");
if (Main.Debug == 1) {
e.printStackTrace();
}
Constants.LOGGER.error("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() + "\"", e);
}
return null;
}
public static Method getMethod(Class<?> clazz, String name, Class... args) {
public static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
try {
Method method = null;
@@ -343,12 +333,7 @@ public class Reflection {
method.setAccessible(true);
return method;
} catch (Exception e) {
Logger.Log("Could not retrieve method \"" + name + "\" from class \"" + clazz.getName()
+ "\"");
if (Main.Debug == 1) {
e.printStackTrace();
}
Constants.LOGGER.error("Could not retrieve method \"" + name + "\" from class \"" + clazz.getName() + "\"", e);
}
return null;
@@ -375,7 +360,7 @@ public class Reflection {
public static void checkObfuscation() {
checkObfuscation = false;
Logger.Log("Obfuscation check, please ignore the following errors (if they come up).");
Constants.LOGGER.info("Obfuscation check, please ignore the following errors (if they come up).");
if (getField(Minecraft.class, Constants.MCPROFILER_MCP_NAME) != null)
obfuscation = Obfuscation.MCP;
@@ -384,6 +369,6 @@ public class Reflection {
else
obfuscation = Obfuscation.VANILLA;
Logger.Log("Obfuscation check completed.");
Constants.LOGGER.info("Obfuscation check completed.");
}
}
@@ -7,9 +7,9 @@ import java.util.HashMap;
public class ReflectionCache {
public boolean available = false;
public boolean compatible = false;
public HashMap<String, Class> classes = new HashMap<String, Class>();
public HashMap<String, Method> methods = new HashMap<String, Method>();
public HashMap<String, Field> fields = new HashMap<String, Field>();
public HashMap<String, Class<?>> classes = new HashMap<>();
public HashMap<String, Method> methods = new HashMap<>();
public HashMap<String, Field> fields = new HashMap<>();
public ReflectionCache() {
}
@@ -21,10 +21,10 @@ public class ReflectionCache {
return field.get(obj);
}
Logger.Log("No such field: " + name);
Constants.LOGGER.error("No such field: " + name);
} catch (Exception e) {
e.printStackTrace();
Logger.Log("Failed to get a value of field: " + name);
Constants.LOGGER.error("Failed to get a value of field: " + name);
}
return null;
@@ -39,10 +39,10 @@ public class ReflectionCache {
return true;
}
Logger.Log("No such field: " + name);
Constants.LOGGER.error("No such field: " + name);
} catch (Exception e) {
e.printStackTrace();
Logger.Log("Failed to set a value of field: " + name);
Constants.LOGGER.error("Failed to set a value of field: " + name);
}
return false;
@@ -59,10 +59,10 @@ public class ReflectionCache {
return method.invoke(obj, new Object[0]);
}
Logger.Log("No such method: " + name);
Constants.LOGGER.error("No such method: " + name);
} catch (Exception e) {
e.printStackTrace();
Logger.Log("Failed to invoke method: " + name);
Constants.LOGGER.error("Failed to invoke method: " + name);
}
return null;
@@ -70,7 +70,7 @@ public class ReflectionCache {
public Object invokeStaticMethod(String className, String name, Object... args) {
if (classes.containsKey(className)) {
Class clazz = classes.get(className);
Class<?> clazz = classes.get(className);
return invokeMethod(clazz, name, args);
}
@@ -80,14 +80,14 @@ public class ReflectionCache {
public boolean isInstance(String className, Object obj) {
if (classes.containsKey(className)) {
Class clazz = classes.get(className);
Class<?> clazz = classes.get(className);
return clazz.isInstance(obj);
}
return false;
}
public void storeClass(String name, Class clazz) {
public void storeClass(String name, Class<?> clazz) {
classes.put(name, clazz);
}