mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-05 06:51:22 +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) {
|
||||
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);
|
||||
final BinaryTag tag = TagStringIOExt.readTag(new String(resourceStream.readAllBytes(), StandardCharsets.UTF_8));
|
||||
if (!(tag instanceof CompoundBinaryTag compound)) {
|
||||
|
@ -35,6 +35,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
@ -135,11 +137,24 @@ public final class Registry {
|
||||
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
|
||||
public static Map<String, Map<String, Object>> load(Resource resource) {
|
||||
Map<String, Map<String, Object>> map = new HashMap<>();
|
||||
try (InputStream resourceStream = Registry.class.getClassLoader().getResourceAsStream(resource.name)) {
|
||||
Check.notNull(resourceStream, "Resource {0} does not exist!", resource);
|
||||
try (InputStream resourceStream = loadRegistryFile(resource)) {
|
||||
try (JsonReader reader = new JsonReader(new InputStreamReader(resourceStream))) {
|
||||
reader.beginObject();
|
||||
while (reader.hasNext()) map.put(reader.nextName(), (Map<String, Object>) readObject(reader));
|
||||
|
Loading…
Reference in New Issue
Block a user