mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
Make CustomAnvil, mostly, work with legacy EcoEnchants
This commit is contained in:
parent
4147f018a9
commit
050da6da5b
15 changed files with 275 additions and 29 deletions
|
|
@ -39,6 +39,7 @@ dependencies {
|
|||
// EcoEnchants
|
||||
compileOnly("com.willfp:EcoEnchants:12.5.1")
|
||||
compileOnly("com.willfp:eco:6.70.1")
|
||||
compileOnly(project(":impl:LegacyEcoEnchant"))
|
||||
|
||||
// ExcellentEnchants
|
||||
compileOnly(files("libs/nightcore-2.6.4.jar"))
|
||||
|
|
|
|||
15
impl/LegacyEcoEnchant/build.gradle.kts
Normal file
15
impl/LegacyEcoEnchant/build.gradle.kts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
group = rootProject.group
|
||||
version = rootProject.version
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "2.0.21"
|
||||
}
|
||||
|
||||
// Imitate needed class and method to support legacy version of EcoEnchant
|
||||
dependencies {
|
||||
// Spigot api
|
||||
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.willfp.ecoenchants.enchantments;
|
||||
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Mock class for legacy package of eco enchants
|
||||
*/
|
||||
public class EcoEnchant {
|
||||
|
||||
public boolean getConflictsWithEverything() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean conflictsWith(@NotNull Enchantment enchant) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Set<EnchantmentTarget> getTargets() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.willfp.ecoenchants.enchantments;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mock class for legacy package of eco enchants
|
||||
*/
|
||||
public class EcoEnchants {
|
||||
|
||||
public static List<EcoEnchant> values(){
|
||||
return null; // We don't care here.
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.willfp.ecoenchants.enchantments.meta;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Mock class for legacy package of eco enchants
|
||||
*/
|
||||
public class EnchantmentTarget {
|
||||
|
||||
public Set<Material> getMaterials() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -38,3 +38,5 @@ include("nms:v1_21R2")
|
|||
findProject(":nms:v1_21R2")?.name = "v1_21R2"
|
||||
include("nms:v1_21R3")
|
||||
findProject(":nms:v1_21R3")?.name = "v1_21R3"
|
||||
include(":impl:LegacyEcoEnchant")
|
||||
findProject(":impl:LegacyEcoEnchant")?.name = "LegacyEcoEnchant"
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import java.util.logging.Level;
|
|||
public class CAEnchantmentRegistry {
|
||||
|
||||
private static final CAEnchantmentRegistry instance = new CAEnchantmentRegistry();
|
||||
|
||||
public static CAEnchantmentRegistry getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
|
@ -59,7 +60,6 @@ public class CAEnchantmentRegistry {
|
|||
BukkitEnchantBulkOperation bukkitOperation = new BukkitEnchantBulkOperation();
|
||||
optimisedGetOperators.add(bukkitOperation);
|
||||
optimisedCleanOperators.add(bukkitOperation);
|
||||
|
||||
}
|
||||
|
||||
private static boolean hasWarnedRegistering = false;
|
||||
|
|
@ -69,16 +69,22 @@ public class CAEnchantmentRegistry {
|
|||
* <p>
|
||||
* No guarantee that the enchantment will be present on the config gui if registered late.
|
||||
* (By late I mean after custom anvil startup.)
|
||||
*
|
||||
* @param enchantment The enchantment to be registered.
|
||||
* @return If the operation was successful.
|
||||
*/
|
||||
public boolean register(@NotNull CAEnchantment enchantment) {
|
||||
if (byKeyMap.containsKey(enchantment.getKey())) {
|
||||
if (!enchantment.equals(byKeyMap.get(enchantment.getKey()))) {
|
||||
// We are trying to register the exact same enchantment. so we just skip it.
|
||||
return false;
|
||||
}
|
||||
|
||||
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
||||
"Duplicate registered enchantment. This should NOT happen any time.\n" +
|
||||
"If you are a custom anvil developer. You maybe custom anvil detected your enchantment as a bukkit enchantment. " +
|
||||
"maybe remove enchantment with the same key before registering yours",
|
||||
new IllegalStateException(enchantment.getKey()+" enchantment was already registered"));
|
||||
"Duplicate distinct registered enchantment. This should NOT happen any time.\n" +
|
||||
"If you are a custom anvil developer: Maybe custom anvil detected your enchantment as a bukkit enchantment. " +
|
||||
"you should maybe remove enchantment with the same key before registering yours",
|
||||
new IllegalStateException("enchantment " + enchantment.getKey() + " was already registered"));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -113,6 +119,7 @@ public class CAEnchantmentRegistry {
|
|||
* <p>
|
||||
* No guarantee that the enchantment will absent if the config guis if unregistered late.
|
||||
* (By late I mean after custom anvil startup.)
|
||||
*
|
||||
* @param enchantment The enchantment to be unregistered.
|
||||
* @return If the operation was successful.
|
||||
*/
|
||||
|
|
@ -131,6 +138,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets the enchantment by the provided key.
|
||||
*
|
||||
* @param key Key to fetch.
|
||||
* @return Registered enchantment. null if absent.
|
||||
*/
|
||||
|
|
@ -141,9 +149,9 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets the enchantment by the provided name.
|
||||
*
|
||||
* @param name Name to fetch.
|
||||
* @return Registered enchantment. null if absent.
|
||||
*
|
||||
* @deprecated use {@link #getListByName(String)}
|
||||
*/
|
||||
@Deprecated(since = "1.6.3")
|
||||
|
|
@ -157,6 +165,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets list of enchantment using the provided name.
|
||||
*
|
||||
* @param name Name to fetch.
|
||||
* @return List of registered enchantment.
|
||||
*/
|
||||
|
|
@ -167,6 +176,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets an array of all the registered enchantments.
|
||||
*
|
||||
* @return Array of enchantments.
|
||||
*/
|
||||
@NotNull
|
||||
|
|
@ -176,6 +186,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets a map of all the registered enchantments.
|
||||
*
|
||||
* @return Immutable map of enchantments.
|
||||
*/
|
||||
public Map<NamespacedKey, CAEnchantment> registeredEnchantments() {
|
||||
|
|
@ -184,6 +195,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets a list of all the unoptimised get operation enchantments.
|
||||
*
|
||||
* @return List of unoptimised enchantments.
|
||||
*/
|
||||
@NotNull
|
||||
|
|
@ -193,6 +205,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets a list of all the unoptimised clean operation enchantments.
|
||||
*
|
||||
* @return List of unoptimised enchantments.
|
||||
*/
|
||||
@NotNull
|
||||
|
|
@ -202,6 +215,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Get "clean optimised operation" for get enchantments.
|
||||
*
|
||||
* @return Mutable "clean enchantments optimised operation" list.
|
||||
*/
|
||||
public List<BulkCleanEnchantOperation> getOptimisedCleanOperators() {
|
||||
|
|
@ -210,6 +224,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Get "get optimised operation" for get enchantments.
|
||||
*
|
||||
* @return Mutable "get enchantments optimised operation" list.
|
||||
*/
|
||||
public List<BulkGetEnchantOperation> getOptimisedGetOperators() {
|
||||
|
|
@ -218,6 +233,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Get custom anvil enchantment sorted by name.
|
||||
*
|
||||
* @return An immutable sorted set of every registered enchantment sorted by name.
|
||||
*/
|
||||
public SortedSet<CAEnchantment> getNameSortedEnchantments() {
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@ import java.util.Map;
|
|||
public class BukkitEnchantBulkOperation implements BulkGetEnchantOperation, BulkCleanEnchantOperation {
|
||||
|
||||
@Override
|
||||
public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentList, @NotNull ItemStack item, @NotNull ItemMeta meta) {
|
||||
public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentMap, @NotNull ItemStack item, @NotNull ItemMeta meta) {
|
||||
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
|
||||
((EnchantmentStorageMeta)meta).getStoredEnchants().forEach((enchantment, level) ->
|
||||
enchantmentList.put(EnchantmentApi.getByKey(enchantment.getKey()), level)
|
||||
enchantmentMap.put(EnchantmentApi.getByKey(enchantment.getKey()), level)
|
||||
);
|
||||
} else {
|
||||
item.getEnchantments().forEach((enchantment, level) ->
|
||||
enchantmentList.put(EnchantmentApi.getByKey(enchantment.getKey()), level)
|
||||
enchantmentMap.put(EnchantmentApi.getByKey(enchantment.getKey()), level)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ public interface BulkGetEnchantOperation {
|
|||
|
||||
/**
|
||||
* Bulk get part of the stored enchantment of this item.
|
||||
* @param enchantmentList Mutable map of collected enchantment. should b
|
||||
* @param enchantmentMap Mutable map of collected enchantment. should b
|
||||
* @param item The item to get enchantment from. Should not get edited.
|
||||
* @param meta The item meta to get enchantment from. Should not get edited.
|
||||
*/
|
||||
void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentList, @NotNull ItemStack item, @NotNull ItemMeta meta);
|
||||
void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentMap, @NotNull ItemStack item, @NotNull ItemMeta meta);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ import java.util.Map;
|
|||
public class EnchantSquaredBulkOperation implements BulkGetEnchantOperation, BulkCleanEnchantOperation {
|
||||
|
||||
@Override
|
||||
public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentList, @NotNull ItemStack item, @NotNull ItemMeta meta) {
|
||||
public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentMap, @NotNull ItemStack item, @NotNull ItemMeta meta) {
|
||||
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
||||
if(enchantmentSquared != null){
|
||||
enchantmentSquared.getEnchantmentsSquared(item, enchantmentList);
|
||||
enchantmentSquared.getEnchantmentsSquared(item, enchantmentMap);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -157,4 +157,13 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof CABukkitEnchantment other)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.enchantment.equals(other.getEnchant());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import java.util.Objects;
|
|||
public class CAEnchantSquaredEnchantment extends CAEnchantmentBase {
|
||||
|
||||
public final @NotNull CustomEnchant enchant;
|
||||
|
||||
public CAEnchantSquaredEnchantment(@NotNull CustomEnchant enchant) {
|
||||
super(Objects.requireNonNull(
|
||||
Objects.requireNonNull(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility()).getKeyFromEnchant(enchant)),
|
||||
|
|
@ -25,6 +26,10 @@ public class CAEnchantSquaredEnchantment extends CAEnchantmentBase {
|
|||
|
||||
}
|
||||
|
||||
public @NotNull CustomEnchant getEnchant() {
|
||||
return enchant;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGetOptimised() {
|
||||
return true;
|
||||
|
|
@ -61,4 +66,14 @@ public class CAEnchantSquaredEnchantment extends CAEnchantmentBase {
|
|||
CustomEnchantManager.getInstance().removeEnchant(item, this.enchant.getType());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof CAEnchantSquaredEnchantment other)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.enchant.equals(other.getEnchant());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package xyz.alexcrea.cuanvil.enchant.wrapped;
|
||||
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchant;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class CALegacyEcoEnchant extends CABukkitEnchantment implements AdditionalTestEnchantment {
|
||||
|
||||
private final @NotNull EcoEnchant ecoEnchant;
|
||||
|
||||
public CALegacyEcoEnchant(@NotNull EcoEnchant ecoEnchant, @NotNull Enchantment enchantment) {
|
||||
super(enchantment, EnchantmentRarity.COMMON);
|
||||
this.ecoEnchant = ecoEnchant;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnchantConflict(@NotNull Map<CAEnchantment, Integer> enchantments, @NotNull Material itemMat) {
|
||||
if (!enchantments.isEmpty()) {
|
||||
if (this.ecoEnchant.getConflictsWithEverything()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (CAEnchantment other : enchantments.keySet()) {
|
||||
if (other instanceof CABukkitEnchantment otherVanilla
|
||||
&& this.ecoEnchant.conflictsWith(otherVanilla.getEnchant())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemConflict(@NotNull Map<CAEnchantment, Integer> enchantments,
|
||||
@NotNull Material itemMat,
|
||||
@NotNull ItemStack item) {
|
||||
if (Material.ENCHANTED_BOOK.equals(itemMat)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (EnchantmentTarget target : this.ecoEnchant.getTargets()) {
|
||||
if (target.getMaterials().contains(itemMat)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,8 +10,26 @@ import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
|
|||
|
||||
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
||||
|
||||
private val isLegacy: Boolean
|
||||
private val legacyDependency: LegacyEcoEnchantDependency?
|
||||
|
||||
init {
|
||||
CustomAnvil.instance.logger.info("Eco Enchant Detected !")
|
||||
|
||||
var isLegacy = true
|
||||
try {
|
||||
Class.forName("com.willfp.ecoenchants.enchant.EcoEnchants")
|
||||
isLegacy = false
|
||||
} catch (_: ClassNotFoundException) {
|
||||
}
|
||||
|
||||
this.isLegacy = isLegacy;
|
||||
if (isLegacy) {
|
||||
this.legacyDependency = LegacyEcoEnchantDependency()
|
||||
} else {
|
||||
this.legacyDependency = null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun disableAnvilListener() {
|
||||
|
|
@ -22,6 +40,11 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
|||
fun registerEnchantments() {
|
||||
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
|
||||
|
||||
if (isLegacy) {
|
||||
legacyDependency!!.registerEnchantments();
|
||||
return
|
||||
}
|
||||
|
||||
val enchantments = EcoEnchants.values()
|
||||
for (ecoEnchant in enchantments) {
|
||||
EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
|
||||
|
|
@ -34,6 +57,11 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
|||
}
|
||||
|
||||
fun handleConfigReload() {
|
||||
if (isLegacy) {
|
||||
legacyDependency!!.handleConfigReload()
|
||||
return
|
||||
}
|
||||
|
||||
// Should not happen in known case.
|
||||
if (this.ecoEnchantOldEnchantments == null) return
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package xyz.alexcrea.cuanvil.dependency
|
||||
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchant
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchants
|
||||
import org.bukkit.enchantments.Enchantment
|
||||
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CALegacyEcoEnchant
|
||||
|
||||
class LegacyEcoEnchantDependency {
|
||||
|
||||
|
||||
private var ecoEnchantOldEnchantments: MutableSet<EcoEnchant>? = null
|
||||
fun registerEnchantments() {
|
||||
val enchantments = EcoEnchants.values()
|
||||
for (ecoEnchant in enchantments) {
|
||||
ecoEnchant as Enchantment
|
||||
|
||||
EnchantmentApi.unregisterEnchantment(ecoEnchant) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
|
||||
EnchantmentApi.registerEnchantment(CALegacyEcoEnchant(ecoEnchant, ecoEnchant))
|
||||
}
|
||||
|
||||
ecoEnchantOldEnchantments = HashSet(enchantments)
|
||||
}
|
||||
|
||||
fun handleConfigReload() {
|
||||
// Should not happen in known case.
|
||||
if (this.ecoEnchantOldEnchantments == null) return
|
||||
|
||||
val newEnchantments = EcoEnchants.values()
|
||||
|
||||
// Add new enchantments
|
||||
for (ecoEnchant in newEnchantments)
|
||||
if (!this.ecoEnchantOldEnchantments!!.contains(ecoEnchant))
|
||||
EnchantmentApi.registerEnchantment(CALegacyEcoEnchant(ecoEnchant, ecoEnchant as Enchantment))
|
||||
|
||||
|
||||
// Remove old enchantments that not now currently used
|
||||
this.ecoEnchantOldEnchantments!!.removeAll(newEnchantments)
|
||||
for (oldEnchantment in this.ecoEnchantOldEnchantments!!) {
|
||||
EnchantmentApi.unregisterEnchantment(oldEnchantment as Enchantment)
|
||||
}
|
||||
|
||||
this.ecoEnchantOldEnchantments = HashSet(newEnchantments)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue