Added player sessions.
This commit is contained in:
parent
e884f37b11
commit
367aa29646
|
@ -39,8 +39,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
|
|||
|
||||
// Boolean for easier checking if player is authenticated
|
||||
public static boolean isAuthenticated(ServerPlayerEntity player) {
|
||||
String uuid = player.getUuidAsString();
|
||||
return !deauthenticatedUsers.containsKey(uuid);
|
||||
return !deauthenticatedUsers.containsKey(player.getUuidAsString());
|
||||
}
|
||||
|
||||
// Getting game directory
|
||||
|
|
|
@ -14,6 +14,9 @@ import net.minecraft.util.math.BlockPos;
|
|||
import net.minecraft.world.World;
|
||||
import org.samo_lego.simpleauth.SimpleAuth;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static net.minecraft.block.NetherPortalBlock.AXIS;
|
||||
import static net.minecraft.util.math.Direction.Axis.Z;
|
||||
|
||||
|
@ -33,7 +36,18 @@ public class AuthEventHandler {
|
|||
|
||||
// Player joining the server
|
||||
public static void onPlayerJoin(ServerPlayerEntity player) {
|
||||
SimpleAuth.deauthenticatePlayer(player);
|
||||
// Checking if session is still valid
|
||||
String uuid = player.getUuidAsString();
|
||||
if(
|
||||
SimpleAuth.deauthenticatedUsers.containsKey(uuid) &&
|
||||
SimpleAuth.deauthenticatedUsers.get(uuid).lastIp.equals(player.getIp()) &&
|
||||
SimpleAuth.deauthenticatedUsers.get(uuid).wasAuthenticated
|
||||
) {
|
||||
SimpleAuth.deauthenticatedUsers.remove(uuid); // Makes player authenticated
|
||||
return;
|
||||
}
|
||||
else
|
||||
SimpleAuth.deauthenticatePlayer(player);
|
||||
|
||||
// Tries to rescue player from nether portal
|
||||
if(SimpleAuth.config.main.tryPortalRescue && player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL)) {
|
||||
|
@ -105,6 +119,27 @@ public class AuthEventHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public static void onPlayerLeave(ServerPlayerEntity player) {
|
||||
if(!SimpleAuth.isAuthenticated(player) || SimpleAuth.config.main.sessionTimeoutTime == -1)
|
||||
return;
|
||||
|
||||
// Starting session
|
||||
// Putting player to deauthenticated player map
|
||||
SimpleAuth.deauthenticatePlayer(player);
|
||||
|
||||
// Setting that player was actually authenticated before leaving
|
||||
SimpleAuth.deauthenticatedUsers.get(player.getUuidAsString()).wasAuthenticated = true;
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(player.removed) // Disproving session
|
||||
SimpleAuth.deauthenticatedUsers.remove(player.getUuidAsString());
|
||||
}
|
||||
}, SimpleAuth.config.main.sessionTimeoutTime * 1000);
|
||||
}
|
||||
|
||||
// Player chatting
|
||||
public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) {
|
||||
// Getting the message to then be able to check it
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
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.server.network.ServerPlayerEntity;
|
||||
|
||||
public interface PlayerLeaveServerCallback {
|
||||
|
||||
Event<PlayerLeaveServerCallback> EVENT = EventFactory.createArrayBacked(PlayerLeaveServerCallback.class, listeners -> (player) -> {
|
||||
for (PlayerLeaveServerCallback callback : listeners) {
|
||||
callback.onPlayerLeave(player);
|
||||
}
|
||||
});
|
||||
void onPlayerLeave(ServerPlayerEntity player);
|
||||
}
|
|
@ -4,6 +4,7 @@ import net.minecraft.network.ClientConnection;
|
|||
import net.minecraft.server.PlayerManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback;
|
||||
import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
|
@ -12,8 +13,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|||
@Mixin(PlayerManager.class)
|
||||
public abstract class MixinPlayerManager {
|
||||
|
||||
@Inject(method = "onPlayerConnect", at = @At("RETURN"))
|
||||
@Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At("RETURN"))
|
||||
private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEntity serverPlayerEntity, CallbackInfo ci) {
|
||||
PlayerJoinServerCallback.EVENT.invoker().onPlayerJoin(serverPlayerEntity);
|
||||
}
|
||||
|
||||
@Inject(method = "remove(Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At("RETURN"))
|
||||
private void onPlayerLeave(ServerPlayerEntity serverPlayerEntity, CallbackInfo ci) {
|
||||
PlayerLeaveServerCallback.EVENT.invoker().onPlayerLeave(serverPlayerEntity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,6 +69,9 @@ public class AuthConfig {
|
|||
// Minimum and maximum length of password. Set -1 to disable max chars
|
||||
public int minPasswordChars = 4;
|
||||
public int maxPasswordChars = -1;
|
||||
// How long to keep session (auto-logging in the player), in seconds
|
||||
// Set to -1 to disable
|
||||
public int sessionTimeoutTime = 300;
|
||||
}
|
||||
public static class LangConfig {
|
||||
public String enterPassword = "§6You need to enter your password!";
|
||||
|
|
Loading…
Reference in New Issue