*WIP*: Merge commit 'ce4cb8974f' into 1.18

This commit is contained in:
Frank 2021-11-13 19:30:01 +01:00
commit c6a7a1d4f7
76 changed files with 2754 additions and 738 deletions

View file

@ -0,0 +1,74 @@
package ru.bclib.util;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.bclib.BCLib;
public class ItemUtil {
public static String toStackString(@NotNull ItemStack stack) {
try {
if (stack == null) {
throw new IllegalStateException("Stack can't be null!");
}
Item item = stack.getItem();
return Registry.ITEM.getKey(item) + ":" + stack.getCount();
}
catch (Exception ex) {
BCLib.LOGGER.error("ItemStack serialization error!", ex);
}
return "";
}
@Nullable
public static ItemStack fromStackString(String stackString) {
if (stackString == null || stackString.equals("")) {
return null;
}
try {
String[] parts = stackString.split(":");
if (parts.length < 2) return null;
if (parts.length == 2) {
ResourceLocation itemId = new ResourceLocation(stackString);
Item item = Registry.ITEM.getOptional(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});
return new ItemStack(item);
}
ResourceLocation itemId = new ResourceLocation(parts[0], parts[1]);
Item item = Registry.ITEM.getOptional(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});
return new ItemStack(item, Integer.valueOf(parts[2]));
}
catch (Exception ex) {
BCLib.LOGGER.error("ItemStack deserialization error!", ex);
}
return null;
}
@Nullable
public static ItemStack fromJsonRecipe(JsonObject recipe) {
try {
if (!recipe.has("item")) {
throw new IllegalStateException("Invalid JsonObject. Entry 'item' does not exists!");
}
ResourceLocation itemId = new ResourceLocation(GsonHelper.getAsString(recipe, "item"));
Item item = Registry.ITEM.getOptional(itemId).orElseThrow(() -> {
return new IllegalStateException("Output item " + itemId + " does not exists!");
});
int count = GsonHelper.getAsInt(recipe, "count", 1);
return new ItemStack(item, count);
}
catch (Exception ex) {
BCLib.LOGGER.error("ItemStack deserialization error!", ex);
}
return null;
}
}

View file

