mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-24 05:54:31 +00:00
Add support for multiple mods remapping the same item/block/tile entity. (#18)
This commit is contained in:
1 parent
2641e10174
commit
c59ec9bb36
36 files changed
+2904
-310
No files matched your search
@@ -1,24 +1,376 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
public class ItemStackReplacementManager {
|
||||
import com.gtnewhorizons.postea.utility.IDRegistry;
|
||||
import com.gtnewhorizons.postea.utility.MissingMappingHandler;
|
||||
import com.gtnewhorizons.postea.utility.SimpleTransformationRegistry;
|
||||
import com.gtnewhorizons.postea.utility.TransformerRegistry;
|
||||
|
||||
private static final Map<String, Function<NBTTagCompound, NBTTagCompound>> replacementMap = new HashMap<>();
|
||||
/**
|
||||
* Public API used to register item stack transformers via postea's handler system.<br>
|
||||
* <br>
|
||||
* Item stack transformers are applied when something tries to convert any NBT that has an "id" field into an item
|
||||
* stack.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class ItemStackReplacementManager {
|
||||
|
||||
// Public API for converting ItemStacks.
|
||||
// region custom transformer registration
|
||||
|
||||
/**
|
||||
* Adds a custom transformer for a given id.
|
||||
*
|
||||
* @apiNote When the transformer function passed to this function returns a null, value Postea will assume that
|
||||
* the transformer has failed to identify and run the handler on any other handler function registered
|
||||
* to the given ID.
|
||||
* <p>
|
||||
* If you need to perform a simple transformations consider using the following methods instead:
|
||||
* <ul>
|
||||
* <li>item -> item (with same meta): {@link #addSimpleReplacement(String, Item)}</li>
|
||||
* <li>item -> item with specific meta: {@link #addSimpleReplacement(String, Item, int)}</li>
|
||||
* <li>item with meta -> item (with same meta): {@link #addSimpleReplacement(String, int, Item)}</li>
|
||||
* <li>item with meta -> item with specific meta:
|
||||
* {@link #addSimpleReplacement(String, int, Item, int)}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @implNote For performance reasons, Postea assumes that the tag that was passed to the handler hasn't been
|
||||
* modified if you return false. This isn't enforced though since returning false after doing some
|
||||
* modifications has a couple niche uses. eg: having separate handlers for different migrations of a
|
||||
* single thing that can be sunset at different times down the line.
|
||||
*
|
||||
* @param originalId The id of the item to transform.
|
||||
* @param transformer The transformer to apply.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addItemReplacement(String itemName, Function<NBTTagCompound, NBTTagCompound> transformer) {
|
||||
replacementMap.put(itemName, transformer);
|
||||
public static void addTransformationHandler(String originalId,
|
||||
BiFunction<String, NBTTagCompound, Boolean> transformer) {
|
||||
if (originalId == null) throw new IllegalArgumentException("original id is null");
|
||||
if (transformer == null) throw new IllegalArgumentException("transformer is null");
|
||||
TransformerRegistry.addStackTransformer(originalId, transformer);
|
||||
}
|
||||
|
||||
// endregion custom transformer registration
|
||||
|
||||
// region id resolving
|
||||
|
||||
/**
|
||||
* Registers a callback function that will be provided the numeric ID of the given namespaced ID when a world is
|
||||
* first loaded. Since forge doesn't discard stacks associated with missing IDs when loading a world for
|
||||
* performance reasons, instead opting to null the result of the nbt data read, you can store that value into a
|
||||
* static field, and use it to compare against an id provided to you during the execution of a transformation
|
||||
* handler.<br>
|
||||
* <br>
|
||||
* This function is mostly intended to speed up larger transformer handlers since resolving the string id of a
|
||||
* removed id inside a transformer will be slower than simply matching for it. Regular item transformers are
|
||||
* already provided the string ID of the item they are transforming so, there is no reason not to do a switch case
|
||||
* on those. This also overcomes the need to create dummy blocks and items in order to identify
|
||||
* items inside a custom stack transformer.<br>
|
||||
* <br>
|
||||
* For compatibility reasons, it's better not to register dummies to take the place of dummy items. If one mod was
|
||||
* to only convert blocks with a specific meta while another handled the rest, the other mod would either need
|
||||
* additional work to detect your own dummy blocks or even worst, fail to identify the original blocks as it's not
|
||||
* aware of your dummy blocks.
|
||||
*
|
||||
* @param originalId The namespaced ID of a block you wish to identify in one of your transformers.
|
||||
* @param resolver The callback executed to provide the numeric ID of the given namespaced ID.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static Function<NBTTagCompound, NBTTagCompound> getItemReplacement(String itemNameInternal) {
|
||||
return replacementMap.get(itemNameInternal);
|
||||
public static void registerIDResolver(String originalId, Consumer<Integer> resolver) {
|
||||
if (originalId == null) throw new NullPointerException("originalId cannot be null");
|
||||
if (resolver == null) throw new NullPointerException("resolver cannot be null");
|
||||
IDRegistry.registerItemIDResolver(originalId, resolver);
|
||||
}
|
||||
|
||||
// endregion id resolving
|
||||
|
||||
// region missing mapping replacements
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a specific removed item to the given item.<br>
|
||||
* <br>
|
||||
* <b><u>IMPORTANT: This only works if the world does not have an existing id set for the target item. If you need
|
||||
* to transform a missing item into something that was previously given an id (e.g.: Transforming a removed item
|
||||
* into a stick), use {@link #addSimpleReplacement(String, int, Item, int)} with both meta values set to
|
||||
* OreDictionary.WILDCARD_VALUE.</u></b>
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param item The item to remap to.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void replaceMissingMapping(String originalId, Item item) {
|
||||
if (originalId == null) throw new IllegalArgumentException("original id is null");
|
||||
if (item == null) throw new IllegalArgumentException("item is null");
|
||||
MissingMappingHandler.addItemMapping(originalId, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppresses any missing mapping warning for the given id.
|
||||
*
|
||||
* @param originalId The id of the block or item to suppress the warning for.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void ignoreMissingMapping(@Nonnull String originalId) {
|
||||
MissingMappingHandler.addIgnore(originalId);
|
||||
}
|
||||
|
||||
// endregion missing mapping replacements
|
||||
|
||||
// region simple replacements
|
||||
|
||||
// item -> item
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item, whilst maintaining its damage/meta value. This
|
||||
* also adds a matching block replacement if this item happens to be an ItemBlock.
|
||||
*
|
||||
* @apiNote Use this if you are replacing an item with something that was already registered to a world's id
|
||||
* mappings,
|
||||
* (e.g.: Transforming a removed item into a stick)
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param item The item to remap to.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull Item item) {
|
||||
addSimpleReplacement(originalId, OreDictionary.WILDCARD_VALUE, item, OreDictionary.WILDCARD_VALUE, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item, whilst maintaining its damage/meta value.
|
||||
*
|
||||
* @apiNote Use this if you are replacing an item with something that was already registered to a world's id
|
||||
* mappings,
|
||||
* (e.g.: Transforming a removed item into a stick)
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param item The item to remap to.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a block remapper if the item happens to be an ItemBlock.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull Item item, boolean skipBlockRemap) {
|
||||
addSimpleReplacement(
|
||||
originalId,
|
||||
OreDictionary.WILDCARD_VALUE,
|
||||
item,
|
||||
OreDictionary.WILDCARD_VALUE,
|
||||
skipBlockRemap);
|
||||
}
|
||||
|
||||
// item -> item+meta
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item with a given damage/meta value. This also adds a
|
||||
* matching block replacement if this item happens to be an ItemBlock.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param item The item to remap to.
|
||||
* @param newMeta The meta of the item to remap to. OreDictionary.WILDCARD_VALUE maintains the existing value.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull Item item, int newMeta) {
|
||||
addSimpleReplacement(originalId, OreDictionary.WILDCARD_VALUE, item, newMeta, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item with a given damage/meta value.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param item The item to remap to.
|
||||
* @param newMeta The meta of the item to remap to. OreDictionary.WILDCARD_VALUE maintains the existing
|
||||
* value.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a block remapper if the item happens to be an ItemBlock.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull Item item, int newMeta,
|
||||
boolean skipBlockRemap) {
|
||||
addSimpleReplacement(originalId, OreDictionary.WILDCARD_VALUE, item, newMeta, skipBlockRemap);
|
||||
}
|
||||
|
||||
// item -> stack
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item with a given damage/meta value. This also adds a
|
||||
* matching block replacement if this item happens to be an ItemBlock.
|
||||
* <br>
|
||||
* DOES NOT COPY NBT FROM STACK.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param stack The item to remap to.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull ItemStack stack) {
|
||||
addSimpleReplacement(
|
||||
originalId,
|
||||
OreDictionary.WILDCARD_VALUE,
|
||||
stack.getItem(),
|
||||
Items.feather.getDamage(stack),
|
||||
false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap stacks of a given item to another item with a given damage/meta value.
|
||||
* <br>
|
||||
* DOES NOT COPY NBT FROM STACK.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param stack A stack of the item to replace to.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a block remapper if the item happens to be an ItemBlock.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, @Nonnull ItemStack stack,
|
||||
boolean skipBlockRemap) {
|
||||
addSimpleReplacement(
|
||||
originalId,
|
||||
OreDictionary.WILDCARD_VALUE,
|
||||
stack.getItem(),
|
||||
Items.feather.getDamage(stack),
|
||||
skipBlockRemap);
|
||||
}
|
||||
|
||||
// item+meta -> item
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item, whilst maintaining its
|
||||
* damage/meta value. This also adds a matching block replacement if this item happens to be an ItemBlock.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param item The item to remap to.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, int originalMeta, @Nonnull Item item) {
|
||||
addSimpleReplacement(originalId, originalMeta, item, OreDictionary.WILDCARD_VALUE, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item, whilst maintaining its
|
||||
* damage/meta value.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param item The item to remap to.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a item remapper if the .
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, int originalMeta, @Nonnull Item item,
|
||||
boolean skipBlockRemap) {
|
||||
addSimpleReplacement(originalId, originalMeta, item, OreDictionary.WILDCARD_VALUE, skipBlockRemap);
|
||||
}
|
||||
|
||||
// item+meta -> stack
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item with a
|
||||
* given damage/meta value. This also adds a matching block replacement if this item happens to be an ItemBlock.
|
||||
* <br>
|
||||
* DOES NOT COPY NBT FROM STACK.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param stack A stack of the item to replace to.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, int originalMeta, @Nonnull ItemStack stack) {
|
||||
addSimpleReplacement(originalId, originalMeta, stack, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item with a
|
||||
* given damage/meta value.
|
||||
* <br>
|
||||
* DOES NOT COPY NBT FROM STACK.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param stack A stack of the item to replace to.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a block remapper if the item happens to be an ItemBlock.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, int originalMeta, @Nonnull ItemStack stack,
|
||||
boolean skipBlockRemap) {
|
||||
addSimpleReplacement(originalId, originalMeta, stack.getItem(), Items.feather.getDamage(stack), skipBlockRemap);
|
||||
}
|
||||
|
||||
// item+meta -> item+meta
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item with a
|
||||
* given damage/meta value. This also adds a matching block replacement if this item happens to be an ItemBlock.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param item The item to remap to.
|
||||
* @param newMeta The meta of the item to remap to. OreDictionary.WILDCARD_VALUE maintains the existing value.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(@Nonnull String originalId, int originalMeta, @Nonnull Item item,
|
||||
int newMeta) {
|
||||
addSimpleReplacement(originalId, originalMeta, item, newMeta, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mapping to remap all stacks of a given item and damage/meta value to another item with a
|
||||
* given damage/meta value.
|
||||
*
|
||||
* @implNote All transformations registered using this handler are executed in constant time regardless of how many
|
||||
* transformations are registered to a single id.
|
||||
*
|
||||
* @param originalId The id of the item to remap.
|
||||
* @param originalMeta The meta of the item to remap. OreDictionary.WILDCARD_VALUE acts as a wildcard.
|
||||
* @param item The item to remap to.
|
||||
* @param newMeta The meta of the item to remap to. OreDictionary.WILDCARD_VALUE maintains the existing
|
||||
* value.
|
||||
* @param skipBlockRemap Set to true to skip auto-adding a item remapper if the .
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static void addSimpleReplacement(String originalId, int originalMeta, Item item, int newMeta,
|
||||
boolean skipBlockRemap) {
|
||||
if (originalId == null) throw new IllegalArgumentException("original id is null");
|
||||
if (item == null) throw new IllegalArgumentException("item is null");
|
||||
SimpleTransformationRegistry.addSimpleTransformer(originalId, originalMeta, item, newMeta, skipBlockRemap);
|
||||
}
|
||||
|
||||
// endregion simple replacements
|
||||
}
|
||||
Reference in new issue
Block a user