|
| 1 | +package turing.tmb.mixin.client; |
| 2 | + |
| 3 | +import net.minecraft.client.gui.container.ScreenContainerAbstract; |
| 4 | +import net.minecraft.client.gui.container.ScreenCrafting; |
| 5 | +import net.minecraft.client.gui.guidebook.SlotGuidebook; |
| 6 | +import net.minecraft.client.gui.guidebook.crafting.RecipePageCrafting; |
| 7 | +import net.minecraft.client.gui.guidebook.crafting.displays.RecipeDisplayAdapter; |
| 8 | +import net.minecraft.core.data.registry.recipe.RecipeEntryBase; |
| 9 | +import net.minecraft.core.data.registry.recipe.RecipeSymbol; |
| 10 | +import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCraftingShaped; |
| 11 | +import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCraftingShapeless; |
| 12 | +import net.minecraft.core.item.ItemStack; |
| 13 | +import net.minecraft.core.player.inventory.menu.MenuAbstract; |
| 14 | +import net.minecraft.core.player.inventory.slot.Slot; |
| 15 | +import net.minecraft.core.util.collection.Pair; |
| 16 | +import org.spongepowered.asm.mixin.Mixin; |
| 17 | +import turing.tmb.TMB; |
| 18 | +import turing.tmb.api.ISupportsRecipeFilling; |
| 19 | +import turing.tmb.api.recipe.IRecipeTranslator; |
| 20 | +import turing.tmb.util.InventoryWrapper; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +@Mixin(value = ScreenCrafting.class, remap = false) |
| 28 | +public abstract class ScreenCraftingMixin extends ScreenContainerAbstract implements ISupportsRecipeFilling { |
| 29 | + private ScreenCraftingMixin(MenuAbstract container) { |
| 30 | + super(container); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void fillRecipe(IRecipeTranslator<?> translator, boolean maximum) { |
| 35 | + InventoryWrapper w = new InventoryWrapper(mc.thePlayer.inventory); |
| 36 | + for (Slot slot : inventorySlots.slots) { |
| 37 | + if(slot.hasItem() && slot.index > 0 && slot.index < 10){ |
| 38 | + mc.thePlayer.inventory.insertItem(slot.getItemStack(), false); |
| 39 | + slot.set(null); |
| 40 | + } |
| 41 | + } |
| 42 | + if (translator.getOriginal() instanceof RecipeEntryCraftingShaped) { |
| 43 | + RecipeEntryCraftingShaped recipe = (RecipeEntryCraftingShaped) translator.getOriginal(); |
| 44 | + List<RecipeSymbol> list = Arrays.stream(recipe.getInput()).collect(Collectors.toList()); |
| 45 | + Pair<Boolean, List<ItemStack>> pair = w.containsRecipe(list); |
| 46 | + if(pair.getLeft()){ |
| 47 | + List<ItemStack> materials = pair.getRight(); |
| 48 | + RecipeDisplayAdapter<RecipeEntryCraftingShaped> adapter = (RecipeDisplayAdapter<RecipeEntryCraftingShaped>) RecipePageCrafting.recipeToDisplayAdapterMap.get(RecipeEntryCraftingShaped.class); |
| 49 | + List<SlotGuidebook> slots = adapter.getSlots(recipe, 0, 0, 0); |
| 50 | + |
| 51 | + int maxAmount = materials.stream().mapToInt(stack -> stack.stackSize).min().orElse(0); |
| 52 | + |
| 53 | + for (int i = 0; i < maxAmount; i++) { |
| 54 | + for (SlotGuidebook slot : slots) { |
| 55 | + if (slot.symbol == null && slot.item == null) continue; |
| 56 | + for (ItemStack material : materials) { |
| 57 | + List<ItemStack> resolved = new ArrayList<>(); |
| 58 | + if (slot.symbol != null) { |
| 59 | + resolved = slot.symbol.resolve(); |
| 60 | + } else if (slot.item != null) { |
| 61 | + resolved.add(slot.item); |
| 62 | + } |
| 63 | + Pair<Boolean, ItemStack> contains = InventoryWrapper.listContains(resolved, material, ItemStack::isItemEqual, true); |
| 64 | + if (contains.getLeft()) { |
| 65 | + ItemStack recipeStack = contains.getRight(); |
| 66 | + ItemStack removedStack = w.removeUntil(recipeStack.itemID, recipeStack.getMetadata(), recipeStack.stackSize, recipeStack.getData(), false, false); |
| 67 | + if(removedStack == null) continue; |
| 68 | + if (inventorySlots.slots.get(slot.index + 1).getItemStack() == null) { |
| 69 | + inventorySlots.slots.get(slot.index + 1).set(removedStack); |
| 70 | + } else if (removedStack.isItemEqual(inventorySlots.slots.get(slot.index + 1).getItemStack())) { |
| 71 | + inventorySlots.slots.get(slot.index + 1).getItemStack().stackSize += removedStack.stackSize; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + if(translator.getOriginal() instanceof RecipeEntryCraftingShapeless) { |
| 81 | + RecipeEntryCraftingShapeless recipe = (RecipeEntryCraftingShapeless) translator.getOriginal(); |
| 82 | + List<RecipeSymbol> list = new ArrayList<>(recipe.getInput()); |
| 83 | + Pair<Boolean, List<ItemStack>> pair = w.containsRecipe(list); |
| 84 | + if(pair.getLeft()){ |
| 85 | + List<ItemStack> materials = pair.getRight(); |
| 86 | + RecipeDisplayAdapter<RecipeEntryCraftingShapeless> adapter = (RecipeDisplayAdapter<RecipeEntryCraftingShapeless>) RecipePageCrafting.recipeToDisplayAdapterMap.get(RecipeEntryCraftingShapeless.class); |
| 87 | + List<SlotGuidebook> slots = adapter.getSlots(recipe, 0, 0, 0); |
| 88 | + |
| 89 | + int maxAmount = materials.stream().mapToInt(stack -> stack.stackSize).min().orElse(0); |
| 90 | + |
| 91 | + for (int i = 0; i < maxAmount; i++) { |
| 92 | + for (SlotGuidebook slot : slots) { |
| 93 | + if (slot.symbol == null && slot.item == null) continue; |
| 94 | + List<ItemStack> resolved = new ArrayList<>(); |
| 95 | + if (slot.symbol != null) { |
| 96 | + resolved = slot.symbol.resolve(); |
| 97 | + } else { |
| 98 | + resolved.add(slot.item); |
| 99 | + } |
| 100 | + for (ItemStack material : materials) { |
| 101 | + Pair<Boolean, ItemStack> contains = InventoryWrapper.listContains(resolved, material, ItemStack::isItemEqual, true); |
| 102 | + if (contains.getLeft()) { |
| 103 | + ItemStack recipeStack = contains.getRight(); |
| 104 | + ItemStack removedStack = w.removeUntil(recipeStack.itemID, recipeStack.getMetadata(), recipeStack.stackSize, recipeStack.getData(), false, false); |
| 105 | + if(removedStack == null) continue; |
| 106 | + if (inventorySlots.slots.get(slot.index + 1).getItemStack() == null) { |
| 107 | + inventorySlots.slots.get(slot.index + 1).set(removedStack); |
| 108 | + } else if (removedStack.isItemEqual(inventorySlots.slots.get(slot.index + 1).getItemStack())) { |
| 109 | + inventorySlots.slots.get(slot.index + 1).getItemStack().stackSize += removedStack.stackSize; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public List<Class<? extends RecipeEntryBase<?, ?, ?>>> getSupportedRecipes() { |
| 121 | + ArrayList<Class<? extends RecipeEntryBase<?, ?, ?>>> list = new ArrayList<>(); |
| 122 | + list.add(RecipeEntryCraftingShaped.class); |
| 123 | + list.add(RecipeEntryCraftingShapeless.class); |
| 124 | + return list; |
| 125 | + } |
| 126 | +} |
0 commit comments