forked from neoforged/NeoForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.20.4] Add GetEnchantmentLevelEvent to inspect and modify enchantme…
…nts on arbitrary items. (neoforged#503)
- Loading branch information
1 parent
2d2d2e1
commit da756fd
Showing
6 changed files
with
209 additions
and
21 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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/net/neoforged/neoforge/event/enchanting/GetEnchantmentLevelEvent.java
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,72 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.event.enchanting; | ||
|
||
import java.util.Map; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.neoforged.bus.api.Event; | ||
import net.neoforged.neoforge.common.extensions.IItemStackExtension; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* This event is fired whenever the enchantment level of a particular item is requested for gameplay purposes.<br> | ||
* It is called from {@link IItemStackExtension#getEnchantmentLevel(Enchantment)} and {@link IItemStackExtension#getAllEnchantments()}. | ||
* <p> | ||
* It is not fired for interactions with NBT, which means these changes will not reflect in the item tooltip. | ||
*/ | ||
public class GetEnchantmentLevelEvent extends Event { | ||
|
||
protected final ItemStack stack; | ||
protected final Map<Enchantment, Integer> enchantments; | ||
@Nullable | ||
protected final Enchantment targetEnchant; | ||
|
||
public GetEnchantmentLevelEvent(ItemStack stack, Map<Enchantment, Integer> enchantments, @Nullable Enchantment targetEnchant) { | ||
this.stack = stack; | ||
this.enchantments = enchantments; | ||
this.targetEnchant = targetEnchant; | ||
} | ||
|
||
/** | ||
* Returns the item stack that is being queried against. | ||
*/ | ||
public ItemStack getStack() { | ||
return this.stack; | ||
} | ||
|
||
/** | ||
* Returns the mutable enchantment->level map. | ||
*/ | ||
public Map<Enchantment, Integer> getEnchantments() { | ||
return this.enchantments; | ||
} | ||
|
||
/** | ||
* This method returns the specific enchantment being queried from {@link IItemStackExtension#getEnchantmentLevel(Enchantment)}. | ||
* <p> | ||
* If this is value is present, you only need to adjust the level of that enchantment. | ||
* <p> | ||
* If this value is null, then the event was fired from {@link IItemStackExtension#getAllEnchantments()} and all enchantments should be populated. | ||
* | ||
* @return The specific enchantment being queried, or null, if all enchantments are being requested. | ||
*/ | ||
@Nullable | ||
public Enchantment getTargetEnchant() { | ||
return this.targetEnchant; | ||
} | ||
|
||
/** | ||
* Helper method around {@link #getTargetEnchant()} that checks if the target is the specified enchantment, or if the target is null. | ||
* | ||
* @param ench The enchantment to check. | ||
* @return If modifications to the passed enchantment are relevant for this event. | ||
* @see {@link #getTargetEnchant()} for more information about the target enchantment. | ||
*/ | ||
public boolean isTargetting(Enchantment ench) { | ||
return this.targetEnchant == null || this.targetEnchant == ench; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/src/main/java/net/neoforged/neoforge/debug/enchantment/EnchantmentLevelTests.java
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,61 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.debug.enchantment; | ||
|
||
import java.util.Map; | ||
import net.minecraft.gametest.framework.GameTest; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.Items; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.minecraft.world.item.enchantment.Enchantments; | ||
import net.neoforged.neoforge.event.enchanting.GetEnchantmentLevelEvent; | ||
import net.neoforged.testframework.DynamicTest; | ||
import net.neoforged.testframework.annotation.ForEachTest; | ||
import net.neoforged.testframework.annotation.TestHolder; | ||
import net.neoforged.testframework.gametest.EmptyTemplate; | ||
import net.neoforged.testframework.registration.RegistrationHelper; | ||
|
||
@ForEachTest(groups = EnchantmentLevelTests.GROUP) | ||
public class EnchantmentLevelTests { | ||
public static final String GROUP = "enchantment.level"; | ||
|
||
@GameTest | ||
@EmptyTemplate | ||
@TestHolder(description = "Tests whether the GetEnchantmentLevelEvent can properly modify enchantment levels.") | ||
static void getEnchLevelEvent(final DynamicTest test, final RegistrationHelper reg) { | ||
test.eventListeners().forge().addListener((GetEnchantmentLevelEvent e) -> { | ||
Map<Enchantment, Integer> enchants = e.getEnchantments(); | ||
|
||
// Increase the level of sharpness by 1 in all cases. | ||
if (e.isTargetting(Enchantments.SHARPNESS)) { | ||
enchants.put(Enchantments.SHARPNESS, enchants.getOrDefault(Enchantments.SHARPNESS, 0) + 1); | ||
} | ||
|
||
// Increase the level of fire aspect by 1 if the stack contains specific NBT. | ||
if (e.isTargetting(Enchantments.FIRE_ASPECT)) { | ||
if (e.getStack().getTagElement("boost_fire_aspect") != null) { | ||
enchants.put(Enchantments.FIRE_ASPECT, enchants.getOrDefault(Enchantments.FIRE_ASPECT, 0) + 1); | ||
} | ||
} | ||
}); | ||
|
||
test.onGameTest(helper -> { | ||
|
||
ItemStack stack = new ItemStack(Items.IRON_SWORD); | ||
|
||
helper.assertTrue(stack.getEnchantmentLevel(Enchantments.FIRE_ASPECT) == 0, "Fire Aspect level was not zero"); | ||
helper.assertTrue(stack.getEnchantmentLevel(Enchantments.SHARPNESS) == 1, "Sharpness level was not one"); | ||
|
||
stack.getOrCreateTagElement("boost_fire_aspect"); // Creates the sub-compound "boost_fire_aspect" which will trigger the event listener above. | ||
stack.enchant(Enchantments.SHARPNESS, 5); | ||
|
||
helper.assertTrue(stack.getEnchantmentLevel(Enchantments.FIRE_ASPECT) == 1, "Fire Aspect level was not one"); | ||
helper.assertTrue(stack.getEnchantmentLevel(Enchantments.SHARPNESS) == 6, "Sharpness level was not six"); | ||
|
||
helper.succeed(); | ||
}); | ||
} | ||
} |