mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 08:17:35 +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
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user