Done updating DIM stuff :)

This commit is contained in:
samo_lego 2020-05-28 18:45:15 +02:00
parent 4223536e49
commit 7dfe1ff5b3
7 changed files with 43 additions and 42 deletions

View File

@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
# Fabric properties # Fabric properties
minecraft_version=20w21a minecraft_version=20w21a
yarn_mappings=20w21a+build.16 yarn_mappings=20w21a+build.19
loader_version=0.8.4+build.198 loader_version=0.8.4+build.198
#Fabric api #Fabric api

View File

@ -11,10 +11,11 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.iq80.leveldb.WriteBatch; import org.iq80.leveldb.WriteBatch;
@ -117,14 +118,14 @@ public class SimpleAuth implements DedicatedServerModInitializer {
private static void onStopServer() { private static void onStopServer() {
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth."); LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
WriteBatch batch = db.levelDBStore.createWriteBatch(); WriteBatch batch = db.getLevelDBStore().createWriteBatch();
// Writing coords of de-authenticated players to database // Writing coords of de-authenticated players to database
deauthenticatedUsers.forEach((uuid, playerCache) -> { deauthenticatedUsers.forEach((uuid, playerCache) -> {
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
data.addProperty("password", playerCache.password); data.addProperty("password", playerCache.password);
JsonObject lastLocation = new JsonObject(); JsonObject lastLocation = new JsonObject();
lastLocation.addProperty("dim", playerCache.lastDim.toString()); lastLocation.addProperty("dim", playerCache.lastDim);
lastLocation.addProperty("x", playerCache.lastX); lastLocation.addProperty("x", playerCache.lastX);
lastLocation.addProperty("y", playerCache.lastY); lastLocation.addProperty("y", playerCache.lastY);
lastLocation.addProperty("z", playerCache.lastZ); lastLocation.addProperty("z", playerCache.lastZ);
@ -135,7 +136,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
}); });
try { try {
// Writing and closing batch // Writing and closing batch
db.levelDBStore.write(batch); db.getLevelDBStore().write(batch);
batch.close(); batch.close();
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("[SimpleAuth] Error saving player data! " + e.getMessage()); LOGGER.error("[SimpleAuth] Error saving player data! " + e.getMessage());
@ -215,25 +216,25 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Teleports player to spawn or last location when authenticating // Teleports player to spawn or last location when authenticating
public static void teleportPlayer(ServerPlayerEntity player, boolean toSpawn) { public static void teleportPlayer(ServerPlayerEntity player, boolean toSpawn) {
MinecraftServer server = player.getServer(); MinecraftServer server = player.getServer();
if(server == null) if(server == null || config.worldSpawn.dimension == null)
return; return;
Registry<DimensionType> registry = server.method_29174().getRegistry(); // registry for dimensions
if (toSpawn) { if (toSpawn) {
// Teleports player to spawn // Teleports player to spawn
player.teleport( player.teleport(
server.getWorld(registry.getKey(config.worldSpawn.dimension)), server.getWorld(RegistryKey.of(Registry.DIMENSION_TYPE_KEY, new Identifier(config.worldSpawn.dimension))),
config.worldSpawn.x, config.worldSpawn.x,
config.worldSpawn.y, config.worldSpawn.y,
config.worldSpawn.z, config.worldSpawn.z,
0, 90,
0 90
); );
return; return;
} }
PlayerCache cache = deauthenticatedUsers.get(convertUuid(player)); PlayerCache cache = deauthenticatedUsers.get(convertUuid(player));
// Puts player to last cached position // Puts player to last cached position
player.teleport( player.teleport(
server.getWorld(registry.getKey(cache.lastDim)), server.getWorld(RegistryKey.of(Registry.DIMENSION_TYPE_KEY, new Identifier(cache.lastDim))),
cache.lastX, cache.lastX,
cache.lastY, cache.lastY,
cache.lastZ, cache.lastZ,

View File

@ -19,6 +19,7 @@ import org.samo_lego.simpleauth.storage.PlayerCache;
import org.samo_lego.simpleauth.utils.AuthHelper; import org.samo_lego.simpleauth.utils.AuthHelper;
import java.io.File; import java.io.File;
import java.util.Objects;
import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word; import static com.mojang.brigadier.arguments.StringArgumentType.word;
@ -58,10 +59,9 @@ public class AuthCommand {
.executes(ctx -> { .executes(ctx -> {
RegistryKey<DimensionType> registryKey = DimensionArgumentType.getDimensionArgument(ctx, "dimension"); RegistryKey<DimensionType> registryKey = DimensionArgumentType.getDimensionArgument(ctx, "dimension");
Registry<DimensionType> registry = ctx.getSource().getWorld().getServer().method_29174().getRegistry(); Registry<DimensionType> registry = ctx.getSource().getWorld().getServer().method_29174().getRegistry();
//RegistryKey<DimensionType> registryKey = ctx.getSource().getEntityOrThrow().getEntityWorld().method_27983();
return setSpawn( return setSpawn(
ctx.getSource(), ctx.getSource(),
registry.get(registryKey), Objects.requireNonNull(registry.get(registryKey)),
//(ctx, "dimension id"), //(ctx, "dimension id"),
BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(), BlockPosArgumentType.getLoadedBlockPos(ctx, "position").getX(),
// +1 to not spawn player in ground // +1 to not spawn player in ground
@ -136,10 +136,17 @@ public class AuthCommand {
// //
private static int setSpawn(ServerCommandSource source, DimensionType dimension, double x, double y, double z) { private static int setSpawn(ServerCommandSource source, DimensionType dimension, double x, double y, double z) {
config.worldSpawn.dimension = dimension; // Getting dimension regitry
Registry<DimensionType> registry = source.getWorld().getServer().method_29174().getRegistry();
// Setting config values and saving
config.worldSpawn.dimension = String.valueOf(registry.getId(dimension));
config.worldSpawn.x = x; config.worldSpawn.x = x;
config.worldSpawn.y = y; config.worldSpawn.y = y;
config.worldSpawn.z = z; config.worldSpawn.z = z;
config.save(new File("./mods/SimpleAuth/config.json"));
// Getting sender
Entity sender = source.getEntity(); Entity sender = source.getEntity();
if(sender != null) if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.worldSpawnSet), false); ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.worldSpawnSet), false);

View File

@ -1,7 +1,6 @@
package org.samo_lego.simpleauth.event; package org.samo_lego.simpleauth.event;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -111,14 +110,15 @@ public class AuthEventHandler {
// Faking portal blocks to be air // Faking portal blocks to be air
BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket(); BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket();
((BlockUpdateS2CPacketAccessor) feetPacket).setState(new BlockState(Blocks.AIR, null, null)); ((BlockUpdateS2CPacketAccessor) feetPacket).setState(Blocks.AIR.getDefaultState());
((BlockUpdateS2CPacketAccessor) feetPacket).setBlockPos(pos); ((BlockUpdateS2CPacketAccessor) feetPacket).setBlockPos(pos);
player.networkHandler.sendPacket(feetPacket); player.networkHandler.sendPacket(feetPacket);
BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket(); BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket();
((BlockUpdateS2CPacketAccessor) headPacket).setState(new BlockState(Blocks.AIR, null, null)); ((BlockUpdateS2CPacketAccessor) headPacket).setState(Blocks.AIR.getDefaultState());
((BlockUpdateS2CPacketAccessor) headPacket).setBlockPos(pos.up()); ((BlockUpdateS2CPacketAccessor) headPacket).setBlockPos(pos.up());
player.networkHandler.sendPacket(headPacket); player.networkHandler.sendPacket(headPacket);
System.out.println(headPacket);
// Teleporting player to the middle of the block // Teleporting player to the middle of the block
player.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); player.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);

View File

@ -19,7 +19,6 @@ package org.samo_lego.simpleauth.storage;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import net.minecraft.world.dimension.DimensionType;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -59,7 +58,7 @@ public class AuthConfig {
// Whether to tp player to spawn when joining (to hide coordinates) // Whether to tp player to spawn when joining (to hide coordinates)
public boolean spawnOnJoin = false; public boolean spawnOnJoin = false;
public static class WorldSpawn { public static class WorldSpawn {
public DimensionType dimension; public String dimension;
public double x; public double x;
public double y; public double y;
public double z; public double z;

View File

@ -5,7 +5,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry;
import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.dimension.DimensionType;
import java.util.Objects; import java.util.Objects;
@ -21,7 +21,7 @@ public class PlayerCache {
public String lastIp; public String lastIp;
public long validUntil; public long validUntil;
public DimensionType lastDim; public String lastDim;
public double lastX; public double lastX;
public double lastY; public double lastY;
public double lastZ; public double lastZ;
@ -37,20 +37,17 @@ public class PlayerCache {
if(player != null) { if(player != null) {
this.lastIp = player.getIp(); this.lastIp = player.getIp();
// Setting last coordinates // Getting dimension registry
this.lastDim = player.getEntityWorld().getDimension(); Registry<DimensionType> registry = Objects.requireNonNull(player.getServer()).method_29174().getRegistry();
// Setting position cache
this.lastDim = String.valueOf(registry.getId(player.getEntityWorld().getDimension()));
this.lastX = player.getX(); this.lastX = player.getX();
this.lastY = player.getY(); this.lastY = player.getY();
this.lastZ = player.getZ(); this.lastZ = player.getZ();
} }
else { else {
this.lastIp = ""; this.lastIp = "";
// Setting last coordinates
this.lastDim = config.worldSpawn.dimension;
this.lastX = config.worldSpawn.x;
this.lastY = config.worldSpawn.y;
this.lastZ = config.worldSpawn.z;
} }
if(db.isUserRegistered(uuid)) { if(db.isUserRegistered(uuid)) {
@ -59,24 +56,18 @@ public class PlayerCache {
// Getting (hashed) password // Getting (hashed) password
JsonObject json = gson.fromJson(data, JsonObject.class); JsonObject json = gson.fromJson(data, JsonObject.class);
this.password = json.get("password").getAsString(); this.password = json.get("password").getAsString();
this.isRegistered = true;
// If coordinates are same as the one from world spawn // We should check the DB for saved coords
// we should check the DB for saved coords
if(config.main.spawnOnJoin) { if(config.main.spawnOnJoin) {
try { try {
JsonElement lastLoc = json.get("lastLocation"); JsonElement lastLoc = json.get("lastLocation");
if ( if (lastLoc != null) {
lastLoc != null &&
this.lastDim == config.worldSpawn.dimension &&
this.lastX == config.worldSpawn.x &&
this.lastY == config.worldSpawn.y &&
this.lastZ == config.worldSpawn.z
) {
// Getting DB coords // Getting DB coords
JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class); JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class);
String dim = lastLocation.get("dim").getAsString(); //String dim = Objects.requireNonNull(Objects.requireNonNull(player).getServer()).method_29174().getRegistry().get(new Identifier(dim));
// Extra long line to get dimension from string // Extra long line to get dimension from string
this.lastDim = Objects.requireNonNull(Objects.requireNonNull(player).getServer()).method_29174().getRegistry().get(new Identifier(dim)); this.lastDim = lastLocation.get("dim").getAsString();
this.lastX = lastLocation.get("x").getAsDouble(); this.lastX = lastLocation.get("x").getAsDouble();
this.lastY = lastLocation.get("y").getAsDouble(); this.lastY = lastLocation.get("y").getAsDouble();
this.lastZ = lastLocation.get("z").getAsDouble(); this.lastZ = lastLocation.get("z").getAsDouble();
@ -89,7 +80,6 @@ public class PlayerCache {
// Player didn't have any coords in db to tp to // Player didn't have any coords in db to tp to
} }
} }
this.isRegistered = true;
} }
else { else {
this.isRegistered = false; this.isRegistered = false;

View File

@ -15,7 +15,11 @@ import static org.iq80.leveldb.impl.Iq80DBFactory.factory;
public class SimpleAuthDatabase { public class SimpleAuthDatabase {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
public DB levelDBStore; private DB levelDBStore;
public DB getLevelDBStore() {
return this.levelDBStore;
}
// Connects to the DB // Connects to the DB
public void openConnection() { public void openConnection() {