11package com .github .clevernucleus .relicex ;
22
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .Reader ;
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+ import java .util .concurrent .CompletableFuture ;
9+ import java .util .concurrent .Executor ;
310import java .util .function .Function ;
411
12+ import org .slf4j .Logger ;
13+
514import com .github .clevernucleus .armorrenderlib .api .ArmorRenderLib ;
615import com .github .clevernucleus .armorrenderlib .api .ArmorRenderProvider ;
716import com .github .clevernucleus .dataattributes .api .event .AttributesReloadedEvent ;
817import com .github .clevernucleus .relicex .impl .EntityAttributeCollection ;
918import com .github .clevernucleus .relicex .impl .Rareness ;
19+ import com .google .gson .Gson ;
20+ import com .google .gson .GsonBuilder ;
21+ import com .google .gson .annotations .Expose ;
22+ import com .mojang .logging .LogUtils ;
1023
1124import net .fabricmc .api .ClientModInitializer ;
25+ import net .fabricmc .fabric .api .resource .ResourceManagerHelper ;
26+ import net .fabricmc .fabric .api .resource .SimpleResourceReloadListener ;
1227import net .minecraft .client .item .ModelPredicateProviderRegistry ;
1328import net .minecraft .client .world .ClientWorld ;
1429import net .minecraft .entity .EquipmentSlot ;
1530import net .minecraft .entity .LivingEntity ;
1631import net .minecraft .item .ItemStack ;
1732import net .minecraft .nbt .NbtCompound ;
1833import net .minecraft .nbt .NbtElement ;
34+ import net .minecraft .resource .Resource ;
35+ import net .minecraft .resource .ResourceManager ;
36+ import net .minecraft .resource .ResourceType ;
37+ import net .minecraft .util .Formatting ;
1938import net .minecraft .util .Identifier ;
39+ import net .minecraft .util .JsonHelper ;
40+ import net .minecraft .util .profiler .Profiler ;
2041
2142public class RelicExClient implements ClientModInitializer {
2243 private static final Identifier RARENESS = new Identifier (RelicEx .MODID , "rareness" );
44+ private static final RarenessColor RARENESS_COLOR = new RarenessColor ();
2345
2446 private static float predicate (ItemStack itemStack , ClientWorld world , LivingEntity livingEntity , int i ) {
2547 NbtCompound tag = itemStack .getNbt ();
@@ -37,10 +59,84 @@ private static ArmorRenderProvider render(final ItemStack itemStack, final Livin
3759 return s -> s .accept (texture .apply (Rareness .fromKey (tag .getString (EntityAttributeCollection .KEY_RARENESS ))), 0xFFFFFF , false );
3860 }
3961
62+ public static Formatting getColor (final Rareness rareness , final Formatting fallback ) {
63+ return RelicExClient .RARENESS_COLOR .data .getOrDefault (rareness , fallback );
64+ }
65+
4066 @ Override
4167 public void onInitializeClient () {
4268 RelicEx .RELICS .forEach (item -> ModelPredicateProviderRegistry .register (item , RARENESS , RelicExClient ::predicate ));
4369 ArmorRenderLib .register (RelicExClient ::render , RelicEx .HEAD_RELIC , RelicEx .CHEST_RELIC );
4470 AttributesReloadedEvent .EVENT .register (RelicEx .RARITY_MANAGER ::onPropertiesLoaded );
71+ ResourceManagerHelper .get (ResourceType .CLIENT_RESOURCES ).registerReloadListener (RARENESS_COLOR );
72+ }
73+
74+ private static final class RarenessFormatting {
75+ @ Expose protected Map <Rareness , Formatting > values ;
76+ }
77+
78+ private static final class RarenessColor implements SimpleResourceReloadListener <Map <Rareness , Formatting >> {
79+ private static final Gson GSON = (new GsonBuilder ()).excludeFieldsWithoutExposeAnnotation ().create ();
80+ private static final int PATH_SUFFIX_LENGTH = ".json" .length ();
81+ private static final Logger LOGGER = LogUtils .getLogger ();
82+ private static final String DIRECTORY = "rareness" ;
83+ private static final Identifier ID = new Identifier (RelicEx .MODID , DIRECTORY );
84+
85+ protected Map <Rareness , Formatting > data = new HashMap <Rareness , Formatting >();
86+
87+ protected RarenessColor () {}
88+
89+ @ Override
90+ public CompletableFuture <Map <Rareness , Formatting >> load (ResourceManager manager , Profiler profiler , Executor executor ) {
91+ return CompletableFuture .supplyAsync (() -> {
92+ Map <Identifier , RarenessFormatting > cache = new HashMap <Identifier , RarenessFormatting >();
93+ int length = DIRECTORY .length () + 1 ;
94+
95+ for (Map .Entry <Identifier , Resource > entry : manager .findResources (DIRECTORY , id -> id .getPath ().endsWith ("colors.json" )).entrySet ()) {
96+ Identifier resource = entry .getKey ();
97+ String path = resource .getPath ();
98+ Identifier identifier = new Identifier (resource .getNamespace (), path .substring (length , path .length () - PATH_SUFFIX_LENGTH ));
99+
100+ try {
101+ BufferedReader reader = entry .getValue ().getReader ();
102+
103+ try {
104+ RarenessFormatting json = JsonHelper .deserialize (GSON , (Reader )reader , RarenessFormatting .class );
105+
106+ if (json != null ) {
107+ RarenessFormatting object = cache .put (identifier , json );
108+
109+ if (object == null ) continue ;
110+ throw new IllegalStateException ("Duplicate asset file ignored with ID " + identifier );
111+ }
112+
113+ LOGGER .error ("Couldn't load asset file {} from {} as it's null or empty" , (Object )identifier , (Object )resource );
114+ } finally {
115+ if (reader == null ) continue ;
116+ ((Reader )reader ).close ();
117+ }
118+ } catch (IOException | IllegalArgumentException exception ) {
119+ LOGGER .error ("Couldn't parse asset file {} from {}" , identifier , resource , exception );
120+ }
121+ }
122+
123+ Map <Rareness , Formatting > data = new HashMap <Rareness , Formatting >();
124+ cache .forEach ((id , json ) -> json .values .forEach (data ::put ));
125+
126+ return data ;
127+ }, executor );
128+ }
129+
130+ @ Override
131+ public CompletableFuture <Void > apply (Map <Rareness , Formatting > data , ResourceManager manager , Profiler profiler , Executor executor ) {
132+ return CompletableFuture .runAsync (() -> {
133+ this .data = data ;
134+ }, executor );
135+ }
136+
137+ @ Override
138+ public Identifier getFabricId () {
139+ return ID ;
140+ }
45141 }
46142}
0 commit comments