Browse Source

recipe id cache

Unknown 6 years ago
parent
commit
ffd6c5c8ae

+ 1 - 1
src/main/java/me/shedaniel/rei/api/RecipeDisplay.java

@@ -18,7 +18,7 @@ public interface RecipeDisplay<T extends Recipe> {
     /**
      * @return the optional recipe
      */
-    Optional<T> getRecipe();
+    Optional<? extends Recipe> getRecipe();
     
     /**
      * @return a list of items

+ 10 - 2
src/main/java/me/shedaniel/rei/api/RecipeHelper.java

@@ -14,6 +14,7 @@ import net.minecraft.util.Identifier;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Function;
 
 public interface RecipeHelper {
     
@@ -106,10 +107,10 @@ public interface RecipeHelper {
     void registerSpeedCraftButtonArea(Identifier category, ButtonAreaSupplier rectangle);
     
     /**
-     * Registers a default speed crafting button area, which is bottom right
-     *
      * @param category the category of the button area
+     * @deprecated Not required anymore
      */
+    @Deprecated
     void registerDefaultSpeedCraftButtonArea(Identifier category);
     
     /**
@@ -183,6 +184,13 @@ public interface RecipeHelper {
      */
     Optional<DisplaySettings> getCachedCategorySettings(Identifier category);
     
+    /**
+     * Registers a live recipe generator.
+     *
+     * @param liveRecipeGenerator the generator to register
+     * @apiNote Still work in progress
+     */
     void registerLiveRecipeGenerator(LiveRecipeGenerator liveRecipeGenerator);
     
+    <T extends Recipe<?>> void registerRecipes(Identifier category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction);
 }

+ 0 - 2
src/main/java/me/shedaniel/rei/client/ConfigObject.java

