Skip to content

Commit

Permalink
Fix #473 Keep recipe category items in order of registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 30, 2016
1 parent 447fbf7 commit 516ce1d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
9 changes: 5 additions & 4 deletions src/main/java/mezz/jei/RecipeRegistry.java
Expand Up @@ -13,6 +13,7 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class RecipeRegistry implements IRecipeRegistry {
private final List<IRecipeHandler> recipeHandlers;
private final ImmutableTable<Class, String, IRecipeTransferHandler> recipeTransferHandlers;
private final ImmutableMultimap<Class<? extends GuiContainer>, RecipeClickableArea> recipeClickableAreasMap;
private final ImmutableMultimap<IRecipeCategory, ItemStack> craftItemsForCategories;
private final ImmutableListMultimap<IRecipeCategory, ItemStack> craftItemsForCategories;
private final ImmutableMultimap<String, String> categoriesForCraftItemKeys;
private final ImmutableMap<String, IRecipeCategory> recipeCategoriesMap;
private final ListMultimap<IRecipeCategory, Object> recipesForCategories = ArrayListMultimap.create();
Expand Down Expand Up @@ -88,7 +89,7 @@ public RecipeRegistry(

addRecipes(recipes);

ImmutableMultimap.Builder<IRecipeCategory, ItemStack> craftItemsForCategoriesBuilder = ImmutableMultimap.builder();
ImmutableListMultimap.Builder<IRecipeCategory, ItemStack> craftItemsForCategoriesBuilder = ImmutableListMultimap.builder();
ImmutableMultimap.Builder<String, String> categoriesForCraftItemKeysBuilder = ImmutableMultimap.builder();

IIngredientHelper<ItemStack> ingredientHelper = ingredientRegistry.getIngredientHelper(ItemStack.class);
Expand Down Expand Up @@ -567,8 +568,8 @@ public ImmutableCollection<ItemStack> getCraftingItems(IRecipeCategory recipeCat
}

@Override
public Collection<ItemStack> getCraftingItems(IRecipeCategory recipeCategory, IFocus focus) {
Collection<ItemStack> craftingItems = craftItemsForCategories.get(recipeCategory);
public List<ItemStack> getCraftingItems(IRecipeCategory recipeCategory, IFocus focus) {
List<ItemStack> craftingItems = craftItemsForCategories.get(recipeCategory);
Object ingredient = focus.getValue();
if (ingredient instanceof ItemStack && focus.getMode() == IFocus.Mode.INPUT) {
ItemStack itemStack = (ItemStack) ingredient;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/api/IRecipeRegistry.java
Expand Up @@ -70,7 +70,7 @@ public interface IRecipeRegistry {
*
* @since JEI 3.11.0
*/
Collection<ItemStack> getCraftingItems(IRecipeCategory recipeCategory, IFocus focus);
List<ItemStack> getCraftingItems(IRecipeCategory recipeCategory, IFocus focus);

/**
* Add a new recipe while the game is running.
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/mezz/jei/gui/IRecipeGuiLogic.java
@@ -1,7 +1,6 @@
package mezz.jei.gui;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;

import mezz.jei.api.recipe.IFocus;
Expand Down Expand Up @@ -44,7 +43,7 @@ public interface IRecipeGuiLogic {
@Nullable
IRecipeCategory getRecipeCategory();

Collection<ItemStack> getRecipeCategoryCraftingItems();
List<ItemStack> getRecipeCategoryCraftingItems();

List<RecipeLayout> getRecipeWidgets(int posX, int posY, int spacingY);
}
29 changes: 17 additions & 12 deletions src/main/java/mezz/jei/gui/RecipeCategoryCraftingItemsArea.java
@@ -1,10 +1,10 @@
package mezz.jei.gui;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import mezz.jei.Internal;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.IRecipeRegistry;
Expand Down Expand Up @@ -48,7 +48,7 @@ public RecipeCategoryCraftingItemsArea(IRecipeRegistry recipeRegistry) {
boxDrawable = guiHelper.createDrawable(recipeBackgroundResource, 196, 40, 18, 25);
}

public void updateLayout(Collection<ItemStack> itemStacks, GuiProperties guiProperties) {
public void updateLayout(List<ItemStack> itemStacks, GuiProperties guiProperties) {
IFocus<ItemStack> focus = recipeRegistry.createFocus(IFocus.Mode.NONE, null);
craftingItems = new GuiItemStackGroup(focus);

Expand All @@ -69,18 +69,23 @@ public void updateLayout(Collection<ItemStack> itemStacks, GuiProperties guiProp
left = guiProperties.getGuiLeft() + (guiProperties.getGuiXSize() - totalWidth) / 2; // center it
top = guiProperties.getGuiTop() - boxDrawable.getHeight() + 3; // overlaps the recipe gui slightly

List<ItemStack> itemStacksCopy = new ArrayList<ItemStack>(itemStacks);
for (int i = 0; i < ingredientCount; i++) {
craftingItems.init(i, true, left + 5 + (i * 20), top + 5);
if (i + 1 < ingredientCount) {
ItemStack itemStack = itemStacksCopy.remove(0);
craftingItems.set(i, itemStack);
ListMultimap<Integer, ItemStack> itemStacksForSlots = ArrayListMultimap.create();
for (int i = 0; i < itemStacks.size(); i++) {
ItemStack itemStack = itemStacks.get(i);
if (i < ingredientCount) {
itemStacksForSlots.put(i, itemStack);
} else {
// we're out of space. put all the rest of the items into the last box together so they cycle
craftingItems.set(i, itemStacksCopy);
break;
// start from the end and work our way back, do not override the first one
int index = ingredientCount - (i % ingredientCount);
itemStacksForSlots.put(index, itemStack);
}
}

for (int i = 0; i < ingredientCount; i++) {
craftingItems.init(i, true, left + 5 + (i * 20), top + 5);
List<ItemStack> itemStacksForSlot = itemStacksForSlots.get(i);
craftingItems.set(i, itemStacksForSlot);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/mezz/jei/gui/RecipeGuiLogic.java
Expand Up @@ -3,7 +3,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
Expand Down Expand Up @@ -56,7 +55,7 @@ public State(IFocus focus, List<IRecipeCategory> recipeCategories, int recipeCat
*
* @see IModRegistry#addRecipeCategoryCraftingItem(ItemStack, String...)
*/
private Collection<ItemStack> recipeCategoryCraftingItems = Collections.emptyList();
private List<ItemStack> recipeCategoryCraftingItems = Collections.emptyList();

public RecipeGuiLogic(RecipeRegistry recipeRegistry) {
this.recipeRegistry = recipeRegistry;
Expand Down Expand Up @@ -174,7 +173,7 @@ public IFocus getFocus() {
}

@Override
public Collection<ItemStack> getRecipeCategoryCraftingItems() {
public List<ItemStack> getRecipeCategoryCraftingItems() {
return recipeCategoryCraftingItems;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/mezz/jei/gui/RecipesGui.java
Expand Up @@ -4,7 +4,6 @@
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import mezz.jei.RecipeRegistry;
Expand Down Expand Up @@ -425,7 +424,7 @@ private void updateLayout() {

pageString = logic.getPageString();

Collection<ItemStack> recipeCategoryCraftingItems = logic.getRecipeCategoryCraftingItems();
List<ItemStack> recipeCategoryCraftingItems = logic.getRecipeCategoryCraftingItems();
recipeCategoryCraftingItemsArea.updateLayout(recipeCategoryCraftingItems, GuiProperties.create(this));
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mezz/jei/util/ModRegistry.java
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import mezz.jei.JeiHelpers;
Expand Down Expand Up @@ -35,7 +36,7 @@ public class ModRegistry implements IModRegistry {
private final List<Object> recipes = new ArrayList<Object>();
private final RecipeTransferRegistry recipeTransferRegistry;
private final Multimap<Class<? extends GuiContainer>, RecipeClickableArea> recipeClickableAreas = HashMultimap.create();
private final Multimap<String, ItemStack> craftItemsForCategories = HashMultimap.create();
private final Multimap<String, ItemStack> craftItemsForCategories = ArrayListMultimap.create();
private final List<IRecipeRegistryPlugin> recipeRegistryPlugins = new ArrayList<IRecipeRegistryPlugin>();

public ModRegistry(JeiHelpers jeiHelpers, IItemRegistry itemRegistry, IIngredientRegistry ingredientRegistry) {
Expand Down

0 comments on commit 516ce1d

Please sign in to comment.