forked from BLOCKFANTASY/LOOHP-Limbo
Minecraft 1.20.6
This commit is contained in:
+39
-9
@@ -27,32 +27,31 @@ import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class Registry {
|
||||
public abstract class BuiltInRegistries {
|
||||
|
||||
public static final BlockEntityRegistry BLOCK_ENTITY_TYPE;
|
||||
public static final ItemRegistry ITEM_REGISTRY;
|
||||
public static final MenuRegistry MENU_REGISTRY;
|
||||
public static final DataComponentTypeRegistry DATA_COMPONENT_TYPE;
|
||||
|
||||
static {
|
||||
String name = "registries.json";
|
||||
String name = "reports/registries.json";
|
||||
|
||||
Map<Key, Integer> blockEntityType = new HashMap<>();
|
||||
Key defaultItemKey = null;
|
||||
BiMap<Key, Integer> itemIds = HashBiMap.create();
|
||||
Map<Key, Integer> menuIds = new HashMap<>();
|
||||
BiMap<Key, Integer> dataComponentTypeIds = HashBiMap.create();
|
||||
|
||||
InputStream inputStream = Limbo.class.getClassLoader().getResourceAsStream(name);
|
||||
if (inputStream == null) {
|
||||
@@ -83,22 +82,33 @@ public class Registry {
|
||||
int id = ((Number) ((JSONObject) menuEntriesJson.get(key)).get("protocol_id")).intValue();
|
||||
menuIds.put(Key.key(key), id);
|
||||
}
|
||||
|
||||
JSONObject dataComponentTypeEntriesJson = (JSONObject) ((JSONObject) json.get("minecraft:data_component_type")).get("entries");
|
||||
for (Object obj : dataComponentTypeEntriesJson.keySet()) {
|
||||
String key = obj.toString();
|
||||
int id = ((Number) ((JSONObject) dataComponentTypeEntriesJson.get(key)).get("protocol_id")).intValue();
|
||||
dataComponentTypeIds.put(Key.key(key), id);
|
||||
}
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
BLOCK_ENTITY_TYPE = new BlockEntityRegistry(blockEntityType);
|
||||
ITEM_REGISTRY = new ItemRegistry(defaultItemKey, itemIds);
|
||||
MENU_REGISTRY = new MenuRegistry(menuIds);
|
||||
DATA_COMPONENT_TYPE = new DataComponentTypeRegistry(dataComponentTypeIds);
|
||||
}
|
||||
|
||||
public abstract int getId(Key key);
|
||||
|
||||
public static class BlockEntityRegistry {
|
||||
public static class BlockEntityRegistry extends BuiltInRegistries {
|
||||
|
||||
private Map<Key, Integer> blockEntityType;
|
||||
|
||||
private BlockEntityRegistry(Map<Key, Integer> blockEntityType) {
|
||||
this.blockEntityType = blockEntityType;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getId(Key key) {
|
||||
Integer exact = blockEntityType.get(key);
|
||||
if (exact != null) {
|
||||
@@ -121,7 +131,7 @@ public class Registry {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ItemRegistry {
|
||||
public static class ItemRegistry extends BuiltInRegistries {
|
||||
|
||||
private final Key defaultKey;
|
||||
private final BiMap<Key, Integer> itemIds;
|
||||
@@ -135,6 +145,7 @@ public class Registry {
|
||||
return defaultKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId(Key key) {
|
||||
Integer id = itemIds.get(key);
|
||||
if (id != null) {
|
||||
@@ -151,7 +162,7 @@ public class Registry {
|
||||
}
|
||||
}
|
||||
|
||||
public static class MenuRegistry {
|
||||
public static class MenuRegistry extends BuiltInRegistries {
|
||||
|
||||
private final Map<Key, Integer> menuIds;
|
||||
|
||||
@@ -159,9 +170,28 @@ public class Registry {
|
||||
this.menuIds = menuIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId(Key key) {
|
||||
return menuIds.getOrDefault(key, -1);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DataComponentTypeRegistry extends BuiltInRegistries {
|
||||
|
||||
private final BiMap<Key, Integer> dataComponentTypeIds;
|
||||
|
||||
private DataComponentTypeRegistry(BiMap<Key, Integer> dataComponentTypeIds) {
|
||||
this.dataComponentTypeIds = dataComponentTypeIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId(Key key) {
|
||||
return dataComponentTypeIds.getOrDefault(key, -1);
|
||||
}
|
||||
|
||||
public Key fromId(int id) {
|
||||
return dataComponentTypeIds.inverse().get(id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* This file is part of Limbo.
|
||||
*
|
||||
* Copyright (C) 2024. LoohpJames <jamesloohp@gmail.com>
|
||||
* Copyright (C) 2024. Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.loohp.limbo.registry;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.loohp.limbo.utils.NbtComponentSerializer;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.querz.nbt.tag.Tag;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class DataComponentTypes<T> {
|
||||
|
||||
public static final DataComponentTypes<Component> CUSTOM_NAME = new DataComponentTypes<>("custom_name", new DataComponentCodec<>(component -> {
|
||||
JsonElement element = GsonComponentSerializer.gson().serializeToTree(component);
|
||||
return NbtComponentSerializer.jsonComponentToTag(element);
|
||||
}, tag -> {
|
||||
JsonElement element = NbtComponentSerializer.tagComponentToJson(tag);
|
||||
return GsonComponentSerializer.gson().deserializeFromTree(element);
|
||||
}));
|
||||
|
||||
private final Key key;
|
||||
private final DataComponentCodec<T> codec;
|
||||
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DataComponentTypes(String key, DataComponentCodec<T> codec) {
|
||||
this(Key.key(key), codec);
|
||||
}
|
||||
|
||||
public DataComponentTypes(Key key, DataComponentCodec<T> codec) {
|
||||
this.key = key;
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
public Key getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public DataComponentCodec<T> getCodec() {
|
||||
return codec;
|
||||
}
|
||||
|
||||
public static class DataComponentCodec<T> {
|
||||
|
||||
private final Function<T, Tag<?>> encode;
|
||||
private final Function<Tag<?>, T> decode;
|
||||
|
||||
public DataComponentCodec(Function<T, Tag<?>> encode, Function<Tag<?>, T> decode) {
|
||||
this.encode = encode;
|
||||
this.decode = decode;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Tag<?> encode(T t) {
|
||||
return encode.apply((T) t);
|
||||
}
|
||||
|
||||
public T decode(Tag<?> tag) {
|
||||
return decode.apply(tag);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* This file is part of Limbo.
|
||||
*
|
||||
* Copyright (C) 2024. LoohpJames <jamesloohp@gmail.com>
|
||||
* Copyright (C) 2024. Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.loohp.limbo.registry;
|
||||
|
||||
import com.loohp.limbo.Limbo;
|
||||
import com.loohp.limbo.utils.ClasspathResourcesUtils;
|
||||
import com.loohp.limbo.utils.CustomNBTUtils;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.querz.nbt.tag.CompoundTag;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RegistryCustom {
|
||||
|
||||
public static final RegistryCustom WORLDGEN_BIOME = new RegistryCustom("worldgen/biome");
|
||||
public static final RegistryCustom CHAT_TYPE = new RegistryCustom("chat_type");
|
||||
public static final RegistryCustom TRIM_PATTERN = new RegistryCustom("trim_pattern");
|
||||
public static final RegistryCustom TRIM_MATERIAL = new RegistryCustom("trim_material");
|
||||
public static final RegistryCustom WOLF_VARIANT = new RegistryCustom("wolf_variant");
|
||||
public static final RegistryCustom DIMENSION_TYPE = new RegistryCustom("dimension_type");
|
||||
public static final RegistryCustom DAMAGE_TYPE = new RegistryCustom("damage_type");
|
||||
public static final RegistryCustom BANNER_PATTERN = new RegistryCustom("banner_pattern");
|
||||
|
||||
private final Key identifier;
|
||||
private final Map<Key, CompoundTag> entries;
|
||||
|
||||
public RegistryCustom(Key identifier, Map<Key, CompoundTag> entries) {
|
||||
this.identifier = identifier;
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public RegistryCustom(String identifier) {
|
||||
this(Key.key(identifier));
|
||||
}
|
||||
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public RegistryCustom(Key identifier) {
|
||||
this.identifier = identifier;
|
||||
Map<Key, CompoundTag> entries = new LinkedHashMap<>();
|
||||
String pathStart = "data/" + identifier.namespace() + "/" + identifier.value() + "/";
|
||||
Pattern pattern = Pattern.compile(Pattern.quote(pathStart) + ".*");
|
||||
for (String path : ClasspathResourcesUtils.getResources(pattern)) {
|
||||
if (path.endsWith(".json")) {
|
||||
try (InputStream inputStream = Limbo.class.getClassLoader().getResourceAsStream(path)) {
|
||||
Key entryKey = Key.key(identifier.namespace(), path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf(".")));
|
||||
JSONObject jsonObject = (JSONObject) new JSONParser().parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||
CompoundTag value = CustomNBTUtils.getCompoundTagFromJson(jsonObject);
|
||||
entries.put(entryKey, value);
|
||||
} catch (IOException | ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public Key getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public Map<Key, CompoundTag> getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
public int indexOf(Key key) {
|
||||
int i = 0;
|
||||
for (Key entryKey : entries.keySet()) {
|
||||
if (key.equals(entryKey)) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user