From 27d5f77d758d8c3e0d4de2c3b47ffcd63f134e62 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Thu, 16 Jul 2020 11:01:57 +0200 Subject: [PATCH] Fixing server crash #7 --- gradle.properties | 6 ++-- .../org/samo_lego/simpleauth/SimpleAuth.java | 2 +- .../simpleauth/event/AuthEventHandler.java | 28 ++++++------------- .../simpleauth/storage/PlayerCache.java | 18 ++++++++---- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/gradle.properties b/gradle.properties index 6712c3b..a15cdc4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,9 +2,9 @@ org.gradle.jvmargs=-Xmx1G # Fabric properties -minecraft_version=20w27a -yarn_mappings=20w27a+build.1 -loader_version=0.8.9+build.203 +minecraft_version=1.16.1 +yarn_mappings=1.16.1+build.21 +loader_version=0.9.0+build.204 #Fabric api fabric_version=0.14.1+build.372-1.16 diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 7fce2a2..be21e17 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -151,7 +151,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { } // Getting not authenticated text - private static LiteralText notAuthenticated(ServerPlayerEntity player) { + public static LiteralText notAuthenticated(PlayerEntity player) { final PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); if(SimpleAuth.config.main.enableGlobalPassword || cache.isRegistered) return new LiteralText( 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 5e7c7f8..3752119 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -9,7 +9,6 @@ import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; @@ -28,12 +27,6 @@ import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; * and cancel them if they aren't authenticated */ public class AuthEventHandler { - private static Text notAuthenticated() { - if(config.main.enableGlobalPassword) { - return new LiteralText(config.lang.loginRequired); - } - return new LiteralText(config.lang.notAuthenticated); - } // Player pre-join // Returns text as a reason for disconnect or null to pass @@ -88,10 +81,7 @@ public class AuthEventHandler { } // Invalidating session playerCache.wasAuthenticated = false; - if(playerCache.isRegistered) - player.sendMessage(new LiteralText(config.lang.loginRequired), false); - else - player.sendMessage(new LiteralText(config.lang.registerRequired), false); + player.sendMessage(notAuthenticated(player), false); } else { deauthenticatePlayer(player); @@ -152,7 +142,7 @@ public class AuthEventHandler { !msg.startsWith("/register") && (!config.experimental.allowChat || msg.startsWith("/")) ) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } return ActionResult.PASS; @@ -169,7 +159,7 @@ public class AuthEventHandler { // Using a block (right-click function) public static ActionResult onUseBlock(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockUse) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } return ActionResult.PASS; @@ -178,7 +168,7 @@ public class AuthEventHandler { // Punching a block public static ActionResult onAttackBlock(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } return ActionResult.PASS; @@ -187,7 +177,7 @@ public class AuthEventHandler { // Using an item public static TypedActionResult onUseItem(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemUse) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return TypedActionResult.fail(ItemStack.EMPTY); } @@ -196,7 +186,7 @@ public class AuthEventHandler { // Dropping an item public static ActionResult onDropItem(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemDrop) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } return ActionResult.PASS; @@ -204,7 +194,7 @@ public class AuthEventHandler { // Changing inventory (item moving etc.) public static ActionResult onTakeItem(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemMoving) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } @@ -213,7 +203,7 @@ public class AuthEventHandler { // Attacking an entity public static ActionResult onAttackEntity(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowEntityPunch) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } @@ -222,7 +212,7 @@ public class AuthEventHandler { // Interacting with entity public static ActionResult onUseEntity(PlayerEntity player) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.main.allowEntityInteract) { - player.sendMessage(notAuthenticated(), false); + player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index 4da6ade..bbc04e4 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -1,9 +1,6 @@ package org.samo_lego.simpleauth.storage; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSyntaxException; +import com.google.gson.*; import net.minecraft.server.network.ServerPlayerEntity; import static org.samo_lego.simpleauth.SimpleAuth.DB; @@ -49,8 +46,17 @@ public class PlayerCache { // Getting (hashed) password JsonObject json = gson.fromJson(data, JsonObject.class); - this.password = json.get("password").getAsString(); - this.isRegistered = true; + JsonElement passwordElement = json.get("password"); + if(passwordElement instanceof JsonNull) { + // This shouldn't have happened, data seems to be corrupted + this.password = null; + this.isRegistered = false; + } + else { + this.password = passwordElement.getAsString(); + this.isRegistered = true; + } + // We should check the DB for saved coords if(config.main.spawnOnJoin) {