mirror of
https://github.com/GTNewHorizons/JustEnoughCalculation.git
synced 2026-11-14 01:04:06 +00:00
161 lines
5.5 KiB
Java
161 lines
5.5 KiB
Java
package me.towdium.jecalculation.data.structure;
|
|
|
|
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 net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.IntStream;
|
|
import java.util.stream.Stream;
|
|
import java.util.stream.StreamSupport;
|
|
|
|
/**
|
|
* Author: towdium
|
|
* Date: 17-10-15.
|
|
*/
|
|
public class User {
|
|
public static final String KEY_RECIPE = "recipe";
|
|
public static final String KEY_RECENT = "recent";
|
|
|
|
public Recipes recipes;
|
|
public Recent recent; // client mode only
|
|
|
|
public User() {
|
|
recipes = new Recipes();
|
|
recent = new Recent();
|
|
}
|
|
|
|
public User(NBTTagCompound nbt) {
|
|
recipes = new Recipes(nbt.getTagList(KEY_RECIPE, 10));
|
|
recent = new Recent(nbt.getTagList(KEY_RECENT, 10));
|
|
}
|
|
|
|
public NBTTagCompound serialize() {
|
|
NBTTagCompound ret = new NBTTagCompound();
|
|
ret.setTag(KEY_RECIPE, recipes.serialize());
|
|
ret.setTag(KEY_RECENT, recent.serialize());
|
|
return ret;
|
|
}
|
|
|
|
public static class Recipes {
|
|
public static final String KEY_NAME = "name";
|
|
public static final String KEY_CONTENT = "content";
|
|
|
|
Utilities.OrderedHashMap<String, List<Recipe>> records = new Utilities.OrderedHashMap<>();
|
|
|
|
public Recipes() {
|
|
}
|
|
|
|
public Recipes(NBTTagList nbt) {
|
|
StreamSupport.stream(NBTHelper.spliterator(nbt), false).filter(g -> g instanceof NBTTagCompound)
|
|
.forEach(g -> {
|
|
NBTTagCompound group = (NBTTagCompound) g;
|
|
String name = group.getString(KEY_NAME);
|
|
NBTTagList recipes = group.getTagList(KEY_CONTENT, 10);
|
|
StreamSupport.stream(NBTHelper.spliterator(recipes), false)
|
|
.filter(r -> r instanceof NBTTagCompound).map(r -> (NBTTagCompound) r)
|
|
.forEach(r -> add(name, new Recipe(r)));
|
|
});
|
|
}
|
|
|
|
public void add(String group, Recipe recipe) {
|
|
records.get(group).orElseGet(() -> {
|
|
ArrayList<Recipe> ret = new ArrayList<>();
|
|
records.put(group, ret);
|
|
return ret;
|
|
}).add(recipe);
|
|
}
|
|
|
|
public void set(String group, int index, Recipe recipe) {
|
|
records.get(group).orElseGet(() -> {
|
|
throw new RuntimeException("Group not found: " + group + ".");
|
|
}).set(index, recipe);
|
|
}
|
|
|
|
public int size() {
|
|
return records.size();
|
|
}
|
|
|
|
public Stream<Pair<String, List<Recipe>>> stream() {
|
|
return records.stream();
|
|
}
|
|
|
|
public void remove(String group, int index) {
|
|
records.get(group).orElseGet(() -> {
|
|
throw new RuntimeException("Group not found: " + group + ".");
|
|
}).remove(index);
|
|
}
|
|
|
|
public List<Recipe> search(String group, ILabel label, Recipe.enumIoType type) {
|
|
HashSet<Recipe> set = new HashSet<>();
|
|
IntStream.range(0, type.getSize()).forEach(i -> get(group).ifPresent(rs -> rs.forEach(r -> {
|
|
ILabel.MERGER.merge(label, r.getLabel(type)[i], true).ifPresent(l -> set.add(r));
|
|
})));
|
|
return new ArrayList<>(set);
|
|
}
|
|
|
|
public void forEach(BiConsumer<String, List<Recipe>> consumer) {
|
|
records.forEach(consumer);
|
|
}
|
|
|
|
public Optional<List<Recipe>> get(String group) {
|
|
return records.get(group);
|
|
}
|
|
|
|
public NBTTagList serialize() {
|
|
NBTTagList ret = new NBTTagList();
|
|
forEach((n, rs) -> {
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
nbt.setString(KEY_NAME, n);
|
|
NBTTagList l = new NBTTagList();
|
|
rs.forEach(r -> l.appendTag(r.serialize()));
|
|
nbt.setTag(KEY_CONTENT, l);
|
|
ret.appendTag(nbt);
|
|
});
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
public static class Recent {
|
|
Utilities.Recent<ILabel> record = new Utilities.Recent<>(9);
|
|
public static final String IDENTIFIER = "recent";
|
|
|
|
public Recent(NBTTagList nbt) {
|
|
List<ILabel> ls = StreamSupport.stream(NBTHelper.spliterator(nbt), false)
|
|
.filter(n -> n instanceof NBTTagCompound)
|
|
.map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n))
|
|
.collect(Collectors.toList());
|
|
new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l));
|
|
}
|
|
|
|
public Recent() {
|
|
}
|
|
|
|
public void push(ILabel label) {
|
|
record.push(label);
|
|
}
|
|
|
|
public ILabel getLatest() {
|
|
return record.toList().get(0);
|
|
}
|
|
|
|
public List<ILabel> getRecords() {
|
|
return record.toList();
|
|
}
|
|
|
|
public NBTTagList serialize() {
|
|
NBTTagList ret = new NBTTagList();
|
|
record.toList().forEach(l -> ret.appendTag(ILabel.DESERIALIZER.serialize(l)));
|
|
return ret;
|
|
}
|
|
}
|
|
}
|