the_big_one/src/main/java/jesse/keeblarcraft/EventMgr/DimensionLoadingEvent.java

135 lines
6.2 KiB
Java
Raw Normal View History

package jesse.keeblarcraft.EventMgr;
import java.util.HashMap;
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;
2024-11-29 09:00:14 +00:00
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
2024-11-29 09:00:14 +00:00
import net.minecraft.text.Text;
public class DimensionLoadingEvent {
2024-11-29 09:00:14 +00:00
private static DimensionLoadingEvent static_inst;
public static DimensionLoadingEvent GetInstance() {
if (static_inst == null) {
static_inst = new DimensionLoadingEvent();
}
return static_inst;
}
private static class InventoryWrapper {
2024-11-29 09:00:14 +00:00
public HashMap<String, NbtList> inventories = new HashMap<String, NbtList>();
}
private static InventoryWrapper iw = new InventoryWrapper();
private static String CONFIG_LOCATION = "misc/dimension_inventories_cached.json";
ConfigManager config = new ConfigManager();
public DimensionLoadingEvent() {
// read config
2024-11-29 09:00:14 +00:00
// Boolean existingFile = false;
// try {
// iw = config.GetJsonObjectFromFile(CONFIG_LOCATION, InventoryWrapper.class);
2024-11-29 09:00:14 +00:00
// 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
// from the that system + story mode because some items will be able to transcend dimensions!
2024-11-29 09:00:14 +00:00
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());
// if (!iw.inventories.containsKey(player.getUuidAsString())) {
// // 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");
// }
// } else if (origin.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE)) {
// 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");
// }
// } 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
2024-11-29 09:00:14 +00:00
public void SaveInventories() {
System.out.println("Call to save inventories. Flashing IW.Inventories with size " + iw.inventories.size());
FlashConfig();
}
2024-11-29 09:00:14 +00:00
public void CheckPlayer(ServerPlayerEntity player) {
// Check the players logged in world. If they are logging into the overworld - we need to see if they are in our
// map and give them the inventory back
if ((!player.getWorld().getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE)) && iw.inventories.containsKey(player.getUuidAsString())) {
// We need to store the contents of the nbt into a regular inventory and magical chest and give it back to the player
player.sendMessage(Text.of("[IMPORTANT]: It appears you have a saved inventory of items on the server! You can claim it with /magic-inventory claim"));
}
}
2024-11-29 09:00:14 +00:00
// Fetches an inventory from this list. Use MagicInventory to store it along the transfer way!
public NbtList GetInventory(String uuid) {
NbtList nbt = iw.inventories.get(uuid);
iw.inventories.remove(uuid);
return nbt;
}
public void FlashConfig() {
try {
2024-11-29 09:00:14 +00:00
config.WriteToJsonFile(CONFIG_LOCATION, iw);
} catch (FILE_WRITE_EXCEPTION e) {
2024-11-29 09:00:14 +00:00
System.out.println(ChatUtil.ColoredString("Could not flash dimension loading configuration file", CONSOLE_COLOR.RED));
}
}
}