mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-11 09:51:35 +01:00
chore: file registries (#2538)
This commit is contained in:
parent
187931e50b
commit
9803f2bfe3
@ -230,7 +230,7 @@ final class DynamicRegistryImpl<T> implements DynamicRegistry<T> {
|
|||||||
|
|
||||||
static <T extends ProtocolObject> void loadStaticSnbtRegistry(@NotNull Registries registries, @NotNull DynamicRegistryImpl<T> registry, @NotNull Registry.Resource resource) {
|
static <T extends ProtocolObject> void loadStaticSnbtRegistry(@NotNull Registries registries, @NotNull DynamicRegistryImpl<T> registry, @NotNull Registry.Resource resource) {
|
||||||
Check.argCondition(!resource.fileName().endsWith(".snbt"), "Resource must be an SNBT file: {0}", resource.fileName());
|
Check.argCondition(!resource.fileName().endsWith(".snbt"), "Resource must be an SNBT file: {0}", resource.fileName());
|
||||||
try (InputStream resourceStream = Registry.class.getClassLoader().getResourceAsStream(resource.fileName())) {
|
try (InputStream resourceStream = Registry.loadRegistryFile(resource)) {
|
||||||
Check.notNull(resourceStream, "Resource {0} does not exist!", resource);
|
Check.notNull(resourceStream, "Resource {0} does not exist!", resource);
|
||||||
final BinaryTag tag = TagStringIOExt.readTag(new String(resourceStream.readAllBytes(), StandardCharsets.UTF_8));
|
final BinaryTag tag = TagStringIOExt.readTag(new String(resourceStream.readAllBytes(), StandardCharsets.UTF_8));
|
||||||
if (!(tag instanceof CompoundBinaryTag compound)) {
|
if (!(tag instanceof CompoundBinaryTag compound)) {
|
||||||
|
@ -35,6 +35,8 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@ -135,11 +137,24 @@ public final class Registry {
|
|||||||
return new JukeboxSongEntry(namespace, main, null);
|
return new JukeboxSongEntry(namespace, main, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static @NotNull InputStream loadRegistryFile(@NotNull Resource resource) throws IOException {
|
||||||
|
// 1. Try to load from jar resources
|
||||||
|
InputStream resourceStream = Registry.class.getClassLoader().getResourceAsStream(resource.name);
|
||||||
|
|
||||||
|
// 2. Try to load from working directory
|
||||||
|
if (resourceStream == null && Files.exists(Path.of(resource.name))) {
|
||||||
|
resourceStream = Files.newInputStream(Path.of(resource.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Not found :(
|
||||||
|
Check.notNull(resourceStream, "Resource {0} does not exist!", resource);
|
||||||
|
return resourceStream;
|
||||||
|
}
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public static Map<String, Map<String, Object>> load(Resource resource) {
|
public static Map<String, Map<String, Object>> load(Resource resource) {
|
||||||
Map<String, Map<String, Object>> map = new HashMap<>();
|
Map<String, Map<String, Object>> map = new HashMap<>();
|
||||||
try (InputStream resourceStream = Registry.class.getClassLoader().getResourceAsStream(resource.name)) {
|
try (InputStream resourceStream = loadRegistryFile(resource)) {
|
||||||
Check.notNull(resourceStream, "Resource {0} does not exist!", resource);
|
|
||||||
try (JsonReader reader = new JsonReader(new InputStreamReader(resourceStream))) {
|
try (JsonReader reader = new JsonReader(new InputStreamReader(resourceStream))) {
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
while (reader.hasNext()) map.put(reader.nextName(), (Map<String, Object>) readObject(reader));
|
while (reader.hasNext()) map.put(reader.nextName(), (Map<String, Object>) readObject(reader));
|
||||||
|
Loading…
Reference in New Issue
Block a user