Skip to content

Commit

Permalink
Move some specially-handled ItemStack cases into the vanilla plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Dec 9, 2018
1 parent 2590a8b commit 92cf9db
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 97 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ curse_project_id=238222

version_major=4
version_minor=14
version_patch=1
version_patch=2
71 changes: 69 additions & 2 deletions src/api/java/mezz/jei/api/ingredients/IIngredientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import javax.annotation.Nullable;
import java.awt.Color;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IIngredientType;
import net.minecraft.item.ItemStack;

Expand All @@ -21,41 +23,69 @@ public interface IIngredientHelper<V> {
/**
* Expands any wildcard ingredients into all its subtypes.
* Ingredients like FluidStack that have no wildcard ingredients should simply return the collection without editing it.
*
* @since JEI 3.11.0
* Has a default implementation since JEI 4.14.2
*/
default List<V> expandSubtypes(List<V> ingredients) {
return ingredients;
}

/**
* Change one focus into a different focus.
* This can be used to treat lookups of one focus as if it were something else.
*
* On example is looking up fluid blocks, which get translated here into looking up the fluid itself.
*
* @since JEI 4.14.2
*/
List<V> expandSubtypes(List<V> ingredients);
default IFocus<?> translateFocus(IFocus<V> focus, IFocusFactory focusFactory) {
return focus;
}

/**
* Find a matching ingredient from a group of them.
* Used for finding a specific focused ingredient in a recipe.
* Return null if there is no match.
*
* @since JEI 3.11.0
*/
@Nullable
V getMatch(Iterable<V> ingredients, V ingredientToMatch);

/**
* Display name used for searching. Normally this is the first line of the tooltip.
*
* @since JEI 3.11.0
*/
String getDisplayName(V ingredient);

/**
* Unique ID for use in comparing, blacklisting, and looking up ingredients.
*
* @since JEI 3.11.0
*/
String getUniqueId(V ingredient);

/**
* Wildcard ID for use in comparing, blacklisting, and looking up ingredients.
* For an example, ItemStack's wildcardId does not include NBT or meta.
* For ingredients like FluidStacks which do not have a wildcardId, just return the uniqueId here.
*
* @since JEI 3.11.0
*/
String getWildcardId(V ingredient);

/**
* Return the modId of the mod that created this ingredient.
*
* @since JEI 3.11.0
*/
String getModId(V ingredient);

/**
* Return the modId of the mod that should be displayed.
*
* @since JEI 4.8.0
*/
default String getDisplayModId(V ingredient) {
Expand All @@ -65,8 +95,13 @@ default String getDisplayModId(V ingredient) {
/**
* Get the main colors of this ingredient. Used for the color search.
* If this is too difficult to implement for your ingredient, just return an empty collection.
*
* @since JEI 3.11.0
* Has a default implementation since JEI 4.14.2
*/
Iterable<Color> getColors(V ingredient);
default Iterable<Color> getColors(V ingredient) {
return Collections.emptyList();
}

/**
* Return the resource id of the given ingredient.
Expand Down Expand Up @@ -120,9 +155,31 @@ default boolean isIngredientOnServer(V ingredient) {
return true;
}

/**
* Get a list of ore dictionary names that include this ingredient.
* Used for searching by ore dictionary name.
*
* @since JEI 4.14.2
*/
default Collection<String> getOreDictNames(V ingredient) {
return Collections.emptyList();
}

/**
* Get a list of creative tab names that include this ingredient.
* Used for searching by creative tab name.
*
* @since JEI 4.14.2
*/
default Collection<String> getCreativeTabNames(V ingredient) {
return Collections.emptyList();
}

/**
* Get information for error messages involving this ingredient.
* Be extremely careful not to crash here, get as much useful info as possible.
*
* @since JEI 3.11.0
*/
String getErrorInfo(@Nullable V ingredient);

Expand All @@ -144,4 +201,14 @@ default boolean isIngredientOnServer(V ingredient) {
default ItemStack cheatIngredient(V ingredient, boolean fullStack) {
return ItemStack.EMPTY;
}

/**
* @since JEI 4.14.2
*/
interface IFocusFactory {
/**
* Returns a new focus.
*/
<V> IFocus<V> createFocus(IFocus.Mode mode, V ingredient);
}
}
4 changes: 2 additions & 2 deletions src/api/java/mezz/jei/api/recipe/IVanillaRecipeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public interface IVanillaRecipeFactory {
/**
* Adds an anvil recipe for the given inputs and output.
* Create an anvil recipe for the given inputs and output.
*
* @param leftInput The itemStack placed on the left slot.
* @param rightInputs The itemStack(s) placed on the right slot.
Expand All @@ -28,7 +28,7 @@ public interface IVanillaRecipeFactory {
IRecipeWrapper createAnvilRecipe(ItemStack leftInput, List<ItemStack> rightInputs, List<ItemStack> outputs);

/**
* Adds an anvil recipe for the given inputs and output.
* Create an anvil recipe for the given inputs and output.
* The number of inputs in the left and right side must match.
*
* @param leftInputs The itemStack(s) placed on the left slot.
Expand Down
39 changes: 8 additions & 31 deletions src/main/java/mezz/jei/ingredients/IngredientListElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import mezz.jei.util.LegacyUtil;
import mezz.jei.util.Log;
import mezz.jei.util.Translator;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -130,36 +125,18 @@ public final List<String> getTooltipStrings() {

@Override
public Collection<String> getOreDictStrings() {
Collection<String> oreDictStrings = new ArrayList<>();

if (ingredient instanceof ItemStack) {
ItemStack itemStack = (ItemStack) ingredient;
for (int oreId : OreDictionary.getOreIDs(itemStack)) {
String oreNameLowercase = OreDictionary.getOreName(oreId).toLowerCase(Locale.ENGLISH);
oreDictStrings.add(oreNameLowercase);
}
}

return oreDictStrings;
Collection<String> oreDictNames = ingredientHelper.getOreDictNames(ingredient);
return oreDictNames.stream()
.map(s -> s.toLowerCase(Locale.ENGLISH))
.collect(Collectors.toList());
}

@Override
public Collection<String> getCreativeTabsStrings() {
Collection<String> creativeTabsStrings = new ArrayList<>();

if (ingredient instanceof ItemStack) {
ItemStack itemStack = (ItemStack) ingredient;
Item item = itemStack.getItem();
for (CreativeTabs creativeTab : item.getCreativeTabs()) {
if (creativeTab != null) {
String creativeTabName = I18n.format(creativeTab.getTranslationKey());
String creativeTabNameLowercase = Translator.toLowercaseWithLocale(creativeTabName);
creativeTabsStrings.add(creativeTabNameLowercase);
}
}
}

return creativeTabsStrings;
Collection<String> creativeTabsStrings = ingredientHelper.getCreativeTabNames(ingredient);
return creativeTabsStrings.stream()
.map(Translator::toLowercaseWithLocale)
.collect(Collectors.toList());
}

@Override
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/mezz/jei/ingredients/Ingredients.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ public <T> void setInputLists(Class<? extends T> ingredientClass, List<List<T>>
}

@Override
public <T> void setInputs(IIngredientType<T> ingredientType, List<T> input) {
public <T> void setInputs(IIngredientType<T> ingredientType, List<T> inputs) {
IIngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
IIngredientHelper<T> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientType);
List<List> expandedInputs = new ArrayList<>();
for (T input1 : input) {
List<T> itemStacks = ingredientHelper.expandSubtypes(Collections.singletonList(input1));
expandedInputs.add(itemStacks);
for (T input : inputs) {
List<T> expandedInput = ingredientHelper.expandSubtypes(Collections.singletonList(input));
expandedInputs.add(expandedInput);
}

this.inputs.put(ingredientType, expandedInputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
import net.minecraft.util.text.TextFormatting;

public class DebugIngredientHelper implements IIngredientHelper<DebugIngredient> {
@Override
public List<DebugIngredient> expandSubtypes(List<DebugIngredient> ingredients) {
return ingredients;
}

@Nullable
@Override
public DebugIngredient getMatch(Iterable<DebugIngredient> ingredients, DebugIngredient ingredientToMatch) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;

Expand Down Expand Up @@ -116,8 +117,15 @@ public void registerIngredients(IModIngredientRegistration ingredientRegistratio
StackHelper stackHelper = Internal.getStackHelper();
ItemStackListFactory itemStackListFactory = new ItemStackListFactory(this.subtypeRegistry);

ingredientRegistration.register(VanillaTypes.ITEM, itemStackListFactory.create(stackHelper), new ItemStackHelper(stackHelper), new ItemStackRenderer());
ingredientRegistration.register(VanillaTypes.FLUID, FluidStackListFactory.create(), new FluidStackHelper(), new FluidStackRenderer());
List<ItemStack> itemStacks = itemStackListFactory.create(stackHelper);
ItemStackHelper itemStackHelper = new ItemStackHelper(stackHelper);
ItemStackRenderer itemStackRenderer = new ItemStackRenderer();
ingredientRegistration.register(VanillaTypes.ITEM, itemStacks, itemStackHelper, itemStackRenderer);

List<FluidStack> fluidStacks = FluidStackListFactory.create();
FluidStackHelper fluidStackHelper = new FluidStackHelper();
FluidStackRenderer fluidStackRenderer = new FluidStackRenderer();
ingredientRegistration.register(VanillaTypes.FLUID, fluidStacks, fluidStackHelper, fluidStackRenderer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
import net.minecraftforge.fluids.FluidUtil;

public class FluidStackHelper implements IIngredientHelper<FluidStack> {
@Override
public List<FluidStack> expandSubtypes(List<FluidStack> contained) {
return contained;
}

@Override
@Nullable
public FluidStack getMatch(Iterable<FluidStack> ingredients, FluidStack toMatch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import javax.annotation.Nullable;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.color.ColorGetter;
import mezz.jei.startup.StackHelper;
import mezz.jei.util.ErrorUtil;
import net.minecraft.block.Block;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.oredict.OreDictionary;

public class ItemStackHelper implements IIngredientHelper<ItemStack> {
private final StackHelper stackHelper;
Expand All @@ -25,6 +37,23 @@ public List<ItemStack> expandSubtypes(List<ItemStack> contained) {
return stackHelper.getAllSubtypes(contained);
}

@Override
public IFocus<?> translateFocus(IFocus<ItemStack> focus, IFocusFactory focusFactory) {
ItemStack itemStack = focus.getValue();
Item item = itemStack.getItem();
// Special case for ItemBlocks containing fluid blocks.
// Nothing crafts those, the player probably wants to look up fluids.
if (item instanceof ItemBlock) {
Block block = ((ItemBlock) item).getBlock();
Fluid fluid = FluidRegistry.lookupFluidForBlock(block);
if (fluid != null) {
FluidStack fluidStack = new FluidStack(fluid, Fluid.BUCKET_VOLUME);
return focusFactory.createFocus(focus.getMode(), fluidStack);
}
}
return focus;
}

@Override
@Nullable
public ItemStack getMatch(Iterable<ItemStack> ingredients, ItemStack toMatch) {
Expand Down Expand Up @@ -115,6 +144,29 @@ public boolean isIngredientOnServer(ItemStack ingredient) {
return ForgeRegistries.ITEMS.containsValue(item);
}

@Override
public Collection<String> getOreDictNames(ItemStack ingredient) {
Collection<String> names = new ArrayList<>();
for (int oreId : OreDictionary.getOreIDs(ingredient)) {
String oreNameLowercase = OreDictionary.getOreName(oreId).toLowerCase(Locale.ENGLISH);
names.add(oreNameLowercase);
}
return names;
}

@Override
public Collection<String> getCreativeTabNames(ItemStack ingredient) {
Collection<String> creativeTabsStrings = new ArrayList<>();
Item item = ingredient.getItem();
for (CreativeTabs creativeTab : item.getCreativeTabs()) {
if (creativeTab != null) {
String creativeTabName = I18n.format(creativeTab.getTranslationKey());
creativeTabsStrings.add(creativeTabName);
}
}
return creativeTabsStrings;
}

@Override
public String getErrorInfo(@Nullable ItemStack ingredient) {
return ErrorUtil.getItemStackInfo(ingredient);
Expand Down
Loading

0 comments on commit 92cf9db

Please sign in to comment.