Added option to convert enchantments from lore

This commit is contained in:
Auxilor
2021-01-10 19:31:44 +00:00
parent f4f7cc41c5
commit 13c922cb61
9 changed files with 119 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ plugins {
dependencies {
implementation project(":eco-core").getSubprojects()
implementation 'com.willfp:eco:1.1.1'
implementation 'com.willfp:eco:1.1.2'
}
allprojects {

View File

@@ -3,6 +3,6 @@ version rootProject.version
subprojects {
dependencies {
compileOnly 'com.willfp:eco:1.1.1'
compileOnly 'com.willfp:eco:1.1.2'
}
}

View File

@@ -28,6 +28,7 @@ import com.willfp.ecoenchants.enchantments.support.merging.grindstone.Grindstone
import com.willfp.ecoenchants.enchantments.support.obtaining.EnchantingListeners;
import com.willfp.ecoenchants.enchantments.support.obtaining.LootPopulator;
import com.willfp.ecoenchants.enchantments.support.obtaining.VillagerListeners;
import com.willfp.ecoenchants.enchantments.util.HoldItemListener;
import com.willfp.ecoenchants.enchantments.util.WatcherTriggers;
import com.willfp.ecoenchants.integrations.essentials.EssentialsManager;
import com.willfp.ecoenchants.integrations.essentials.plugins.IntegrationEssentials;
@@ -188,7 +189,8 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin {
new GrindstoneListeners(this),
new AnvilListeners(this),
new WatcherTriggers(this),
new VillagerListeners()
new VillagerListeners(),
new HoldItemListener()
);
}

View File

@@ -53,6 +53,12 @@ public class DisplayOptions {
@Getter
private final List<EnchantmentRarity> sortedRarities = new ArrayList<>();
/**
* Allow reading enchantments from lore-based plugins.
*/
@Getter
private boolean useLoreGetter = false;
/**
* Instantiate new display options.
*/
@@ -83,6 +89,8 @@ public class DisplayOptions {
.collect(Collectors.toList()));
sortedRarities.addAll(EnchantmentRarity.values().stream().filter(enchantmentRarity -> !sortedRarities.contains(enchantmentRarity)).collect(Collectors.toList()));
useLoreGetter = Configs.CONFIG.getBool("advanced.lore-getter");
boolean byType = Configs.CONFIG.getBool("lore.sort-by-type");
boolean byLength = Configs.CONFIG.getBool("lore.sort-by-length");
boolean byRarity = Configs.CONFIG.getBool("lore.sort-by-rarity");

View File

@@ -247,6 +247,7 @@ public class EcoEnchants {
public static final String GENERAL_LOCATION = "general-config.";
private static final BiMap<NamespacedKey, EcoEnchant> BY_KEY = HashBiMap.create();
private static final BiMap<String, EcoEnchant> BY_NAME = HashBiMap.create();
public static final EcoEnchant TELEKINESIS = new Telekinesis();
public static final EcoEnchant MARKSMAN = new Marksman();
@@ -496,8 +497,7 @@ public class EcoEnchants {
* @return The matching {@link EcoEnchant}, or null if not found.
*/
public static EcoEnchant getByName(@NotNull final String name) {
Optional<EcoEnchant> matching = values().stream().filter(enchant -> enchant.getName().equalsIgnoreCase(name)).findFirst();
return matching.orElse(null);
return BY_NAME.get(name);
}
/**
@@ -567,7 +567,9 @@ public class EcoEnchants {
*/
public static void addNewEcoEnchant(@NotNull final EcoEnchant enchant) {
BY_KEY.remove(enchant.getKey());
BY_NAME.inverse().remove(enchant);
BY_KEY.put(enchant.getKey(), enchant);
BY_NAME.put(enchant.getName(), enchant);
}
/**
@@ -577,5 +579,6 @@ public class EcoEnchants {
*/
public static void removeEcoEnchant(@NotNull final EcoEnchant enchant) {
BY_KEY.remove(enchant.getKey());
BY_NAME.inverse().remove(enchant);
}
}

View File

@@ -1,11 +1,13 @@
package com.willfp.ecoenchants.enchantments.support.merging.anvil;
import com.willfp.eco.util.ProxyUtils;
import com.willfp.eco.util.config.Configs;
import com.willfp.eco.util.tuplets.Pair;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.proxy.proxies.FastGetEnchantsProxy;
import lombok.experimental.UtilityClass;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
@@ -109,13 +111,13 @@ public class AnvilMerge {
if (left.getItemMeta() instanceof EnchantmentStorageMeta) {
leftEnchants.putAll(((EnchantmentStorageMeta) left.getItemMeta()).getStoredEnchants());
} else {
leftEnchants.putAll(left.getItemMeta().getEnchants());
rightEnchants.putAll(ProxyUtils.getProxy(FastGetEnchantsProxy.class).getEnchantmentsOnItem(left));
}
if (right.getItemMeta() instanceof EnchantmentStorageMeta) {
rightEnchants.putAll(((EnchantmentStorageMeta) right.getItemMeta()).getStoredEnchants());
} else {
rightEnchants.putAll(right.getItemMeta().getEnchants());
rightEnchants.putAll(ProxyUtils.getProxy(FastGetEnchantsProxy.class).getEnchantmentsOnItem(right));
}
leftEnchants.forEach(((enchantment, integer) -> {

View File

@@ -0,0 +1,89 @@
package com.willfp.ecoenchants.enchantments.util;
import com.willfp.eco.util.NumberUtils;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class HoldItemListener implements Listener {
/**
* On player hold item.
* <p>
* Listener for lore conversion.
*
* @param event The event to listen for.
*/
@EventHandler
public void onHoldItem(@NotNull final PlayerItemHeldEvent event) {
ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getNewSlot());
if (itemStack == null) {
return;
}
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
}
if (EnchantDisplay.OPTIONS.isUseLoreGetter()) {
List<String> lore = meta.getLore();
if (lore == null) {
lore = new ArrayList<>();
}
for (String line : new ArrayList<>(lore)) {
String rawLine = line;
line = ChatColor.stripColor(line);
EcoEnchant enchant;
int level;
String[] lineSplit = line.split(" ");
if (lineSplit.length == 1) {
enchant = EcoEnchants.getByName(lineSplit[0]);
level = 1;
} else {
String levelString = lineSplit[lineSplit.length - 1];
levelString = levelString.replace(" ", "");
try {
level = NumberUtils.fromNumeral(levelString);
} catch (IllegalArgumentException e) {
continue;
}
StringBuilder nameBuilder = new StringBuilder();
for (int i = 0; i < lineSplit.length - 1; i++) {
nameBuilder.append(lineSplit[i]);
if (i != lineSplit.length - 2) {
nameBuilder.append(" ");
}
}
enchant = EcoEnchants.getByName(nameBuilder.toString());
}
if (enchant != null) {
lore.remove(rawLine);
meta.addEnchant(enchant, level, true);
}
}
meta.setLore(lore);
itemStack.setItemMeta(meta);
}
}
}

View File

@@ -109,4 +109,10 @@ drops:
force-dropqueue: true
# When combined with collate, this massively reduces calls to getItemMeta, which can hurt performance when many enchants are on an item.
# Without collate, the number of calls may be increased - which will further hurt performance
# Enabling this option makes all drops go through telekinesis listeners
# Enabling this option makes all drops go through telekinesis listeners
advanced:
lore-getter: false
# This converts enchantments with matching names from other enchantment plugins from lore into real enchantments.
# This is very performance-intensive and is not recommended to be used for extended periods of time: enchantments are automatically converted.
# Only use this if absolutely necessary.

View File

@@ -5,7 +5,7 @@ subprojects {
dependencies {
compileOnly project(":eco-core:core-plugin")
compileOnly project(":eco-core:core-proxy")
compileOnly 'com.willfp:eco:1.1.1'
compileOnly 'com.willfp:eco:1.1.2'
compileOnly 'org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT'
}