-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPMWClientStorages.java
More file actions
161 lines (143 loc) · 6.16 KB
/
PMWClientStorages.java
File metadata and controls
161 lines (143 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package net.nullved.pmweatherapi.client.data;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.nullved.pmweatherapi.PMWeatherAPI;
import net.nullved.pmweatherapi.client.metar.MetarClientStorage;
import net.nullved.pmweatherapi.client.radar.RadarClientStorage;
import net.nullved.pmweatherapi.client.radar.WSRClientStorage;
import net.nullved.pmweatherapi.client.storage.ClientStorageInstance;
import net.nullved.pmweatherapi.metar.MetarStorage;
import net.nullved.pmweatherapi.metar.MetarStorageData;
import net.nullved.pmweatherapi.radar.RadarMode;
import net.nullved.pmweatherapi.radar.storage.RadarStorage;
import net.nullved.pmweatherapi.radar.storage.WSRStorage;
import net.nullved.pmweatherapi.radar.storage.WSRStorageData;
import net.nullved.pmweatherapi.storage.data.IStorageData;
import net.nullved.pmweatherapi.storage.data.StorageData;
import net.nullved.pmweatherapi.radar.storage.RadarStorageData;
import java.awt.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
/**
* A class holding the specific storage instances for the client
* @since 0.14.15.3
*/
@OnlyIn(Dist.CLIENT)
public class PMWClientStorages {
/**
* A {@link Map} of {@link RadarMode}s to {@link Map}s of pixel ids and their {@link Color}
* @since 0.14.15.6
*/
public static Map<BlockPos, Map<RadarMode, Map<Integer, Color>>> RADAR_MODE_COLORS = new HashMap<>();
public static final Map<ResourceLocation, ClientStorageInstance<?, ?>> STORAGE_INSTANCES = new HashMap<>();
private static ClientLevel lastLevel;
/**
* Gets the {@link ClientStorageInstance} of the {@link RadarClientStorage}
* @return The {@link ClientStorageInstance}
* @since 0.14.15.3
*/
public static ClientStorageInstance<RadarStorageData, RadarClientStorage> radars() {
return get(RadarStorage.ID, RadarClientStorage.class).orElseThrow();
}
/**
* Gets the {@link ClientStorageInstance} of the {@link MetarClientStorage}
* @return The {@link ClientStorageInstance}
* @since 0.15.3.3
*/
public static ClientStorageInstance<MetarStorageData, MetarClientStorage> metars() {
return get(MetarStorage.ID, MetarClientStorage.class).orElseThrow();
}
/**
* Gets the {@link ClientStorageInstance} of the {@link WSRClientStorage}
* @return The {@link ClientStorageInstance}
* @since 0.15.3.3
*/
public static ClientStorageInstance<WSRStorageData, WSRClientStorage> wsrs() {
return get(WSRStorage.ID, WSRClientStorage.class).orElseThrow();
}
/**
* Get a {@link ClientStorageInstance} for a given {@link ResourceLocation} ID
* @param location The ID of the storage
* @return A {@link ClientStorageInstance}
* @since 0.15.3.3
*/
public static ClientStorageInstance<?, ?> get(ResourceLocation location) {
if (!STORAGE_INSTANCES.containsKey(location)) {
PMWeatherAPI.LOGGER.error("No storage instance found for location {}", location);
}
ClientLevel curLevel = Minecraft.getInstance().level;
if (curLevel != null && curLevel != lastLevel) {
loadDimension(curLevel);
}
ClientStorageInstance<?, ?> csi = STORAGE_INSTANCES.get(location);
if (csi.get() == null) {
csi.load(curLevel);
}
return csi;
}
/**
* Overwrite a {@link ClientStorageInstance}
* @param location The ID {@link ResourceLocation}
* @param instance The new {@link ClientStorageInstance}
* @since 0.15.3.3
*/
public static void set(ResourceLocation location, ClientStorageInstance<?, ?> instance) {
STORAGE_INSTANCES.put(location, instance);
}
/**
* Casts the {@link ClientStorageInstance} to the specified {@link IClientStorage} class after retrieval
* @param location The ID {@link ResourceLocation}
* @param clazz The {@link Class} of an {@link IClientStorage} to cast to
* @return The casted {@link ClientStorageInstance}
* @param <D> The {@link IStorageData} of the {@link IClientStorage}
* @param <T> The {@link IClientStorage}
* @since 0.15.3.3
*/
public static <D extends StorageData, T extends IClientStorage<D>> Optional<ClientStorageInstance<D, T>> get(ResourceLocation location, Class<T> clazz) {
return get(location).cast(clazz);
}
/**
* Gets all {@link ClientStorageInstance}s
* @return A {@link Collection} of all {@link ClientStorageInstance}s
* @since 0.15.3.3
*/
public static Collection<? extends ClientStorageInstance<?, ?>> getAll() {
return STORAGE_INSTANCES.values();
}
/**
* Resets all data for all {@link ClientStorageInstance}s
* @since 0.15.3.3
*/
public static void resetAll() {
getAll().forEach(ClientStorageInstance::clear);
}
/**
* Loads a new {@link ClientLevel} for all {@link ClientStorageInstance}s
* @param clientLevel The new {@link ClientLevel} to load
* @since 0.15.3.3
*/
public static void loadDimension(ClientLevel clientLevel) {
lastLevel = clientLevel;
STORAGE_INSTANCES.forEach((location, instance) -> instance.load(clientLevel));
}
/**
* Register a new {@link IClientStorage}
* @param id The {@link ResourceLocation} to save this {@link IClientStorage} as
* @param clazz The {@link Class} of the {@link IClientStorage}
* @param creator A function creating another {@link IClientStorage} for the given {@link ClientLevel}
* @param <D> The {@link IStorageData} of the {@link IClientStorage}
* @param <C> The {@link IClientStorage}
* @since 0.15.3.3
*/
public static <D extends StorageData, C extends IClientStorage<D>> void registerStorage(ResourceLocation id, Class<C> clazz, Function<ClientLevel, C> creator) {
ClientStorageInstance<D, C> instance = new ClientStorageInstance<>(id, clazz, creator);
STORAGE_INSTANCES.put(id, instance);
}
}