[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 afc3340283d1101601bd4d2ca96341a58eceaf83 data/keeblarcraft/dimension_type/keeblarcraftdim_type.json
4398eda2b0c28b2c754c45f5805534bf1921b243 data/keeblarcraft/worldgen/biome/test_biome.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) { public int Warp(ServerPlayerEntity player, String location) {
if (player.hasPermissionLevel(4)) { 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 // hard coding spawn as only valid warp location. a more robust warp system can come later
DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords(); DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords();
var server = player.getServer();
System.out.println("Warping! Is server null? " + (server == null ? "YES":"NO")); // 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
System.out.println("Vakye if coords.x: " + coords.x); // not be a big deal since I have never seen a server with hundreds of worlds... but you never know I guess.
System.out.println("Value of coords.world: " + coords.world.toString()); for (ServerWorld world : player.getServer().getWorlds()) {
player.teleport(server.getWorld(coords.world), (float)coords.x, (float)coords.y, System.out.println("Matched!");
(float)coords.z, (float)coords.yaw, (float)coords.pitch); 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 { } else {
player.sendMessage(Text.of("This command is only available to server admins at the moment!")); 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.pitch = player.getPitch();
spawnVec.yaw = player.getYaw(); spawnVec.yaw = player.getYaw();
spawnVec.world = player.getWorld().getRegistryKey(); 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]: [" + 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 " + spawnVec.x + " " + spawnVec.y + " " + spawnVec.z + "]--" + "[" + spawnVec.yaw + " " + spawnVec.pitch + "]" + " IN WORLD " +
player.getWorld().asString())); player.getWorld().asString()));

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import jesse.keeblarcraft.Utils.ChatUtil; import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR; import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
@ -91,21 +92,62 @@ public class ConfigManager {
} }
// Writes NBT (binary?) data to a file // 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; fileName = "config/keeblarcraft/" + fileName;
File file = new File(fileName); File file = new File(fileName);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
try { try {
for (int i = 0; i < data.size(); i++) { NbtCompound compound = new NbtCompound();
NbtIo.write(data.getCompound(i), file); compound.put(key, data);
} NbtIo.writeCompressed(compound, file);
} catch (IOException e) {
System.out.println("In an attempt to write nbt list to file we encountered an error..."); } catch (Exception e) {
e.printStackTrace(); 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 // WriteToFile
// //
// Will write or append to file (valid modes: "w" or "a") if file is available. Returns false if not // 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! // Can return null; if null then do not spawn the player because global spawn wasn't set!
public DirectionalVec GetSpawnCoords() { public DirectionalVec GetSpawnCoords() {
System.out.println("Among us");
System.out.println("GetSpawnCoords called. is global_spawn null? " + (config.global_spawn == null ? "YES": "NO")); System.out.println("GetSpawnCoords called. is global_spawn null? " + (config.global_spawn == null ? "YES": "NO"));
return config.global_spawn; return config.global_spawn;
} }

View File

@ -6,9 +6,9 @@ import java.util.Map.Entry;
import jesse.keeblarcraft.ConfigMgr.ConfigManager; import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil; import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR; import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION;
import jesse.keeblarcraft.world.dimension.ModDimensions; 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.nbt.NbtList;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
@ -31,78 +31,43 @@ public class DimensionLoadingEvent {
private static InventoryWrapper iw = new InventoryWrapper(); 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(); ConfigManager config = new ConfigManager();
public DimensionLoadingEvent() { public DimensionLoadingEvent() {
// read config // read config
// Boolean existingFile = false; iw.inventories = config.ReadAllNbtListFromDirectory(CONFIG_LOCATION, NbtElement.COMPOUND_TYPE);
// 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));
// }
} }
// TODO: In the future when the attribute system is more complete this will need to filter a whitelist of items // 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! // from the that system + story mode because some items will be able to transcend dimensions!
public void HandleWorldMove(ServerPlayerEntity player, ServerWorld origin, ServerWorld destination) { public void HandleWorldMove(ServerPlayerEntity player, ServerWorld origin, ServerWorld destination) {
// System.out.println("World move event called!"); 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 // Player is ENTERING the custom dimension; strip their inventory!
// // the dimension inventory! if (destination.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE) && (!iw.inventories.containsKey(player.getUuidAsString()))) {
// System.out.println("Trying to locate player in inventory map. Player uuid is " + 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())) { // if (!iw.inventories.containsKey(player.getUuidAsString())) {
// // Copy the nbt into the list // Copy the nbt into the list
// NbtList inventoryNbt = new NbtList(); NbtList inventoryNbt = new NbtList();
// player.getInventory().writeNbt(inventoryNbt); player.getInventory().writeNbt(inventoryNbt);
// iw.inventories.put(player.getUuidAsString(), inventoryNbt); iw.inventories.put(player.getUuidAsString(), inventoryNbt);
// player.getInventory().clear(); player.getInventory().clear();
// } else {
// System.out.println("Player in system. Ignoring");
// } // }
// } 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())) { // 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()));
// player.getInventory().readNbt(iw.inventories.get(player.getUuidAsString())); iw.inventories.remove(player.getUuidAsString());
// iw.inventories.remove(player.getUuidAsString());
// } else {
// System.out.println("Player not in system. Ignoring");
// } // }
// } 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() { public void SaveInventories() {
System.out.println("Call to save inventories. Flashing IW.Inventories with size " + iw.inventories.size()); System.out.println("Call to save inventories. Flashing IW.Inventories with size " + iw.inventories.size());
FlashConfig(); FlashConfig();
@ -126,8 +91,14 @@ public class DimensionLoadingEvent {
public void FlashConfig() { public void FlashConfig() {
try { try {
config.WriteToJsonFile(CONFIG_LOCATION, iw); // config.WriteToJsonFile(CONFIG_LOCATION, iw);
} catch (FILE_WRITE_EXCEPTION e) { // 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)); 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 { public class PlayerJoinListener {
private static PlayerJoinListener static_inst; private static PlayerJoinListener static_inst;
private ConfigManager config = new ConfigManager();
public static PlayerJoinListener GetInstance() { public static PlayerJoinListener GetInstance() {
if (static_inst == null) { if (static_inst == null) {
@ -36,22 +35,23 @@ public class PlayerJoinListener {
IsFirstTimeLogin(player, server); IsFirstTimeLogin(player, server);
} }
// this will be reworked in the future
private void IsFirstTimeLogin(ServerPlayerEntity player, MinecraftServer server) { private void IsFirstTimeLogin(ServerPlayerEntity player, MinecraftServer server) {
// Send the MOTD + Spawn in global spawn // Send the MOTD + Spawn in global spawn
player.sendMessage(Text.of(GeneralConfig.GetInstance().GetMOTD())); player.sendMessage(Text.of(GeneralConfig.GetInstance().GetMOTD()));
if (GeneralConfig.GetInstance().IsNewPlayer(player.getUuidAsString())) { if (GeneralConfig.GetInstance().IsNewPlayer(player.getUuidAsString())) {
DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords(); DirectionalVec coords = GeneralConfig.GetInstance().GetSpawnCoords();
if (coords != null) { if (coords != null) {
System.out.println("Coords is not null"); for (ServerWorld world : player.getServer().getWorlds()) {
System.out.println("Coords world key: " + server.getWorld(coords.world).asString()); if (world.getRegistryKey().toString().equals(coords.world.toString())) {
player.teleport(server.getWorld(coords.world), (float)coords.x, (float)coords.y, try {
(float)coords.z, (float)coords.yaw, (float)coords.pitch); player.teleport(world, coords.x, coords.y, coords.z, coords.yaw, coords.pitch);
} else { } catch (Exception e) {
System.out.println("Coords is a null object"); e.printStackTrace();
System.out.println("Coords were null. Not warping player"); }
} }
} else { }
System.out.println("Not a new player! Not running anything"); }
} }
} }
} }

View File

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

View File

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