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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Roughly Enough Items by Danielshe.
* Licensed under the MIT License.
*/
package me.shedaniel.rei.impl;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import io.netty.buffer.Unpooled;
import me.shedaniel.rei.RoughlyEnoughItemsCore;
import me.shedaniel.rei.RoughlyEnoughItemsNetwork;
import me.shedaniel.rei.api.*;
import me.shedaniel.rei.gui.PreRecipeViewingScreen;
import me.shedaniel.rei.gui.RecipeViewingScreen;
import me.shedaniel.rei.gui.VillagerRecipeViewingScreen;
import me.shedaniel.rei.gui.config.RecipeScreenType;
import me.zeroeightsix.fiber.exception.FiberException;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.DefaultedList;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.PacketByteBuf;
import net.minecraft.util.registry.Registry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class ClientHelperImpl implements ClientHelper, ClientModInitializer {
public static ClientHelperImpl instance;
private final Identifier recipeKeybind = new Identifier("roughlyenoughitems", "recipe_keybind");
private final Identifier usageKeybind = new Identifier("roughlyenoughitems", "usage_keybind");
private final Identifier hideKeybind = new Identifier("roughlyenoughitems", "hide_keybind");
private final Identifier previousPageKeybind = new Identifier("roughlyenoughitems", "previous_page");
private final Identifier nextPageKeybind = new Identifier("roughlyenoughitems", "next_page");
private final Identifier focusSearchFieldKeybind = new Identifier("roughlyenoughitems", "focus_search");
private final Identifier copyRecipeIdentifierKeybind = new Identifier("roughlyenoughitems", "copy_recipe_id");
private final Map<String, String> modNameCache = Maps.newHashMap();
public KeyBinding recipe, usage, hide, previousPage, nextPage, focusSearchField, copyRecipeIdentifier;
@Override
public String getFormattedModFromItem(Item item) {
String mod = getModFromItem(item);
if (mod.isEmpty())
return "";
return Formatting.BLUE.toString() + Formatting.ITALIC.toString() + mod;
}
@Override
public String getFormattedModFromIdentifier(Identifier identifier) {
String mod = getModFromIdentifier(identifier);
if (mod.isEmpty())
return "";
return Formatting.BLUE.toString() + Formatting.ITALIC.toString() + mod;
}
@Override
public KeyBinding getRecipeKeyBinding() {
return recipe;
}
@Override
public KeyBinding getUsageKeyBinding() {
return usage;
}
@Override
public KeyBinding getHideKeyBinding() {
return hide;
}
@Override
public KeyBinding getPreviousPageKeyBinding() {
return previousPage;
}
@Override
public KeyBinding getNextPageKeyBinding() {
return nextPage;
}
@Override
public KeyBinding getFocusSearchFieldKeyBinding() {
return focusSearchField;
}
@Override
public KeyBinding getCopyRecipeIdentifierKeyBinding() {
return copyRecipeIdentifier;
}
@Override
public String getModFromItem(Item item) {
if (item.equals(Items.AIR))
return "";
return getModFromIdentifier(Registry.ITEM.getId(item));
}
@Override
public String getModFromIdentifier(Identifier identifier) {
if (identifier == null)
return "";
Optional<String> any = Optional.ofNullable(modNameCache.getOrDefault(identifier.getNamespace(), null));
if (any.isPresent())
return any.get();
String modid = identifier.getNamespace();
String s = FabricLoader.getInstance().getModContainer(modid).map(ModContainer::getMetadata).map(ModMetadata::getName).orElse(modid);
modNameCache.put(modid, s);
return s;
}
@Override
public boolean isCheating() {
return RoughlyEnoughItemsCore.getConfigManager().getConfig().isCheating();
}
@Override
public void setCheating(boolean cheating) {
RoughlyEnoughItemsCore.getConfigManager().getConfig().setCheating(cheating);
try {
RoughlyEnoughItemsCore.getConfigManager().saveConfig();
} catch (IOException | FiberException e) {
e.printStackTrace();
}
}
@Override
public void sendDeletePacket() {
if (ScreenHelper.getLastContainerScreen() instanceof CreativeInventoryScreen) {
MinecraftClient.getInstance().player.inventory.setCursorStack(ItemStack.EMPTY);
return;
}
ClientSidePacketRegistry.INSTANCE.sendToServer(RoughlyEnoughItemsNetwork.DELETE_ITEMS_PACKET, new PacketByteBuf(Unpooled.buffer()));
}
@Override
public boolean tryCheatingEntry(EntryStack entry) {
if (entry.getType() != EntryStack.Type.ITEM)
return false;
ItemStack cheatedStack = entry.getItemStack().copy();
if (RoughlyEnoughItemsCore.canUsePackets()) {
try {
ClientSidePacketRegistry.INSTANCE.sendToServer(RoughlyEnoughItemsNetwork.CREATE_ITEMS_PACKET, new PacketByteBuf(Unpooled.buffer()).writeItemStack(cheatedStack));
return true;
} catch (Exception e) {
return false;
}
} else {
Identifier identifier = entry.getIdentifier().orElse(null);
if (identifier == null)
return false;
String tagMessage = cheatedStack.copy().getTag() != null && !cheatedStack.copy().getTag().isEmpty() ? cheatedStack.copy().getTag().asString() : "";
String og = cheatedStack.getCount() == 1 ? RoughlyEnoughItemsCore.getConfigManager().getConfig().getGiveCommand().replaceAll(" \\{count}", "") : RoughlyEnoughItemsCore.getConfigManager().getConfig().getGiveCommand();
String madeUpCommand = og.replaceAll("\\{player_name}", MinecraftClient.getInstance().player.getEntityName()).replaceAll("\\{item_name}", identifier.getPath()).replaceAll("\\{item_identifier}", identifier.toString()).replaceAll("\\{nbt}", tagMessage).replaceAll("\\{count}", String.valueOf(cheatedStack.getCount()));
if (madeUpCommand.length() > 256) {
madeUpCommand = og.replaceAll("\\{player_name}", MinecraftClient.getInstance().player.getEntityName()).replaceAll("\\{item_name}", identifier.getPath()).replaceAll("\\{item_identifier}", identifier.toString()).replaceAll("\\{nbt}", "").replaceAll("\\{count}", String.valueOf(cheatedStack.getCount()));
MinecraftClient.getInstance().player.addChatMessage(new TranslatableText("text.rei.too_long_nbt"), false);
}
MinecraftClient.getInstance().player.sendChatMessage(madeUpCommand);
return true;
}
}
@Override
public boolean executeRecipeKeyBind(EntryStack stack) {
Map<RecipeCategory<?>, List<RecipeDisplay>> map = RecipeHelper.getInstance().getRecipesFor(stack);
if (map.keySet().size() > 0)
openRecipeViewingScreen(map);
return map.keySet().size() > 0;
}
@Override
public boolean executeUsageKeyBind(EntryStack stack) {
Map<RecipeCategory<?>, List<RecipeDisplay>> map = RecipeHelper.getInstance().getUsagesFor(stack);
if (map.keySet().size() > 0)
openRecipeViewingScreen(map);
return map.keySet().size() > 0;
}
@Override
public List<ItemStack> getInventoryItemsTypes() {
List<DefaultedList<ItemStack>> field_7543 = ImmutableList.of(MinecraftClient.getInstance().player.inventory.main, MinecraftClient.
|