Skip to content

Commit 8ccdce9

Browse files
committed
Fix and rareness customisation
+Fixed bug with Tieredz (Relics can still be combined with Tieredz items and keep both Tieredz and RelicEx attributes). +Added resourcepack customisation of rareness text colours. *Incremented version.
1 parent 1c4af42 commit 8ccdce9

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ minecraft_version=1.19.2
44
yarn_mappings=1.19.2+build.28
55
loader_version=0.14.14
66

7-
mod_version = 3.3.0+1.19.2
7+
mod_version = 3.3.1+1.19.2
88
maven_group = com.github.clevernucleus
99
archives_base_name = relicex
1010

src/main/java/com/github/clevernucleus/relicex/RelicExClient.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
package 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;
310
import java.util.function.Function;
411

12+
import org.slf4j.Logger;
13+
514
import com.github.clevernucleus.armorrenderlib.api.ArmorRenderLib;
615
import com.github.clevernucleus.armorrenderlib.api.ArmorRenderProvider;
716
import com.github.clevernucleus.dataattributes.api.event.AttributesReloadedEvent;
817
import com.github.clevernucleus.relicex.impl.EntityAttributeCollection;
918
import 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

1124
import net.fabricmc.api.ClientModInitializer;
25+
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
26+
import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener;
1227
import net.minecraft.client.item.ModelPredicateProviderRegistry;
1328
import net.minecraft.client.world.ClientWorld;
1429
import net.minecraft.entity.EquipmentSlot;
1530
import net.minecraft.entity.LivingEntity;
1631
import net.minecraft.item.ItemStack;
1732
import net.minecraft.nbt.NbtCompound;
1833
import 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;
1938
import net.minecraft.util.Identifier;
39+
import net.minecraft.util.JsonHelper;
40+
import net.minecraft.util.profiler.Profiler;
2041

2142
public 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
}

src/main/java/com/github/clevernucleus/relicex/impl/Rareness.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.github.clevernucleus.dataattributes.api.util.Maths;
66
import com.github.clevernucleus.relicex.RelicEx;
7+
import com.github.clevernucleus.relicex.RelicExClient;
78

89
import net.minecraft.sound.SoundEvent;
910
import net.minecraft.sound.SoundEvents;
@@ -71,7 +72,7 @@ public SoundEvent equipSound() {
7172
}
7273

7374
public Text formatted() {
74-
return Text.translatable("rareness." + RelicEx.MODID + "." + this.key).formatted(this.formatting);
75+
return Text.translatable("rareness." + RelicEx.MODID + "." + this.key).formatted(RelicExClient.getColor(this, this.formatting));
7576
}
7677

7778
@Override
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"replace": false,
3+
"values": [
4+
"relicex:chest_relic"
5+
]
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"replace": false,
3+
"values": [
4+
"relicex:head_relic"
5+
]
6+
}

0 commit comments

Comments
 (0)