Skip to content

Commit 647f0eb

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 32eed47 commit 647f0eb

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-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.18.2
44
yarn_mappings=1.18.2+build.4
55
loader_version=0.14.14
66

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

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

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

15+
import org.slf4j.Logger;
16+
517
import com.github.clevernucleus.armorrenderlib.api.ArmorRenderLib;
618
import com.github.clevernucleus.armorrenderlib.api.ArmorRenderProvider;
719
import com.github.clevernucleus.dataattributes.api.event.AttributesReloadedEvent;
820
import com.github.clevernucleus.relicex.impl.EntityAttributeCollection;
921
import 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

1127
import net.fabricmc.api.ClientModInitializer;
28+
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
29+
import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener;
1230
import net.fabricmc.fabric.api.util.NbtType;
1331
import net.minecraft.client.item.ModelPredicateProviderRegistry;
1432
import net.minecraft.client.world.ClientWorld;
1533
import net.minecraft.entity.EquipmentSlot;
1634
import net.minecraft.entity.LivingEntity;
1735
import net.minecraft.item.ItemStack;
1836
import 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;
1941
import net.minecraft.util.Identifier;
42+
import net.minecraft.util.JsonHelper;
43+
import net.minecraft.util.profiler.Profiler;
2044

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

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;
@@ -72,7 +73,7 @@ public SoundEvent equipSound() {
7273
}
7374

7475
public Text formatted() {
75-
return (new TranslatableText("rareness." + RelicEx.MODID + "." + this.key)).formatted(this.formatting);
76+
return (new TranslatableText("rareness." + RelicEx.MODID + "." + this.key)).formatted(RelicExClient.getColor(this, this.formatting));
7677
}
7778

7879
@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)