-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathGregTechGuiScreen.java
More file actions
132 lines (114 loc) · 5.78 KB
/
Copy pathGregTechGuiScreen.java
File metadata and controls
132 lines (114 loc) · 5.78 KB
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
package gregtech.api.mui;
import gregtech.api.GTValues;
import gregtech.client.ClientProxy;
import gregtech.integration.jei.JustEnoughItemsModule;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import com.cleanroommc.modularui.integration.recipeviewer.RecipeViewerRecipeTransferHandler;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widget.Widget;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntComparators;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@SuppressWarnings("UnstableApiUsage")
@SideOnly(Side.CLIENT)
public class GregTechGuiScreen extends ModularScreen implements RecipeViewerRecipeTransferHandler {
// Stores lists of higher priority recipe receivers to the left of the tree
@SideOnly(Side.CLIENT)
private static final Int2ObjectMap<Map<String, IRecipeTransferReceiver>> registeredRecipeTransferReceivers = new Int2ObjectAVLTreeMap<>(
IntComparators.OPPOSITE_COMPARATOR);
public GregTechGuiScreen(ModularPanel mainPanel) {
this(mainPanel, GTGuiTheme.STANDARD);
}
public GregTechGuiScreen(ModularPanel mainPanel, GTGuiTheme theme) {
this(GTValues.MODID, mainPanel, theme);
}
public GregTechGuiScreen(String owner, ModularPanel mainPanel, GTGuiTheme theme) {
this(owner, mainPanel, theme.getId());
}
public GregTechGuiScreen(String owner, ModularPanel mainPanel, String themeId) {
super(owner, mainPanel);
useTheme(themeId);
}
@Override
public void onClose() {
// Only clear all registered recipe receivers when the UI is truly closing, ie not just opening JEI over it.
if (ClientProxy.isGUIClosingPermanently) {
// Clear all registered recipe receivers on UI close, just in case.
registeredRecipeTransferReceivers.clear();
}
}
@Override
public IRecipeTransferError transferRecipe(IRecipeLayout recipeLayout, boolean maxTransfer, boolean simulate) {
// Receivers are sorted high to low on registration
for (Map<String, IRecipeTransferReceiver> subMap : registeredRecipeTransferReceivers.values()) {
for (IRecipeTransferReceiver receiver : subMap.values()) {
IRecipeTransferError result = receiver.receiveRecipe(recipeLayout, maxTransfer, simulate);
if (result != null && result.getType() == IRecipeTransferError.Type.INTERNAL) continue;
return result;
}
}
// No valid transfer handler was found
return JustEnoughItemsModule.transferHelper.createInternalError();
}
/**
* Register an {@link IRecipeTransferReceiver} to this screen. <br/>
* Recipe transfer handlers registered through this method will have a priority of {@code 0}. <br/>
* <b>Important:</b> ensure that you remove this handler with {@link #removeRecipeTransferHandler(String)} when it's
* disposed of! <br/>
* Remove it by calling {@link #removeRecipeTransferHandler(String)} from {@link Widget#dispose()} for widgets and
* {@link SyncHandler#dispose()} for sync handlers.
*
* @throws IllegalArgumentException if a receiver with the given key already exists.
*/
@SideOnly(Side.CLIENT)
public static void registerRecipeTransferHandler(@NotNull String key,
@NotNull IRecipeTransferReceiver transferReceiver) {
registerRecipeTransferHandler(key, transferReceiver, 0);
}
/**
* Register an {@link IRecipeTransferReceiver} to this screen with a certain priority. Higher numbers will be tried
* first. <br/>
* <b>Important:</b> ensure that you remove this handler with {@link #removeRecipeTransferHandler(String)} when it's
* disposed of! <br/>
* Remove it by calling {@link #removeRecipeTransferHandler(String)} from {@link Widget#dispose()} for widgets and
* {@link SyncHandler#dispose()} for sync handlers.
*
* @throws IllegalArgumentException if a receiver with the given key already exists.
*/
@SideOnly(Side.CLIENT)
public static void registerRecipeTransferHandler(@NotNull String key,
@NotNull IRecipeTransferReceiver transferReceiver,
int priority) {
for (Map<String, IRecipeTransferReceiver> subMap : registeredRecipeTransferReceivers.values()) {
if (subMap.containsKey(key)) {
throw new IllegalArgumentException(
"Tried to register a recipe transfer receiver to a key that's already used!");
}
}
registeredRecipeTransferReceivers.computeIfAbsent(priority, $ -> new Object2ObjectOpenHashMap<>())
.put(key, transferReceiver);
}
/**
* Remove a registered {@link IRecipeTransferReceiver} from this screen.
*
* @throws IllegalArgumentException if no receiver exists with the given key.
*/
@SideOnly(Side.CLIENT)
public static void removeRecipeTransferHandler(@NotNull String key) {
for (Map<String, IRecipeTransferReceiver> subMap : registeredRecipeTransferReceivers.values()) {
if (subMap.containsKey(key)) {
subMap.remove(key);
return;
}
}
throw new IllegalArgumentException("Tried to remove a recipe transfer receiver by a key that didn't exist!");
}
}