Added hard cap clamp

This commit is contained in:
Auxilor 2021-08-18 17:32:16 +01:00
parent f7d47fa624
commit 4001d7ca46
3 changed files with 105 additions and 0 deletions

View File

@ -62,12 +62,23 @@ public class ItemConversionOptions {
@Getter
private boolean deletingIllegal = false;
/**
* If hard cap clamp is enabled.
*/
@Getter
private boolean usingHardCapClamp = false;
/**
* If disabled enchantments should be removed entirely.
*/
@Getter
private boolean removeDisabled = false;
/**
* Reload the options.
*
* @param plugin Instance of ecoenchants.
*/
public void reload(@NotNull final EcoPlugin plugin) {
usingLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.enabled");
usingAggressiveLoreGetter = plugin.getConfigYml().getBool("advanced.lore-getter.aggressive");
@ -79,6 +90,7 @@ public class ItemConversionOptions {
removingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.enabled");
deletingIllegal = plugin.getConfigYml().getBool("advanced.remove-illegal.delete-item");
removeDisabled = plugin.getConfigYml().getBool("advanced.remove-disabled.enabled");
usingHardCapClamp = plugin.getConfigYml().getBool("advanced.hard-cap-clamp.enabled");
}
static {

View File

@ -415,4 +415,92 @@ public class ItemConversions extends PluginDependent<EcoPlugin> implements Liste
itemStack.setItemMeta(meta);
}
/**
* On player hold item.
* <p>
* Listener for conversion.
*
* @param event The event to listen for.
*/
@EventHandler
public void hardCapClamp(@NotNull final PlayerItemHeldEvent event) {
if (!ItemConversionOptions.isUsingHardCapClamp()) {
return;
}
if (!this.getPlugin().getConfigYml().getBool("anvil.hard-cap.enabled")) {
return;
}
if (event.getPlayer().hasPermission("ecoenchants.anvil.bypasshardcap")) {
return;
}
ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot());
clampHardCap(itemStack);
}
private void clampHardCap(@Nullable final ItemStack itemStack) {
if (itemStack == null) {
return;
}
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
}
Map<Enchantment, Integer> enchants = FastItemStack.wrap(itemStack).getEnchantmentsOnItem(true);
Map<Enchantment, Integer> replacement = new HashMap<>();
int i = 0;
for (Map.Entry<Enchantment, Integer> entry : enchants.entrySet()) {
if (i >= this.getPlugin().getConfigYml().getInt("anvil.hard-cap.cap")) {
return;
}
Enchantment enchantment = entry.getKey();
if (enchantment instanceof EcoEnchant enchant) {
if (!enchant.hasFlag("hard-cap-ignore")) {
i++;
replacement.put(entry.getKey(), entry.getValue());
}
} else {
i++;
replacement.put(entry.getKey(), entry.getValue());
}
}
for (Map.Entry<Enchantment, Integer> entry : enchants.entrySet()) {
Enchantment enchantment = entry.getKey();
if (enchantment instanceof EcoEnchant enchant) {
if (enchant.hasFlag("hard-cap-ignore")) {
replacement.put(entry.getKey(), entry.getValue());
}
}
}
if (meta instanceof EnchantmentStorageMeta storageMeta) {
storageMeta.getStoredEnchants().forEach((enchantment, integer) -> {
storageMeta.removeStoredEnchant(enchantment);
});
replacement.forEach((enchantment, integer) -> {
storageMeta.addStoredEnchant(enchantment, integer, true);
});
} else {
meta.getEnchants().forEach((enchantment, integer) -> {
meta.removeEnchant(enchantment);
});
replacement.forEach((enchantment, integer) -> {
meta.addEnchant(enchantment, integer, true);
});
}
itemStack.setItemMeta(meta);
}
}

View File

@ -176,3 +176,8 @@ advanced:
# Useful if you're disabling enchantments permanently and don't mind removing them from existing players
# Also helps remove deleted enchantments or enchantments from old plugins
enabled: false
hard-cap-clamp:
# Helps reverse players who have items with more enchants than the hard cap.
# Players with ecoenchants.anvil.bypasshardcap permission will be ignored.
# This may impact performance.
enabled: false