Skip to content

Commit

Permalink
Clarify API for ISubtypeInterpreter (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Aug 26, 2017
1 parent 64e9f32 commit 251bd0a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,4 +5,4 @@ curse_project_id=238222

version_major=4
version_minor=7
version_patch=7
version_patch=8
23 changes: 21 additions & 2 deletions src/api/java/mezz/jei/api/ISubtypeRegistry.java
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

import java.util.function.Function;

/**
* Tell JEI how to interpret NBT tags and capabilities when comparing and looking up items.
* <p>
Expand Down Expand Up @@ -59,13 +61,30 @@ public interface ISubtypeRegistry {
boolean hasSubtypeInterpreter(ItemStack itemStack);

@FunctionalInterface
interface ISubtypeInterpreter {
interface ISubtypeInterpreter extends Function<ItemStack, String> {
String NONE = "";

/**
* Get the data from an itemStack that is relevant to telling subtypes apart.
* This should account for meta, nbt, and anything else that's relevant.
* Return {@link #NONE} if there is no data used for subtypes.
*
* @since JEI 4.7.8
*/
@Override
String apply(ItemStack itemStack);

/**
* Get the data from an itemStack that is relevant to telling subtypes apart.
* This should account for meta, nbt, and anything else that's relevant.
* Returns null if there is no data used for subtypes.
*
* @deprecated since JEI 4.7.8
*/
@Deprecated
@Nullable
String getSubtypeInfo(ItemStack itemStack);
default String getSubtypeInfo(ItemStack itemStack) {
return apply(itemStack);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java
Expand Up @@ -83,7 +83,7 @@ public void registerItemSubtypes(ISubtypeRegistry subtypeRegistry) {
});
subtypeRegistry.registerSubtypeInterpreter(Items.SPAWN_EGG, itemStack -> {
ResourceLocation resourceLocation = ItemMonsterPlacer.getNamedIdFrom(itemStack);
return resourceLocation == null ? null : resourceLocation.toString();
return resourceLocation == null ? ISubtypeRegistry.ISubtypeInterpreter.NONE : resourceLocation.toString();
});
}

Expand Down
@@ -1,6 +1,5 @@
package mezz.jei.plugins.vanilla.brewing;

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

import mezz.jei.api.ISubtypeRegistry;
Expand All @@ -16,11 +15,10 @@ private PotionSubtypeInterpreter() {

}

@Nullable
@Override
public String getSubtypeInfo(ItemStack itemStack) {
public String apply(ItemStack itemStack) {
if (!itemStack.hasTagCompound()) {
return null;
return ISubtypeRegistry.ISubtypeInterpreter.NONE;
}
PotionType potionType = PotionUtils.getPotionFromItem(itemStack);
String potionTypeString = potionType.getNamePrefixed("");
Expand Down
Expand Up @@ -154,9 +154,8 @@ private FluidSubtypeInterpreter() {

}

@Nullable
@Override
public String getSubtypeInfo(ItemStack itemStack) {
public String apply(ItemStack itemStack) {
if (itemStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
IFluidHandler capability = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
if (capability != null) {
Expand All @@ -178,7 +177,7 @@ public String getSubtypeInfo(ItemStack itemStack) {
}
}
}
return null;
return ISubtypeRegistry.ISubtypeInterpreter.NONE;
}

@Nullable
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/mezz/jei/runtime/SubtypeRegistry.java
Expand Up @@ -67,12 +67,11 @@ private static class AllNbt implements ISubtypeInterpreter {
private AllNbt() {
}

@Nullable
@Override
public String getSubtypeInfo(ItemStack itemStack) {
public String apply(ItemStack itemStack) {
NBTTagCompound nbtTagCompound = itemStack.getTagCompound();
if (nbtTagCompound == null || nbtTagCompound.hasNoTags()) {
return null;
return ISubtypeInterpreter.NONE;
}
return nbtTagCompound.toString();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mezz/jei/startup/StackHelper.java
Expand Up @@ -382,7 +382,9 @@ public String getUniqueIdentifierForStack(ItemStack stack, UidMode mode) {
if (mode != UidMode.WILDCARD && metadata != OreDictionary.WILDCARD_VALUE) {
String subtypeInfo = subtypeRegistry.getSubtypeInfo(stack);
if (subtypeInfo != null) {
itemKey.append(':').append(subtypeInfo);
if (!subtypeInfo.isEmpty()) {
itemKey.append(':').append(subtypeInfo);
}
} else {
if (mode == UidMode.FULL) {
itemKey.append(':').append(metadata);
Expand Down

0 comments on commit 251bd0a

Please sign in to comment.