DefaultPlugin.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.plugin;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import it.unimi.dsi.fastutil.objects.Object2FloatMap;
  27. import me.shedaniel.math.Rectangle;
  28. import me.shedaniel.rei.api.*;
  29. import me.shedaniel.rei.api.fluid.FluidSupportProvider;
  30. import me.shedaniel.rei.api.plugins.REIPluginV0;
  31. import me.shedaniel.rei.plugin.autocrafting.DefaultRecipeBookHandler;
  32. import me.shedaniel.rei.plugin.beacon.DefaultBeaconBaseCategory;
  33. import me.shedaniel.rei.plugin.beacon.DefaultBeaconBaseDisplay;
  34. import me.shedaniel.rei.plugin.blasting.DefaultBlastingDisplay;
  35. import me.shedaniel.rei.plugin.brewing.DefaultBrewingCategory;
  36. import me.shedaniel.rei.plugin.brewing.DefaultBrewingDisplay;
  37. import me.shedaniel.rei.plugin.brewing.RegisteredBrewingRecipe;
  38. import me.shedaniel.rei.plugin.campfire.DefaultCampfireCategory;
  39. import me.shedaniel.rei.plugin.campfire.DefaultCampfireDisplay;
  40. import me.shedaniel.rei.plugin.composting.DefaultCompostingCategory;
  41. import me.shedaniel.rei.plugin.composting.DefaultCompostingDisplay;
  42. import me.shedaniel.rei.plugin.cooking.DefaultCookingCategory;
  43. import me.shedaniel.rei.plugin.crafting.DefaultCraftingCategory;
  44. import me.shedaniel.rei.plugin.crafting.DefaultCustomDisplay;
  45. import me.shedaniel.rei.plugin.crafting.DefaultShapedDisplay;
  46. import me.shedaniel.rei.plugin.crafting.DefaultShapelessDisplay;
  47. import me.shedaniel.rei.plugin.fuel.DefaultFuelCategory;
  48. import me.shedaniel.rei.plugin.fuel.DefaultFuelDisplay;
  49. import me.shedaniel.rei.plugin.information.DefaultInformationCategory;
  50. import me.shedaniel.rei.plugin.information.DefaultInformationDisplay;
  51. import me.shedaniel.rei.plugin.smelting.DefaultSmeltingDisplay;
  52. import me.shedaniel.rei.plugin.smithing.DefaultSmithingCategory;
  53. import me.shedaniel.rei.plugin.smithing.DefaultSmithingDisplay;
  54. import me.shedaniel.rei.plugin.smoking.DefaultSmokingDisplay;
  55. import me.shedaniel.rei.plugin.stonecutting.DefaultStoneCuttingCategory;
  56. import me.shedaniel.rei.plugin.stonecutting.DefaultStoneCuttingDisplay;
  57. import me.shedaniel.rei.plugin.stripping.DefaultStrippingCategory;
  58. import me.shedaniel.rei.plugin.stripping.DefaultStrippingDisplay;
  59. import me.shedaniel.rei.plugin.stripping.DummyAxeItem;
  60. import me.shedaniel.rei.utils.CollectionUtils;
  61. import net.fabricmc.api.EnvType;
  62. import net.fabricmc.api.Environment;
  63. import net.minecraft.block.ComposterBlock;
  64. import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
  65. import net.minecraft.client.MinecraftClient;
  66. import net.minecraft.client.gui.screen.ingame.*;
  67. import net.minecraft.client.gui.screen.recipebook.RecipeBookProvider;
  68. import net.minecraft.enchantment.Enchantment;
  69. import net.minecraft.enchantment.EnchantmentHelper;
  70. import net.minecraft.fluid.Fluid;
  71. import net.minecraft.item.*;
  72. import net.minecraft.potion.PotionUtil;
  73. import net.minecraft.recipe.*;
  74. import net.minecraft.tag.BlockTags;
  75. import net.minecraft.tag.Tag;
  76. import net.minecraft.util.Identifier;
  77. import net.minecraft.util.Lazy;
  78. import net.minecraft.util.math.MathHelper;
  79. import net.minecraft.util.registry.Registry;
  80. import org.apache.logging.log4j.LogManager;
  81. import org.apache.logging.log4j.Logger;
  82. import org.jetbrains.annotations.ApiStatus;
  83. import org.jetbrains.annotations.NotNull;
  84. import java.util.*;
  85. @Environment(EnvType.CLIENT)
  86. public class DefaultPlugin implements REIPluginV0 {
  87. private static final Logger LOGGER = LogManager.getFormatterLogger("REI/DefaultPlugin");
  88. public static final Identifier CRAFTING = new Identifier("minecraft", "plugins/crafting");
  89. public static final Identifier SMELTING = new Identifier("minecraft", "plugins/smelting");
  90. public static final Identifier SMOKING = new Identifier("minecraft", "plugins/smoking");
  91. public static final Identifier BLASTING = new Identifier("minecraft", "plugins/blasting");
  92. public static final Identifier CAMPFIRE = new Identifier("minecraft", "plugins/campfire");
  93. public static final Identifier STONE_CUTTING = new Identifier("minecraft", "plugins/stone_cutting");
  94. public static final Identifier STRIPPING = new Identifier("minecraft", "plugins/stripping");
  95. public static final Identifier BREWING = new Identifier("minecraft", "plugins/brewing");
  96. public static final Identifier PLUGIN = new Identifier("roughlyenoughitems", "default_plugin");
  97. public static final Identifier COMPOSTING = new Identifier("minecraft", "plugins/composting");
  98. public static final Identifier FUEL = new Identifier("minecraft", "plugins/fuel");
  99. public static final Identifier SMITHING = new Identifier("minecraft", "plugins/smithing");
  100. public static final Identifier BEACON = new Identifier("minecraft", "plugins/beacon");
  101. public static final Identifier INFO = new Identifier("roughlyenoughitems", "plugins/information");
  102. private static final Identifier DISPLAY_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/display.png");
  103. private static final Identifier DISPLAY_TEXTURE_DARK = new Identifier("roughlyenoughitems", "textures/gui/display_dark.png");
  104. private static final List<Lazy<DefaultBrewingDisplay>> BREWING_DISPLAYS = Lists.newArrayList();
  105. private static final List<DefaultInformationDisplay> INFO_DISPLAYS = Lists.newArrayList();
  106. public static Identifier getDisplayTexture() {
  107. return REIHelper.getInstance().getDefaultDisplayTexture();
  108. }
  109. @Deprecated
  110. @ApiStatus.ScheduledForRemoval
  111. public static void registerBrewingDisplay(DefaultBrewingDisplay recipe) {
  112. BREWING_DISPLAYS.add(new Lazy<>(() -> recipe));
  113. }
  114. public static void registerBrewingRecipe(RegisteredBrewingRecipe recipe) {
  115. BREWING_DISPLAYS.add(new Lazy<>(() -> new DefaultBrewingDisplay(recipe.input, recipe.ingredient, recipe.output)));
  116. }
  117. public static void registerInfoDisplay(DefaultInformationDisplay display) {
  118. INFO_DISPLAYS.add(display);
  119. }
  120. @Override
  121. public Identifier getPluginIdentifier() {
  122. return PLUGIN;
  123. }
  124. @Override
  125. public void preRegister() {
  126. INFO_DISPLAYS.clear();
  127. }
  128. @Override
  129. public void registerEntries(EntryRegistry entryRegistry) {
  130. for (Item item : Registry.ITEM) {
  131. List<ItemStack> stacks = null;
  132. try {
  133. stacks = entryRegistry.appendStacksForItem(item);
  134. } catch (Exception ignored) {
  135. }
  136. if (stacks != null) {
  137. for (ItemStack stack : entryRegistry.appendStacksForItem(item)) {
  138. entryRegistry.registerEntry(EntryStack.create(stack));
  139. }
  140. } else
  141. entryRegistry.registerEntry(EntryStack.create(item));
  142. }
  143. EntryStack stack = EntryStack.create(Items.ENCHANTED_BOOK);
  144. List<EntryStack> enchantments = new ArrayList<>();
  145. for (Enchantment enchantment : Registry.ENCHANTMENT) {
  146. for (int i = enchantment.getMinimumLevel(); i <= enchantment.getMaximumLevel(); i++) {
  147. Map<Enchantment, Integer> map = new HashMap<>();
  148. map.put(enchantment, i);
  149. ItemStack itemStack = new ItemStack(Items.ENCHANTED_BOOK);
  150. EnchantmentHelper.set(map, itemStack);
  151. enchantments.add(EntryStack.create(itemStack).setting(EntryStack.Settings.CHECK_TAGS, EntryStack.Settings.TRUE));
  152. }
  153. }
  154. entryRegistry.registerEntriesAfter(stack, enchantments);
  155. for (Fluid fluid : Registry.FLUID) {
  156. if (!fluid.getDefaultState().isEmpty() && fluid.getDefaultState().isStill())
  157. entryRegistry.registerEntry(EntryStack.create(fluid));
  158. }
  159. }
  160. @Override
  161. public void registerPluginCategories(RecipeHelper recipeHelper) {
  162. recipeHelper.registerCategories(
  163. new DefaultCraftingCategory(),
  164. new DefaultCookingCategory(SMELTING, EntryStack.create(Items.FURNACE), "category.rei.smelting"),
  165. new DefaultCookingCategory(SMOKING, EntryStack.create(Items.SMOKER), "category.rei.smoking"),
  166. new DefaultCookingCategory(BLASTING, EntryStack.create(Items.BLAST_FURNACE), "category.rei.blasting"), new DefaultCampfireCategory(),
  167. new DefaultStoneCuttingCategory(),
  168. new DefaultFuelCategory(),
  169. new DefaultBrewingCategory(),
  170. new DefaultCompostingCategory(),
  171. new DefaultStrippingCategory(),
  172. new DefaultSmithingCategory(),
  173. new DefaultBeaconBaseCategory(),
  174. new DefaultInformationCategory()
  175. );
  176. }
  177. @Override
  178. public void registerRecipeDisplays(RecipeHelper recipeHelper) {
  179. recipeHelper.registerRecipes(CRAFTING, ShapelessRecipe.class, DefaultShapelessDisplay::new);
  180. recipeHelper.registerRecipes(CRAFTING, ShapedRecipe.class, DefaultShapedDisplay::new);
  181. recipeHelper.registerRecipes(SMELTING, SmeltingRecipe.class, DefaultSmeltingDisplay::new);
  182. recipeHelper.registerRecipes(SMOKING, SmokingRecipe.class, DefaultSmokingDisplay::new);
  183. recipeHelper.registerRecipes(BLASTING, BlastingRecipe.class, DefaultBlastingDisplay::new);
  184. recipeHelper.registerRecipes(CAMPFIRE, CampfireCookingRecipe.class, DefaultCampfireDisplay::new);
  185. recipeHelper.registerRecipes(STONE_CUTTING, StonecuttingRecipe.class, DefaultStoneCuttingDisplay::new);
  186. recipeHelper.registerRecipes(SMITHING, SmithingRecipe.class, DefaultSmithingDisplay::new);
  187. for (Lazy<DefaultBrewingDisplay> display : BREWING_DISPLAYS) {
  188. recipeHelper.registerDisplay(display.get());
  189. }
  190. for (Map.Entry<Item, Integer> entry : AbstractFurnaceBlockEntity.createFuelTimeMap().entrySet()) {
  191. recipeHelper.registerDisplay(new DefaultFuelDisplay(EntryStack.create(entry.getKey()), entry.getValue()));
  192. }
  193. List<EntryStack> arrowStack = Collections.singletonList(EntryStack.create(Items.ARROW));
  194. EntryRegistry.getInstance().getEntryStacks().filter(entry -> entry.getItem() == Items.LINGERING_POTION).forEach(entry -> {
  195. List<List<EntryStack>> input = new ArrayList<>();
  196. for (int i = 0; i < 4; i++)
  197. input.add(arrowStack);
  198. input.add(Collections.singletonList(EntryStack.create(entry.getItemStack())));
  199. for (int i = 0; i < 4; i++)
  200. input.add(arrowStack);
  201. ItemStack outputStack = new ItemStack(Items.TIPPED_ARROW, 8);
  202. PotionUtil.setPotion(outputStack, PotionUtil.getPotion(entry.getItemStack()));
  203. PotionUtil.setCustomPotionEffects(outputStack, PotionUtil.getCustomPotionEffects(entry.getItemStack()));
  204. List<EntryStack> output = Collections.singletonList(EntryStack.create(outputStack).addSetting(EntryStack.Settings.CHECK_TAGS, EntryStack.Settings.TRUE));
  205. recipeHelper.registerDisplay(new DefaultCustomDisplay(null, input, output));
  206. });
  207. Map<ItemConvertible, Float> map = Maps.newLinkedHashMap();
  208. if (ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.isEmpty())
  209. ComposterBlock.registerDefaultCompostableItems();
  210. for (Object2FloatMap.Entry<ItemConvertible> entry : ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.object2FloatEntrySet()) {
  211. if (entry.getFloatValue() > 0)
  212. map.put(entry.getKey(), entry.getFloatValue());
  213. }
  214. List<ItemConvertible> stacks = Lists.newArrayList(map.keySet());
  215. stacks.sort(Comparator.comparing(map::get));
  216. for (int i = 0; i < stacks.size(); i += MathHelper.clamp(48, 1, stacks.size() - i)) {
  217. List<ItemConvertible> thisStacks = Lists.newArrayList();
  218. for (int j = i; j < i + 48; j++)
  219. if (j < stacks.size())
  220. thisStacks.add(stacks.get(j));
  221. recipeHelper.registerDisplay(new DefaultCompostingDisplay(MathHelper.floor(i / 48f), thisStacks, map, new ItemStack(Items.BONE_MEAL)));
  222. }
  223. DummyAxeItem.getStrippedBlocksMap().entrySet().stream().sorted(Comparator.comparing(b -> Registry.BLOCK.getId(b.getKey()))).forEach(set -> {
  224. recipeHelper.registerDisplay(new DefaultStrippingDisplay(EntryStack.create(set.getKey()), EntryStack.create(set.getValue())));
  225. });
  226. recipeHelper.registerDisplay(new DefaultBeaconBaseDisplay(CollectionUtils.map(Lists.newArrayList(BlockTags.BEACON_BASE_BLOCKS.values()), ItemStack::new)));
  227. }
  228. @Override
  229. public void postRegister() {
  230. for (DefaultInformationDisplay display : INFO_DISPLAYS)
  231. RecipeHelper.getInstance().registerDisplay(display);
  232. // TODO Turn this into an API
  233. // Sit tight! This will be a fast journey!
  234. long time = System.currentTimeMillis();
  235. EntryRegistry.getInstance().getEntryStacks().forEach(this::applyPotionTransformer);
  236. for (List<RecipeDisplay> displays : RecipeHelper.getInstance().getAllRecipesNoHandlers().values()) {
  237. for (RecipeDisplay display : displays) {
  238. for (List<EntryStack> entries : display.getInputEntries())
  239. for (EntryStack stack : entries)
  240. applyPotionTransformer(stack);
  241. for (List<EntryStack> entries : display.getResultingEntries())
  242. for (EntryStack stack : entries)
  243. applyPotionTransformer(stack);
  244. }
  245. }
  246. time = System.currentTimeMillis() - time;
  247. LOGGER.info("Applied Check Tags for potion in %dms.", time);
  248. }
  249. private void applyPotionTransformer(EntryStack stack) {
  250. if (stack.getItem() instanceof PotionItem)
  251. stack.addSetting(EntryStack.Settings.CHECK_TAGS, EntryStack.Settings.TRUE);
  252. }
  253. @Override
  254. public void registerBounds(DisplayHelper displayHelper) {
  255. BaseBoundsHandler baseBoundsHandler = BaseBoundsHandler.getInstance();
  256. baseBoundsHandler.registerExclusionZones(AbstractInventoryScreen.class, new DefaultPotionEffectExclusionZones());
  257. baseBoundsHandler.registerExclusionZones(RecipeBookProvider.class, new DefaultRecipeBookExclusionZones());
  258. displayHelper.registerProvider(new DisplayHelper.DisplayBoundsProvider<ContainerScreen<?>>() {
  259. @Override
  260. public Rectangle getScreenBounds(ContainerScreen<?> screen) {
  261. return new Rectangle(screen.x, screen.y, screen.containerWidth, screen.containerHeight);
  262. }
  263. @Override
  264. public Class<?> getBaseSupportedClass() {
  265. return ContainerScreen.class;
  266. }
  267. });
  268. }
  269. @Override
  270. public void registerOthers(RecipeHelper recipeHelper) {
  271. recipeHelper.registerAutoCraftingHandler(new DefaultRecipeBookHandler());
  272. recipeHelper.registerWorkingStations(CRAFTING, EntryStack.create(Items.CRAFTING_TABLE));
  273. recipeHelper.registerWorkingStations(SMELTING, EntryStack.create(Items.FURNACE));
  274. recipeHelper.registerWorkingStations(SMOKING, EntryStack.create(Items.SMOKER));
  275. recipeHelper.registerWorkingStations(BLASTING, EntryStack.create(Items.BLAST_FURNACE));
  276. recipeHelper.registerWorkingStations(CAMPFIRE, EntryStack.create(Items.CAMPFIRE));
  277. recipeHelper.registerWorkingStations(FUEL, EntryStack.create(Items.FURNACE), EntryStack.create(Items.SMOKER), EntryStack.create(Items.BLAST_FURNACE));
  278. recipeHelper.registerWorkingStations(BREWING, EntryStack.create(Items.BREWING_STAND));
  279. recipeHelper.registerWorkingStations(STONE_CUTTING, EntryStack.create(Items.STONECUTTER));
  280. recipeHelper.registerWorkingStations(COMPOSTING, EntryStack.create(Items.COMPOSTER));
  281. recipeHelper.registerWorkingStations(SMITHING, EntryStack.create(Items.SMITHING_TABLE));
  282. recipeHelper.registerWorkingStations(BEACON, EntryStack.create(Items.BEACON));
  283. Tag<Item> axes = MinecraftClient.getInstance().getNetworkHandler().getTagManager().getItems().getTag(new Identifier("fabric", "axes"));
  284. if (axes != null) {
  285. for (Item item : axes.values()) {
  286. recipeHelper.registerWorkingStations(STRIPPING, EntryStack.create(item));
  287. }
  288. }
  289. recipeHelper.removeAutoCraftButton(FUEL);
  290. recipeHelper.removeAutoCraftButton(COMPOSTING);
  291. recipeHelper.removeAutoCraftButton(BEACON);
  292. recipeHelper.removeAutoCraftButton(INFO);
  293. recipeHelper.registerScreenClickArea(new Rectangle(88, 32, 28, 23), CraftingTableScreen.class, CRAFTING);
  294. recipeHelper.registerScreenClickArea(new Rectangle(137, 29, 10, 13), InventoryScreen.class, CRAFTING);
  295. recipeHelper.registerScreenClickArea(new Rectangle(97, 16, 14, 30), BrewingStandScreen.class, BREWING);
  296. recipeHelper.registerScreenClickArea(new Rectangle(78, 32, 28, 23), FurnaceScreen.class, SMELTING);
  297. recipeHelper.registerScreenClickArea(new Rectangle(78, 32, 28, 23), SmokerScreen.class, SMOKING);
  298. recipeHelper.registerScreenClickArea(new Rectangle(78, 32, 28, 23), BlastFurnaceScreen.class, BLASTING);
  299. FluidSupportProvider.getInstance().registerFluidProvider(new FluidSupportProvider.FluidProvider() {
  300. @Override
  301. public @NotNull EntryStack itemToFluid(@NotNull EntryStack itemStack) {
  302. Item item = itemStack.getItem();
  303. if (item instanceof BucketItem)
  304. return EntryStack.create(((BucketItem) item).fluid, 1000);
  305. return EntryStack.empty();
  306. }
  307. });
  308. // SubsetsRegistry subsetsRegistry = SubsetsRegistry.INSTANCE;
  309. // subsetsRegistry.registerPathEntry("roughlyenoughitems:food", EntryStack.create(Items.MILK_BUCKET));
  310. // subsetsRegistry.registerPathEntry("roughlyenoughitems:food/roughlyenoughitems:cookies", EntryStack.create(Items.COOKIE));
  311. }
  312. @Override
  313. public int getPriority() {
  314. return -1;
  315. }
  316. }