Added level clamp option

This commit is contained in:
Auxilor 2021-05-07 19:17:26 +01:00
parent 1120f8455b
commit 38737de455
3 changed files with 64 additions and 0 deletions

View File

@ -241,4 +241,58 @@ public class ItemConversions extends PluginDependent implements Listener {
itemStack.setItemMeta(meta);
}
/**
* On player hold item.
* <p>
* Listener for hide fixer.
*
* @param event The event to listen for.
*/
@EventHandler
public void levelClamp(@NotNull final PlayerItemHeldEvent event) {
if (!this.getPlugin().getConfigYml().getBool("advanced.level-clamp.enabled")) {
return;
}
if (event.getPlayer().hasPermission("ecoenchants.bypasslevelclamp")) {
return;
}
ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot());
clampItemLevels(itemStack);
}
private void clampItemLevels(@Nullable final ItemStack itemStack) {
if (itemStack == null) {
return;
}
if (!EnchantmentTarget.ALL.getMaterials().contains(itemStack.getType())) {
return;
}
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
}
if (meta instanceof EnchantmentStorageMeta) {
new HashMap<>(((EnchantmentStorageMeta) meta).getStoredEnchants()).forEach((enchantment, integer) -> {
if (integer > enchantment.getMaxLevel()) {
((EnchantmentStorageMeta) meta).removeStoredEnchant(enchantment);
((EnchantmentStorageMeta) meta).addStoredEnchant(enchantment, enchantment.getMaxLevel(), true);
}
});
} else {
new HashMap<>(meta.getEnchants()).forEach((enchantment, integer) -> {
if (integer > enchantment.getMaxLevel()) {
meta.removeEnchant(enchantment);
}
});
}
itemStack.setItemMeta(meta);
}
}

View File

@ -136,3 +136,9 @@ advanced:
# Registers byName both the internal enchantment name (new method) and the display name (old method).
# There may be potential bugs with having duplicate enchantments, so enable this only if you need it.
enabled: true
level-clamp:
# Helps reverse players who have illegal above-max-level enchantments.
# Any enchantments above their max level will be set to the max level in order to prevent illegal items.
# Players with ecoenchants.bypasslevelclamp permission will be ignored.
# This may impact performance.
enabled: true

View File

@ -53,6 +53,7 @@ permissions:
ecoenchants.randomenchant: true
ecoenchants.randomenchant.bypasshardcap: true
ecoenchants.randombook: true
ecoenchants.bypasslevelclamp: true
ecoenchants.anvil.*: true
ecoenchants.anvil.*:
description: Allows all anvil permissions
@ -94,6 +95,9 @@ permissions:
ecoenchants.anvil.color:
description: Allows color from anvil renames
default: op
ecoenchants.bypasslevelclamp:
description: Allows bypassing the advanced level clamp option
default: op
# Perks
ecoenchants.cooldowntime.half: