From 9965e8d804819952ec82e14d17daf084766f7d22 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Mon, 30 Dec 2019 20:19:53 +0100 Subject: [PATCH] Config added Some cleanups as well. --- README.md | 1 + build.gradle | 1 + gradle.properties | 10 +-- settings.gradle | 1 - .../org/samo_lego/simpleauth/SimpleAuth.java | 27 +++--- .../simpleauth/commands/AuthCommand.java | 24 ++++- .../simpleauth/commands/ChangepwCommand.java | 14 +-- .../simpleauth/commands/LoginCommand.java | 16 ++-- .../simpleauth/commands/RegisterCommand.java | 15 ++-- .../commands/UnregisterCommand.java | 2 - .../database/SimpleAuthDatabase.java | 20 +++-- .../simpleauth/event/AuthEventHandler.java | 42 +++++---- ...{OnChatCallback.java => ChatCallback.java} | 6 +- ...eCallback.java => PlayerMoveCallback.java} | 7 +- .../simpleauth/mixin/MixinPlayerEntity.java | 2 - .../mixin/MixinServerPlayNetworkHandler.java | 18 ++-- .../simpleauth/utils/AuthConfig.java | 90 +++++++++++++++++++ .../simpleauth/utils/AuthHelper.java | 4 +- .../assets/simpleauth/lang/en_us.json | 9 -- 19 files changed, 207 insertions(+), 102 deletions(-) rename src/main/java/org/samo_lego/simpleauth/event/entity/player/{OnChatCallback.java => ChatCallback.java} (74%) rename src/main/java/org/samo_lego/simpleauth/event/entity/player/{OnPlayerMoveCallback.java => PlayerMoveCallback.java} (63%) create mode 100644 src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java delete mode 100644 src/main/resources/assets/simpleauth/lang/en_us.json diff --git a/README.md b/README.md index d9ffeaf..7cacb75 100644 --- a/README.md +++ b/README.md @@ -16,5 +16,6 @@ Requires Fabric API. Libraries that the project is using: - `Argon2 (LGPLv3)` https://github.com/phxql/argon2-jvm - `JDBC (Apache 2)` https://github.com/xerial/sqlite-jdbc +- `JNA (Apache 2 || LGPLv3)` https://github.com/java-native-access/jna This project is licensed under the `MIT` license. diff --git a/build.gradle b/build.gradle index ef84d66..5e1ea45 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ dependencies { include 'de.mkammerer:argon2-jvm:2.6' // JDBC SQLite Driver + implementation group:'org.xerial', name:'sqlite-jdbc', version:'3.8.11.2' include group:'org.xerial', name:'sqlite-jdbc', version:'3.8.11.2' // JNA lib diff --git a/gradle.properties b/gradle.properties index 153ad76..546c960 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,14 +2,14 @@ org.gradle.jvmargs=-Xmx1G # Fabric properties -minecraft_version=1.15-pre3 -yarn_mappings=1.15-pre3+build.2 -loader_version=0.7.2+build.174 +minecraft_version=1.15.1 +yarn_mappings=1.15.1+build.18 +loader_version=0.7.3+build.176 #Fabric api -fabric_version=0.4.18+build.271-1.15 +fabric_version=0.4.26+build.283-1.15 # Mod Properties - mod_version = 1.0.0 + mod_version = 1.1.0 maven_group = org.samo_lego archives_base_name = simpleauth \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 92ef51b..5b60df3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,7 +5,6 @@ pluginManagement { name = 'Fabric' url = 'https://maven.fabricmc.net/' } - gradlePluginPortal() } } diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 6e8bb9a..4043f1e 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -11,11 +11,12 @@ import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.commands.*; import org.samo_lego.simpleauth.database.SimpleAuthDatabase; import org.samo_lego.simpleauth.event.AuthEventHandler; -import org.samo_lego.simpleauth.event.entity.player.OnChatCallback; -import org.samo_lego.simpleauth.event.entity.player.OnPlayerMoveCallback; +import org.samo_lego.simpleauth.event.entity.player.ChatCallback; +import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; import org.samo_lego.simpleauth.event.item.DropItemCallback; +import org.samo_lego.simpleauth.utils.AuthConfig; import java.io.File; import java.util.HashSet; @@ -25,6 +26,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { public static SimpleAuthDatabase db = new SimpleAuthDatabase(); public static HashSet authenticatedUsers = new HashSet<>(); public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); } + public static AuthConfig config; + @Override public void onInitializeServer() { @@ -32,14 +35,17 @@ public class SimpleAuth implements DedicatedServerModInitializer { LOGGER.info("[SimpleAuth] 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!"); - // Connecting to db - db.openConnection(); - // Creating data directory (database is stored there) + // Creating data directory (database and config files are stored there) File file = new File("./mods/SimpleAuth"); if (!file.exists() && !file.mkdir()) LOGGER.error("[SimpleAuth] Error creating directory!"); - + // Loading config + config = AuthConfig.load(new File("./mods/SimpleAuth/config.json")); + // Connecting to db + db.openConnection(); + // Making a table in the database + db.makeTable(); // Registering the commands CommandRegistry.INSTANCE.register(false, dispatcher -> { @@ -54,8 +60,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave); DropItemCallback.EVENT.register(AuthEventHandler::onDropItem); - OnChatCallback.EVENT.register(AuthEventHandler::onPlayerChat); - OnPlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove); + ChatCallback.EVENT.register(AuthEventHandler::onPlayerChat); + PlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove); // From Fabric API AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity)); UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player)); @@ -63,10 +69,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onAttackEntity(player)); UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player)); ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer()); - // Making a table in the database - db.makeTable(); - } - + } private static void onStopServer() { LOGGER.info("[SimpleAuth] Shutting down SimpleAuth."); db.close(); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java index 69b2869..25233ca 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -3,12 +3,16 @@ package org.samo_lego.simpleauth.commands; import com.mojang.brigadier.CommandDispatcher; import net.minecraft.entity.Entity; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.SimpleAuth; +import org.samo_lego.simpleauth.utils.AuthConfig; import org.samo_lego.simpleauth.utils.AuthHelper; +import java.io.File; + import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; @@ -17,13 +21,17 @@ import static net.minecraft.server.command.CommandManager.literal; public class AuthCommand { private static final Logger LOGGER = LogManager.getLogger(); - private static TranslatableText userdataDeleted = new TranslatableText("§aUserdata deleted."); - private static TranslatableText userdataUpdated = new TranslatableText("§aUserdata updated."); + private static Text userdataDeleted = new LiteralText(SimpleAuth.config.lang.userdataDeleted); + private static Text userdataUpdated = new LiteralText(SimpleAuth.config.lang.userdataUpdated); + private static Text configurationReloaded = new LiteralText(SimpleAuth.config.lang.configurationReloaded); public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/auth" command dispatcher.register(literal("auth") .requires(source -> source.hasPermissionLevel(4)) + .then(literal("reload") + .executes( ctx -> reloadConfig(ctx.getSource())) + ) .then(literal("update") .then(literal("byUuid") .then(argument("uuid", word()) @@ -102,4 +110,14 @@ public class AuthCommand { LOGGER.info(userdataDeleted); return 1; // Success } + private static int reloadConfig(ServerCommandSource source) { + Entity sender = source.getEntity(); + SimpleAuth.config = AuthConfig.load(new File("./mods/SimpleAuth/config.json")); + + if(sender != null) + sender.sendMessage(configurationReloaded); + else + LOGGER.info(configurationReloaded); + return 1; + } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java index 27bec2c..db645d2 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java @@ -2,11 +2,11 @@ package org.samo_lego.simpleauth.commands; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import de.mkammerer.argon2.Argon2; -import de.mkammerer.argon2.Argon2Factory; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; +import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.utils.AuthHelper; @@ -16,10 +16,10 @@ import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; public class ChangepwCommand { - private static TranslatableText enterNewPassword = new TranslatableText("§4You need to enter new password!"); - private static TranslatableText enterPassword = new TranslatableText("§6You need to enter your password!"); - private static TranslatableText wrongPassword = new TranslatableText("§4Wrong password!"); - private static TranslatableText passwordUpdated = new TranslatableText("§4Your password was updated successfully!"); + private static Text enterNewPassword = new LiteralText(SimpleAuth.config.lang.enterNewPassword); + private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); + private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); + private static Text passwordUpdated = new LiteralText(SimpleAuth.config.lang.passwordUpdated); public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/changepw" command diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index bbce329..f6a7792 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -6,7 +6,7 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.utils.AuthHelper; @@ -16,10 +16,11 @@ import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; public class LoginCommand { - private static TranslatableText enterPassword = new TranslatableText("§6You need to enter your password!"); - private static TranslatableText wrongPassword = new TranslatableText("§4Wrong password!"); - private static TranslatableText alreadyAuthenticated = new TranslatableText("§4You are already authenticated."); - private static Text text = new LiteralText("§aYou are now authenticated."); + private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); + private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); + private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); + private static Text loginTriesExceeded = new LiteralText("§4Too many login tries."); + private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated); public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/login" command @@ -37,7 +38,6 @@ public class LoginCommand { private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException { // Getting the player who send the command ServerPlayerEntity player = source.getPlayer(); - if(SimpleAuth.isAuthenticated(player)) { player.sendMessage(alreadyAuthenticated); return 0; @@ -47,7 +47,9 @@ public class LoginCommand { // Player no longer needs to be invisible and invulnerable player.setInvulnerable(false); player.setInvisible(false); - player.sendMessage(text); + //player.setAir(AuthEventHandler.playerAir); + //player.getDataTracker().startTracking(); + player.sendMessage(successfullyAuthenticated); return 1; } player.sendMessage(wrongPassword); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index fa97bd6..dcae593 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -5,7 +5,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.utils.AuthHelper; @@ -16,11 +16,11 @@ import static net.minecraft.server.command.CommandManager.literal; public class RegisterCommand { - private static TranslatableText enterPassword = new TranslatableText("§6You need to enter your password!"); - private static TranslatableText alreadyAuthenticated = new TranslatableText("§4You are already authenticated."); - private static TranslatableText alreadyRegistered = new TranslatableText("§6This account name is already registered!"); - private static TranslatableText registerSuccess = new TranslatableText("§aYou are now authenticated."); - private static TranslatableText matchPass = new TranslatableText( "§6Passwords must match!"); + private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); + private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); + private static Text alreadyRegistered = new LiteralText(SimpleAuth.config.lang.alreadyRegistered); + private static Text registerSuccess = new LiteralText(SimpleAuth.config.lang.registerSuccess); + private static Text matchPass = new LiteralText( SimpleAuth.config.lang.matchPassword); public static void registerCommand(CommandDispatcher dispatcher) { @@ -41,6 +41,7 @@ public class RegisterCommand { ServerPlayerEntity player = source.getPlayer(); if(SimpleAuth.isAuthenticated(player)) { player.sendMessage(alreadyAuthenticated); + return 0; } else if(pass1.equals(pass2)) { String hash = AuthHelper.hashPass(pass1.toCharArray()); @@ -49,6 +50,8 @@ public class RegisterCommand { // Player no longer needs to be invisible and invulnerable player.setInvulnerable(false); player.setInvisible(false); + if(player.isInWater()) + player.setAir(10); player.sendMessage(registerSuccess); return 1; } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java index beb063d..d2b8987 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java @@ -2,8 +2,6 @@ package org.samo_lego.simpleauth.commands; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import de.mkammerer.argon2.Argon2; -import de.mkammerer.argon2.Argon2Factory; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TranslatableText; diff --git a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java index f38616a..d62520b 100644 --- a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java @@ -22,17 +22,17 @@ public class SimpleAuthDatabase { try { conn = DriverManager.getConnection(url); } catch (SQLException e) { - LOGGER.error(e.getMessage()); + LOGGER.error("[SimpleAuth] " + e.getMessage()); } } - // Cllosing connection + // Closing connection public void close() { if (conn != null) { try { conn.close(); LOGGER.info("[SimpleAuth] Database connection closed successfully."); } catch (SQLException e) { - LOGGER.info("[SimpleAuth] Error: " + e); + LOGGER.info("[SimpleAuth] Error: " + e.getMessage()); } } } @@ -52,7 +52,7 @@ public class SimpleAuthDatabase { Statement stmt = conn.createStatement(); stmt.execute(sql); } catch (SQLException e) { - LOGGER.error(e.getMessage()); + LOGGER.error("[SimpleAuth] Error: " + e.getMessage()); } } @@ -74,15 +74,17 @@ public class SimpleAuthDatabase { rs.getString("UUID"); return false; } catch(SQLException ignored) { + // User is not registered + } finally { pstmt.setString(1, uuid); pstmt.setString(2, username); pstmt.setString(3, password); pstmt.executeUpdate(); - return true; } + return true; } catch (SQLException e) { - LOGGER.error("Register error: " + e.getMessage()); + LOGGER.error("[SimpleAuth] Register error: " + e.getMessage()); return false; } } @@ -100,7 +102,7 @@ public class SimpleAuthDatabase { pstmt.executeUpdate(); } catch (SQLException e) { - LOGGER.error(e.getMessage()); + LOGGER.error("[SimpleAuth] " + e.getMessage()); } } @@ -119,7 +121,7 @@ public class SimpleAuthDatabase { // update pstmt.executeUpdate(); } catch (SQLException e) { - LOGGER.error(e.getMessage()); + LOGGER.error("[SimpleAuth] " + e.getMessage()); } } @@ -137,7 +139,7 @@ public class SimpleAuthDatabase { // Getting the password pass = rs.getString("Password"); } catch (SQLException e) { - LOGGER.error(e.getMessage()); + LOGGER.error("[SimpleAuth] Error getting password: " + e.getMessage()); } return pass; } diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 878a1ad..ba35888 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -14,7 +14,7 @@ import org.samo_lego.simpleauth.SimpleAuth; * and cancels them if they aren't authenticated */ public class AuthEventHandler { - private static TranslatableText notAuthenticated = new TranslatableText("§4You are not authenticated!\nTry with /login or /register."); + private static TranslatableText notAuthenticated = new TranslatableText(SimpleAuth.config.lang.notAuthenticated); // Player joining the server public static void onPlayerJoin(ServerPlayerEntity player) { @@ -22,11 +22,10 @@ public class AuthEventHandler { if (!SimpleAuth.isAuthenticated(player)) { player.sendMessage(notAuthenticated); // Setting the player to be invisible to mobs and also invulnerable - player.setInvulnerable(true); - player.setInvisible(true); + player.setInvulnerable(SimpleAuth.config.main.playerInvulnerable); + player.setInvisible(SimpleAuth.config.main.playerInvisible); } } - // Player leaving the server public static void onPlayerLeave(ServerPlayerEntity player) { SimpleAuth.authenticatedUsers.remove(player); @@ -34,7 +33,12 @@ public class AuthEventHandler { public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) { String msg = chatMessageC2SPacket.getChatMessage(); - if(!SimpleAuth.authenticatedUsers.contains(player) && !msg.startsWith("/login") && !msg.startsWith("/register")) { + if( + !SimpleAuth.authenticatedUsers.contains(player) && + !msg.startsWith("/login") && + !msg.startsWith("/register") && + (!SimpleAuth.config.main.allowChat || msg.startsWith("/")) + ) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -42,7 +46,7 @@ public class AuthEventHandler { } // Player movement public static ActionResult onPlayerMove(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowMovement) { return ActionResult.FAIL; } return ActionResult.PASS; @@ -50,7 +54,7 @@ public class AuthEventHandler { // Using a block (right-click function) public static ActionResult onUseBlock(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowBlockUse) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -59,7 +63,7 @@ public class AuthEventHandler { // Punching a block public static ActionResult onAttackBlock(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowBlockPunch) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -68,16 +72,24 @@ public class AuthEventHandler { // Using an item public static TypedActionResult onUseItem(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowItemUse) { player.sendMessage(notAuthenticated); return TypedActionResult.fail(ItemStack.EMPTY); } return TypedActionResult.pass(ItemStack.EMPTY); } + // Dropping an item + public static ActionResult onDropItem(PlayerEntity player) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowItemDrop) { + player.sendMessage(notAuthenticated); + return ActionResult.FAIL; + } + return ActionResult.PASS; + } // Attacking an entity public static ActionResult onAttackEntity(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowEntityPunch) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -86,19 +98,11 @@ public class AuthEventHandler { } // Interacting with entity public static ActionResult onUseEntity(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { + if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowEntityInteract) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } return ActionResult.PASS; } - // Dropping an item - public static ActionResult onDropItem(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player)) { - player.sendMessage(notAuthenticated); - return ActionResult.FAIL; - } - return ActionResult.PASS; - } } \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/ChatCallback.java similarity index 74% rename from src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java rename to src/main/java/org/samo_lego/simpleauth/event/entity/player/ChatCallback.java index dca8ed1..8805ca1 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/entity/player/ChatCallback.java @@ -6,9 +6,9 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.packet.ChatMessageC2SPacket; import net.minecraft.util.ActionResult; -public interface OnChatCallback { - Event EVENT = EventFactory.createArrayBacked(OnChatCallback.class, listeners -> (player, chatMessageC2SPacket) -> { - for (OnChatCallback event : listeners) { +public interface ChatCallback { + Event EVENT = EventFactory.createArrayBacked(ChatCallback.class, listeners -> (player, chatMessageC2SPacket) -> { + for (ChatCallback event : listeners) { ActionResult result = event.onPlayerChat(player, chatMessageC2SPacket); if (result != ActionResult.PASS) { diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerMoveCallback.java similarity index 63% rename from src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java rename to src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerMoveCallback.java index 6ad4e06..6ec185f 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerMoveCallback.java @@ -3,12 +3,11 @@ package org.samo_lego.simpleauth.event.entity.player; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.server.network.packet.ChatMessageC2SPacket; import net.minecraft.util.ActionResult; -public interface OnPlayerMoveCallback { - Event EVENT = EventFactory.createArrayBacked(OnPlayerMoveCallback.class, listeners -> (player) -> { - for (OnPlayerMoveCallback event : listeners) { +public interface PlayerMoveCallback { + Event EVENT = EventFactory.createArrayBacked(PlayerMoveCallback.class, listeners -> (player) -> { + for (PlayerMoveCallback event : listeners) { ActionResult result = event.onPlayerMove(player); if (result != ActionResult.PASS) { diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java index 597078c..f3f0bcc 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java @@ -1,12 +1,10 @@ package org.samo_lego.simpleauth.mixin; -import net.minecraft.client.network.packet.InventoryS2CPacket; import net.minecraft.container.PlayerContainer; import net.minecraft.entity.ItemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.server.network.packet.UpdateSelectedSlotC2SPacket; import net.minecraft.util.ActionResult; import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.spongepowered.asm.mixin.Final; diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java index 9aaa6b0..9aed3b2 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java @@ -1,16 +1,14 @@ package org.samo_lego.simpleauth.mixin; -import net.minecraft.client.network.packet.InventoryS2CPacket; -import net.minecraft.client.network.packet.PlayerSpawnPositionS2CPacket; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.Packet; +import net.minecraft.client.network.packet.EntityS2CPacket; +import net.minecraft.network.NetworkThreadUtils; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.packet.ChatMessageC2SPacket; import net.minecraft.server.network.packet.PlayerMoveC2SPacket; import net.minecraft.util.ActionResult; -import org.samo_lego.simpleauth.event.entity.player.OnChatCallback; -import org.samo_lego.simpleauth.event.entity.player.OnPlayerMoveCallback; +import org.samo_lego.simpleauth.event.entity.player.ChatCallback; +import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -22,8 +20,6 @@ public abstract class MixinServerPlayNetworkHandler { @Shadow public ServerPlayerEntity player; - @Shadow public abstract void sendPacket(Packet packet_1); - @Inject( method = "onChatMessage(Lnet/minecraft/server/network/packet/ChatMessageC2SPacket;)V", at = @At( @@ -34,14 +30,14 @@ public abstract class MixinServerPlayNetworkHandler { ), cancellable = true ) + // todo: redirect packets to off-thread packet manager? private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfo ci) { - ActionResult result = OnChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); + ActionResult result = ChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); if (result == ActionResult.FAIL) { ci.cancel(); } } - // TODO @Inject( method="onPlayerMove(Lnet/minecraft/server/network/packet/PlayerMoveC2SPacket;)V", at = @At( @@ -53,7 +49,7 @@ public abstract class MixinServerPlayNetworkHandler { cancellable = true ) private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo ci) { - ActionResult result = OnPlayerMoveCallback.EVENT.invoker().onPlayerMove(player); + ActionResult result = PlayerMoveCallback.EVENT.invoker().onPlayerMove(player); if (result == ActionResult.FAIL) { // A bit ugly, I know. (we need to update player position) player.teleport(player.getX(), player.getY(), player.getZ()); diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java new file mode 100644 index 0000000..4ca0501 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java @@ -0,0 +1,90 @@ +/* + Original author: + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +package org.samo_lego.simpleauth.utils; + +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.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +public class AuthConfig { + public static class MainConfig { + public boolean allowChat = false; + public boolean allowMovement = false; + public boolean allowBlockUse = false; + public boolean allowBlockPunch = false; + public boolean allowItemDrop = false; + public boolean allowItemUse = false; + public boolean allowEntityPunch = false; + public boolean allowEntityInteract = false; + public boolean playerInvulnerable = true; + public boolean playerInvisible = true; + } + public static class LangConfig { + public String enterPassword = "§6You need to enter your password!"; + public String enterNewPassword = "§4You need to enter new password!"; + public String wrongPassword = "§4Wrong password!"; + public String matchPassword = "§6Passwords must match!"; + public String passwordUpdated = "§4Your password was updated successfully!"; + public String notAuthenticated = "§cYou are not authenticated!\n§6Try with /login or /register."; + public String alreadyAuthenticated = "§4You are already authenticated."; + public String successfullyAuthenticated = "§aYou are now authenticated."; + public String alreadyRegistered = "§6This account name is already registered!"; + public String registerSuccess = "§aYou are now authenticated."; + public String userdataDeleted = "§aUserdata deleted."; + public String userdataUpdated = "§aUserdata updated."; + public String configurationReloaded = "§aConfiguration file was reloaded successfully."; + + } + private static final Logger LOGGER = LogManager.getLogger(); + private static final Gson gson = new GsonBuilder() + .setPrettyPrinting() + .create(); + + public MainConfig main = new MainConfig(); + public LangConfig lang = new LangConfig(); + + public static AuthConfig load(File file) { + AuthConfig config; + if (file.exists()) { + try (FileReader fileReader = new FileReader(file)) { + config = gson.fromJson(fileReader, AuthConfig.class); + } catch (IOException e) { + throw new RuntimeException("[SimpleAuth] Problem occurred when trying to load config: ", e); + } + } + else { + config = new AuthConfig(); + } + config.save(file); + + return config; + } + private void save(File file) { + try (FileWriter writer = new FileWriter(file)) { + gson.toJson(this, writer); + } catch (IOException e) { + LOGGER.error("[SimpleAuth] Problem occurred when saving config: ", e); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index e0d0941..16ece20 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -20,18 +20,18 @@ public class AuthHelper { return argon2.verify(hashed, pass); } catch(Error e) { LOGGER.error("[SimpleAuth] error: " + e); + return false; } finally { // Wipe confidential data argon2.wipeArray(pass); } - return false; } // Hashing the password with the Argon2 power public static String hashPass(char[] pass) { try { return argon2.hash(10, 65536, 1, pass); } catch (Error e) { - LOGGER.error(e); + LOGGER.error("[SimpleAuth] " + e); } return null; } diff --git a/src/main/resources/assets/simpleauth/lang/en_us.json b/src/main/resources/assets/simpleauth/lang/en_us.json deleted file mode 100644 index 36d0892..0000000 --- a/src/main/resources/assets/simpleauth/lang/en_us.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "command.simpleauth.password": "§6You need to enter your password!", - "command.simpleauth.passwordTwice": "§6You need to enter your password twice!", - "command.simpleauth.passwordMatch": "§6Passwords must match!", - "command.simpleauth.passwordWrong": "§4Wrong password!", - "command.simpleauth.alreadyRegistered": "§6This account name is already registered!", - "command.simpleauth.notAuthenticated": "§4You are not authenticated!", - "command.simpleauth.authenticated": "§aYou are now authenticated." -} \ No newline at end of file