From a5bd4372276295ed7c65df0b842eed0b93bfcd3f Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sun, 23 Aug 2020 22:02:55 +0200 Subject: [PATCH] Support /reload --- .../org/samo_lego/simpleauth/SimpleAuth.java | 23 +++++++++++-------- .../simpleauth/commands/AuthCommand.java | 5 ++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 0d49cb7..8b6553d 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -51,9 +51,10 @@ public class SimpleAuth implements DedicatedServerModInitializer { private static final Timer TIMER = new Timer(); - // HashMap of players that are not authenticated - // Rather than storing all the authenticated players, we just store ones that are not authenticated - // It stores some data as well, e.g. login tries and user password + /** + * HashMap of non-authenticated players. + * Stores their data as {@link org.samo_lego.simpleauth.storage.PlayerCache PlayerCache} object + */ public static HashMap deauthenticatedUsers = new HashMap<>(); /** @@ -120,18 +121,22 @@ public class SimpleAuth implements DedicatedServerModInitializer { // From Fabric API AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity)); - UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player)); - UseItemCallback.EVENT.register((player, world, hand) -> AuthEventHandler.onUseItem(player)); - AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onAttackEntity(player)); + UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player)); + UseItemCallback.EVENT.register((player, world, hand) -> AuthEventHandler.onUseItem(player)); + AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onAttackEntity(player)); UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player)); - ServerLifecycleEvents.SERVER_STOPPED.register(minecraftServer -> this.onStopServer()); + ServerLifecycleEvents.START_DATA_PACK_RELOAD.register((server, serverResourceManager) -> AuthCommand.reloadConfig(null)); + ServerLifecycleEvents.SERVER_STOPPED.register(this::onStopServer); } - private void onStopServer() { + /** + * Called on server stop. + */ + private void onStopServer(MinecraftServer server) { logInfo("Shutting down SimpleAuth."); WriteBatch batch = DB.getLevelDBStore().createWriteBatch(); - // Writing coords of de-authenticated players to database + // Writing coordinates of de-authenticated players to database deauthenticatedUsers.forEach((uuid, playerCache) -> { JsonObject data = new JsonObject(); data.addProperty("password", playerCache.password); 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 865cbdb..0bebf8d 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -30,7 +30,7 @@ public class AuthCommand { dispatcher.register(literal("auth") .requires(source -> source.hasPermissionLevel(4)) .then(literal("reload") - .executes( ctx -> reloadConfig(ctx.getSource())) + .executes( ctx -> reloadConfig(ctx.getSource().getEntity())) ) .then(literal("setGlobalPassword") .then(argument("password", word()) @@ -96,8 +96,7 @@ public class AuthCommand { } // Reloading the config - private static int reloadConfig(ServerCommandSource source) { - Entity sender = source.getEntity(); + public static int reloadConfig(Entity sender) { config = AuthConfig.load(new File("./mods/SimpleAuth/config.json")); if(sender != null)