@@ -0,0 +1,116 @@
|
||||
package com.fossy.oauth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
|
||||
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.inventory.PlayerEnderChestContainer;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
final class InventoryQuarantine {
|
||||
private static final Codec<List<ItemStack>> STACK_LIST_CODEC = ItemStack.OPTIONAL_CODEC.listOf();
|
||||
private static final Codec<Snapshot> SNAPSHOT_CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
STACK_LIST_CODEC.fieldOf("inventory").forGetter(Snapshot::inventory),
|
||||
STACK_LIST_CODEC.fieldOf("ender_chest").forGetter(Snapshot::enderChest),
|
||||
ItemStack.OPTIONAL_CODEC.fieldOf("carried").forGetter(Snapshot::carried)
|
||||
).apply(instance, Snapshot::new));
|
||||
private static final AttachmentType<Snapshot> SNAPSHOT = AttachmentRegistry.create(
|
||||
Identifier.fromNamespaceAndPath("oauth", "quarantined_inventory"),
|
||||
builder -> builder.persistent(SNAPSHOT_CODEC).copyOnDeath());
|
||||
private static final Codec<ReturnLocation> RETURN_LOCATION_CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Level.RESOURCE_KEY_CODEC.fieldOf("dimension").forGetter(ReturnLocation::dimension),
|
||||
Codec.DOUBLE.fieldOf("x").forGetter(location -> location.position.x),
|
||||
Codec.DOUBLE.fieldOf("y").forGetter(location -> location.position.y),
|
||||
Codec.DOUBLE.fieldOf("z").forGetter(location -> location.position.z),
|
||||
Codec.FLOAT.fieldOf("yaw").forGetter(ReturnLocation::yaw),
|
||||
Codec.FLOAT.fieldOf("pitch").forGetter(ReturnLocation::pitch)
|
||||
).apply(instance, (dimension, x, y, z, yaw, pitch) ->
|
||||
new ReturnLocation(dimension, new Vec3(x, y, z), yaw, pitch)));
|
||||
private static final AttachmentType<ReturnLocation> RETURN_LOCATION = AttachmentRegistry.create(
|
||||
Identifier.fromNamespaceAndPath("oauth", "return_location"),
|
||||
builder -> builder.persistent(RETURN_LOCATION_CODEC).copyOnDeath());
|
||||
|
||||
private InventoryQuarantine() {}
|
||||
|
||||
static void quarantine(ServerPlayer player, MinecraftServer server) {
|
||||
if (!player.hasAttached(SNAPSHOT)) {
|
||||
player.setAttached(SNAPSHOT, capture(player));
|
||||
}
|
||||
clear(player);
|
||||
server.getPlayerList().saveAll();
|
||||
}
|
||||
|
||||
static ReturnLocation getOrCaptureReturnLocation(ServerPlayer player, MinecraftServer server) {
|
||||
ReturnLocation saved = player.getAttached(RETURN_LOCATION);
|
||||
if (saved != null && server.getLevel(saved.dimension) != null) return saved;
|
||||
|
||||
ReturnLocation captured = new ReturnLocation(player.level().dimension(), player.position(),
|
||||
player.getYRot(), player.getXRot());
|
||||
player.setAttached(RETURN_LOCATION, captured);
|
||||
server.getPlayerList().saveAll();
|
||||
return captured;
|
||||
}
|
||||
|
||||
static void clearReturnLocation(ServerPlayer player) {
|
||||
player.removeAttached(RETURN_LOCATION);
|
||||
}
|
||||
|
||||
static void restore(ServerPlayer player) {
|
||||
Snapshot snapshot = player.removeAttached(SNAPSHOT);
|
||||
if (snapshot == null) return;
|
||||
|
||||
Inventory inventory = player.getInventory();
|
||||
inventory.clearContent();
|
||||
for (int i = 0; i < Math.min(inventory.getContainerSize(), snapshot.inventory.size()); i++) {
|
||||
inventory.setItem(i, snapshot.inventory.get(i).copy());
|
||||
}
|
||||
|
||||
PlayerEnderChestContainer enderChest = player.getEnderChestInventory();
|
||||
enderChest.clearContent();
|
||||
for (int i = 0; i < Math.min(enderChest.getContainerSize(), snapshot.enderChest.size()); i++) {
|
||||
enderChest.setItem(i, snapshot.enderChest.get(i).copy());
|
||||
}
|
||||
|
||||
player.containerMenu.setCarried(snapshot.carried.copy());
|
||||
player.inventoryMenu.setCarried(snapshot.carried.copy());
|
||||
inventory.setChanged();
|
||||
enderChest.setChanged();
|
||||
}
|
||||
|
||||
private static Snapshot capture(ServerPlayer player) {
|
||||
Inventory inventory = player.getInventory();
|
||||
List<ItemStack> inventoryItems = new ArrayList<>(inventory.getContainerSize());
|
||||
for (int i = 0; i < inventory.getContainerSize(); i++) {
|
||||
inventoryItems.add(inventory.getItem(i).copy());
|
||||
}
|
||||
|
||||
PlayerEnderChestContainer enderChest = player.getEnderChestInventory();
|
||||
List<ItemStack> enderChestItems = new ArrayList<>(enderChest.getContainerSize());
|
||||
for (int i = 0; i < enderChest.getContainerSize(); i++) {
|
||||
enderChestItems.add(enderChest.getItem(i).copy());
|
||||
}
|
||||
|
||||
return new Snapshot(inventoryItems, enderChestItems, player.containerMenu.getCarried().copy());
|
||||
}
|
||||
|
||||
private static void clear(ServerPlayer player) {
|
||||
player.getInventory().clearContent();
|
||||
player.getEnderChestInventory().clearContent();
|
||||
player.containerMenu.setCarried(ItemStack.EMPTY);
|
||||
player.inventoryMenu.setCarried(ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
private record Snapshot(List<ItemStack> inventory, List<ItemStack> enderChest, ItemStack carried) {}
|
||||
|
||||
record ReturnLocation(ResourceKey<Level> dimension, Vec3 position, float yaw, float pitch) {}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
|
||||
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
|
||||
@@ -34,6 +35,7 @@ public final class LoginGate {
|
||||
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||
ServerPlayer player = handler.player;
|
||||
InventoryQuarantine.quarantine(player, server);
|
||||
// Let vanilla finish registering the player with chunk tracking before teleporting.
|
||||
PENDING_LOGIN_GATE.put(player.getUUID(), false);
|
||||
});
|
||||
@@ -80,15 +82,43 @@ public final class LoginGate {
|
||||
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
|
||||
{
|
||||
dispatcher.register(Commands.literal("login").executes(context -> {
|
||||
ServerPlayer player = context.getSource().getPlayerOrException();
|
||||
if (!isLocked(player)) {
|
||||
player.sendSystemMessage(Component.literal("You are already logged in."));
|
||||
return 0;
|
||||
}
|
||||
AuthServiceClient.requestLogin(player);
|
||||
return 1;
|
||||
}));
|
||||
dispatcher.register(Commands.literal("login")
|
||||
.executes(context -> {
|
||||
ServerPlayer player = context.getSource().getPlayerOrException();
|
||||
if (!isLocked(player)) {
|
||||
player.sendSystemMessage(Component.literal("You are already logged in."));
|
||||
return 0;
|
||||
}
|
||||
AuthServiceClient.requestLogin(player);
|
||||
return 1;
|
||||
})
|
||||
.then(Commands.literal("accept")
|
||||
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER))
|
||||
.then(Commands.argument("username", StringArgumentType.word()).executes(context -> {
|
||||
var source = context.getSource();
|
||||
if (source.getEntity() instanceof ServerPlayer operator && isUnauthenticated(operator)) {
|
||||
source.sendFailure(Component.literal("You must authenticate before using this command."));
|
||||
return 0;
|
||||
}
|
||||
String username = StringArgumentType.getString(context, "username");
|
||||
ServerPlayer target = source.getServer().getPlayerList().getPlayerByName(username);
|
||||
if (target == null) {
|
||||
source.sendFailure(Component.literal("No online player named " + username + "."));
|
||||
return 0;
|
||||
}
|
||||
if (!LOCKED_PLAYERS.containsKey(target.getUUID())) {
|
||||
source.sendFailure(Component.literal(target.getGameProfile().name() + " is not waiting to log in."));
|
||||
return 0;
|
||||
}
|
||||
AuthServiceClient.cancelPlayerLogin(target.getUUID());
|
||||
if (!approve(target)) {
|
||||
source.sendFailure(Component.literal("Could not authenticate " + target.getGameProfile().name() + "."));
|
||||
return 0;
|
||||
}
|
||||
target.sendSystemMessage(Component.literal("An operator authenticated you."));
|
||||
source.sendSuccess(() -> Component.literal("Authenticated " + target.getGameProfile().name() + "."), true);
|
||||
return 1;
|
||||
}))));
|
||||
dispatcher.register(Commands.literal("unlink").executes(context -> {
|
||||
ServerPlayer player = context.getSource().getPlayerOrException();
|
||||
if (isLocked(player)) {
|
||||
@@ -112,35 +142,49 @@ public final class LoginGate {
|
||||
}
|
||||
|
||||
private static void startLoginGate(ServerPlayer player, net.minecraft.server.MinecraftServer server) {
|
||||
ServerLevel returnLevel = player.level();
|
||||
Vec3 returnPosition = player.position();
|
||||
float returnYaw = player.getYRot();
|
||||
float returnPitch = player.getXRot();
|
||||
InventoryQuarantine.ReturnLocation returnLocation =
|
||||
InventoryQuarantine.getOrCaptureReturnLocation(player, server);
|
||||
ServerLevel returnLevel = server.getLevel(returnLocation.dimension());
|
||||
Vec3 returnPosition = returnLocation.position();
|
||||
float returnYaw = returnLocation.yaw();
|
||||
float returnPitch = returnLocation.pitch();
|
||||
AuthServiceConfig config = AuthServiceConfig.load();
|
||||
ServerLevel loginSpawnLevel = config.getLoginSpawnLevel(server);
|
||||
Vec3 loginSpawnPosition = config.getLoginSpawnPosition(server);
|
||||
float loginSpawnYaw = config.loginSpawnSet ? config.loginSpawnYaw : 0.0f;
|
||||
float loginSpawnPitch = config.loginSpawnSet ? config.loginSpawnPitch : 0.0f;
|
||||
boolean wasInvisible = player.isInvisible();
|
||||
LOCKED_PLAYERS.put(player.getUUID(), new LockedPlayer(returnLevel, returnPosition, returnYaw, returnPitch,
|
||||
loginSpawnLevel, loginSpawnPosition, loginSpawnYaw, loginSpawnPitch));
|
||||
loginSpawnLevel, loginSpawnPosition, loginSpawnYaw, loginSpawnPitch, wasInvisible));
|
||||
player.setInvisible(true);
|
||||
player.teleport(new TeleportTransition(loginSpawnLevel, loginSpawnPosition, Vec3.ZERO,
|
||||
loginSpawnYaw, loginSpawnPitch, TeleportTransition.DO_NOTHING));
|
||||
player.setDeltaMovement(Vec3.ZERO);
|
||||
player.sendSystemMessage(Component.literal("You must run /login and complete login to play."));
|
||||
}
|
||||
|
||||
static void approve(ServerPlayer player) {
|
||||
static boolean approve(ServerPlayer player) {
|
||||
LockedPlayer locked = LOCKED_PLAYERS.remove(player.getUUID());
|
||||
if (locked == null) return;
|
||||
if (locked == null) return false;
|
||||
net.minecraft.server.MinecraftServer server = player.level().getServer();
|
||||
player.teleport(new TeleportTransition(locked.returnLevel, locked.returnPosition, Vec3.ZERO,
|
||||
locked.returnYaw, locked.returnPitch, TeleportTransition.DO_NOTHING));
|
||||
player.setDeltaMovement(Vec3.ZERO);
|
||||
player.setInvisible(locked.wasInvisible);
|
||||
InventoryQuarantine.restore(player);
|
||||
InventoryQuarantine.clearReturnLocation(player);
|
||||
server.getPlayerList().saveAll();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isLocked(net.minecraft.world.entity.player.Player player) {
|
||||
return player instanceof ServerPlayer serverPlayer && LOCKED_PLAYERS.containsKey(serverPlayer.getUUID());
|
||||
}
|
||||
|
||||
public static boolean isUnauthenticated(ServerPlayer player) {
|
||||
return LOCKED_PLAYERS.containsKey(player.getUUID()) || PENDING_LOGIN_GATE.containsKey(player.getUUID());
|
||||
}
|
||||
|
||||
public static boolean allowCommand(ServerPlayer player, String command) {
|
||||
if (!isLocked(player)) return true;
|
||||
|
||||
@@ -153,5 +197,5 @@ public final class LoginGate {
|
||||
|
||||
private record LockedPlayer(ServerLevel returnLevel, Vec3 returnPosition, float returnYaw, float returnPitch,
|
||||
ServerLevel loginSpawnLevel, Vec3 loginSpawnPosition, float loginSpawnYaw,
|
||||
float loginSpawnPitch) { }
|
||||
float loginSpawnPitch, boolean wasInvisible) { }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.fossy.oauth.mixin;
|
||||
|
||||
import com.fossy.oauth.LoginGate;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.util.Prediction;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(ServerPlayer.class)
|
||||
abstract class ServerPlayerMixin {
|
||||
@Inject(method = "drop", at = @At("HEAD"), cancellable = true)
|
||||
private void oauth$preventUnauthenticatedItemDrop(ItemStack stack, boolean throwRandomly, Prediction prediction,
|
||||
CallbackInfoReturnable<ItemEntity> cir) {
|
||||
if (LoginGate.isUnauthenticated((ServerPlayer) (Object) this)) {
|
||||
cir.setReturnValue(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@
|
||||
"compatibilityLevel": "JAVA_25",
|
||||
"mixins": [
|
||||
"ExampleMixin",
|
||||
"ServerGamePacketListenerMixin"
|
||||
"ServerGamePacketListenerMixin",
|
||||
"ServerPlayerMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
||||
Reference in New Issue
Block a user