Fixing server crash #7

This commit is contained in:
samo_lego 2020-07-16 11:01:57 +02:00
parent 94aca23f97
commit 27d5f77d75
4 changed files with 25 additions and 29 deletions

View File

@ -2,9 +2,9 @@
org.gradle.jvmargs=-Xmx1G org.gradle.jvmargs=-Xmx1G
# Fabric properties # Fabric properties
minecraft_version=20w27a minecraft_version=1.16.1
yarn_mappings=20w27a+build.1 yarn_mappings=1.16.1+build.21
loader_version=0.8.9+build.203 loader_version=0.9.0+build.204
#Fabric api #Fabric api
fabric_version=0.14.1+build.372-1.16 fabric_version=0.14.1+build.372-1.16

View File

@ -151,7 +151,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
} }
// Getting not authenticated text // Getting not authenticated text
private static LiteralText notAuthenticated(ServerPlayerEntity player) { public static LiteralText notAuthenticated(PlayerEntity player) {
final PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); final PlayerCache cache = deauthenticatedUsers.get(convertUuid(player));
if(SimpleAuth.config.main.enableGlobalPassword || cache.isRegistered) if(SimpleAuth.config.main.enableGlobalPassword || cache.isRegistered)
return new LiteralText( return new LiteralText(

View File

@ -9,7 +9,6 @@ import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.server.PlayerManager; import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult; import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos; 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 * and cancel them if they aren't authenticated
*/ */
public class AuthEventHandler { 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 // Player pre-join
// Returns text as a reason for disconnect or null to pass // Returns text as a reason for disconnect or null to pass
@ -88,10 +81,7 @@ public class AuthEventHandler {
} }
// Invalidating session // Invalidating session
playerCache.wasAuthenticated = false; playerCache.wasAuthenticated = false;
if(playerCache.isRegistered) player.sendMessage(notAuthenticated(player), false);
player.sendMessage(new LiteralText(config.lang.loginRequired), false);
else
player.sendMessage(new LiteralText(config.lang.registerRequired), false);
} }
else { else {
deauthenticatePlayer(player); deauthenticatePlayer(player);
@ -152,7 +142,7 @@ public class AuthEventHandler {
!msg.startsWith("/register") && !msg.startsWith("/register") &&
(!config.experimental.allowChat || msg.startsWith("/")) (!config.experimental.allowChat || msg.startsWith("/"))
) { ) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
return ActionResult.PASS; return ActionResult.PASS;
@ -169,7 +159,7 @@ public class AuthEventHandler {
// Using a block (right-click function) // Using a block (right-click function)
public static ActionResult onUseBlock(PlayerEntity player) { public static ActionResult onUseBlock(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockUse) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockUse) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
return ActionResult.PASS; return ActionResult.PASS;
@ -178,7 +168,7 @@ public class AuthEventHandler {
// Punching a block // Punching a block
public static ActionResult onAttackBlock(PlayerEntity player) { public static ActionResult onAttackBlock(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
return ActionResult.PASS; return ActionResult.PASS;
@ -187,7 +177,7 @@ public class AuthEventHandler {
// Using an item // Using an item
public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) { public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemUse) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemUse) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return TypedActionResult.fail(ItemStack.EMPTY); return TypedActionResult.fail(ItemStack.EMPTY);
} }
@ -196,7 +186,7 @@ public class AuthEventHandler {
// Dropping an item // Dropping an item
public static ActionResult onDropItem(PlayerEntity player) { public static ActionResult onDropItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemDrop) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemDrop) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
return ActionResult.PASS; return ActionResult.PASS;
@ -204,7 +194,7 @@ public class AuthEventHandler {
// Changing inventory (item moving etc.) // Changing inventory (item moving etc.)
public static ActionResult onTakeItem(PlayerEntity player) { public static ActionResult onTakeItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemMoving) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemMoving) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -213,7 +203,7 @@ public class AuthEventHandler {
// Attacking an entity // Attacking an entity
public static ActionResult onAttackEntity(PlayerEntity player) { public static ActionResult onAttackEntity(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowEntityPunch) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowEntityPunch) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -222,7 +212,7 @@ public class AuthEventHandler {
// Interacting with entity // Interacting with entity
public static ActionResult onUseEntity(PlayerEntity player) { public static ActionResult onUseEntity(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.main.allowEntityInteract) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.main.allowEntityInteract) {
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;
} }

View File

@ -1,9 +1,6 @@
package org.samo_lego.simpleauth.storage; package org.samo_lego.simpleauth.storage;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import static org.samo_lego.simpleauth.SimpleAuth.DB; import static org.samo_lego.simpleauth.SimpleAuth.DB;
@ -49,8 +46,17 @@ public class PlayerCache {
// Getting (hashed) password // Getting (hashed) password
JsonObject json = gson.fromJson(data, JsonObject.class); JsonObject json = gson.fromJson(data, JsonObject.class);
this.password = json.get("password").getAsString(); JsonElement passwordElement = json.get("password");
this.isRegistered = true; 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 // We should check the DB for saved coords
if(config.main.spawnOnJoin) { if(config.main.spawnOnJoin) {