Minestom/src/main/java/net/minestom/server/registry/Registry.java

184 lines
5.4 KiB
Java

package net.minestom.server.registry;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minestom.server.item.Material;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.InputStreamReader;
import java.util.function.Supplier;
@ApiStatus.Internal
public class Registry {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
public static BlockEntry block(@NotNull JsonObject jsonObject, JsonObject override) {
return new BlockEntry(jsonObject, override);
}
public static JsonObject load(Resource resource) {
final String path = String.format("/%s.json", resource.name);
final var resourceStream = Registry.class.getResourceAsStream(path);
return GSON.fromJson(new InputStreamReader(resourceStream), JsonObject.class);
}
public enum Resource {
BLOCK("blocks");
private final String name;
Resource(String name) {
this.name = name;
}
}
public static class BlockEntry extends Entry {
private final NamespaceID namespace;
private final int id;
private final int stateId;
private final double hardness;
private final double explosionResistance;
private final double friction;
private final double speedFactor;
private final double jumpFactor;
private final boolean air;
private final boolean solid;
private final boolean liquid;
private final String blockEntity;
private final Supplier<Material> materialSupplier;
private BlockEntry(JsonObject main, JsonObject override) {
super(main, override);
this.namespace = NamespaceID.from(getString("namespaceId"));
this.id = getInt("id");
this.stateId = getInt("stateId");
this.hardness = getDouble("hardness");
this.explosionResistance = getDouble("explosionResistance");
this.friction = getDouble("friction");
this.speedFactor = getDouble("speedFactor", 1);
this.jumpFactor = getDouble("jumpFactor", 1);
this.air = getBoolean("air", false);
this.solid = getBoolean("solid");
this.liquid = getBoolean("liquid", false);
this.blockEntity = getString("blockEntity", null);
{
final String materialNamespace = getString("correspondingItem", null);
this.materialSupplier = materialNamespace != null ? () -> Registries.getMaterial(materialNamespace) : () -> null;
}
}
public @NotNull NamespaceID namespace() {
return namespace;
}
public int id() {
return id;
}
public int stateId() {
return stateId;
}
public double hardness() {
return hardness;
}
public double explosionResistance() {
return explosionResistance;
}
public double friction() {
return friction;
}
public double speedFactor() {
return speedFactor;
}
public double jumpFactor() {
return jumpFactor;
}
public boolean isAir() {
return air;
}
public boolean isSolid() {
return solid;
}
public boolean isLiquid() {
return liquid;
}
public boolean isBlockEntity() {
return blockEntity != null;
}
public @Nullable String blockEntity() {
return blockEntity;
}
public @Nullable Material material() {
return materialSupplier.get();
}
}
public static class Entry {
private final JsonObject main, override;
private Entry(JsonObject main, JsonObject override) {
this.main = main;
this.override = override;
}
public String getString(String name, String defaultValue) {
var element = element(name);
return element != null ? element.getAsString() : defaultValue;
}
public String getString(String name) {
return element(name).getAsString();
}
public double getDouble(String name, double defaultValue) {
var element = element(name);
return element != null ? element.getAsDouble() : defaultValue;
}
public double getDouble(String name) {
return element(name).getAsDouble();
}
public int getInt(String name, int defaultValue) {
var element = element(name);
return element != null ? element.getAsInt() : defaultValue;
}
public int getInt(String name) {
return element(name).getAsInt();
}
public boolean getBoolean(String name, boolean defaultValue) {
var element = element(name);
return element != null ? element.getAsBoolean() : defaultValue;
}
public boolean getBoolean(String name) {
return element(name).getAsBoolean();
}
protected JsonElement element(String name) {
if (override.has(name)) {
return override.get(name);
}
return main.get(name);
}
}
}