Adding logger util

This commit is contained in:
samo_lego 2020-07-16 11:18:39 +02:00
parent 27d5f77d75
commit 60328df15e
8 changed files with 65 additions and 48 deletions

View File

@ -16,8 +16,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.iq80.leveldb.WriteBatch;
import org.samo_lego.simpleauth.commands.*;
import org.samo_lego.simpleauth.event.AuthEventHandler;
@ -40,10 +38,11 @@ import java.util.concurrent.Executors;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.samo_lego.simpleauth.utils.CarpetHelper.isPlayerCarpetFake;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
public static SimpleAuthDatabase DB = new SimpleAuthDatabase();
@ -72,9 +71,9 @@ public class SimpleAuth implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
// Info I guess :D
LOGGER.info("[SimpleAuth] SimpleAuth mod by samo_lego.");
logInfo("SimpleAuth mod by samo_lego.");
// The support on discord was great! I really appreciate your help.
LOGGER.info("[SimpleAuth] This mod wouldn't exist without the awesome Fabric Community. TYSM guys!");
logInfo("This mod wouldn't exist without the awesome Fabric Community. TYSM guys!");
// Creating data directory (database and config files are stored there)
File file = new File(gameDirectory + "/mods/SimpleAuth/leveldbStore");
@ -88,7 +87,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
try {
serverProp.load(new FileReader(gameDirectory + "/server.properties"));
} catch (IOException e) {
LOGGER.error("[SimpleAuth] Error while reading server properties: " + e.getMessage());
logError("Error while reading server properties: " + e.getMessage());
}
@ -120,7 +119,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
}
private void onStopServer() {
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
logInfo("Shutting down SimpleAuth.");
WriteBatch batch = DB.getLevelDBStore().createWriteBatch();
// Writing coords of de-authenticated players to database
@ -143,7 +142,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
DB.getLevelDBStore().write(batch);
batch.close();
} catch (IOException e) {
LOGGER.error("[SimpleAuth] Error saving player data! " + e.getMessage());
logError("Error saving player data! " + e.getMessage());
}
// Closing DB connection
@ -257,10 +256,10 @@ public class SimpleAuth implements DedicatedServerModInitializer {
0
);
} catch (Error e) {
player.sendMessage(new LiteralText(config.lang.corruptedPosition), false);
LOGGER.error("[SimpleAuth] Couldn't teleport player " + player.getName().asString());
LOGGER.error(
String.format("[SimpleAuth] Last recorded position is X: %s, Y: %s, Z: %s in dimension %s",
player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false);
logError("Couldn't teleport player " + player.getName().asString());
logError(
String.format("Last recorded position is X: %s, Y: %s, Z: %s in dimension %s",
cache.lastX,
cache.lastY,
cache.lastZ,

View File

@ -9,8 +9,6 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.storage.AuthConfig;
import org.samo_lego.simpleauth.storage.PlayerCache;
@ -23,9 +21,9 @@ import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.SimpleAuth.*;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class AuthCommand {
private static final Logger LOGGER = LogManager.getLogger();
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/auth" command
@ -105,7 +103,7 @@ public class AuthCommand {
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.configurationReloaded), false);
else
LOGGER.info(config.lang.configurationReloaded);
logInfo(config.lang.configurationReloaded);
return 1;
}
@ -124,7 +122,7 @@ public class AuthCommand {
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false);
else
LOGGER.info(config.lang.globalPasswordSet);
logInfo(config.lang.globalPasswordSet);
return 1;
}
@ -142,7 +140,7 @@ public class AuthCommand {
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.worldSpawnSet), false);
else
LOGGER.info(config.lang.worldSpawnSet);
logInfo(config.lang.worldSpawnSet);
return 1;
}
@ -157,7 +155,7 @@ public class AuthCommand {
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false);
else
LOGGER.info(config.lang.userdataDeleted);
logInfo(config.lang.userdataDeleted);
return 1; // Success
}
@ -176,7 +174,7 @@ public class AuthCommand {
if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
logInfo(config.lang.userdataUpdated);
}
});
return 0;
@ -197,7 +195,7 @@ public class AuthCommand {
if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
logInfo(config.lang.userdataUpdated);
});
return 0;
}

View File

