[MIXED] Fixed world gen inventory moving + spawn setting
Some checks failed
build / build (21) (push) Has been cancelled

This commit is contained in:
Jkibbels 2024-12-05 22:37:27 -05:00
parent 30ec133358
commit f7126a0555
8 changed files with 118 additions and 89 deletions

View File

@ -1,3 +1,3 @@
// 1.20 2024-11-28T23:06:30.379882793 keeblarcraft/Keeblarcraft World Generation
// 1.20 2024-12-05T19:59:23.022622275 keeblarcraft/Keeblarcraft World Generation
afc3340283d1101601bd4d2ca96341a58eceaf83 data/keeblarcraft/dimension_type/keeblarcraftdim_type.json
4398eda2b0c28b2c754c45f5805534bf1921b243 data/keeblarcraft/worldgen/biome/test_biome.json

View File

@ -80,15 +80,27 @@ public class MiscCommands {
public int Warp(ServerPlayerEntity player, String location) {
if (player.hasPermissionLevel(4)) {
System.out.println("Player is opped");
// hard coding spawn as only valid warp location. a more robust warp system can come later
DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords();
var server = player.getServer();
System.out.println("Warping! Is server null? " + (server == null ? "YES":"NO"));
System.out.println("Vakye if coords.x: " + coords.x);
System.out.println("Value of coords.world: " + coords.world.toString());
player.teleport(server.getWorld(coords.world), (float)coords.x, (float)coords.y,
(float)coords.z, (float)coords.yaw, (float)coords.pitch);
// DIRTY HACK: I am unsure how to compare the straight registry key. So for now, we will just iterate over all the worlds in the server. This really should
// not be a big deal since I have never seen a server with hundreds of worlds... but you never know I guess.
for (ServerWorld world : player.getServer().getWorlds()) {
System.out.println("Matched!");
if (world.getRegistryKey().toString().equals(coords.world.toString())) {
try {
player.teleport(world, coords.x, coords.y, coords.z, coords.yaw, coords.pitch);
} catch (Exception e) {
e.printStackTrace();
}
break;
} else {
System.out.println("KEY {" + world.getRegistryKey() + "} DID NOT MATCH OURS {" + coords.world + "}");
}
}
System.out.println("POST TELEPORT");
} else {
player.sendMessage(Text.of("This command is only available to server admins at the moment!"));
}
@ -106,6 +118,8 @@ public class MiscCommands {
spawnVec.pitch = player.getPitch();
spawnVec.yaw = player.getYaw();
spawnVec.world = player.getWorld().getRegistryKey();
System.out.println("REG KEY OF SET: " + player.getWorld().getRegistryKey().toString());
System.out.println("REG KEY OF SET: " + player.getWorld().getRegistryKey());
player.sendMessage(Text.of("Global spawn has been set to [X, Y, Z]--[YAW, PITCH]: [" +
spawnVec.x + " " + spawnVec.y + " " + spawnVec.z + "]--" + "[" + spawnVec.yaw + " " + spawnVec.pitch + "]" + " IN WORLD " +
player.getWorld().asString()));

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.commons.io.FileUtils;
import java.util.ArrayList;
import java.util.HashMap;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
@ -91,21 +92,62 @@ public class ConfigManager {
}
// Writes NBT (binary?) data to a file
public void WriteNbtListToFile(String fileName, NbtList data) {
public void WriteNbtListToFile(String fileName, String key, NbtList data) {
fileName = "config/keeblarcraft/" + fileName;
File file = new File(fileName);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
try {
for (int i = 0; i < data.size(); i++) {
NbtIo.write(data.getCompound(i), file);
}
} catch (IOException e) {
System.out.println("In an attempt to write nbt list to file we encountered an error...");
NbtCompound compound = new NbtCompound();
compound.put(key, data);
NbtIo.writeCompressed(compound, file);
} catch (Exception e) {
e.printStackTrace();
}
}
// Side effect: Files are deleted after being read. Use WriteNbt to write back
public HashMap<String, NbtList> ReadAllNbtListFromDirectory(String dir, int listType) {
HashMap<String, NbtList> list = new HashMap<String, NbtList>();
dir = "config/keeblarcraft/" + dir;
File directory = new File(dir);
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
String key = file.getName().substring(0, file.getName().length() - 4);
NbtList nbtList = ReadNbtListFromFile(file, key, listType);
if (nbtList != null) {
// Subtract out '.nbt' from name
list.put(key, nbtList);
file.delete();
}
}
}
return list;
}
public NbtList ReadNbtListFromFile(File file, String key, int listType) {
NbtList list = null;
try {
NbtCompound c = NbtIo.readCompressed(file);
list = c.getList(key, listType);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
// WriteToFile
//
// Will write or append to file (valid modes: "w" or "a") if file is available. Returns false if not

View File

@ -54,6 +54,7 @@ public class GeneralConfig {
// Can return null; if null then do not spawn the player because global spawn wasn't set!
public DirectionalVec GetSpawnCoords() {
System.out.println("Among us");
System.out.println("GetSpawnCoords called. is global_spawn null? " + (config.global_spawn == null ? "YES": "NO"));
return config.global_spawn;
}

View File

@ -6,9 +6,9 @@ import java.util.Map.Entry;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION;
import jesse.keeblarcraft.world.dimension.ModDimensions;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
@ -31,78 +31,43 @@ public class DimensionLoadingEvent {
private static InventoryWrapper iw = new InventoryWrapper();
private static String CONFIG_LOCATION = "misc/dimension_inventories_cached.json";
private static String CONFIG_LOCATION = "misc/dim_loading_cached_inventories/";
ConfigManager config = new ConfigManager();
public DimensionLoadingEvent() {
// read config
// Boolean existingFile = false;
// try {
// iw = config.GetJsonObjectFromFile(CONFIG_LOCATION, InventoryWrapper.class);
// for (Entry<String, NbtList> entry : iw.inventories.entrySet()) {
// System.out.println("Found entry for UUID " + entry.getKey());
// }
// System.out.println("Size of iw.inventories: " + iw.inventories.size());
// System.out.println("Json object was found!");
// existingFile = true;
// } catch (Exception e) {
// System.out.println("Exception was raised when reading json file for stuff");
// e.printStackTrace();
// // Do nothing. This means the file does not exist
// }
// // In the event the above code failed out, this means a new file has to be created for the player's uuid
// if (!existingFile)
// {
// System.out.println("Creating new file");
// System.out.println(ChatUtil.ColoredString("Trying to create new file", CONSOLE_COLOR.BLUE));
// try {
// FlashConfig();
// } catch (Exception e) {
// System.out.println(ChatUtil.ColoredString("Could not write to file", CONSOLE_COLOR.RED));
// }
// } else {
// System.out.println(ChatUtil.ColoredString("Moving on", CONSOLE_COLOR.BLUE));
// }
iw.inventories = config.ReadAllNbtListFromDirectory(CONFIG_LOCATION, NbtElement.COMPOUND_TYPE);
}
// TODO: In the future when the attribute system is more complete this will need to filter a whitelist of items
// from the that system + story mode because some items will be able to transcend dimensions!
public void HandleWorldMove(ServerPlayerEntity player, ServerWorld origin, ServerWorld destination) {
// System.out.println("World move event called!");
// if (destination.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE)) {
// // Make sure player is in map. For now we only care about storing OVERWORLD inventory. We DO NOT care about
// // the dimension inventory!
// System.out.println("Trying to locate player in inventory map. Player uuid is " + player.getUuidAsString());
System.out.println("World move event called!");
// Player is ENTERING the custom dimension; strip their inventory!
if (destination.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE) && (!iw.inventories.containsKey(player.getUuidAsString()))) {
// Make sure player is in map. For now we only care about storing OVERWORLD inventory. We DO NOT care about
// the dimension inventory!
// if (!iw.inventories.containsKey(player.getUuidAsString())) {
// // Copy the nbt into the list
// NbtList inventoryNbt = new NbtList();
// player.getInventory().writeNbt(inventoryNbt);
// Copy the nbt into the list
NbtList inventoryNbt = new NbtList();
player.getInventory().writeNbt(inventoryNbt);
// iw.inventories.put(player.getUuidAsString(), inventoryNbt);
// player.getInventory().clear();
// } else {
// System.out.println("Player in system. Ignoring");
iw.inventories.put(player.getUuidAsString(), inventoryNbt);
player.getInventory().clear();
// }
// } else if (origin.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE)) {
// Player is LEAVING the custom dimension. Give them their stuff back
} else if (origin.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE) && iw.inventories.containsKey(player.getUuidAsString())) {
// if (iw.inventories.containsKey(player.getUuidAsString())) {
// System.out.println("Player is in map. Cloning our inventory back to them...");
// player.getInventory().readNbt(iw.inventories.get(player.getUuidAsString()));
// iw.inventories.remove(player.getUuidAsString());
// } else {
// System.out.println("Player not in system. Ignoring");
player.getInventory().readNbt(iw.inventories.get(player.getUuidAsString()));
iw.inventories.remove(player.getUuidAsString());
// }
// } else {
// System.out.println("dest TYPE - KEY" + destination.getDimensionEntry().getType().toString() + " -- " + destination.getDimensionEntry().getKey().toString());
// System.out.println("orig TYPE - KEY: " + origin.getDimensionEntry().getType().toString() + " -- "+ origin.getDimensionEntry().getKey().toString());
// }
// FlashConfig();
}
// needs to be tested
FlashConfig();
}
// Call on server close before we lose the volatile memory
public void SaveInventories() {
System.out.println("Call to save inventories. Flashing IW.Inventories with size " + iw.inventories.size());
FlashConfig();
@ -126,8 +91,14 @@ public class DimensionLoadingEvent {
public void FlashConfig() {
try {
config.WriteToJsonFile(CONFIG_LOCATION, iw);
} catch (FILE_WRITE_EXCEPTION e) {
// config.WriteToJsonFile(CONFIG_LOCATION, iw);
// First, ensure list is size > 0
for (Entry<String, NbtList> list : iw.inventories.entrySet()) {
if (list.getValue().size() > 0) {
config.WriteNbtListToFile(CONFIG_LOCATION + list.getKey() + ".nbt", list.getKey(), list.getValue());
}
}
} catch (Exception e) {
System.out.println(ChatUtil.ColoredString("Could not flash dimension loading configuration file", CONSOLE_COLOR.RED));
}
}

View File

@ -14,7 +14,6 @@ import net.minecraft.text.Text;
public class PlayerJoinListener {
private static PlayerJoinListener static_inst;
private ConfigManager config = new ConfigManager();
public static PlayerJoinListener GetInstance() {
if (static_inst == null) {
@ -36,22 +35,23 @@ public class PlayerJoinListener {
IsFirstTimeLogin(player, server);
}
// this will be reworked in the future
private void IsFirstTimeLogin(ServerPlayerEntity player, MinecraftServer server) {
// Send the MOTD + Spawn in global spawn
player.sendMessage(Text.of(GeneralConfig.GetInstance().GetMOTD()));
if (GeneralConfig.GetInstance().IsNewPlayer(player.getUuidAsString())) {
DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords();
if (coords != null) {
System.out.println("Coords is not null");
System.out.println("Coords world key: " + server.getWorld(coords.world).asString());
player.teleport(server.getWorld(coords.world), (float)coords.x, (float)coords.y,
(float)coords.z, (float)coords.yaw, (float)coords.pitch);
} else {
System.out.println("Coords is a null object");
System.out.println("Coords were null. Not warping player");
}
} else {
System.out.println("Not a new player! Not running anything");
for (ServerWorld world : player.getServer().getWorlds()) {
if (world.getRegistryKey().toString().equals(coords.world.toString())) {
try {
player.teleport(world, coords.x, coords.y, coords.z, coords.yaw, coords.pitch);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}

View File

@ -4,11 +4,10 @@ import net.minecraft.registry.RegistryKey;
import net.minecraft.world.World;
public class DirectionalVec {
// public String RegistryKeyStringWorld;
public RegistryKey<World> world;
public double x;
public double y;
public double z;
public double yaw;
public double pitch;
public float yaw;
public float pitch;
}

View File

@ -1,3 +1,5 @@
package jesse.keeblarcraft.world;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;