DefaultCustomDisplay.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.plugin.crafting;
  24. import com.google.common.collect.Lists;
  25. import me.shedaniel.rei.api.EntryStack;
  26. import me.shedaniel.rei.utils.CollectionUtils;
  27. import net.fabricmc.api.EnvType;
  28. import net.fabricmc.api.Environment;
  29. import net.minecraft.item.ItemStack;
  30. import net.minecraft.recipe.Recipe;
  31. import net.minecraft.util.Identifier;
  32. import java.util.Collections;
  33. import java.util.List;
  34. import java.util.Optional;
  35. @Environment(EnvType.CLIENT)
  36. public class DefaultCustomDisplay implements DefaultCraftingDisplay {
  37. private List<List<EntryStack>> input;
  38. private List<EntryStack> output;
  39. private Recipe<?> possibleRecipe;
  40. private int width, height;
  41. public DefaultCustomDisplay(List<List<ItemStack>> input, List<ItemStack> output, Recipe<?> possibleRecipe) {
  42. this(possibleRecipe, CollectionUtils.map(input, EntryStack::ofItemStacks), EntryStack.ofItemStacks(output));
  43. }
  44. public DefaultCustomDisplay(Recipe<?> possibleRecipe, List<List<EntryStack>> input, List<EntryStack> output) {
  45. this.input = input;
  46. this.output = output;
  47. this.possibleRecipe = possibleRecipe;
  48. List<Boolean> row = Lists.newArrayList(false, false, false);
  49. List<Boolean> column = Lists.newArrayList(false, false, false);
  50. for (int i = 0; i < 9; i++)
  51. if (i < this.input.size()) {
  52. List<EntryStack> stacks = this.input.get(i);
  53. if (stacks.stream().anyMatch(stack -> !stack.isEmpty())) {
  54. row.set((i - (i % 3)) / 3, true);
  55. column.set(i % 3, true);
  56. }
  57. }
  58. this.width = (int) column.stream().filter(Boolean::booleanValue).count();
  59. this.height = (int) row.stream().filter(Boolean::booleanValue).count();
  60. }
  61. public DefaultCustomDisplay(List<List<ItemStack>> input, List<ItemStack> output) {
  62. this(input, output, null);
  63. }
  64. protected Optional<Recipe<?>> getRecipe() {
  65. return Optional.ofNullable(possibleRecipe);
  66. }
  67. @Override
  68. public Optional<Identifier> getRecipeLocation() {
  69. return getRecipe().map(Recipe::getId);
  70. }
  71. @Override
  72. public List<List<EntryStack>> getInputEntries() {
  73. return input;
  74. }
  75. @Override
  76. public List<List<EntryStack>> getResultingEntries() {
  77. return Collections.singletonList(output);
  78. }
  79. @Override
  80. public List<List<EntryStack>> getRequiredEntries() {
  81. return input;
  82. }
  83. @Override
  84. public int getWidth() {
  85. return width;
  86. }
  87. @Override
  88. public int getHeight() {
  89. return height;
  90. }
  91. @Override
  92. public Optional<Recipe<?>> getOptionalRecipe() {
  93. return Optional.ofNullable(possibleRecipe);
  94. }
  95. }