11package com .github .clevernucleus .relicex ;
22
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStream ;
6+ import java .io .InputStreamReader ;
7+ import java .io .Reader ;
8+ import java .nio .charset .StandardCharsets ;
9+ import java .util .HashMap ;
10+ import java .util .Map ;
11+ import java .util .concurrent .CompletableFuture ;
12+ import java .util .concurrent .Executor ;
313import java .util .function .Function ;
414
15+ import org .slf4j .Logger ;
16+
517import com .github .clevernucleus .armorrenderlib .api .ArmorRenderLib ;
618import com .github .clevernucleus .armorrenderlib .api .ArmorRenderProvider ;
719import com .github .clevernucleus .dataattributes .api .event .AttributesReloadedEvent ;
820import com .github .clevernucleus .relicex .impl .EntityAttributeCollection ;
921import com .github .clevernucleus .relicex .impl .Rareness ;
22+ import com .google .gson .Gson ;
23+ import com .google .gson .GsonBuilder ;
24+ import com .google .gson .annotations .Expose ;
25+ import com .mojang .logging .LogUtils ;
1026
1127import net .fabricmc .api .ClientModInitializer ;
28+ import net .fabricmc .fabric .api .resource .ResourceManagerHelper ;
29+ import net .fabricmc .fabric .api .resource .SimpleResourceReloadListener ;
1230import net .fabricmc .fabric .api .util .NbtType ;
1331import net .minecraft .client .item .ModelPredicateProviderRegistry ;
1432import net .minecraft .client .world .ClientWorld ;
1533import net .minecraft .entity .EquipmentSlot ;
1634import net .minecraft .entity .LivingEntity ;
1735import net .minecraft .item .ItemStack ;
1836import net .minecraft .nbt .NbtCompound ;
37+ import net .minecraft .resource .Resource ;
38+ import net .minecraft .resource .ResourceManager ;
39+ import net .minecraft .resource .ResourceType ;
40+ import net .minecraft .util .Formatting ;
1941import net .minecraft .util .Identifier ;
42+ import net .minecraft .util .JsonHelper ;
43+ import net .minecraft .util .profiler .Profiler ;
2044
2145public class RelicExClient implements ClientModInitializer {
2246 private static final Identifier RARENESS = new Identifier (RelicEx .MODID , "rareness" );
47+ private static final RarenessColor RARENESS_COLOR = new RarenessColor ();
2348
2449 private static float predicate (ItemStack itemStack , ClientWorld world , LivingEntity livingEntity , int i ) {
2550 NbtCompound tag = itemStack .getNbt ();
@@ -37,10 +62,82 @@ private static ArmorRenderProvider render(final ItemStack itemStack, final Livin
3762 return s -> s .accept (texture .apply (Rareness .fromKey (tag .getString (EntityAttributeCollection .KEY_RARENESS ))), 0xFFFFFF , false );
3863 }
3964
65+ public static Formatting getColor (final Rareness rareness , final Formatting fallback ) {
66+ return RelicExClient .RARENESS_COLOR .data .getOrDefault (rareness , fallback );
67+ }
68+
4069 @ Override
4170 public void onInitializeClient () {
4271 RelicEx .RELICS .forEach (item -> ModelPredicateProviderRegistry .register (item , RARENESS , RelicExClient ::predicate ));
4372 ArmorRenderLib .register (RelicExClient ::render , RelicEx .HEAD_RELIC , RelicEx .CHEST_RELIC );
4473 AttributesReloadedEvent .EVENT .register (RelicEx .RARITY_MANAGER ::onPropertiesLoaded );
74+ ResourceManagerHelper .get (ResourceType .CLIENT_RESOURCES ).registerReloadListener (RARENESS_COLOR );
75+ }
76+
77+
78+ private static final class RarenessFormatting {
79+ @ Expose protected Map <Rareness , Formatting > values ;
80+ }
81+
82+ private static final class RarenessColor implements SimpleResourceReloadListener <Map <Rareness , Formatting >> {
83+ private static final Gson GSON = (new GsonBuilder ()).excludeFieldsWithoutExposeAnnotation ().create ();
84+ private static final int PATH_SUFFIX_LENGTH = ".json" .length ();
85+ private static final Logger LOGGER = LogUtils .getLogger ();
86+ private static final String DIRECTORY = "rareness" ;
87+ private static final Identifier ID = new Identifier (RelicEx .MODID , DIRECTORY );
88+
89+ protected Map <Rareness , Formatting > data = new HashMap <Rareness , Formatting >();
90+
91+ protected RarenessColor () {}
92+
93+ @ Override
94+ public CompletableFuture <Map <Rareness , Formatting >> load (ResourceManager manager , Profiler profiler , Executor executor ) {
95+ return CompletableFuture .supplyAsync (() -> {
96+ Map <Identifier , RarenessFormatting > cache = new HashMap <Identifier , RarenessFormatting >();
97+ int length = DIRECTORY .length () + 1 ;
98+
99+ for (Identifier resource : manager .findResources (DIRECTORY , file -> file .endsWith ("colors.json" ))) {
100+ String path = resource .getPath ();
101+ Identifier identifier = new Identifier (resource .getNamespace (), path .substring (length , path .length () - PATH_SUFFIX_LENGTH ));
102+
103+ try (
104+ Resource resourceStream = manager .getResource (resource );
105+ InputStream inputStream = resourceStream .getInputStream ();
106+ Reader readerStream = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 ));
107+ ) {
108+ RarenessFormatting json = JsonHelper .deserialize (GSON , readerStream , RarenessFormatting .class );
109+
110+ if (json != null ) {
111+ RarenessFormatting object = cache .put (identifier , json );
112+
113+ if (object != null ) throw new IllegalStateException ("Duplicate asset file ignored with ID " + identifier );
114+ } else {
115+ LOGGER .error ("Couldn't load asset file {} from {} as it's null or empty" , identifier , resource );
116+ }
117+
118+ resourceStream .close ();
119+ } catch (IOException exception ) {
120+ LOGGER .error ("Couldn't parse asset file {} from {}" , identifier , resource , exception );
121+ }
122+ }
123+
124+ Map <Rareness , Formatting > data = new HashMap <Rareness , Formatting >();
125+ cache .forEach ((id , json ) -> json .values .forEach (data ::put ));
126+
127+ return data ;
128+ }, executor );
129+ }
130+
131+ @ Override
132+ public CompletableFuture <Void > apply (Map <Rareness , Formatting > data , ResourceManager manager , Profiler profiler , Executor executor ) {
133+ return CompletableFuture .runAsync (() -> {
134+ this .data = data ;
135+ }, executor );
136+ }
137+
138+ @ Override
139+ public Identifier getFabricId () {
140+ return ID ;
141+ }
45142 }
46143}
0 commit comments