11package net .nullved .pmweatherapi .client .data ;
22
33import net .minecraft .client .Minecraft ;
4+ import net .minecraft .client .multiplayer .ClientLevel ;
45import net .minecraft .core .BlockPos ;
5- import net .minecraft .world . level . Level ;
6+ import net .minecraft .resources . ResourceLocation ;
67import net .neoforged .api .distmarker .Dist ;
78import net .neoforged .api .distmarker .OnlyIn ;
89import net .nullved .pmweatherapi .PMWeatherAPI ;
10+ import net .nullved .pmweatherapi .client .metar .MetarClientStorage ;
911import net .nullved .pmweatherapi .client .radar .RadarClientStorage ;
12+ import net .nullved .pmweatherapi .client .radar .WSRClientStorage ;
13+ import net .nullved .pmweatherapi .client .storage .ClientStorageInstance ;
14+ import net .nullved .pmweatherapi .metar .MetarStorage ;
15+ import net .nullved .pmweatherapi .metar .MetarStorageData ;
1016import net .nullved .pmweatherapi .radar .RadarMode ;
17+ import net .nullved .pmweatherapi .radar .storage .RadarStorage ;
18+ import net .nullved .pmweatherapi .radar .storage .WSRStorage ;
19+ import net .nullved .pmweatherapi .radar .storage .WSRStorageData ;
20+ import net .nullved .pmweatherapi .storage .data .IStorageData ;
21+ import net .nullved .pmweatherapi .storage .data .StorageData ;
22+ import net .nullved .pmweatherapi .radar .storage .RadarStorageData ;
1123
1224import java .awt .*;
25+ import java .util .Collection ;
1326import java .util .HashMap ;
1427import java .util .Map ;
28+ import java .util .Optional ;
29+ import java .util .function .Function ;
1530
1631/**
1732 * A class holding the specific storage instances for the client
@@ -25,44 +40,122 @@ public class PMWClientStorages {
2540 */
2641 public static Map <BlockPos , Map <RadarMode , Map <Integer , Color >>> RADAR_MODE_COLORS = new HashMap <>();
2742
28- private static Level lastLevel ;
29- private static RadarClientStorage radar ;
43+ public static final Map <ResourceLocation , ClientStorageInstance <?, ?>> STORAGE_INSTANCES = new HashMap <>();
44+
45+ private static ClientLevel lastLevel ;
3046
3147 /**
32- * Resets this client's internal radar storage
48+ * Gets the {@link ClientStorageInstance} of the {@link RadarClientStorage}
49+ * @return The {@link ClientStorageInstance}
3350 * @since 0.14.15.3
3451 */
35- public static void resetRadars () {
36- radar = null ;
52+ public static ClientStorageInstance < RadarStorageData , RadarClientStorage > radars () {
53+ return get ( RadarStorage . ID , RadarClientStorage . class ). orElseThrow () ;
3754 }
3855
3956 /**
40- * Gets the radar storage for the dimension this client is currently in
41- * @return A {@link RadarClientStorage }
42- * @since 0.14.15 .3
57+ * Gets the {@link ClientStorageInstance} of the {@link MetarClientStorage}
58+ * @return The {@link ClientStorageInstance }
59+ * @since 0.15.3 .3
4360 */
44- public static RadarClientStorage getRadars () {
45- try {
46- Level level = Minecraft .getInstance ().level ;
47- if (radar == null || level != lastLevel ) {
48- init (level );
49- }
50- } catch (Exception e ) {
51- PMWeatherAPI .LOGGER .error (e .getMessage (), e );
52- }
61+ public static ClientStorageInstance <MetarStorageData , MetarClientStorage > metars () {
62+ return get (MetarStorage .ID , MetarClientStorage .class ).orElseThrow ();
63+ }
5364
54- return radar ;
65+ /**
66+ * Gets the {@link ClientStorageInstance} of the {@link WSRClientStorage}
67+ * @return The {@link ClientStorageInstance}
68+ * @since 0.15.3.3
69+ */
70+ public static ClientStorageInstance <WSRStorageData , WSRClientStorage > wsrs () {
71+ return get (WSRStorage .ID , WSRClientStorage .class ).orElseThrow ();
5572 }
5673
5774 /**
58- * Initializes this client's storages to be the storages for the given level
59- * @param level The {@link Level} to initialize storages for
60- * @since 0.14.15.3
75+ * Get a {@link ClientStorageInstance} for a given {@link ResourceLocation} ID
76+ * @param location The ID of the storage
77+ * @return A {@link ClientStorageInstance}
78+ * @since 0.15.3.3
6179 */
62- public static void init (Level level ) {
63- lastLevel = level ;
64- if (level != null ) {
65- radar = new RadarClientStorage (level .dimension ());
80+ public static ClientStorageInstance <?, ?> get (ResourceLocation location ) {
81+ if (!STORAGE_INSTANCES .containsKey (location )) {
82+ PMWeatherAPI .LOGGER .error ("No storage instance found for location {}" , location );
83+ }
84+
85+ ClientLevel curLevel = Minecraft .getInstance ().level ;
86+ if (curLevel != null && curLevel != lastLevel ) {
87+ loadDimension (curLevel );
6688 }
89+
90+ ClientStorageInstance <?, ?> csi = STORAGE_INSTANCES .get (location );
91+ if (csi .get () == null ) {
92+ csi .load (curLevel );
93+ }
94+
95+ return csi ;
96+ }
97+
98+ /**
99+ * Overwrite a {@link ClientStorageInstance}
100+ * @param location The ID {@link ResourceLocation}
101+ * @param instance The new {@link ClientStorageInstance}
102+ * @since 0.15.3.3
103+ */
104+ public static void set (ResourceLocation location , ClientStorageInstance <?, ?> instance ) {
105+ STORAGE_INSTANCES .put (location , instance );
106+ }
107+
108+ /**
109+ * Casts the {@link ClientStorageInstance} to the specified {@link IClientStorage} class after retrieval
110+ * @param location The ID {@link ResourceLocation}
111+ * @param clazz The {@link Class} of an {@link IClientStorage} to cast to
112+ * @return The casted {@link ClientStorageInstance}
113+ * @param <D> The {@link IStorageData} of the {@link IClientStorage}
114+ * @param <T> The {@link IClientStorage}
115+ * @since 0.15.3.3
116+ */
117+ public static <D extends StorageData , T extends IClientStorage <D >> Optional <ClientStorageInstance <D , T >> get (ResourceLocation location , Class <T > clazz ) {
118+ return get (location ).cast (clazz );
119+ }
120+
121+ /**
122+ * Gets all {@link ClientStorageInstance}s
123+ * @return A {@link Collection} of all {@link ClientStorageInstance}s
124+ * @since 0.15.3.3
125+ */
126+ public static Collection <? extends ClientStorageInstance <?, ?>> getAll () {
127+ return STORAGE_INSTANCES .values ();
128+ }
129+
130+ /**
131+ * Resets all data for all {@link ClientStorageInstance}s
132+ * @since 0.15.3.3
133+ */
134+ public static void resetAll () {
135+ getAll ().forEach (ClientStorageInstance ::clear );
136+ }
137+
138+ /**
139+ * Loads a new {@link ClientLevel} for all {@link ClientStorageInstance}s
140+ * @param clientLevel The new {@link ClientLevel} to load
141+ * @since 0.15.3.3
142+ */
143+ public static void loadDimension (ClientLevel clientLevel ) {
144+ lastLevel = clientLevel ;
145+ STORAGE_INSTANCES .forEach ((location , instance ) -> instance .load (clientLevel ));
146+ }
147+
148+ /**
149+ * Register a new {@link IClientStorage}
150+ * @param id The {@link ResourceLocation} to save this {@link IClientStorage} as
151+ * @param clazz The {@link Class} of the {@link IClientStorage}
152+ * @param creator A function creating another {@link IClientStorage} for the given {@link ClientLevel}
153+ * @param <D> The {@link IStorageData} of the {@link IClientStorage}
154+ * @param <C> The {@link IClientStorage}
155+ * @since 0.15.3.3
156+ */
157+ public static <D extends StorageData , C extends IClientStorage <D >> void registerStorage (ResourceLocation id , Class <C > clazz , Function <ClientLevel , C > creator ) {
158+ ClientStorageInstance <D , C > instance = new ClientStorageInstance <>(id , clazz , creator );
159+ STORAGE_INSTANCES .put (id , instance );
67160 }
68161}
0 commit comments