"Updating position protection."

This commit is contained in:
samo_lego 2020-05-05 18:18:38 +02:00
parent f5d7de0b41
commit 1b0b72afa1
5 changed files with 87 additions and 34 deletions

View File

@ -112,6 +112,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Authenticates player and sends the message
public static void authenticatePlayer(ServerPlayerEntity player, Text msg) {
// Teleporting player back
if(config.main.spawnOnJoin)
teleportPlayer(player, false);
deauthenticatedUsers.remove(convertUuid(player));
@ -131,6 +132,7 @@ 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
if(config.main.spawnOnJoin)
teleportPlayer(player, true);
// Player is now not authenticated
@ -156,9 +158,9 @@ 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(server == null)
return;
if (toSpawn) {
// Teleports player to spawn
player.teleport(
@ -173,13 +175,13 @@ public class SimpleAuth implements DedicatedServerModInitializer {
}
PlayerCache cache = deauthenticatedUsers.get(convertUuid(player));
// Puts player to last cached position
player.setWorld(server.getWorld(DimensionType.byRawId(cache.lastDimId)));
player.setPos(
player.teleport(
server.getWorld(DimensionType.byRawId(cache.lastDimId)),
cache.lastX,
cache.lastY,
cache.lastZ
cache.lastZ,
0,
0
);
player.updateNeeded = true;
}
}
}

View File

@ -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

View File

@ -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
);
}
}
}

View File

@ -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());
}

View File

@ -9,6 +9,7 @@
"samo_lego"
],
"contact": {
"homepage": "samolego.github.io",
"sources": "https://github.com/samolego/SimpleAuth"
},