Merge pull request #1

Fix essentials integration
This commit is contained in:
Auxilor 2020-08-28 22:19:38 +01:00 committed by GitHub
commit 9ef586ad56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View File

@ -1,10 +1,12 @@
package com.willfp.ecoenchants.enchantments;
import com.earth2me.essentials.Enchantments;
import com.earth2me.essentials.Essentials;
import com.willfp.ecoenchants.Main;
import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.nms.Target;
import com.willfp.ecoenchants.util.EssentialsUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.bukkit.Material;
@ -102,9 +104,12 @@ public abstract class EcoEnchant extends Enchantment implements Listener {
Enchantment.registerEnchantment(this);
if(Main.hasEssentials) {
Map<String, Enchantment> essentialsMap = (Map<String, Enchantment>) FieldUtils.readDeclaredStaticField(Enchantments.class, "ENCHANTMENTS", true);
essentialsMap.remove(this.getName());
essentialsMap.put(this.getName(), this);
Map<String, Enchantment> essentialsMap = EssentialsUtils.getEnchantmentsMap();
if (essentialsMap != null) {
String key = this.getKey().getKey();
essentialsMap.remove(key);
essentialsMap.put(key, this);
}
}
} catch (NoSuchFieldException | IllegalAccessException ignored) {}
}

View File

@ -0,0 +1,37 @@
package com.willfp.ecoenchants.util;
import com.earth2me.essentials.Enchantments;
import org.apache.commons.lang.reflect.FieldUtils;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
public class EssentialsUtils {
private static Map<String, Enchantment> essentialsEnchantmentsMap;
private static boolean hasCheckedEnchantmentsMap;
@Nullable
public static Map<String, Enchantment> getEnchantmentsMap() {
if (hasCheckedEnchantmentsMap) return essentialsEnchantmentsMap;
if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) {
try {
Class<?> essEnchantmentsClass = Enchantments.class;
Object enchantments = FieldUtils.readDeclaredStaticField(essEnchantmentsClass, "ENCHANTMENTS", true);
if (enchantments instanceof Map) {
hasCheckedEnchantmentsMap = true;
//noinspection unchecked - we know the type of it
return essentialsEnchantmentsMap = (Map<String, Enchantment>) enchantments;
}
} catch (IllegalAccessException e) {
hasCheckedEnchantmentsMap = true;
return null;
}
}
hasCheckedEnchantmentsMap = true;
return null;
}
}