@ -1,22 +1,35 @@
package ru.bclib.util;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.VersionParsingException;
import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModDependency;
import net.fabricmc.loader.api.metadata.ModEnvironment;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.impl.metadata.ModMetadataParser;
import net.fabricmc.loader.impl.metadata.ParseMetadataException;
import net.fabricmc.loader.api.metadata.Person;
import net.fabricmc.loader.util.version.SemanticVersionImpl;
import org.apache.logging.log4j.LogManager;
import ru.bclib.BCLib;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
@ -59,17 +72,19 @@ public class ModUtil {
Path modMetaFile = fs.getPath("fabric.mod.json");
if (modMetaFile != null) {
try (InputStream is = Files.newInputStream(modMetaFile)) {
ModMetadata mc = ModMetadataParser.parseMetadata(is, uri.toString(), new LinkedList<String>());
mods.put(mc.getId(), new ModInfo(mc, file));
//ModMetadata mc = ModMetadataParser.parseMetadata(is, uri.toString(), new LinkedList<String>());
ModMetadata mc = readJSON(is, uri.toString());
if (mc!=null){
mods.put(mc.getId(), new ModInfo(mc, file));
}
}
}
} catch (ParseMetadataException e) {
} catch (Exception e) {
BCLib.LOGGER.error(e.getMessage());
}
}
}
catch (IOException e) {
catch (Exception e) {
BCLib.LOGGER.error(e.getMessage());
}
}));
@ -77,6 +92,176 @@ public class ModUtil {
return mods;
}
private static ModMetadata readJSON(InputStream is, String sourceFile) throws IOException {
try (com.google.gson.stream.JsonReader reader = new JsonReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
JsonObject data = new JsonParser().parse(reader).getAsJsonObject();
Version ver;
try {
ver = new SemanticVersionImpl(data.get("version").getAsString(), false);
}
catch (VersionParsingException e) {
BCLib.LOGGER.error("Unable to parse Version in " + sourceFile);
return null;
}
if (data.get("id") == null){
BCLib.LOGGER.error("Unable to read ID in " + sourceFile);
return null;
}
if (data.get("name") == null){
BCLib.LOGGER.error("Unable to read name in " + sourceFile);
return null;
}
return new ModMetadata() {
@Override
public Version getVersion() {
return ver;
}
@Override
public String getType() {
return "fabric";
}
@Override
public String getId() {
return data.get("id").getAsString();
}
@Override
public Collection<String> getProvides() {
return new ArrayList<>();
}
@Override
public ModEnvironment getEnvironment() {
JsonElement env = data.get("environment");
if (env==null) {
BCLib.LOGGER.error("No environment specified in " + sourceFile);
return ModEnvironment.UNIVERSAL;
}
final String environment = env.getAsString().toLowerCase(Locale.ROOT);
if (environment.isEmpty() || environment.equals("*") || environment.equals("common")) {
JsonElement entrypoints = data.get("entrypoints");
boolean hasClient = true;
//check if there is an actual client entrypoint
if (entrypoints!=null && entrypoints.isJsonObject()){
JsonElement client = entrypoints.getAsJsonObject().get("client");
if (client!=null && client.isJsonArray()){
hasClient = client.getAsJsonArray().size() > 0;
} else if (client==null || !client.isJsonPrimitive()){
hasClient = false;
} else if (!client.getAsJsonPrimitive().isString()){
hasClient = false;
}
}
if (hasClient == false) return ModEnvironment.SERVER;
return ModEnvironment.UNIVERSAL;
} else if (environment.equals("client")) {
return ModEnvironment.CLIENT;
} else if (environment.equals("server")) {
return ModEnvironment.SERVER;
} else {
BCLib.LOGGER.error("Unable to read environment in " + sourceFile);
return ModEnvironment.UNIVERSAL;
}
}
@Override
public Collection<ModDependency> getDepends() {
return new ArrayList<>();
}
@Override
public Collection<ModDependency> getRecommends() {
return new ArrayList<>();
}
@Override
public Collection<ModDependency> getSuggests() {
return new ArrayList<>();
}
@Override
public Collection<ModDependency> getConflicts() {
return new ArrayList<>();
}
@Override
public Collection<ModDependency> getBreaks() {
return new ArrayList<>();
}
public Collection<ModDependency> getDependencies() {
return new ArrayList<>();
}
@Override
public String getName() {
return data.get("name").getAsString();
}
@Override
public String getDescription() {
return "";
}
@Override
public Collection<Person> getAuthors() {
return new ArrayList<>();
}
@Override
public Collection<Person> getContributors() {
return new ArrayList<>();
}
@Override
public ContactInformation getContact() {
return null;
}
@Override
public Collection<String> getLicense() {
return new ArrayList<>();
}
@Override
public Optional<String> getIconPath(int size) {
return Optional.empty();
}
@Override
public boolean containsCustomValue(String key) {
return false;
}
@Override
public CustomValue getCustomValue(String key) {
return null;
}
@Override
public Map<String, CustomValue> getCustomValues() {
return new HashMap<>();
}
@Override
public boolean containsCustomElement(String key) {
return false;
}
public JsonElement getCustomElement(String key) {
return null;
}
};
}
}
/**
* Returns the {@link ModInfo} or {@code null} if the mod was not found.
* <p>

View file

@ -0,0 +1,25 @@
package ru.bclib.util;
import net.minecraft.core.Registry;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
public class RecipeHelper {
public static boolean exists(ItemLike item) {
if (item instanceof Block) {
return Registry.BLOCK.getKey((Block) item) != Registry.BLOCK.getDefaultKey();
}
else {
return Registry.ITEM.getKey(item.asItem()) != Registry.ITEM.getDefaultKey();
}
}
public static boolean exists(ItemLike... items) {
for (ItemLike item : items) {
if (!exists(item)) {
return false;
}
}
return true;
}
}

View file

@ -30,7 +30,7 @@ public class TranslationHelper {
Gson gson = new Gson();
InputStream inputStream = TranslationHelper.class.getResourceAsStream("/assets/" + modID + "/lang/" + languageCode + ".json");
JsonObject translation = gson.fromJson(new InputStreamReader(inputStream), JsonObject.class);
JsonObject translation = inputStream == null ? new JsonObject() : gson.fromJson(new InputStreamReader(inputStream), JsonObject.class);
Registry.BLOCK.forEach(block -> {
if (Registry.BLOCK.getKey(block).getNamespace().equals(modID)) {

View file

@ -1,6 +0,0 @@
package ru.bclib.util;
@FunctionalInterface
public interface TriConsumer<A, B, C> {
void accept(A a, B b, C c);
}