@@ -42,8 +42,6 @@ public class ConfigObject {
     
     @Comment("Disable Recipe Book") public boolean disableRecipeBook = false;
     
-    @Comment("Force enable 2019 REI April Fools' joke") public boolean aprilFoolsFish2019 = false;
-    
     public ItemCheatingMode itemCheatingMode = ItemCheatingMode.REI_LIKE;
     
     public boolean lightGrayRecipeBorder = false;

+ 44 - 3
src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java

@@ -13,12 +13,12 @@ import net.minecraft.item.ItemStack;
 import net.minecraft.recipe.Recipe;
 import net.minecraft.recipe.RecipeManager;
 import net.minecraft.util.Identifier;
-import org.apache.logging.log4j.Level;
 
 import java.awt.*;
 import java.util.List;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 public class RecipeHelperImpl implements RecipeHelper {
@@ -36,6 +36,7 @@ public class RecipeHelperImpl implements RecipeHelper {
         VISIBILITY_HANDLER_COMPARATOR = comparator.reversed();
     }
     
+    public final List<RecipeFunction> recipeFunctions = Lists.newArrayList();
     private final AtomicInteger recipeCount = new AtomicInteger();
     private final Map<Identifier, List<RecipeDisplay>> recipeCategoryListMap = Maps.newHashMap();
     private final Map<Identifier, DisplaySettings> categoryDisplaySettingsMap = Maps.newHashMap();
@@ -91,6 +92,13 @@ public class RecipeHelperImpl implements RecipeHelper {
         recipeCategoryListMap.get(categoryIdentifier).add(display);
     }
     
+    private void registerDisplay(Identifier categoryIdentifier, RecipeDisplay display, int index) {
+        if (!recipeCategoryListMap.containsKey(categoryIdentifier))
+            return;
+        recipeCount.incrementAndGet();
+        recipeCategoryListMap.get(categoryIdentifier).add(index, display);
+    }
+    
     @Override
     public Map<RecipeCategory, List<RecipeDisplay>> getRecipesFor(ItemStack stack) {
         Map<Identifier, List<RecipeDisplay>> categoriesMap = new HashMap<>();
@@ -166,13 +174,17 @@ public class RecipeHelperImpl implements RecipeHelper {
     @Override
     public Optional<ButtonAreaSupplier> getSpeedCraftButtonArea(RecipeCategory category) {
         if (!speedCraftAreaSupplierMap.containsKey(category.getIdentifier()))
-            return Optional.empty();
+            return Optional.ofNullable(bounds -> new Rectangle((int) bounds.getMaxX() - 16, (int) bounds.getMaxY() - 16, 10, 10));
         return Optional.ofNullable(speedCraftAreaSupplierMap.get(category.getIdentifier()));
     }
     
     @Override
     public void registerSpeedCraftButtonArea(Identifier category, ButtonAreaSupplier rectangle) {
-        speedCraftAreaSupplierMap.put(category, rectangle);
+        if (rectangle == null) {
+            if (speedCraftAreaSupplierMap.containsKey(category))
+                speedCraftAreaSupplierMap.remove(category);
+        } else
+            speedCraftAreaSupplierMap.put(category, rectangle);
     }
     
     @Override
@@ -203,6 +215,7 @@ public class RecipeHelperImpl implements RecipeHelper {
         this.speedCraftAreaSupplierMap.clear();
         this.speedCraftFunctionalMap.clear();
         this.categoryDisplaySettingsMap.clear();
+        this.recipeFunctions.clear();
         this.displayVisibilityHandlers.clear();
         this.liveRecipeGenerators.clear();
         ((DisplayHelperImpl) RoughlyEnoughItemsCore.getDisplayHelper()).resetCache();
@@ -235,6 +248,17 @@ public class RecipeHelperImpl implements RecipeHelper {
                 RoughlyEnoughItemsCore.LOGGER.error("[REI] " + identifier.toString() + " plugin failed to load!", e);
             }
         });
+        if (!recipeFunctions.isEmpty()) {
+            List<Recipe> allSortedRecipes = getAllSortedRecipes();
+            Collections.reverse(allSortedRecipes);
+            recipeFunctions.forEach(recipeFunction -> {
+                try {
+                    allSortedRecipes.stream().filter(recipe -> recipeFunction.recipeClass.isAssignableFrom(recipe.getClass())).forEach(t -> registerDisplay(recipeFunction.category, (RecipeDisplay) recipeFunction.mappingFunction.apply(t), 0));
+                } catch (Exception e) {
+                    RoughlyEnoughItemsCore.LOGGER.error("[REI] Failed to add recipes!", e);
+                }
+            });
+        }
         if (getDisplayVisibilityHandlers().isEmpty())
             registerRecipeVisibilityHandler(new DisplayVisibilityHandler() {
                 @Override
@@ -312,6 +336,11 @@ public class RecipeHelperImpl implements RecipeHelper {
         return true;
     }
     
+    @Override
+    public <T extends Recipe<?>> void registerRecipes(Identifier category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction) {
+        recipeFunctions.add(new RecipeFunction(category, recipeClass, mappingFunction));
+    }
+    
     @Override
     public Optional<DisplaySettings> getCachedCategorySettings(Identifier category) {
         return categoryDisplaySettingsMap.entrySet().stream().filter(entry -> entry.getKey().equals(category)).map(Map.Entry::getValue).findAny();
@@ -322,4 +351,16 @@ public class RecipeHelperImpl implements RecipeHelper {
         liveRecipeGenerators.add(liveRecipeGenerator);
     }
     
+    private class RecipeFunction {
+        Identifier category;
+        Class recipeClass;
+        Function mappingFunction;
+        
+        public RecipeFunction(Identifier category, Class<?> recipeClass, Function<?, RecipeDisplay> mappingFunction) {
+            this.category = category;
+            this.recipeClass = recipeClass;
+            this.mappingFunction = mappingFunction;
+        }
+    }
+    
 }

+ 0 - 3
src/main/java/me/shedaniel/rei/gui/widget/SlotWidget.java

@@ -32,7 +32,6 @@ public class SlotWidget extends HighlightableWidget {
     
     private static final Identifier RECIPE_GUI = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
     private static final Identifier RECIPE_GUI_DARK = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer_dark.png");
-    private static final ItemStackRenderer TROPICAL_FISH_RENDERABLE = Renderable.fromItemStack(Items.TROPICAL_FISH.getDefaultStack());
     private List<Renderer> renderers = new LinkedList<>();
     private boolean drawBackground, showToolTips, clickToMoreRecipes, drawHighlightedBackground;
     private int x, y;
@@ -117,8 +116,6 @@ public class SlotWidget extends HighlightableWidget {
             GlStateManager.enableDepthTest();
         }
         if (isCurrentRendererItem() && !getCurrentItemStack().isEmpty()) {
-            if (RoughlyEnoughItemsCore.getConfigManager().getConfig().aprilFoolsFish2019 && !highlighted)
-                renderer = TROPICAL_FISH_RENDERABLE;
             renderer.setBlitOffset(200);
             renderer.render(x + 8, y + 6, mouseX, mouseY, delta);
             if (!getCurrentItemStack().isEmpty() && highlighted && showToolTips)

+ 10 - 0
src/main/java/me/shedaniel/rei/gui/widget/SpeedCraftingButtonWidget.java

@@ -8,7 +8,9 @@ package me.shedaniel.rei.gui.widget;
 import me.shedaniel.rei.api.RecipeDisplay;
 import me.shedaniel.rei.api.SpeedCraftFunctional;
 import me.shedaniel.rei.client.ScreenHelper;
+import net.minecraft.ChatFormat;
 import net.minecraft.client.resource.language.I18n;
+import net.minecraft.recipe.Recipe;
 
 import java.awt.*;
 import java.util.Optional;
@@ -18,11 +20,14 @@ public class SpeedCraftingButtonWidget extends ButtonWidget {
     
     private final Supplier<RecipeDisplay> displaySupplier;
     private final SpeedCraftFunctional functional;
+    private String extraTooltip;
     
     public SpeedCraftingButtonWidget(Rectangle rectangle, String text, SpeedCraftFunctional functional, Supplier<RecipeDisplay> displaySupplier) {
         super(rectangle, text);
         this.displaySupplier = displaySupplier;
         this.functional = functional;
+        Optional<Recipe> recipe = displaySupplier.get().getRecipe();
+        extraTooltip = recipe.isPresent() ? I18n.translate("text.rei.recipe_id", ChatFormat.GRAY.toString(), recipe.get().getId().toString()) : "";
     }
     
     @Override
@@ -40,6 +45,11 @@ public class SpeedCraftingButtonWidget extends ButtonWidget {
     
     @Override
     public Optional<String> getTooltips() {
+        if (this.minecraft.options.advancedItemTooltips)
+            if (enabled)
+                return Optional.ofNullable(I18n.translate("text.speed_craft.move_items") + extraTooltip);
+            else
+                return Optional.ofNullable(I18n.translate("text.speed_craft.failed_move_items") + extraTooltip);
         if (enabled)
             return Optional.ofNullable(I18n.translate("text.speed_craft.move_items"));
         else

+ 12 - 23
src/main/java/me/shedaniel/rei/plugin/DefaultPlugin.java

@@ -102,21 +102,13 @@ public class DefaultPlugin implements REIPluginEntry {
     
     @Override
     public void registerRecipeDisplays(RecipeHelper recipeHelper) {
-        for(Recipe recipe : recipeHelper.getAllSortedRecipes())
-            if (recipe instanceof ShapelessRecipe)
-                recipeHelper.registerDisplay(CRAFTING, new DefaultShapelessDisplay((ShapelessRecipe) recipe));
-            else if (recipe instanceof ShapedRecipe)
-                recipeHelper.registerDisplay(CRAFTING, new DefaultShapedDisplay((ShapedRecipe) recipe));
-            else if (recipe instanceof SmeltingRecipe)
-                recipeHelper.registerDisplay(SMELTING, new DefaultSmeltingDisplay((SmeltingRecipe) recipe));
-            else if (recipe instanceof SmokingRecipe)
-                recipeHelper.registerDisplay(SMOKING, new DefaultSmokingDisplay((SmokingRecipe) recipe));
-            else if (recipe instanceof BlastingRecipe)
-                recipeHelper.registerDisplay(BLASTING, new DefaultBlastingDisplay((BlastingRecipe) recipe));
-            else if (recipe instanceof CampfireCookingRecipe)
-                recipeHelper.registerDisplay(CAMPFIRE, new DefaultCampfireDisplay((CampfireCookingRecipe) recipe));
-            else if (recipe instanceof StonecuttingRecipe)
-                recipeHelper.registerDisplay(STONE_CUTTING, new DefaultStoneCuttingDisplay((StonecuttingRecipe) recipe));
+        recipeHelper.registerRecipes(CRAFTING, ShapelessRecipe.class, DefaultShapelessDisplay::new);
+        recipeHelper.registerRecipes(CRAFTING, ShapedRecipe.class, DefaultShapedDisplay::new);
+        recipeHelper.registerRecipes(SMELTING, SmeltingRecipe.class, DefaultSmeltingDisplay::new);
+        recipeHelper.registerRecipes(SMOKING, SmokingRecipe.class, DefaultSmokingDisplay::new);
+        recipeHelper.registerRecipes(BLASTING, BlastingRecipe.class, DefaultBlastingDisplay::new);
+        recipeHelper.registerRecipes(CAMPFIRE, CampfireCookingRecipe.class, DefaultCampfireDisplay::new);
+        recipeHelper.registerRecipes(STONE_CUTTING, StonecuttingRecipe.class, DefaultStoneCuttingDisplay::new);
         BREWING_DISPLAYS.stream().forEachOrdered(display -> recipeHelper.registerDisplay(BREWING, display));
         List<ItemStack> arrowStack = Collections.singletonList(Items.ARROW.getDefaultStack());
         RoughlyEnoughItemsCore.getItemRegisterer().getItemList().stream().filter(stack -> stack.getItem().equals(Items.LINGERING_POTION)).forEach(stack -> {
@@ -254,10 +246,7 @@ public class DefaultPlugin implements REIPluginEntry {
                 return -1f;
             }
         });
-        recipeHelper.registerDefaultSpeedCraftButtonArea(DefaultPlugin.CRAFTING);
-        recipeHelper.registerDefaultSpeedCraftButtonArea(DefaultPlugin.SMELTING);
-        recipeHelper.registerDefaultSpeedCraftButtonArea(DefaultPlugin.SMOKING);
-        recipeHelper.registerDefaultSpeedCraftButtonArea(DefaultPlugin.BLASTING);
+        recipeHelper.registerSpeedCraftButtonArea(DefaultPlugin.CAMPFIRE, bounds -> new Rectangle((int) bounds.getMaxX() - 16, bounds.y + 6, 10, 10));
         recipeHelper.registerSpeedCraftFunctional(DefaultPlugin.CRAFTING, new SpeedCraftFunctional<DefaultCraftingDisplay>() {
             @Override
             public Class[] getFunctioningFor() {
@@ -274,7 +263,7 @@ public class DefaultPlugin implements REIPluginEntry {
                     ((RecipeBookGuiHooks) (((InventoryScreen) screen).getRecipeBookGui())).rei_getGhostSlots().reset();
                 else
                     return false;
-                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe) recipe.getRecipe().get(), Screen.hasShiftDown());
+                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe<?>) recipe.getRecipe().get(), Screen.hasShiftDown());
                 return true;
             }
             
@@ -297,7 +286,7 @@ public class DefaultPlugin implements REIPluginEntry {
                     ((RecipeBookGuiHooks) (((FurnaceScreen) screen).getRecipeBookGui())).rei_getGhostSlots().reset();
                 else
                     return false;
-                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe) recipe.getRecipe().get(), Screen.hasShiftDown());
+                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe<?>) recipe.getRecipe().get(), Screen.hasShiftDown());
                 return true;
             }
             
@@ -320,7 +309,7 @@ public class DefaultPlugin implements REIPluginEntry {
                     ((RecipeBookGuiHooks) (((SmokerScreen) screen).getRecipeBookGui())).rei_getGhostSlots().reset();
                 else
                     return false;
-                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe) recipe.getRecipe().get(), Screen.hasShiftDown());
+                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe<?>) recipe.getRecipe().get(), Screen.hasShiftDown());
                 return true;
             }
             
@@ -348,7 +337,7 @@ public class DefaultPlugin implements REIPluginEntry {
                     ((RecipeBookGuiHooks) (((BlastFurnaceScreen) screen).getRecipeBookGui())).rei_getGhostSlots().reset();
                 else
                     return false;
-                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe) recipe.getRecipe().get(), Screen.hasShiftDown());
+                MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, (Recipe<?>) recipe.getRecipe().get(), Screen.hasShiftDown());
                 return true;
             }
         });

