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 jesse.keeblarcraft.ConfigMgr.ConfigManager ;
import jesse.keeblarcraft.Utils.ChatUtil ;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR ;
2024-11-24 03:06:00 +00:00
import jesse.keeblarcraft.world.dimension.ModDimensions ;
2024-12-06 03:37:27 +00:00
import net.minecraft.entity.player.PlayerInventory ;
import net.minecraft.nbt.NbtElement ;
2024-11-29 09:00:14 +00:00
import net.minecraft.nbt.NbtList ;
2024-11-24 03:06:00 +00:00
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 ;
2024-11-24 03:06:00 +00:00
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 ;
}
2024-11-24 03:06:00 +00:00
2024-11-24 20:06:34 +00:00
private static class InventoryWrapper {
2024-11-29 09:00:14 +00:00
public HashMap < String , NbtList > inventories = new HashMap < String , NbtList > ( ) ;
2024-11-24 20:06:34 +00:00
}
private static InventoryWrapper iw = new InventoryWrapper ( ) ;
2024-12-06 03:37:27 +00:00
private static String CONFIG_LOCATION = " misc/dim_loading_cached_inventories/ " ;
2024-11-24 20:06:34 +00:00
ConfigManager config = new ConfigManager ( ) ;
public DimensionLoadingEvent ( ) {
// read config
2024-12-06 03:37:27 +00:00
iw . inventories = config . ReadAllNbtListFromDirectory ( CONFIG_LOCATION , NbtElement . COMPOUND_TYPE ) ;
2024-11-24 20:06:34 +00:00
}
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!
2024-11-29 09:00:14 +00:00
public void HandleWorldMove ( ServerPlayerEntity player , ServerWorld origin , ServerWorld destination ) {
2024-12-06 03:37:27 +00:00
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 ) ;
iw . inventories . put ( player . getUuidAsString ( ) , inventoryNbt ) ;
player . getInventory ( ) . clear ( ) ;
// }
// 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())) {
player . getInventory ( ) . readNbt ( iw . inventories . get ( player . getUuidAsString ( ) ) ) ;
iw . inventories . remove ( player . getUuidAsString ( ) ) ;
// }
}
FlashConfig ( ) ;
2024-11-24 03:06:00 +00:00
}
2024-12-06 03:37:27 +00:00
// Call on server close before we lose the volatile memory
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-24 03:06:00 +00:00
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-24 03:06:00 +00:00
}
}
2024-11-24 20:06:34 +00:00
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 ;
}
2024-11-24 20:06:34 +00:00
public void FlashConfig ( ) {
try {
2024-12-06 03:37:27 +00:00
// 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 ) {
2024-11-29 09:00:14 +00:00
System . out . println ( ChatUtil . ColoredString ( " Could not flash dimension loading configuration file " , CONSOLE_COLOR . RED ) ) ;
2024-11-24 20:06:34 +00:00
}
}
2024-11-24 03:06:00 +00:00
}