2024-11-24 03:06:00 +00:00
|
|
|
package jesse.keeblarcraft.EventMgr;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2024-11-24 20:06:34 +00:00
|
|
|
import javax.naming.InvalidNameException;
|
|
|
|
|
|
|
|
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;
|
2024-11-24 03:06:00 +00:00
|
|
|
import jesse.keeblarcraft.world.dimension.ModDimensions;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
|
|
|
|
public class DimensionLoadingEvent {
|
|
|
|
|
|
|
|
// private static List<Inventory> inventories = new ArrayList<Inventory>();
|
2024-11-24 20:06:34 +00:00
|
|
|
private static class InventoryWrapper {
|
|
|
|
public HashMap<String, PlayerInventory> inventories = new HashMap<String, PlayerInventory>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static InventoryWrapper iw = new InventoryWrapper();
|
|
|
|
|
|
|
|
private static String CONFIG_LOCATION = "misc/dimension_inventories_cached.json";
|
|
|
|
ConfigManager config = new ConfigManager();
|
|
|
|
|
|
|
|
public DimensionLoadingEvent() {
|
|
|
|
// read config
|
|
|
|
Boolean existingFile = false;
|
|
|
|
try {
|
|
|
|
iw = config.GetJsonObjectFromFile(CONFIG_LOCATION, InventoryWrapper.class);
|
|
|
|
existingFile = true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
// 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(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));
|
|
|
|
}
|
|
|
|
}
|
2024-11-24 03:06:00 +00:00
|
|
|
|
|
|
|
// 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 static void HandleWorldMove(ServerPlayerEntity player, ServerWorld origin, ServerWorld destination) {
|
|
|
|
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!
|
2024-11-24 20:06:34 +00:00
|
|
|
if (!iw.inventories.containsKey(player.getUuidAsString())) {
|
2024-11-24 03:06:00 +00:00
|
|
|
PlayerInventory copyInv = new PlayerInventory(player);
|
|
|
|
copyInv.clone(player.getInventory());
|
2024-11-24 20:06:34 +00:00
|
|
|
iw.inventories.put(player.getUuidAsString(), copyInv);
|
2024-11-24 03:06:00 +00:00
|
|
|
player.getInventory().clear();
|
|
|
|
} else {
|
|
|
|
System.out.println("Player in system. Ignoring");
|
|
|
|
}
|
|
|
|
} else if (origin.getDimensionEntry().matchesKey(ModDimensions.KEEBLAR_DIM_TYPE)) {
|
2024-11-24 20:06:34 +00:00
|
|
|
if (iw.inventories.containsKey(player.getUuidAsString())) {
|
2024-11-24 03:06:00 +00:00
|
|
|
System.out.println("Player is in map. Cloning our inventory back to them...");
|
2024-11-24 20:06:34 +00:00
|
|
|
player.getInventory().clone(iw.inventories.get(player.getUuidAsString()));
|
|
|
|
iw.inventories.remove(player.getUuidAsString());
|
2024-11-24 03:06:00 +00:00
|
|
|
} 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// needs to be tested
|
|
|
|
public static void ResetInventories() {
|
2024-11-24 20:06:34 +00:00
|
|
|
System.out.println("ResetInventories: inventory size: " + iw.inventories.size());
|
|
|
|
for (Entry<String, PlayerInventory> entry : iw.inventories.entrySet()) {
|
2024-11-24 03:06:00 +00:00
|
|
|
PlayerEntity player = entry.getValue().player;
|
|
|
|
|
|
|
|
System.out.println("Found PlayerEntity");
|
|
|
|
player.getInventory().clone(entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
2024-11-24 20:06:34 +00:00
|
|
|
|
|
|
|
public void FlashConfig() {
|
|
|
|
try {
|
|
|
|
config.WriteToJsonFile(CONFIG_LOCATION, iw.inventories);
|
|
|
|
} catch (FILE_WRITE_EXCEPTION e) {
|
|
|
|
System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED));
|
|
|
|
}
|
|
|
|
}
|
2024-11-24 03:06:00 +00:00
|
|
|
}
|