Fix several minor issues with the enchanted loot events (#3620)

This commit is contained in:
KnightMiner
2026-10-12 02:17:34 +08:00
committed by GitHub
parent 2e544320d3
commit 7729606851
3 changed files with 5 additions and 5 deletions
@@ -1,12 +1,14 @@
--- a/net/minecraft/world/level/storage/loot/predicates/BonusLevelTableCondition.java
+++ b/net/minecraft/world/level/storage/loot/predicates/BonusLevelTableCondition.java
@@ -37,6 +_,10 @@
@@ -37,6 +_,12 @@
public boolean test(LootContext context) {
ItemInstance tool = context.getOptional(LootContextParams.TOOL);
int level = tool != null ? EnchantmentHelper.getItemEnchantmentLevel(this.enchantment, tool) : 0;
+
+ // Neo: Allow mods to modify the enchantment level for loot calculations with relevant context.
+ level = net.neoforged.neoforge.event.EventHooks.getBlockLootEnchantmentLevel(tool, this.enchantment, level, context);
+ if (tool != null) {
+ level = net.neoforged.neoforge.event.EventHooks.getBlockLootEnchantmentLevel(tool, this.enchantment, level, context);
+ }
+
float chance = this.values.get(Math.min(level, this.values.size() - 1));
return context.getRandom().nextFloat() < chance;
@@ -1202,7 +1202,7 @@ public class EventHooks {
/// @param enchLevel The original enchantment level. How it gets determined depends on the particular call site. Generally it's the attacker's effective enchantment level.
/// @param ctx The loot context for the current entity loot evaluation.
public static int getEntityLootEnchantmentLevel(Holder<Enchantment> ench, int enchLevel, LootContext ctx) {
Entity entity = ctx.getOptional(LootContextParams.THIS_ENTITY);
Entity entity = ctx.getOptional(LootContextParams.ATTACKING_ENTITY);
DamageSource src = ctx.getOptional(LootContextParams.DAMAGE_SOURCE);
if (src != null && entity instanceof LivingEntity living) {
var event = new EnchantedEntityLootEvent(living, src, ench, enchLevel);
@@ -15,7 +15,6 @@ import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceWit
import net.neoforged.neoforge.event.entity.living.GetEquipmentDropChanceEvent;
import net.neoforged.neoforge.event.entity.living.LivingEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.Nullable;
/// Fired on the server when an entity is dropping loot and the level of a specific enchantment is being queried.
///
@@ -26,7 +25,6 @@ import org.jspecify.annotations.Nullable;
///
/// If you do not need the additional context of this event, prefer [GetEnchantmentLevelEvent] for managing enchantment levels.
public class EnchantedEntityLootEvent extends LivingEvent {
@Nullable
private final DamageSource damageSource;
private final Holder<Enchantment> enchantment;
private int enchantmentLevel;