mirror of
https://github.com/GTNewHorizons/JustEnoughCalculation.git
synced 2026-11-26 06:42:55 +00:00
sync
This commit is contained in:
1 parent
cd92230f07
commit
de1c2abe7b
52 files changed
+1046
-643
No files matched your search
@@ -1,64 +1,62 @@
|
||||
package me.towdium.jecalculation.data.structure;
|
||||
|
||||
import me.towdium.jecalculation.data.label.ILabel;
|
||||
import me.towdium.jecalculation.polyfill.MethodsReturnNonnullByDefault;
|
||||
import me.towdium.jecalculation.polyfill.NBTHelper;
|
||||
import me.towdium.jecalculation.utils.IllegalPositionException;
|
||||
import me.towdium.jecalculation.utils.Utilities;
|
||||
import me.towdium.jecalculation.utils.wrappers.Wrapper;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
/**
|
||||
* Author: towdium
|
||||
* Date: 17-10-6.
|
||||
*/
|
||||
@MethodsReturnNonnullByDefault
|
||||
@ParametersAreNonnullByDefault
|
||||
public class Recipe {
|
||||
public static final String KEY_INPUT = "input";
|
||||
public static final String KEY_CATALYST = "catalyst";
|
||||
public static final String KEY_OUTPUT = "output";
|
||||
static final ILabel[] EMPTY_ARRAY = new ILabel[0];
|
||||
ILabel[] input;
|
||||
ILabel[] catalyst;
|
||||
ILabel[] output;
|
||||
List<ILabel> input;
|
||||
List<ILabel> catalyst;
|
||||
List<ILabel> output;
|
||||
|
||||
public Recipe(NBTTagCompound nbt) {
|
||||
this(readNbtList(nbt.getTagList(KEY_INPUT, 10)),
|
||||
readNbtList(nbt.getTagList(KEY_CATALYST, 10)),
|
||||
this(readNbtList(nbt.getTagList(KEY_INPUT, 10)), readNbtList(nbt.getTagList(KEY_CATALYST, 10)),
|
||||
readNbtList(nbt.getTagList(KEY_OUTPUT, 10)));
|
||||
}
|
||||
|
||||
public Recipe(List<ILabel> input, List<ILabel> catalyst, List<ILabel> output) {
|
||||
this(input.toArray(EMPTY_ARRAY), catalyst.toArray(EMPTY_ARRAY), output.toArray(EMPTY_ARRAY));
|
||||
}
|
||||
|
||||
public Recipe(ILabel[] input, ILabel[] catalyst, ILabel[] output) {
|
||||
BiFunction<ILabel[], Integer, ILabel[]> convert = (ls, i) -> {
|
||||
ILabel[] ret = new ILabel[i];
|
||||
if (ls.length > i) throw new RuntimeException("Too many labels");
|
||||
System.arraycopy(ls, 0, ret, 0, ls.length);
|
||||
for (int j = ls.length; j < i; j++) ret[j] = ILabel.EMPTY;
|
||||
return ret;
|
||||
Consumer<List<ILabel>> check = ls -> {
|
||||
boolean ret = false;
|
||||
for (ILabel i : ls) {
|
||||
Objects.requireNonNull(i);
|
||||
if (i != ILabel.EMPTY)
|
||||
ret = true;
|
||||
}
|
||||
if (!ret || ls.get(ls.size() - 1) == ILabel.EMPTY)
|
||||
throw new IllegalArgumentException("Invalid recipe");
|
||||
};
|
||||
this.input = convert.apply(input, 14);
|
||||
this.catalyst = convert.apply(catalyst, 7);
|
||||
this.output = convert.apply(output, 7);
|
||||
Stream.of(input, output).forEach(i -> Arrays.stream(i).filter(j -> j != ILabel.EMPTY).findAny()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid recipe")));
|
||||
check.accept(input);
|
||||
check.accept(catalyst);
|
||||
check.accept(output);
|
||||
this.input = input;
|
||||
this.catalyst = catalyst;
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static private List<ILabel> readNbtList(NBTTagList list) {
|
||||
return StreamSupport.stream(NBTHelper.spliterator(list), false)
|
||||
.filter(n -> n instanceof NBTTagCompound)
|
||||
@@ -68,29 +66,37 @@ public class Recipe {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
Wrapper<Integer> hash = new Wrapper<>(0);
|
||||
Consumer<ILabel[]> hasher = (ls) -> Arrays.stream(ls)
|
||||
.filter(Objects::nonNull).forEach(i -> hash.value ^= i.hashCode());
|
||||
int[] hash = new int[1];
|
||||
Consumer<List<ILabel>> hasher = (ls) -> ls.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(i -> hash[0] ^= i.hashCode());
|
||||
hasher.accept(input);
|
||||
hasher.accept(catalyst);
|
||||
hasher.accept(output);
|
||||
return hash.value;
|
||||
return hash[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Recipe)) return false;
|
||||
if (!(obj instanceof Recipe))
|
||||
return false;
|
||||
Recipe r = (Recipe) obj;
|
||||
BiPredicate<ILabel[], ILabel[]> p = (i, j) -> {
|
||||
if (i.length != j.length) return false;
|
||||
for (int k = 0; k < i.length; k++)
|
||||
if (!i[k].equals(j[k])) return false;
|
||||
BiPredicate<List<ILabel>, List<ILabel>> p = (i, j) -> {
|
||||
if (i.size() != j.size())
|
||||
return false;
|
||||
for (int k = 0; k < i.size(); k++)
|
||||
if (!i.get(k).equals(j.get(k)))
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
return p.test(input, r.input) && p.test(catalyst, r.catalyst) && p.test(output, r.output);
|
||||
}
|
||||
|
||||
public ILabel[] getLabel(IO type) {
|
||||
public List<ILabel> getLabel(IO type) {
|
||||
return get(type, input, output, catalyst);
|
||||
}
|
||||
|
||||
public static <T> T get(IO type, T input, T output, T catalyst) {
|
||||
switch (type) {
|
||||
case INPUT:
|
||||
return input;
|
||||
@@ -99,31 +105,31 @@ public class Recipe {
|
||||
case CATALYST:
|
||||
return catalyst;
|
||||
default:
|
||||
throw new IllegalPositionException();
|
||||
throw new RuntimeException("Internal error");
|
||||
}
|
||||
}
|
||||
|
||||
public ILabel getRep() {
|
||||
for (int i = 0; i < 8; i++)
|
||||
if (output[i] != ILabel.EMPTY) return output[i];
|
||||
if (output.get(i) != ILabel.EMPTY)
|
||||
return output.get(i);
|
||||
return ILabel.EMPTY;
|
||||
}
|
||||
|
||||
public NBTTagCompound serialize() {
|
||||
NBTTagCompound ret = new NBTTagCompound();
|
||||
Function<ILabel[], NBTTagList> convert = (ls) -> {
|
||||
Function<List<ILabel>, NBTTagList> convert = (ls) -> {
|
||||
ArrayList<ILabel> labels = new ArrayList<>();
|
||||
boolean start = false;
|
||||
for (int i = ls.length - 1; i >= 0; i--) {
|
||||
if (start || ls[i] != ILabel.EMPTY) {
|
||||
labels.add(ls[i]);
|
||||
for (int i = ls.size() - 1; i >= 0; i--) {
|
||||
if (start || ls.get(i) != ILabel.EMPTY) {
|
||||
labels.add(ls.get(i));
|
||||
start = true;
|
||||
}
|
||||
}
|
||||
|
||||
NBTTagList r = new NBTTagList();
|
||||
new Utilities.ReversedIterator<>(labels).stream()
|
||||
.forEach(l -> r.appendTag(ILabel.SERIALIZER.serialize(l)));
|
||||
new Utilities.ReversedIterator<>(labels).stream().forEach(l -> r.appendTag(ILabel.SERIALIZER.serialize(l)));
|
||||
return r;
|
||||
};
|
||||
ret.setTag(KEY_INPUT, convert.apply(input));
|
||||
@@ -133,18 +139,19 @@ public class Recipe {
|
||||
}
|
||||
|
||||
public Optional<ILabel> matches(ILabel label) {
|
||||
return Arrays.stream(output).filter(i -> ILabel.MERGER.merge(label, i).isPresent()).findAny();
|
||||
return output.stream().filter(i -> ILabel.MERGER.merge(label, i).isPresent()).findAny();
|
||||
}
|
||||
|
||||
public long multiplier(ILabel label) {
|
||||
return Arrays.stream(output).filter(i -> ILabel.MERGER.merge(label, i).isPresent()).findAny()
|
||||
.map(i -> {
|
||||
long amountA = label.getAmount();
|
||||
if (!label.isPercent()) amountA = Math.multiplyExact(amountA, 100L);
|
||||
long amountB = i.getAmount();
|
||||
if (!i.isPercent()) amountB = Math.multiplyExact(amountB, 100L);
|
||||
return (amountB + Math.abs(amountA) - 1) / amountB;
|
||||
}).orElse(0L);
|
||||
return output.stream().filter(i -> ILabel.MERGER.merge(label, i).isPresent()).findAny().map(i -> {
|
||||
long amountA = label.getAmount();
|
||||
if (!label.isPercent())
|
||||
amountA = Math.multiplyExact(amountA, 100L);
|
||||
long amountB = i.getAmount();
|
||||
if (!i.isPercent())
|
||||
amountB = Math.multiplyExact(amountB, 100L);
|
||||
return (amountB + Math.abs(amountA) - 1) / amountB;
|
||||
}).orElse(0L);
|
||||
}
|
||||
|
||||
public enum IO {
|
||||
|
||||
Reference in new issue
Block a user