aboutsummaryrefslogtreecommitdiff
path: root/RoughlyEnoughItems-api/src/main/java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2020-08-05 17:14:07 +0800
committershedaniel <daniel@shedaniel.me>2020-08-05 17:14:07 +0800
commit8a2df51193fd3db145f3b1ace974e9e0a0bc09a5 (patch)
tree894244ea7e84d9bd78748e4e295e528cfc9e7979 /RoughlyEnoughItems-api/src/main/java
parent1ce5ca77bc2f98df6cc370c96547c2c39c1fd897 (diff)
downloadRoughlyEnoughItems-8a2df51193fd3db145f3b1ace974e9e0a0bc09a5.tar.gz
RoughlyEnoughItems-8a2df51193fd3db145f3b1ace974e9e0a0bc09a5.tar.bz2
RoughlyEnoughItems-8a2df51193fd3db145f3b1ace974e9e0a0bc09a5.zip
REI 5.1.0:
- Improve auto crafting, now able to detect things on the grid - Introduce getResultingEntries as a replacement for getOutputEntries, it can now handle lists of lists of stacks - Caching for fluid hashing - Full fractions support for simple recipe display - Made searchFavorites defaulted false Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'RoughlyEnoughItems-api/src/main/java')
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeCategory.java2
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java15
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/gui/entries/SimpleRecipeEntry.java79
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/server/RecipeFinder.java4
4 files changed, 68 insertions, 32 deletions
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeCategory.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeCategory.java
index 1821658c7..809a7e171 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeCategory.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeCategory.java
@@ -70,7 +70,7 @@ public interface RecipeCategory<T extends RecipeDisplay> {
*/
@ApiStatus.OverrideOnly
default RecipeEntry getSimpleRenderer(T recipe) {
- return SimpleRecipeEntry.create(recipe::getInputEntries, recipe::getOutputEntries);
+ return SimpleRecipeEntry.from(recipe::getInputEntries, recipe::getResultingEntries);
}
/**
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java
index e215d5f7c..32177e121 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java
@@ -23,7 +23,9 @@
package me.shedaniel.rei.api;
+import me.shedaniel.rei.utils.CollectionUtils;
import net.minecraft.util.Identifier;
+import org.jetbrains.annotations.ApiStatus;
import java.util.Collections;
import java.util.List;
@@ -39,7 +41,18 @@ public interface RecipeDisplay {
/**
* @return a list of outputs
*/
- List<EntryStack> getOutputEntries();
+ @Deprecated
+ @ApiStatus.ScheduledForRemoval
+ default List<EntryStack> getOutputEntries() {
+ return Collections.emptyList();
+ }
+
+ /**
+ * @return a list of outputs
+ */
+ default List<List<EntryStack>> getResultingEntries() {
+ return CollectionUtils.map(getOutputEntries(), Collections::singletonList);
+ }
/**
* Gets the required items used in craftable filters
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/gui/entries/SimpleRecipeEntry.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/gui/entries/SimpleRecipeEntry.java
index b4c248693..f563d9407 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/gui/entries/SimpleRecipeEntry.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/gui/entries/SimpleRecipeEntry.java
@@ -23,10 +23,11 @@
package me.shedaniel.rei.gui.entries;
-import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import me.shedaniel.math.Point;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.EntryStack;
+import me.shedaniel.rei.api.fractions.Fraction;
import me.shedaniel.rei.api.widgets.Slot;
import me.shedaniel.rei.api.widgets.Tooltip;
import me.shedaniel.rei.api.widgets.Widgets;
@@ -34,14 +35,15 @@ import me.shedaniel.rei.utils.CollectionUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
-import net.minecraft.util.Pair;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
+import java.util.Collections;
import java.util.Comparator;
import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -50,35 +52,51 @@ public class SimpleRecipeEntry extends RecipeEntry {
private static final Comparator<EntryStack> ENTRY_COMPARATOR = Comparator.comparingLong(EntryStack::hashCode);
private static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
private List<Slot> inputWidgets;
- private Slot outputWidget;
+ private List<Slot> outputWidgets;
@ApiStatus.Internal
- protected SimpleRecipeEntry(List<List<EntryStack>> input, List<EntryStack> output) {
- List<Pair<List<EntryStack>, AtomicInteger>> newList = Lists.newArrayList();
- List<Pair<List<EntryStack>, Integer>> a = CollectionUtils.map(input, stacks -> new Pair<>(stacks, stacks.stream().map(EntryStack::getAmount).max(Integer::compareTo).orElse(1)));
- for (Pair<List<EntryStack>, Integer> pair : a) {
- Pair<List<EntryStack>, AtomicInteger> any = CollectionUtils.findFirstOrNull(newList, pairr -> equalsList(pair.getLeft(), pairr.getLeft()));
- if (any != null) {
- any.getRight().addAndGet(pair.getRight());
- } else
- newList.add(new Pair<>(pair.getLeft(), new AtomicInteger(pair.getRight())));
- }
- List<List<EntryStack>> b = Lists.newArrayList();
- for (Pair<List<EntryStack>, AtomicInteger> pair : newList)
- b.add(pair.getLeft().stream().map(stack -> {
- EntryStack s = stack.copy();
- s.setAmount(pair.getRight().get());
- return s;
- }).collect(Collectors.toList()));
- this.inputWidgets = b.stream().filter(stacks -> !stacks.isEmpty()).map(stacks -> Widgets.createSlot(new Point(0, 0)).entries(stacks).disableBackground().disableHighlight().disableTooltips()).collect(Collectors.toList());
- this.outputWidget = Widgets.createSlot(new Point(0, 0)).entries(CollectionUtils.filter(output, stack -> !stack.isEmpty())).disableBackground().disableHighlight().disableTooltips();
+ protected SimpleRecipeEntry(List<List<EntryStack>> input, List<List<EntryStack>> output) {
+ this.inputWidgets = simplify(input).stream().filter(stacks -> !stacks.isEmpty()).map(stacks -> Widgets.createSlot(new Point(0, 0)).entries(stacks).disableBackground().disableHighlight().disableTooltips()).collect(Collectors.toList());
+ this.outputWidgets = CollectionUtils.map(simplify(output), outputStacks ->
+ Widgets.createSlot(new Point(0, 0)).entries(CollectionUtils.filter(outputStacks, stack -> !stack.isEmpty())).disableBackground().disableHighlight().disableTooltips());
+ }
+
+ private static List<List<EntryStack>> simplify(List<List<EntryStack>> original) {
+ Map<List<EntryStack>, AtomicReference<Fraction>> inputCounter = Maps.newLinkedHashMap();
+ original.stream().collect(Collectors.groupingBy(stacks -> CollectionUtils.mapAndMax(stacks, EntryStack::getAccurateAmount, Fraction::compareTo).orElse(Fraction.empty())))
+ .forEach((fraction, value) -> {
+ if (!fraction.equals(Fraction.empty())) {
+ value.forEach(stackList -> {
+ List<EntryStack> stacks = inputCounter.keySet().stream().filter(s -> equalsList(stackList, s)).findFirst().orElse(stackList);
+ AtomicReference<Fraction> reference = inputCounter.computeIfAbsent(stacks, s -> new AtomicReference<>(Fraction.empty()));
+ reference.set(reference.get().add(fraction));
+ });
+ }
+ });
+ return inputCounter.entrySet().stream().map(entry -> CollectionUtils.map(entry.getKey(), stack -> {
+ EntryStack s = stack.copy();
+ s.setAmount(entry.getValue().get());
+ return s;
+ })).collect(Collectors.toList());
}
+ @Deprecated
+ @ApiStatus.ScheduledForRemoval
public static RecipeEntry create(Supplier<List<List<EntryStack>>> input, Supplier<List<EntryStack>> output) {
return create(input.get(), output.get());
}
+ public static RecipeEntry from(Supplier<List<List<EntryStack>>> input, Supplier<List<List<EntryStack>>> output) {
+ return from(input.get(), output.get());
+ }
+
+ @Deprecated
+ @ApiStatus.ScheduledForRemoval
public static RecipeEntry create(List<List<EntryStack>> input, List<EntryStack> output) {
+ return from(input, CollectionUtils.map(output, Collections::singletonList));
+ }
+
+ public static RecipeEntry from(List<List<EntryStack>> input, List<List<EntryStack>> output) {
return new SimpleRecipeEntry(input, output);
}
@@ -117,9 +135,12 @@ public class SimpleRecipeEntry extends RecipeEntry {
MinecraftClient.getInstance().getTextureManager().bindTexture(CHEST_GUI_TEXTURE);
drawTexture(matrices, xx, yy, 0, 28, 18, 18);
xx += 18;
- outputWidget.setZ(getZ() + 50);
- outputWidget.getBounds().setLocation(xx, yy);
- outputWidget.render(matrices, mouseX, mouseY, delta);
+ yy += outputWidgets.size() * -9 + 9;
+ for (Slot outputWidget : outputWidgets) {
+ outputWidget.setZ(getZ() + 50);
+ outputWidget.getBounds().setLocation(xx, yy);
+ outputWidget.render(matrices, mouseX, mouseY, delta);
+ }
}
@Nullable
@@ -129,8 +150,10 @@ public class SimpleRecipeEntry extends RecipeEntry {
if (widget.containsMouse(point))
return widget.getCurrentTooltip(point);
}
- if (outputWidget.containsMouse(point))
- return outputWidget.getCurrentTooltip(point);
+ for (Slot widget : outputWidgets) {
+ if (widget.containsMouse(point))
+ return widget.getCurrentTooltip(point);
+ }
return null;
}
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/server/RecipeFinder.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/server/RecipeFinder.java
index fb792805e..d9e0d429d 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/server/RecipeFinder.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/server/RecipeFinder.java
@@ -71,7 +71,7 @@ public class RecipeFinder {
}
- private boolean contains(int itemId) {
+ public boolean contains(int itemId) {
return this.idToAmountMap.get(itemId) > 0;
}
@@ -80,7 +80,7 @@ public class RecipeFinder {
*
* @return the amount taken
*/
- private int take(int itemId, int amount) {
+ public int take(int itemId, int amount) {
int mapAmount = this.idToAmountMap.get(itemId);
if (mapAmount >= amount) {
this.idToAmountMap.put(itemId, mapAmount - amount);