/
PlayerRecipeTransferHandler.java
175 lines (150 loc) · 6.84 KB
/
PlayerRecipeTransferHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mezz.jei.transfer;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import com.google.common.collect.ImmutableSet;
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ingredient.IGuiIngredient;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.helpers.IStackHelper;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper;
import mezz.jei.api.recipe.transfer.IRecipeTransferInfo;
import mezz.jei.config.ServerInfo;
import mezz.jei.gui.ingredients.GuiItemStackGroup;
import mezz.jei.network.Network;
import mezz.jei.network.packets.PacketRecipeTransfer;
import mezz.jei.util.Translator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PlayerRecipeTransferHandler implements IRecipeTransferHandler<ContainerPlayer> {
private static final Logger LOGGER = LogManager.getLogger();
private final IStackHelper stackHelper;
private final IRecipeTransferHandlerHelper handlerHelper;
private final IRecipeTransferInfo<ContainerPlayer> transferHelper;
public PlayerRecipeTransferHandler(IStackHelper stackhelper, IRecipeTransferHandlerHelper handlerHelper) {
this.stackHelper = stackhelper;
this.handlerHelper = handlerHelper;
this.transferHelper = new BasicRecipeTransferInfo<>(ContainerPlayer.class, VanillaRecipeCategoryUid.CRAFTING, 1, 4, 9, 36);
}
@Override
public Class<ContainerPlayer> getContainerClass() {
return transferHelper.getContainerClass();
}
@Nullable
@Override
public IRecipeTransferError transferRecipe(ContainerPlayer container, IRecipeLayout recipeLayout, EntityPlayer player, boolean maxTransfer, boolean doTransfer) {
if (!ServerInfo.isJeiOnServer()) {
String tooltipMessage = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.no.server");
return handlerHelper.createUserErrorWithTooltip(tooltipMessage);
}
if (!transferHelper.canHandle(container)) {
return handlerHelper.createInternalError();
}
Map<Integer, Slot> inventorySlots = new HashMap<>();
for (Slot slot : transferHelper.getInventorySlots(container)) {
inventorySlots.put(slot.slotNumber, slot);
}
Map<Integer, Slot> craftingSlots = new HashMap<>();
for (Slot slot : transferHelper.getRecipeSlots(container)) {
craftingSlots.put(slot.slotNumber, slot);
}
IGuiItemStackGroup itemStackGroup = recipeLayout.getItemStacks();
int inputCount = 0;
{
// indexes that do not fit into the player crafting grid
Set<Integer> badIndexes = ImmutableSet.of(2, 5, 6, 7, 8);
int inputIndex = 0;
for (IGuiIngredient<ItemStack> ingredient : itemStackGroup.getGuiIngredients().values()) {
if (ingredient.isInput()) {
if (!ingredient.getAllIngredients().isEmpty()) {
inputCount++;
if (badIndexes.contains(inputIndex)) {
String tooltipMessage = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.too.large.player.inventory");
return handlerHelper.createUserErrorWithTooltip(tooltipMessage);
}
}
inputIndex++;
}
}
}
// compact the crafting grid into a 2x2 area
List<IGuiIngredient<ItemStack>> guiIngredients = new ArrayList<>();
for (IGuiIngredient<ItemStack> guiIngredient : itemStackGroup.getGuiIngredients().values()) {
if (guiIngredient.isInput()) {
guiIngredients.add(guiIngredient);
}
}
IGuiItemStackGroup playerInvItemStackGroup = new GuiItemStackGroup(null, 0);
int[] playerGridIndexes = {0, 1, 3, 4};
for (int i = 0; i < 4; i++) {
int index = playerGridIndexes[i];
if (index < guiIngredients.size()) {
IGuiIngredient<ItemStack> ingredient = guiIngredients.get(index);
playerInvItemStackGroup.init(i, true, 0, 0);
playerInvItemStackGroup.set(i, ingredient.getAllIngredients());
}
}
Map<Integer, ItemStack> availableItemStacks = new HashMap<>();
int filledCraftSlotCount = 0;
int emptySlotCount = 0;
for (Slot slot : craftingSlots.values()) {
final ItemStack stack = slot.getStack();
if (!stack.isEmpty()) {
if (!slot.canTakeStack(player)) {
LOGGER.error("Recipe Transfer helper {} does not work for container {}. Player can't move item out of Crafting Slot number {}", transferHelper.getClass(), container.getClass(), slot.slotNumber);
return handlerHelper.createInternalError();
}
filledCraftSlotCount++;
availableItemStacks.put(slot.slotNumber, stack.copy());
}
}
for (Slot slot : inventorySlots.values()) {
final ItemStack stack = slot.getStack();
if (!stack.isEmpty()) {
availableItemStacks.put(slot.slotNumber, stack.copy());
} else {
emptySlotCount++;
}
}
// check if we have enough inventory space to shuffle items around to their final locations
if (filledCraftSlotCount - inputCount > emptySlotCount) {
String message = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.inventory.full");
return handlerHelper.createUserErrorWithTooltip(message);
}
RecipeTransferUtil.MatchingItemsResult matchingItemsResult = RecipeTransferUtil.getMatchingItems(stackHelper, availableItemStacks, playerInvItemStackGroup.getGuiIngredients());
if (matchingItemsResult.missingItems.size() > 0) {
String message = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.missing");
matchingItemsResult = RecipeTransferUtil.getMatchingItems(stackHelper, availableItemStacks, itemStackGroup.getGuiIngredients());
return handlerHelper.createUserErrorForSlots(message, matchingItemsResult.missingItems);
}
List<Integer> craftingSlotIndexes = new ArrayList<>(craftingSlots.keySet());
Collections.sort(craftingSlotIndexes);
List<Integer> inventorySlotIndexes = new ArrayList<>(inventorySlots.keySet());
Collections.sort(inventorySlotIndexes);
// check that the slots exist and can be altered
for (Map.Entry<Integer, Integer> entry : matchingItemsResult.matchingItems.entrySet()) {
int craftNumber = entry.getKey();
int slotNumber = craftingSlotIndexes.get(craftNumber);
if (slotNumber < 0 || slotNumber >= container.inventorySlots.size()) {
LOGGER.error("Recipes Transfer Helper {} references slot {} outside of the inventory's size {}", transferHelper.getClass(), slotNumber, container.inventorySlots.size());
return handlerHelper.createInternalError();
}
}
if (doTransfer) {
PacketRecipeTransfer packet = new PacketRecipeTransfer(matchingItemsResult.matchingItems, craftingSlotIndexes, inventorySlotIndexes, maxTransfer, false);
Network.sendPacketToServer(packet);
}
return null;
}
}