ClientHelperImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.impl;
  6. import com.google.common.collect.ImmutableList;
  7. import com.google.common.collect.Maps;
  8. import io.netty.buffer.Unpooled;
  9. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  10. import me.shedaniel.rei.RoughlyEnoughItemsNetwork;
  11. import me.shedaniel.rei.api.*;
  12. import me.shedaniel.rei.api.annotations.Internal;
  13. import me.shedaniel.rei.gui.PreRecipeViewingScreen;
  14. import me.shedaniel.rei.gui.RecipeViewingScreen;
  15. import me.shedaniel.rei.gui.VillagerRecipeViewingScreen;
  16. import me.shedaniel.rei.gui.config.RecipeScreenType;
  17. import net.fabricmc.api.ClientModInitializer;
  18. import net.fabricmc.fabric.api.client.keybinding.FabricKeyBinding;
  19. import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
  20. import net.fabricmc.fabric.impl.client.keybinding.KeyBindingRegistryImpl;
  21. import net.fabricmc.loader.api.FabricLoader;
  22. import net.fabricmc.loader.api.ModContainer;
  23. import net.fabricmc.loader.api.metadata.ModMetadata;
  24. import net.minecraft.client.MinecraftClient;
  25. import net.minecraft.client.gui.screen.Screen;
  26. import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
  27. import net.minecraft.client.util.InputUtil;
  28. import net.minecraft.item.Item;
  29. import net.minecraft.item.ItemStack;
  30. import net.minecraft.item.Items;
  31. import net.minecraft.text.TranslatableText;
  32. import net.minecraft.util.DefaultedList;
  33. import net.minecraft.util.Formatting;
  34. import net.minecraft.util.Identifier;
  35. import net.minecraft.util.PacketByteBuf;
  36. import net.minecraft.util.registry.Registry;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Optional;
  41. @Deprecated
  42. @Internal
  43. public class ClientHelperImpl implements ClientHelper, ClientModInitializer {
  44. public static ClientHelperImpl instance;
  45. private final Identifier recipeKeybind = new Identifier("roughlyenoughitems", "recipe_keybind");
  46. private final Identifier usageKeybind = new Identifier("roughlyenoughitems", "usage_keybind");
  47. private final Identifier hideKeybind = new Identifier("roughlyenoughitems", "hide_keybind");
  48. private final Identifier previousPageKeybind = new Identifier("roughlyenoughitems", "previous_page");
  49. private final Identifier nextPageKeybind = new Identifier("roughlyenoughitems", "next_page");
  50. private final Identifier focusSearchFieldKeybind = new Identifier("roughlyenoughitems", "focus_search");
  51. private final Identifier copyRecipeIdentifierKeybind = new Identifier("roughlyenoughitems", "copy_recipe_id");
  52. private final Identifier favoriteEntryKeybind = new Identifier("roughlyenoughitems", "favorite_entry");
  53. private final Map<String, String> modNameCache = Maps.newHashMap();
  54. public FabricKeyBinding recipe, usage, hide, previousPage, nextPage, focusSearchField, copyRecipeIdentifier;
  55. @Override
  56. public String getFormattedModFromItem(Item item) {
  57. String mod = getModFromItem(item);
  58. if (mod.isEmpty())
  59. return "";
  60. return Formatting.BLUE.toString() + Formatting.ITALIC.toString() + mod;
  61. }
  62. @Override
  63. public String getFormattedModFromIdentifier(Identifier identifier) {
  64. String mod = getModFromIdentifier(identifier);
  65. if (mod.isEmpty())
  66. return "";
  67. return Formatting.BLUE.toString() + Formatting.ITALIC.toString() + mod;
  68. }
  69. @Override
  70. public FabricKeyBinding[] getREIKeyBindings() {
  71. return new FabricKeyBinding[]{recipe, usage, hide, previousPage, nextPage, focusSearchField, copyRecipeIdentifier};
  72. }
  73. @Override
  74. public FabricKeyBinding getRecipeKeyBinding() {
  75. return recipe;
  76. }
  77. @Override
  78. public FabricKeyBinding getUsageKeyBinding() {
  79. return usage;
  80. }
  81. @Override
  82. public FabricKeyBinding getHideKeyBinding() {
  83. return hide;
  84. }
  85. @Override
  86. public FabricKeyBinding getPreviousPageKeyBinding() {
  87. return previousPage;
  88. }
  89. @Override
  90. public FabricKeyBinding getNextPageKeyBinding() {
  91. return nextPage;
  92. }
  93. @Override
  94. public FabricKeyBinding getFocusSearchFieldKeyBinding() {
  95. return focusSearchField;
  96. }
  97. @Override
  98. public FabricKeyBinding getCopyRecipeIdentifierKeyBinding() {
  99. return copyRecipeIdentifier;
  100. }
  101. @Override
  102. public String getModFromItem(Item item) {
  103. if (item.equals(Items.AIR))
  104. return "";
  105. return getModFromIdentifier(Registry.ITEM.getId(item));
  106. }
  107. @Override
  108. public String getModFromIdentifier(Identifier identifier) {
  109. if (identifier == null)
  110. return "";
  111. Optional<String> any = Optional.ofNullable(modNameCache.getOrDefault(identifier.getNamespace(), null));
  112. if (any.isPresent())
  113. return any.get();
  114. String modid = identifier.getNamespace();
  115. String s = FabricLoader.getInstance().getModContainer(modid).map(ModContainer::getMetadata).map(ModMetadata::getName).orElse(modid);
  116. modNameCache.put(modid, s);
  117. return s;
  118. }
  119. @Override
  120. public boolean isCheating() {
  121. return ConfigObject.getInstance().isCheating();
  122. }
  123. @Override
  124. public void setCheating(boolean cheating) {
  125. ConfigObject.getInstance().setCheating(cheating);
  126. ConfigManager.getInstance().saveConfig();
  127. }
  128. @Override
  129. public void sendDeletePacket() {
  130. if (ScreenHelper.getLastContainerScreen() instanceof CreativeInventoryScreen) {
  131. MinecraftClient.getInstance().player.inventory.setCursorStack(ItemStack.EMPTY);
  132. return;
  133. }
  134. ClientSidePacketRegistry.INSTANCE.sendToServer(RoughlyEnoughItemsNetwork.DELETE_ITEMS_PACKET, new PacketByteBuf(Unpooled.buffer()));
  135. }
  136. @Override
  137. public boolean tryCheatingEntry(EntryStack entry) {
  138. if (entry.getType() != EntryStack.Type.ITEM)
  139. return false;
  140. ItemStack cheatedStack = entry.getItemStack().copy();
  141. if (RoughlyEnoughItemsCore.canUsePackets()) {
  142. try {
  143. ClientSidePacketRegistry.INSTANCE.sendToServer(RoughlyEnoughItemsNetwork.CREATE_ITEMS_PACKET, new PacketByteBuf(Unpooled.buffer()).writeItemStack(cheatedStack));
  144. return true;
  145. } catch (Exception e) {
  146. return false;
  147. }
  148. } else {
  149. Identifier identifier = entry.getIdentifier().orElse(null);
  150. if (identifier == null)
  151. return false;
  152. String tagMessage = cheatedStack.copy().getTag() != null && !cheatedStack.copy().getTag().isEmpty() ? cheatedStack.copy().getTag().asString() : "";
  153. String og = cheatedStack.getCount() == 1 ? ConfigObject.getInstance().getGiveCommand().replaceAll(" \\{count}", "") : ConfigObject.getInstance().getGiveCommand();
  154. 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()));
  155. if (madeUpCommand.length() > 256) {
  156. 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()));
  157. MinecraftClient.getInstance().player.addChatMessage(new TranslatableText("text.rei.too_long_nbt"), false);
  158. }
  159. MinecraftClient.getInstance().player.sendChatMessage(madeUpCommand);
  160. return true;
  161. }
  162. }
  163. @Override
  164. public boolean executeRecipeKeyBind(EntryStack stack) {
  165. Map<RecipeCategory<?>, List<RecipeDisplay>> map = RecipeHelper.getInstance().getRecipesFor(stack);
  166. if (map.keySet().size() > 0)
  167. openRecipeViewingScreen(map);
  168. return map.keySet().size() > 0;
  169. }
  170. @Override
  171. public boolean executeUsageKeyBind(EntryStack stack) {
  172. Map<RecipeCategory<?>, List<RecipeDisplay>> map = RecipeHelper.getInstance().getUsagesFor(stack);
  173. if (map.keySet().size() > 0)
  174. openRecipeViewingScreen(map);
  175. return map.keySet().size() > 0;
  176. }
  177. @Override
  178. public List<ItemStack> getInventoryItemsTypes() {
  179. List<DefaultedList<ItemStack>> field_7543 = ImmutableList.of(MinecraftClient.getInstance().player.inventory.main, MinecraftClient.getInstance().player.inventory.armor, MinecraftClient.getInstance().player.inventory.offHand);
  180. List<ItemStack> inventoryStacks = new ArrayList<>();
  181. field_7543.forEach(itemStacks -> itemStacks.forEach(itemStack -> {
  182. if (!itemStack.isEmpty())
  183. inventoryStacks.add(itemStack);
  184. }));
  185. return inventoryStacks;
  186. }
  187. @Override
  188. public boolean executeViewAllRecipesKeyBind() {
  189. Map<RecipeCategory<?>, List<RecipeDisplay>> map = RecipeHelper.getInstance().getAllRecipes();
  190. if (map.keySet().size() > 0)
  191. openRecipeViewingScreen(map);
  192. return map.keySet().size() > 0;
  193. }
  194. @Override
  195. public boolean executeViewAllRecipesFromCategory(Identifier category) {
  196. Map<RecipeCategory<?>, List<RecipeDisplay>> map = Maps.newLinkedHashMap();
  197. Optional<RecipeCategory<?>> any = RecipeHelper.getInstance().getAllCategories().stream().filter(c -> c.getIdentifier().equals(category)).findAny();
  198. if (!any.isPresent())
  199. return false;
  200. RecipeCategory<?> recipeCategory = any.get();
  201. map.put(recipeCategory, RecipeHelper.getInstance().getAllRecipesFromCategory(recipeCategory));
  202. if (map.keySet().size() > 0)
  203. openRecipeViewingScreen(map);
  204. return map.keySet().size() > 0;
  205. }
  206. @Override
  207. public boolean executeViewAllRecipesFromCategories(List<Identifier> categories) {
  208. Map<RecipeCategory<?>, List<RecipeDisplay>> map = Maps.newLinkedHashMap();
  209. for (Identifier category : categories) {
  210. Optional<RecipeCategory<?>> any = RecipeHelper.getInstance().getAllCategories().stream().filter(c -> c.getIdentifier().equals(category)).findAny();
  211. if (!any.isPresent())
  212. continue;
  213. RecipeCategory<?> recipeCategory = any.get();
  214. map.put(recipeCategory, RecipeHelper.getInstance().getAllRecipesFromCategory(recipeCategory));
  215. }
  216. if (map.keySet().size() > 0)
  217. openRecipeViewingScreen(map);
  218. return map.keySet().size() > 0;
  219. }
  220. @Override
  221. public void openRecipeViewingScreen(Map<RecipeCategory<?>, List<RecipeDisplay>> map) {
  222. Screen screen = null;
  223. if (ConfigObject.getInstance().getRecipeScreenType() == RecipeScreenType.VILLAGER)
  224. screen = new VillagerRecipeViewingScreen(map);
  225. else if (ConfigObject.getInstance().getRecipeScreenType() == RecipeScreenType.UNSET)
  226. screen = new PreRecipeViewingScreen(map);
  227. else
  228. screen = new RecipeViewingScreen(map);
  229. ScreenHelper.storeRecipeScreen(MinecraftClient.getInstance().currentScreen);
  230. MinecraftClient.getInstance().openScreen(screen);
  231. }
  232. @Override
  233. public void onInitializeClient() {
  234. ClientHelperImpl.instance = (ClientHelperImpl) this;
  235. registerFabricKeyBinds();
  236. modNameCache.put("minecraft", "Minecraft");
  237. modNameCache.put("c", "Common");
  238. }
  239. @Override
  240. public void registerFabricKeyBinds() {
  241. String category = "key.rei.category";
  242. KeyBindingRegistryImpl.INSTANCE.addCategory(category);
  243. KeyBindingRegistryImpl.INSTANCE.register(recipe = FabricKeyBinding.Builder.create(recipeKeybind, InputUtil.Type.KEYSYM, 82, category).build());
  244. KeyBindingRegistryImpl.INSTANCE.register(usage = FabricKeyBinding.Builder.create(usageKeybind, InputUtil.Type.KEYSYM, 85, category).build());
  245. KeyBindingRegistryImpl.INSTANCE.register(hide = FabricKeyBinding.Builder.create(hideKeybind, InputUtil.Type.KEYSYM, 79, category).build());
  246. KeyBindingRegistryImpl.INSTANCE.register(previousPage = FabricKeyBinding.Builder.create(previousPageKeybind, InputUtil.Type.KEYSYM, -1, category).build());
  247. KeyBindingRegistryImpl.INSTANCE.register(nextPage = FabricKeyBinding.Builder.create(nextPageKeybind, InputUtil.Type.KEYSYM, -1, category).build());
  248. KeyBindingRegistryImpl.INSTANCE.register(focusSearchField = FabricKeyBinding.Builder.create(focusSearchFieldKeybind, InputUtil.Type.KEYSYM, -1, category).build());
  249. KeyBindingRegistryImpl.INSTANCE.register(copyRecipeIdentifier = FabricKeyBinding.Builder.create(copyRecipeIdentifierKeybind, InputUtil.Type.KEYSYM, -1, category).build());
  250. }
  251. }