This commit is contained in:
Discreater committed 2021-05-01 22:34:55 +08:00
1 parent d6ead4a4e9
commit 1c05e9d154
37 files changed
+763 -474

No files matched your search

@@ -1,7 +1,10 @@
package me.towdium.jecalculation.data.structure;
import me.towdium.jecalculation.JecaConfig;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.polyfill.NBTHelper;
import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import me.towdium.jecalculation.utils.wrappers.Trio;
import net.minecraft.nbt.NBTTagCompound;
@@ -9,6 +12,7 @@ import net.minecraft.nbt.NBTTagList;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@@ -19,32 +23,41 @@ import java.util.stream.StreamSupport;
@ParametersAreNonnullByDefault
public class Recipes {
LinkedHashMap<String, List<Recipe>> records = new LinkedHashMap<>();
HashSet<Recipe> cache = new HashSet<>();
public Recipes() {
File file = JecaConfig.defaultFile;
NBTTagCompound nbt = Utilities.Json.read(file);
if (nbt == null) JustEnoughCalculation.logger.info("Failed to load default records at " + file + ".");
else {
JustEnoughCalculation.logger.info("Loading default records at " + file + ".");
deserialize(nbt);
}
}
public Recipes(NBTTagCompound nbt) {
deserialize(nbt);
}
public void deserialize(NBTTagCompound nbt) {
protected void deserialize(NBTTagCompound nbt) {
Set<String> keySet = (Set<String>) nbt.func_150296_c();
keySet.stream().sorted().forEach(i -> {
NBTTagList group = nbt.getTagList(i, 10);
StreamSupport.stream(NBTHelper.spliterator(group), false)
.filter(r -> r instanceof NBTTagCompound)
.forEach(r -> add(i, new Recipe((NBTTagCompound) r)));
.forEach(r -> {
try {
add(i, new Recipe((NBTTagCompound) r));
} catch (IllegalArgumentException e) {
JustEnoughCalculation.logger.warn("Invalid recipe record :" + r);
}
});
});
}
public void add(String group, Recipe recipe) {
if (records.containsKey(group)) {
records.get(group).add(recipe);
} else {
ArrayList<Recipe> l = new ArrayList<>();
l.add(recipe);
records.put(group, l);
}
records.computeIfAbsent(group, k -> new ArrayList<>()).add(recipe);
cache.add(recipe);
}
public void modify(String group, int index, @Nullable Recipe recipe) {
@@ -54,7 +67,9 @@ public class Recipes {
}
public void set(String group, int index, Recipe recipe) {
records.get(group).set(index, recipe);
Recipe r = records.get(group).set(index, recipe);
cache.remove(r);
cache.add(recipe);
}
public int size() {
@@ -67,7 +82,7 @@ public class Recipes {
public void remove(String group, int index) {
List<Recipe> l = records.get(group);
l.remove(index);
cache.remove(l.remove(index));
if (l.isEmpty()) records.remove(group);
}
@@ -87,6 +102,12 @@ public class Recipes {
records.forEach(consumer);
}
/**
* Do not modify return value!
*
* @param group Name of group to get
* @return List of Recipes in group
*/
public List<Recipe> getGroup(String group) {
return records.get(group);
}
@@ -132,5 +153,13 @@ public class Recipes {
while (j == null || !j.hasNext()) if (i.hasNext()) j = i.next().getValue().iterator();
return j.next();
}
public Stream<Recipe> stream() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED), false);
}
}
public boolean hasDuplicate(Recipe r) {
return cache.contains(r);
}
}