mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-28 07:55:53 +00:00
[1.19] Add support for items to add enchantments without setting them in NBT (#8719)
This commit is contained in:
1 parent
683bf99fe4
commit
eabba46595
8 files changed
+115
-2
No files matched your search
@@ -6,6 +6,7 @@
|
||||
package net.minecraftforge.common.extensions;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
@@ -16,6 +17,7 @@ import net.minecraft.world.damagesource.DamageSource;
|
||||
import net.minecraft.world.food.FoodProperties;
|
||||
import net.minecraft.world.item.*;
|
||||
import net.minecraft.world.item.enchantment.Enchantment;
|
||||
import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
@@ -519,6 +521,35 @@ public interface IForgeItem
|
||||
return enchantment.category.canEnchant(stack.getItem());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the level of the enchantment currently present on the stack. By default, returns the enchantment level present in NBT.
|
||||
* Most enchantment implementations rely upon this method.
|
||||
* For consistency, results of this method should be the same as getting the enchantment from {@link #getAllEnchantments(ItemStack)}
|
||||
*
|
||||
* @param stack the item stack being checked
|
||||
* @param enchantment the enchantment being checked for
|
||||
* @return Level of the enchantment, or 0 if not present
|
||||
* @see #getAllEnchantments(ItemStack)
|
||||
*/
|
||||
default int getEnchantmentLevel(ItemStack stack, Enchantment enchantment)
|
||||
{
|
||||
return EnchantmentHelper.getTagEnchantmentLevel(enchantment, stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a map of all enchantments present on the stack. By default, returns the enchantments present in NBT.
|
||||
* Used in several places in code including armor enchantment hooks.
|
||||
* For consistency, any enchantments in the returned map should include the same level in {@link #getEnchantmentLevel(ItemStack, Enchantment)}
|
||||
*
|
||||
* @param stack the item stack being checked
|
||||
* @return Map of all enchantments on the stack, empty if no enchantments are present
|
||||
* @see #getEnchantmentLevel(ItemStack, Enchantment)
|
||||
*/
|
||||
default Map<Enchantment, Integer> getAllEnchantments(ItemStack stack)
|
||||
{
|
||||
return EnchantmentHelper.deserializeEnchantments(stack.getEnchantmentTags());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the player switching between these two item stacks
|
||||
*
|
||||
|
||||
@@ -35,6 +35,8 @@ import net.minecraftforge.common.capabilities.ICapabilitySerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* Extension added to ItemStack that bounces to ItemSack sensitive Item methods. Typically this is just for convince.
|
||||
*/
|
||||
@@ -158,6 +160,38 @@ public interface IForgeItemStack extends ICapabilitySerializable<CompoundTag>
|
||||
return self().getItem().canApplyAtEnchantingTable(self(), enchantment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the level of the enchantment currently present on the stack. By default, returns the enchantment level present in NBT.
|
||||
*
|
||||
* Equivalent to calling {@link net.minecraft.world.item.enchantment.EnchantmentHelper#getItemEnchantmentLevel(Enchantment, ItemStack)}
|
||||
* Use in place of {@link net.minecraft.world.item.enchantment.EnchantmentHelper#getTagEnchantmentLevel(Enchantment, ItemStack)} for checking presence of an enchantment in logic implementing the enchantment behavior.
|
||||
* Use {@link net.minecraft.world.item.enchantment.EnchantmentHelper#getTagEnchantmentLevel(Enchantment, ItemStack)} instead when modifying an item's enchantments.
|
||||
*
|
||||
* @param enchantment the enchantment being checked for
|
||||
* @return Level of the enchantment, or 0 if not present
|
||||
* @see #getAllEnchantments()
|
||||
* @see net.minecraft.world.item.enchantment.EnchantmentHelper#getTagEnchantmentLevel(Enchantment, ItemStack)
|
||||
*/
|
||||
default int getEnchantmentLevel(Enchantment enchantment)
|
||||
{
|
||||
return self().getItem().getEnchantmentLevel(self(), enchantment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a map of all enchantments present on the stack. By default, returns the enchantments present in NBT, ignoring book enchantments.
|
||||
*
|
||||
* Use in place of {@link net.minecraft.world.item.enchantment.EnchantmentHelper#getEnchantments(ItemStack)} for checking presence of an enchantment in logic implementing the enchantment behavior.
|
||||
* Use {@link net.minecraft.world.item.enchantment.EnchantmentHelper#getEnchantments(ItemStack)} instead when modifying an item's enchantments.
|
||||
*
|
||||
* @return Map of all enchantments on the stack, empty if no enchantments are present
|
||||
* @see #getEnchantmentLevel(Enchantment)
|
||||
* @see net.minecraft.world.item.enchantment.EnchantmentHelper#getEnchantments(ItemStack)
|
||||
*/
|
||||
default Map<Enchantment, Integer> getAllEnchantments()
|
||||
{
|
||||
return self().getItem().getAllEnchantments(self());
|
||||
}
|
||||
|
||||
/**
|
||||
* ItemStack sensitive version of getItemEnchantability
|
||||
*
|
||||
|
||||
@@ -56,6 +56,7 @@ net/minecraft/world/item/ItemStack.onItemUse(Lnet/minecraft/world/item/context/U
|
||||
net/minecraft/world/item/ItemStack.onItemUseFirst(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;=|p_41662_
|
||||
net/minecraft/world/item/MapItem.getCustomMapData(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;)Lnet/minecraft/world/level/saveddata/maps/MapItemSavedData;=|p_42910_,p_42911_
|
||||
net/minecraft/world/item/alchemy/PotionBrewing$Mix.<init>(Lnet/minecraftforge/registries/IForgeRegistry;Ljava/lang/Object;Lnet/minecraft/world/item/crafting/Ingredient;Ljava/lang/Object;)V=|registry,p_43536_,p_43537_,p_43538_
|
||||
net/minecraft/world/item/enchantment/EnchantmentHelper.getTagEnchantmentLevel(Lnet/minecraft/world/item/enchantment/Enchantment;Lnet/minecraft/world/item/ItemStack;)I=|p_44844_,p_44845_
|
||||
net/minecraft/world/item/crafting/RecipeManager.<init>(Lnet/minecraftforge/common/crafting/conditions/ICondition$IContext;)V=|context
|
||||
net/minecraft/world/item/crafting/RecipeManager.fromJson(Lnet/minecraft/resources/ResourceLocation;Lcom/google/gson/JsonObject;Lnet/minecraftforge/common/crafting/conditions/ICondition$IContext;)Lnet/minecraft/world/item/crafting/Recipe;=|p_44046_,p_44047_,context
|
||||
net/minecraft/world/level/Level.markAndNotifyBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;II)V=|p_46605_,levelchunk,blockstate,p_46606_,p_46607_,p_46608_
|
||||
|
||||
@@ -177,7 +177,7 @@ public class GlobalLootModifiersTest {
|
||||
public ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
|
||||
ItemStack ctxTool = context.getParamOrNull(LootContextParams.TOOL);
|
||||
//return early if silk-touch is already applied (otherwise we'll get stuck in an infinite loop).
|
||||
if(EnchantmentHelper.getEnchantments(ctxTool).containsKey(Enchantments.SILK_TOUCH)) return generatedLoot;
|
||||
if(ctxTool == null || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, ctxTool) > 0) return generatedLoot;
|
||||
ItemStack fakeTool = ctxTool.copy();
|
||||
fakeTool.enchant(Enchantments.SILK_TOUCH, 1);
|
||||
LootContext.Builder builder = new LootContext.Builder(context);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
"condition": "minecraft:match_tool",
|
||||
"predicate": {
|
||||
"item": "minecraft:bamboo"
|
||||
"items": [ "minecraft:bamboo" ]
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user