@ -1,6 +1,6 @@
/*
Original author:
https://github.com/jellysquid3/Lithium/blob/1.15.x/fabric/src/main/java/me/jellysquid/mods/lithium/common/config/LithiumConfig.java
/**
* This class has been adapted from old Lithium's config file
* @author jellysquid https://github.com/jellysquid3/Lithium/blob/1.15.x/fabric/src/main/java/me/jellysquid/mods/lithium/common/config/LithiumConfig.java
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -19,12 +19,12 @@ package org.samo_lego.simpleauth.storage;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.nio.charset.StandardCharsets;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
public class AuthConfig {
// If player is not authenticated, following conditions apply
public static class MainConfig {
@ -90,7 +90,7 @@ public class AuthConfig {
public String disallowedUsername = "§6Invalid username characters! Allowed character regex: %s";
public String playerAlreadyOnline = "§cPlayer %s is already online!";
public String worldSpawnSet = "§aSpawn for logging in was set successfully.";
public String corruptedPosition = "§cYour position data is probably corrupted. Please contact admin.";
public String corruptedPlayerData = "§cYour data is probably corrupted. Please contact admin.";
}
public static class ExperimentalConfig {
// Prevents player being kicked because another player with the same name has joined the server
@ -116,7 +116,6 @@ public class AuthConfig {
// Allows attacking mobs
public boolean allowEntityPunch = false;
}
private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
@ -148,7 +147,7 @@ public class AuthConfig {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
gson.toJson(this, writer);
} catch (IOException e) {
LOGGER.error("[SimpleAuth] Problem occurred when saving config: ", e);
logError("Problem occurred when saving config: " + e.getMessage());
}
}
}

View File

@ -2,6 +2,7 @@ package org.samo_lego.simpleauth.storage;
import com.google.gson.*;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import static org.samo_lego.simpleauth.SimpleAuth.DB;
import static org.samo_lego.simpleauth.SimpleAuth.config;
@ -48,6 +49,10 @@ public class PlayerCache {
JsonObject json = gson.fromJson(data, JsonObject.class);
JsonElement passwordElement = json.get("password");
if(passwordElement instanceof JsonNull) {
if(player != null) {
player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false);
}
// This shouldn't have happened, data seems to be corrupted
this.password = null;
this.isRegistered = false;

View File

@ -1,7 +1,5 @@
package org.samo_lego.simpleauth.storage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
import org.iq80.leveldb.Options;
@ -12,9 +10,10 @@ import java.io.IOException;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.iq80.leveldb.impl.Iq80DBFactory.factory;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class SimpleAuthDatabase {
private static final Logger LOGGER = LogManager.getLogger();
private DB levelDBStore;
public DB getLevelDBStore() {
@ -28,7 +27,7 @@ public class SimpleAuthDatabase {
Options options = new Options();
levelDBStore = factory.open(new File(SimpleAuth.gameDirectory + "/mods/SimpleAuth/levelDBStore"), options);
} catch (Error | IOException e) {
LOGGER.error("[SimpleAuth] " + e.getMessage());
logError(e.getMessage());
}
}
// Closing connection
@ -36,9 +35,9 @@ public class SimpleAuthDatabase {
if (levelDBStore != null) {
try {
levelDBStore.close();
LOGGER.info("[SimpleAuth] Database connection closed successfully.");
logInfo("Database connection closed successfully.");
} catch (Error | IOException e) {
LOGGER.info("[SimpleAuth] Error: " + e.getMessage());
logError(e.getMessage());
}
}
}
@ -58,7 +57,7 @@ public class SimpleAuthDatabase {
}
return false;
} catch (Error e) {
LOGGER.error("[SimpleAuth] Register error: " + e.getMessage());
logError("Register error: " + e.getMessage());
return false;
}
}
@ -68,7 +67,7 @@ public class SimpleAuthDatabase {
try {
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
} catch (DBException e) {
LOGGER.error("[SimpleAuth] " + e.getMessage());
logError(e.getMessage());
}
return false;
}
@ -78,7 +77,7 @@ public class SimpleAuthDatabase {
try {
levelDBStore.delete(bytes("UUID:" + uuid));
} catch (Error e) {
LOGGER.error("[SimpleAuth] " + e.getMessage());
logError(e.getMessage());
}
}
@ -87,7 +86,7 @@ public class SimpleAuthDatabase {
try {
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data));
} catch (Error e) {
LOGGER.error("[SimpleAuth] " + e.getMessage());
logError(e.getMessage());
}
}
@ -97,7 +96,7 @@ public class SimpleAuthDatabase {
if(this.isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it
return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5);
} catch (Error e) {
LOGGER.error("[SimpleAuth] Error getting password: " + e.getMessage());
logError("Error getting password: " + e.getMessage());
}
return "";
}

View File

@ -4,12 +4,11 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.samo_lego.simpleauth.SimpleAuth;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
public class AuthHelper {
private static final Logger LOGGER = LogManager.getLogger();
// Creating the instance
private static final Argon2 argon2 = Argon2Factory.create();
@ -26,7 +25,7 @@ public class AuthHelper {
return argon2.verify(SimpleAuth.config.main.globalPassword, pass) ? 1 : 0;
}
catch (Error e) {
LOGGER.error("[SimpleAuth] Argon2 error: " + e);
logError("Argon2 error: " + e);
return 0;
} finally {
// Wipe confidential data
@ -50,7 +49,7 @@ public class AuthHelper {
// Verify password
return argon2.verify(hashed, pass) ? 1 : 0;
} catch (Error e) {
LOGGER.error("[SimpleAuth] Argon2 error: " + e);
logError("Argon2 error: " + e);
return 0;
} finally {
// Wipe confidential data
@ -63,7 +62,7 @@ public class AuthHelper {
try {
return argon2.hash(10, 65536, 1, pass);
} catch (Error e) {
LOGGER.error("[SimpleAuth] " + e);
logError(e.getMessage());
}
return null;
}

View File

@ -1,12 +1,13 @@
package org.samo_lego.simpleauth.utils;
import carpet.patches.EntityPlayerMPFake;
//import carpet.patches.EntityPlayerMPFake;
import net.minecraft.entity.player.PlayerEntity;
public class CarpetHelper {
// Checking if player is actually a fake one
// This is in its own class since we need carpet import
public static boolean isPlayerCarpetFake(PlayerEntity player) {
return player instanceof EntityPlayerMPFake;
//return player instanceof EntityPlayerMPFake;
return false;
}
}

View File

@ -0,0 +1,17 @@
package org.samo_lego.simpleauth.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SimpleLogger {
private static final Logger LOGGER = LogManager.getLogger();
public static void logError(String error) {
LOGGER.error("[SimpleAuth] " + error);
}
public static void logInfo(String info) {
LOGGER.info("[SimpleAuth] " + info);
}
}