+ 0 - 2
src/main/java/me/shedaniel/rei/utils/ClothScreenRegistry.java

@@ -79,8 +79,6 @@ public class ClothScreenRegistry {
         modules.addOption(new BooleanListEntry("text.rei.config.enable_craftable_only", RoughlyEnoughItemsCore.getConfigManager().getConfig().enableCraftableOnlyButton, RESET, () -> true, bool -> RoughlyEnoughItemsCore.getConfigManager().getConfig().enableCraftableOnlyButton = bool, () -> getConfigTooltip("enable_craftable_only")));
         modules.addOption(new BooleanListEntry("text.rei.config.enable_util_buttons", RoughlyEnoughItemsCore.getConfigManager().getConfig().showUtilsButtons, RESET, () -> false, bool -> RoughlyEnoughItemsCore.getConfigManager().getConfig().showUtilsButtons = bool, () -> getConfigTooltip("enable_util_buttons")));
         modules.addOption(new BooleanListEntry("text.rei.config.disable_recipe_book", RoughlyEnoughItemsCore.getConfigManager().getConfig().disableRecipeBook, RESET, () -> false, bool -> RoughlyEnoughItemsCore.getConfigManager().getConfig().disableRecipeBook = bool, () -> getConfigTooltip("disable_recipe_book")));
-        ConfigScreenBuilder.CategoryBuilder aprilFools = builder.addCategory("text.rei.config.april_fools");
-        aprilFools.addOption(new BooleanListEntry("text.rei.config.april_fools.2019", RoughlyEnoughItemsCore.getConfigManager().getConfig().aprilFoolsFish2019, RESET, () -> false, bool -> RoughlyEnoughItemsCore.getConfigManager().getConfig().aprilFoolsFish2019 = bool, () -> getConfigTooltip("april_fools.2019")));
         return builder.build(screen -> {
             ButtonWidget w = new ButtonWidget(6, 6, 60, 20, I18n.translate("text.rei.credits"), widget -> MinecraftClient.getInstance().openScreen(new CreditsScreen(MinecraftClient.getInstance().currentScreen)));
             ((ScreenHooks) screen).cloth_getButtonWidgets().add(0, w);

+ 1 - 0
src/main/resources/assets/roughlyenoughitems/lang/en_us.json

@@ -100,6 +100,7 @@
   "text.rei.config.recipe_screen_type.original": "Original",
   "text.rei.config.recipe_screen_type.villager": "Villager",
   "text.rei.select": "Select",
+  "text.rei.recipe_id": "\n%sRecipe Id: %s",
   "text.rei.recipe_screen_type.selection": "Recipe Screen Type Selection",
   "text.rei.recipe_screen_type.selection.sub": "You can always edit this setting again via the config screen.",
   "_comment": "Config Tooltips",