diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index f1198b5..11947bb 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -112,7 +112,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Authenticates player and sends the message public static void authenticatePlayer(ServerPlayerEntity player, Text msg) { // Teleporting player back - teleportPlayer(player, false); + if(config.main.spawnOnJoin) + teleportPlayer(player, false); deauthenticatedUsers.remove(convertUuid(player)); @@ -131,7 +132,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { String uuid = convertUuid(player); SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, player)); // Teleporting player to spawn to hide its position - teleportPlayer(player, true); + if(config.main.spawnOnJoin) + teleportPlayer(player, true); // Player is now not authenticated player.sendMessage(notAuthenticated(), false); @@ -156,30 +158,30 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Teleports player to spawn or last location when authenticating public static void teleportPlayer(ServerPlayerEntity player, boolean toSpawn) { - if(config.main.spawnOnJoin) { - MinecraftServer server = player.getServer(); - assert server != null; - if (toSpawn) { - // Teleports player to spawn - player.teleport( - server.getWorld(DimensionType.byRawId(config.worldSpawn.dimensionId)), - config.worldSpawn.x, - config.worldSpawn.y, - config.worldSpawn.z, - 0, - 0 - ); - return; - } - PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); - // Puts player to last cached position - player.setWorld(server.getWorld(DimensionType.byRawId(cache.lastDimId))); - player.setPos( - cache.lastX, - cache.lastY, - cache.lastZ + MinecraftServer server = player.getServer(); + if(server == null) + return; + if (toSpawn) { + // Teleports player to spawn + player.teleport( + server.getWorld(DimensionType.byRawId(config.worldSpawn.dimensionId)), + config.worldSpawn.x, + config.worldSpawn.y, + config.worldSpawn.z, + 0, + 0 ); - player.updateNeeded = true; + return; } + PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); + // Puts player to last cached position + player.teleport( + server.getWorld(DimensionType.byRawId(cache.lastDimId)), + cache.lastX, + cache.lastY, + cache.lastZ, + 0, + 0 + ); } } \ No newline at end of file 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 5db809d..b2d346f 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -163,13 +163,7 @@ public class AuthEventHandler { } public static void onPlayerLeave(ServerPlayerEntity player) { - if(isPlayerFake(player)) - return; - - // Teleporting player back - teleportPlayer(player, false); - - if(!isAuthenticated(player) || config.main.sessionTimeoutTime == -1) + if(isPlayerFake(player) || !isAuthenticated(player) || config.main.sessionTimeoutTime == -1) return; // Starting session 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 9426cec..622deb3 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java @@ -3,12 +3,17 @@ package org.samo_lego.simpleauth.mixin; import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket; import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; import net.minecraft.util.ActionResult; +import net.minecraft.world.dimension.DimensionType; import org.samo_lego.simpleauth.event.entity.player.ChatCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.samo_lego.simpleauth.event.item.TakeItemCallback; +import org.samo_lego.simpleauth.storage.PlayerCache; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -16,12 +21,19 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import static net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action.SWAP_HELD_ITEMS; +import static org.samo_lego.simpleauth.SimpleAuth.config; +import static org.samo_lego.simpleauth.SimpleAuth.deauthenticatedUsers; +import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; @Mixin(ServerPlayNetworkHandler.class) public abstract class MixinServerPlayNetworkHandler { @Shadow public ServerPlayerEntity player; + @Final + @Shadow + private MinecraftServer server; + @Inject( method = "onGameMessage(Lnet/minecraft/network/packet/c2s/play/ChatMessageC2SPacket;)V", at = @At( @@ -74,4 +86,48 @@ public abstract class MixinServerPlayNetworkHandler { ci.cancel(); } } + + @Inject( + method="disconnect(Lnet/minecraft/text/Text;)V", + at = @At(value = "HEAD"), + cancellable = true + ) + // If player is disconnected because of sth (e.g. wrong password) + // its position is set back to previous (e.g. not spawn) + private void disconnect(Text reason, CallbackInfo ci) { + if(config.main.spawnOnJoin) { + PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); + // Puts player to last cached position + this.player.teleport( + this.server.getWorld(DimensionType.byRawId(cache.lastDimId)), + cache.lastX, + cache.lastY, + cache.lastZ, + 0, + 0 + ); + } + } + + @Inject( + method="onDisconnected(Lnet/minecraft/text/Text;)V", + at = @At(value = "HEAD"), + cancellable = true + ) + // If player is disconnected because of sth (e.g. wrong password) + // its position is set back to previous (e.g. not spawn) + private void onDisconnected(Text reason, CallbackInfo ci) { + if(config.main.spawnOnJoin) { + PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); + // Puts player to last cached position + this.player.teleport( + this.server.getWorld(DimensionType.byRawId(cache.lastDimId)), + cache.lastX, + cache.lastY, + cache.lastZ, + 0, + 0 + ); + } + } } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java index 3845088..622b32e 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java @@ -79,9 +79,9 @@ public class SimpleAuthDatabase { } // Updates the password of the user - public void updateUserData(String uuid, String password) { + public void updateUserData(String uuid, String data) { try { - levelDBStore.put(bytes("UUID:" + uuid),bytes("data:" + password)); + levelDBStore.put(bytes("UUID:" + uuid),bytes("data:" + data)); } catch (Error e) { LOGGER.error("[SimpleAuth] " + e.getMessage()); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 00a1852..db080cf 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -9,6 +9,7 @@ "samo_lego" ], "contact": { + "homepage": "samolego.github.io", "sources": "https://github.com/samolego/SimpleAuth" },