Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add INbtRegistry to use instead of INbtIgnoreList
- Loading branch information
Showing
8 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mezz.jei; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import mezz.jei.api.INbtRegistry; | ||
import mezz.jei.util.Log; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
|
||
public class NbtRegistry implements INbtRegistry { | ||
private final Map<Item, INbtInterpreter> nbtInterpreters = new HashMap<>(); | ||
|
||
@Override | ||
public void useNbtForSubtypes(@Nonnull Item... items) { | ||
for (Item item : items) { | ||
registerNbtInterpreter(item, AllNbt.INSTANCE); | ||
} | ||
} | ||
|
||
@Override | ||
public void registerNbtInterpreter(@Nullable Item item, @Nullable INbtInterpreter nbtInterpreter) { | ||
if (item == null) { | ||
Log.error("Null item", new NullPointerException()); | ||
return; | ||
} | ||
if (nbtInterpreter == null) { | ||
Log.error("Null nbtInterpreter", new NullPointerException()); | ||
return; | ||
} | ||
|
||
if (nbtInterpreters.containsKey(item)) { | ||
Log.error("An nbtInterpreter is already registered for this item: {}", item, new IllegalArgumentException()); | ||
return; | ||
} | ||
|
||
nbtInterpreters.put(item, nbtInterpreter); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getSubtypeInfoFromNbt(@Nullable ItemStack itemStack) { | ||
if (itemStack == null) { | ||
Log.error("Null itemStack", new NullPointerException()); | ||
return null; | ||
} | ||
|
||
NBTTagCompound tagCompound = itemStack.getTagCompound(); | ||
if (tagCompound == null) { | ||
return null; | ||
} | ||
|
||
Item item = itemStack.getItem(); | ||
if (item == null) { | ||
Log.error("Null item", new NullPointerException()); | ||
return null; | ||
} | ||
|
||
INbtRegistry.INbtInterpreter nbtInterpreter = nbtInterpreters.get(item); | ||
if (nbtInterpreter == null) { | ||
return null; | ||
} | ||
|
||
return nbtInterpreter.getSubtypeInfoFromNbt(tagCompound); | ||
} | ||
|
||
private static class AllNbt implements INbtInterpreter { | ||
public static final AllNbt INSTANCE = new AllNbt(); | ||
|
||
private AllNbt() { } | ||
|
||
@Nullable | ||
@Override | ||
public String getSubtypeInfoFromNbt(@Nonnull NBTTagCompound nbtTagCompound) { | ||
return nbtTagCompound.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package mezz.jei.api; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
|
||
/** | ||
* Tell JEI how to interpret NBT tags are used when comparing and looking up items. | ||
* | ||
* Some items have subtypes, most of them use meta values for this. | ||
* If your item has subtypes that depend on NBT, use this interface so JEI can tell those subtypes apart. | ||
* | ||
* Most items do not use nbt to differentiate subtypes, so this interface is being used instead of a blacklist. | ||
* Replaces {@link INbtIgnoreList}. | ||
* | ||
* @since 3.6.0 | ||
*/ | ||
public interface INbtRegistry { | ||
/** | ||
* Tells JEI to treat all NBT as relevant to these items' subtypes. | ||
*/ | ||
void useNbtForSubtypes(@Nonnull Item... items); | ||
|
||
/** | ||
* Add an nbt interpreter to turn nbt into data that can be used to compare item subtypes. | ||
* | ||
* @param item the item that uses nbt to tell subtypes apart. | ||
* @param nbtInterpreter the nbt interpreter for the item. | ||
*/ | ||
void registerNbtInterpreter(@Nonnull Item item, @Nonnull INbtInterpreter nbtInterpreter); | ||
|
||
/** | ||
* Get the data from an itemStack that is relevant to comparing and telling subtypes apart. | ||
* Returns null if the itemStack has no NBT or the NBT is not used for subtypes. | ||
*/ | ||
@Nullable | ||
String getSubtypeInfoFromNbt(@Nonnull ItemStack itemStack); | ||
|
||
interface INbtInterpreter { | ||
/** | ||
* Get the data from an itemStack that is relevant to telling subtypes apart. | ||
* Returns null if the NBT has no data used for subtypes. | ||
*/ | ||
@Nullable | ||
String getSubtypeInfoFromNbt(@Nonnull NBTTagCompound nbtTagCompound); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters