Search in sources :

Example 1 with EcoEnchant

use of com.willfp.ecoenchants.enchantments.EcoEnchant in project EcoEnchants by Auxilor.

the class CommandLocaleDownload method onExecute.

@Override
public void onExecute(@NotNull final CommandSender sender, @NotNull final List<String> args) {
    if (args.isEmpty()) {
        sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-locale"));
        return;
    }
    Paste paste = Paste.getFromHastebin(args.get(0));
    if (paste == null) {
        sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-locale"));
        return;
    }
    String contents = paste.getContents();
    Config configuration = new TransientConfig(contents, ConfigType.YAML);
    for (String key : configuration.getKeys(true)) {
        this.getPlugin().getLangYml().set(key, configuration.get(key));
    }
    try {
        this.getPlugin().getLangYml().save();
        this.getPlugin().getLangYml().clearCache();
        for (EcoEnchant value : EcoEnchants.values()) {
            value.getConfig().loadFromLang();
            value.getConfig().save();
            value.getConfig().clearCache();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    sender.sendMessage(this.getPlugin().getLangYml().getMessage("downloaded-locale"));
}
Also used : EcoEnchant(com.willfp.ecoenchants.enchantments.EcoEnchant) Paste(com.willfp.eco.core.web.Paste) TransientConfig(com.willfp.eco.core.config.TransientConfig) Config(com.willfp.eco.core.config.interfaces.Config) IOException(java.io.IOException) TransientConfig(com.willfp.eco.core.config.TransientConfig)

Example 2 with EcoEnchant

use of com.willfp.ecoenchants.enchantments.EcoEnchant in project EcoEnchants by Auxilor.

the class EnchantmentUtils method unregister.

/**
 * Unregister enchantment with the server.
 *
 * @param enchantment The enchantment.
 */
public static void unregister(@NotNull final Enchantment enchantment) {
    try {
        Field byIdField = Enchantment.class.getDeclaredField("byKey");
        Field byNameField = Enchantment.class.getDeclaredField("byName");
        byIdField.setAccessible(true);
        byNameField.setAccessible(true);
        Map<NamespacedKey, Enchantment> byKey = (Map<NamespacedKey, Enchantment>) byIdField.get(null);
        Map<String, Enchantment> byName = (Map<String, Enchantment>) byNameField.get(null);
        byKey.remove(enchantment.getKey());
        byName.remove(enchantment.getName());
        if (enchantment instanceof EcoEnchant) {
            byName.remove(((EcoEnchant) enchantment).getDisplayName());
        }
        Map<String, Enchantment> byNameClone = new HashMap<>(byName);
        for (Map.Entry<String, Enchantment> entry : byNameClone.entrySet()) {
            if (entry.getValue().getKey().equals(enchantment.getKey())) {
                byName.remove(entry.getKey());
            }
        }
        Field f = Enchantment.class.getDeclaredField("acceptingNew");
        f.setAccessible(true);
        f.set(null, true);
        f.setAccessible(false);
    } catch (NoSuchFieldException | IllegalAccessException ignored) {
    }
}
Also used : EcoEnchant(com.willfp.ecoenchants.enchantments.EcoEnchant) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) NamespacedKey(org.bukkit.NamespacedKey) Enchantment(org.bukkit.enchantments.Enchantment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with EcoEnchant

use of com.willfp.ecoenchants.enchantments.EcoEnchant in project EcoEnchants by Auxilor.

the class EnchantmentUtils method registerPlaceholders.

/**
 * Register the placeholders for an enchantment.
 *
 * @param enchantment The enchantment to register placeholders for.
 */
public static void registerPlaceholders(@NotNull final EcoEnchant enchantment) {
    PlaceholderManager.registerPlaceholder(new PlaceholderEntry(enchantment.getPlugin(), enchantment.getPermissionName() + "_" + "enabled", player -> String.valueOf(enchantment.isEnabled())));
    enchantment.getConfig().getKeys(true).forEach(string -> {
        String key = string.replace(".", "_").replace("-", "_");
        Object object = enchantment.getConfig().get(string);
        PlaceholderManager.registerPlaceholder(new PlaceholderEntry(enchantment.getPlugin(), enchantment.getPermissionName() + "_" + key, player -> StringUtils.internalToString(object)));
    });
}
Also used : EcoPlugin(com.willfp.eco.core.EcoPlugin) NamespacedKey(org.bukkit.NamespacedKey) EcoEnchants(com.willfp.ecoenchants.enchantments.EcoEnchants) Enchantment(org.bukkit.enchantments.Enchantment) Set(java.util.Set) EcoEnchantsPlugin(com.willfp.ecoenchants.EcoEnchantsPlugin) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) LivingEntity(org.bukkit.entity.LivingEntity) Field(java.lang.reflect.Field) UtilityClass(lombok.experimental.UtilityClass) NumberUtils(com.willfp.eco.util.NumberUtils) EcoEnchant(com.willfp.ecoenchants.enchantments.EcoEnchant) Block(org.bukkit.block.Block) PlaceholderManager(com.willfp.eco.core.integrations.placeholder.PlaceholderManager) Map(java.util.Map) StringUtils(com.willfp.eco.util.StringUtils) NotNull(org.jetbrains.annotations.NotNull) BlockUtils(com.willfp.eco.util.BlockUtils) PlaceholderEntry(com.willfp.eco.core.integrations.placeholder.PlaceholderEntry) PlaceholderEntry(com.willfp.eco.core.integrations.placeholder.PlaceholderEntry)

Example 4 with EcoEnchant

use of com.willfp.ecoenchants.enchantments.EcoEnchant in project EcoEnchants by Auxilor.

the class EcoEnchantsPlugin method handleReloadAdditional.

@Override
public void handleReloadAdditional() {
    this.getDisplayModule().update();
    for (EcoEnchant enchant : EcoEnchants.values()) {
        HandlerList.unregisterAll(enchant);
        this.getScheduler().runLater(() -> {
            if (enchant.isEnabled()) {
                this.getEventManager().registerListener(enchant);
                if (enchant instanceof TimedRunnable) {
                    this.getScheduler().syncRepeating((TimedRunnable) enchant, 5, ((TimedRunnable) enchant).getTime());
                }
            }
        }, 1);
    }
    this.getScheduler().runTimer(() -> {
        for (EcoEnchant enchant : EcoEnchants.values()) {
            enchant.clearCachedRequirements();
        }
    }, 300, 300);
}
Also used : EcoEnchant(com.willfp.ecoenchants.enchantments.EcoEnchant) TimedRunnable(com.willfp.ecoenchants.enchantments.util.TimedRunnable)

Example 5 with EcoEnchant

use of com.willfp.ecoenchants.enchantments.EcoEnchant in project EcoEnchants by Auxilor.

the class CommandLocaleExport method onExecute.

@Override
public void onExecute(@NotNull final CommandSender sender, @NotNull final List<String> args) {
    YamlConfiguration configuration = YamlConfiguration.loadConfiguration(new StringReader(this.getPlugin().getLangYml().toPlaintext()));
    for (EcoEnchant enchant : EcoEnchants.values()) {
        configuration.set("enchantments." + enchant.getKey().getKey() + ".name", enchant.getConfig().getHandle().getString("name"));
        configuration.set("enchantments." + enchant.getKey().getKey() + ".description", enchant.getConfig().getHandle().getString("description"));
    }
    Paste paste = new Paste(configuration.saveToString());
    paste.getHastebinToken(token -> {
        if (token.length() > 15) {
            sender.sendMessage(this.getPlugin().getLangYml().getMessage("export-failed"));
        } else {
            sender.sendMessage(this.getPlugin().getLangYml().getMessage("link-to-locale").replace("%token%", token));
        }
    });
}
Also used : EcoEnchant(com.willfp.ecoenchants.enchantments.EcoEnchant) Paste(com.willfp.eco.core.web.Paste) StringReader(java.io.StringReader) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Aggregations

EcoEnchant (com.willfp.ecoenchants.enchantments.EcoEnchant)8 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NamespacedKey (org.bukkit.NamespacedKey)3 Enchantment (org.bukkit.enchantments.Enchantment)3 Config (com.willfp.eco.core.config.interfaces.Config)2 Paste (com.willfp.eco.core.web.Paste)2 EcoPlugin (com.willfp.eco.core.EcoPlugin)1 TransientConfig (com.willfp.eco.core.config.TransientConfig)1 PlaceholderEntry (com.willfp.eco.core.integrations.placeholder.PlaceholderEntry)1 PlaceholderManager (com.willfp.eco.core.integrations.placeholder.PlaceholderManager)1 BlockUtils (com.willfp.eco.util.BlockUtils)1 NumberUtils (com.willfp.eco.util.NumberUtils)1 StringUtils (com.willfp.eco.util.StringUtils)1 EcoEnchantsPlugin (com.willfp.ecoenchants.EcoEnchantsPlugin)1 EcoEnchants (com.willfp.ecoenchants.enchantments.EcoEnchants)1 TimedRunnable (com.willfp.ecoenchants.enchantments.util.TimedRunnable)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1