feat: add manifest, export and import record

This commit is contained in:
Discreater committed 2021-05-01 20:01:28 +08:00
1 parent fa3ba7542c
commit 0e7c1b4f73
39 files changed
+922 -263

No files matched your search

@@ -1,6 +1,5 @@
package me.towdium.jecalculation.data;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.JecaConfig;
@@ -8,9 +7,9 @@ import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.data.structure.Recents;
import me.towdium.jecalculation.data.structure.Recipe;
import me.towdium.jecalculation.data.structure.Recipes;
import me.towdium.jecalculation.utils.wrappers.Triple;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.InventoryPlayer;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import me.towdium.jecalculation.utils.wrappers.Trio;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
@@ -19,10 +18,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -33,8 +29,10 @@ import java.util.stream.Collectors;
public class Controller {
public static final String KEY_RECIPES = "recipes";
public static final String KEY_RECENTS = "recents";
public static final String KEY_AMOUNT = "amount";
static Recipes recipesClient;
static Recents recentsClient;
static String amountClient;
static Recipes getRecord() {
return recipesClient;
@@ -44,12 +42,40 @@ public class Controller {
return Optional.empty();
}
// file, recipes
public static List<Pair<String, Recipes>> discover() {
File dir = JecaConfig.dataDir;
File[] fs = dir.listFiles();
if (fs == null) return new ArrayList<>();
return Arrays.stream(fs)
.map(i -> new Pair<>(i.getName(), new Recipes(Utilities.Json.read(i))))
.collect(Collectors.toList());
}
public static void inport(Recipes recipes, String group) {
ArrayList<Recipe> buffer = new ArrayList<>();
recipes.flatStream(group).forEach(i ->
getRecord().flatStream(group).filter(j -> j.one.equals(i.one)).findAny().orElseGet(() -> {
buffer.add(i.one);
return null;
}));
for (Recipe r : buffer) addRecipe(group, r);
}
public static File export(String group) {
File f = JecaConfig.getDataFile(group);
Utilities.Json.write(getRecord().serialize(Collections.singleton(group)), f);
return f;
}
public static File export() {
File f = JecaConfig.getDataFile("groups");
Utilities.Json.write(getRecord().serialize(), f);
return f;
}
public static List<String> getGroups() {
Recipes user = getRecord();
if (user.size() != 0)
return user.stream().map(Map.Entry::getKey).collect(Collectors.toList());
else
return new ArrayList<>();
return getRecord().getGroups();
}
public static void addRecipe(String group, Recipe recipe) {
@@ -71,11 +97,11 @@ public class Controller {
return getRecord().getRecipe(group, index);
}
public static List<Triple<Recipe, String, Integer>> getRecipes() {
public static List<Trio<Recipe, String, Integer>> getRecipes() {
return getRecord().getRecipes();
}
public static List<Triple<Recipe, String, Integer>> getRecipes(String group) {
public static List<Trio<Recipe, String, Integer>> getRecipes(String group) {
return getRecord().getRecipes(group);
}
@@ -83,6 +109,15 @@ public class Controller {
return getRecord().getRecipe(label);
}
public static String getAmount() {
return amountClient;
}
public static void setAmount(String amount) {
amountClient = amount;
}
public static List<ILabel> getRecent() {
return recentsClient.getRecords();
}
@@ -93,29 +128,29 @@ public class Controller {
}
public static void loadFromLocal() {
try {
File file = JecaConfig.recordFile;
FileInputStream stream = new FileInputStream(file);
NBTTagCompound nbt = CompressedStreamTools.readCompressed(stream);
recipesClient = nbt.hasKey(KEY_RECIPES) ? new Recipes(nbt.getTagList(KEY_RECIPES, 10)) : new Recipes();
//noinspection ResultOfMethodCallIgnored
JecaConfig.dataDir.mkdirs();
File file = JecaConfig.recordFile;
NBTTagCompound nbt = Utilities.Json.read(file);
if (nbt != null) {
recipesClient = nbt.hasKey(KEY_RECIPES) ? new Recipes(nbt.getCompoundTag(KEY_RECIPES)) : new Recipes();
recentsClient = nbt.hasKey(KEY_RECENTS) ? new Recents(nbt.getTagList(KEY_RECENTS, 10)) : new Recents();
} catch (IOException e) {
e.printStackTrace();
recipesClient = new Recipes();
recentsClient = new Recents();
amountClient = nbt.hasKey(KEY_AMOUNT) ? nbt.getString(KEY_AMOUNT) : "";
return;
}
file = JecaConfig.defaultFile;
nbt = Utilities.Json.read(file);
recipesClient = nbt == null ? new Recipes() : new Recipes(nbt);
recentsClient = new Recents();
amountClient = "";
}
public static void writeToLocal() {
try {
File file = JecaConfig.recordFile;
NBTTagCompound nbt = new NBTTagCompound();
nbt.setTag(KEY_RECENTS, recentsClient.serialize());
nbt.setTag(KEY_RECIPES, recipesClient.serialize());
FileOutputStream stream = new FileOutputStream(file);
CompressedStreamTools.writeCompressed(nbt, stream);
} catch (IOException e) {
e.printStackTrace();
}
File file = JecaConfig.recordFile;
NBTTagCompound nbt = new NBTTagCompound();
nbt.setTag(KEY_RECENTS, recentsClient.serialize());
nbt.setTag(KEY_RECIPES, recipesClient.serialize());
nbt.setString(KEY_AMOUNT, amountClient);
Utilities.Json.write(nbt, file);
}
}