Adding `forceOfflineUuids` option, some minor fixes

This commit is contained in:
samo_lego 2020-10-31 18:28:17 +01:00
parent 0e369bf99f
commit b0fe67669c
8 changed files with 28 additions and 19 deletions

View File

@ -36,7 +36,7 @@ import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class SimpleAuth implements DedicatedServerModInitializer {
public static SimpleAuthDatabase DB = new SimpleAuthDatabase();
public static final SimpleAuthDatabase DB = new SimpleAuthDatabase();
public static final ExecutorService THREADPOOL = Executors.newCachedThreadPool();
@ -45,19 +45,19 @@ public class SimpleAuth implements DedicatedServerModInitializer {
* It's cleared on server stop in order to save some interactions with database during runtime.
* Stores their data as {@link org.samo_lego.simpleauth.storage.PlayerCache PlayerCache} object.
*/
public static HashMap<String, PlayerCache> playerCacheMap = new HashMap<>();
public static final HashMap<String, PlayerCache> playerCacheMap = new HashMap<>();
/**
* HashSet of player names that have Mojang accounts.
* If player is saved in here, they will be treated as online-mode ones.
*/
public static HashSet<String> mojangAccountNamesCache = new HashSet<>();
public static final HashSet<String> mojangAccountNamesCache = new HashSet<>();
// Getting game directory
public static final Path gameDirectory = FabricLoader.getInstance().getGameDir();
// Server properties
public static Properties serverProp = new Properties();
public static final Properties serverProp = new Properties();
/**
* Config of the SimpleAuth mod.

View File

@ -23,7 +23,7 @@ public class LogoutCommand {
private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException {
ServerPlayerEntity player = serverCommandSource.getPlayer();
if(mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase())) {
if(!mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase())) {
((PlayerAuth) player).setAuthenticated(false);
player.sendMessage(new LiteralText(config.lang.successfulLogout), false);
}

View File

@ -30,7 +30,6 @@ public class MixinPlayerAdvancementTracker {
@Inject(method = "load(Lnet/minecraft/server/ServerAdvancementLoader;)V", at = @At("HEAD"))
private void startMigratingOfflineAdvancements(ServerAdvancementLoader advancementLoader, CallbackInfo ci) {
System.out.println(this.advancementFile.isFile());
if(config.experimental.premiumAutologin && ((PlayerAuth) this.owner).isUsingMojangAccount() && !this.advancementFile.isFile()) {
// Migrate
String playername = owner.getGameProfile().getName().toLowerCase();

View File

@ -21,13 +21,15 @@ import static org.samo_lego.simpleauth.SimpleAuth.*;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
@Mixin(ServerLoginNetworkHandler.class)
public class MixinServerLoginNetworkHandler {
public abstract class MixinServerLoginNetworkHandler {
@Shadow
private GameProfile profile;
@Shadow
private int loginTicks;
@Shadow protected abstract GameProfile toOfflineProfile(GameProfile profile);
/**
* Fake state of current player.
*/
@ -49,6 +51,13 @@ public class MixinServerLoginNetworkHandler {
}
}
@Inject(method = "acceptPlayer()V", at = @At("HEAD"))
private void acceptPlayer(CallbackInfo ci) {
if(config.experimental.forceoOfflineUuids) {
this.profile = this.toOfflineProfile(this.profile);
}
}
/**
* Checks whether the player has purchased an account.
* If so, server is presented as online, and continues as in normal-online mode.

View File

@ -25,6 +25,7 @@ import java.nio.charset.StandardCharsets;
import static org.samo_lego.simpleauth.SimpleAuth.serverProp;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class AuthConfig {
private static final Gson gson = new GsonBuilder()
@ -230,9 +231,15 @@ public class AuthConfig {
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)
)) {
config = gson.fromJson(fileReader, AuthConfig.class);
if(config.experimental.premiumAutologin && !Boolean.parseBoolean(serverProp.getProperty("online-mode"))) {
logError("You cannot use server in offline mode and premiumAutologin! Disabling the latter.");
config.experimental.premiumAutologin = false;
if(!Boolean.parseBoolean(serverProp.getProperty("online-mode"))) {
if(config.experimental.forceoOfflineUuids) {
logInfo("Server is in offline mode, forceoOfflineUuids option is irrelevant. Setting it to false.");
config.experimental.forceoOfflineUuids = false;
}
if(config.experimental.premiumAutologin) {
logError("You cannot use server in offline mode and premiumAutologin! Disabling the latter.");
config.experimental.premiumAutologin = false;
}
}
} catch (IOException e) {
throw new RuntimeException("[SimpleAuth] Problem occurred when trying to load config: ", e);

View File

@ -60,7 +60,7 @@ public class PlayerCache {
public float pitch;
}
public PlayerCache.LastLocation lastLocation = new PlayerCache.LastLocation();
public final PlayerCache.LastLocation lastLocation = new PlayerCache.LastLocation();
private static final Gson gson = new Gson();

View File

@ -64,6 +64,7 @@ public class SimpleAuthDatabase {
* @param data data to put inside database
* @return true if operation was successful, otherwise false
*/
@Deprecated
public boolean registerUser(String uuid, String data) {
try {
if(!this.isUserRegistered(uuid)) {
@ -111,6 +112,7 @@ public class SimpleAuthDatabase {
* @param uuid uuid of the player to update data for
* @param data data to put inside database
*/
@Deprecated
public void updateUserData(String uuid, String data) {
try {
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data));

View File

@ -1,10 +1,5 @@
package org.samo_lego.simpleauth.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.hashing.HasherArgon2;
import org.samo_lego.simpleauth.utils.hashing.HasherBCrypt;
@ -12,9 +7,6 @@ import static org.samo_lego.simpleauth.SimpleAuth.config;
import static org.samo_lego.simpleauth.SimpleAuth.playerCacheMap;
public class AuthHelper {
// Json parser
private static final JsonParser parser = new JsonParser();
/**
* Checks password of user
*