1. Вы находитесь в сообществе Rubukkit. Мы - администраторы серверов Minecraft, разрабатываем собственные плагины и переводим на различные языки плагины наших коллег из других стран.
    Скрыть объявление
Скрыть объявление
В преддверии глобального обновления, мы проводим исследования, которые помогут нам сделать опыт пользования форумом ещё удобнее. Помогите нам, примите участие!

Помогите [Решено] Ошибка при использовании команды

Тема в разделе "Разработка плагинов для новичков", создана пользователем Вова Петренко, 29 мар 2022.

Метки:
Статус темы:
Закрыта.
  1. Автор темы
    Вова Петренко

    Вова Петренко Активный участник Пользователь

    Баллы:
    61
    Имя в Minecraft:
    Baraban4ik
    При создании команды я столкнулся с ошибкой
    Сама команда:
    Код:
    package com.sp3ctr0.ecolobby.commands;
    
    import com.sp3ctr0.ecolobby.Ecolobby;
    import com.sp3ctr0.ecolobby.configurations.Configurations;
    import com.sp3ctr0.ecolobby.utils.Chat;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class EcoLobbyCommand implements CommandExecutor {
    
        private Configurations configurations;
        private Ecolobby plugin;
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName().equalsIgnoreCase("ecolobby")) {
                this.reload(sender);
            }
            return true;
        }
    
        private void reload(CommandSender sender)
        {
            if (!sender.hasPermission("ecolobby.reload"))
            {
                sender.sendMessage(this.configurations.get("messages.yml").getString("no-permissions"));
                return;
            }
            this.plugin.reload();
            sender.sendMessage("Reloaded");
        }
    
    }
    Майн класс:
    Код:
    package com.sp3ctr0.ecolobby;
    
    import com.sp3ctr0.ecolobby.commands.EcoLobbyCommand;
    import com.sp3ctr0.ecolobby.configurations.Configurations;
    import com.sp3ctr0.ecolobby.handler.SimpleEventHandler;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Ecolobby extends JavaPlugin {
    
        private Configurations configurations = new Configurations(this, "config.yml", "messages.yml");
    
        @Override
        public void onEnable() {
            System.out.println(ChatColor.GREEN + "EcoLobby plugin is enabled");
            this.configurations.loadConfigurations();
            getServer().getPluginCommand("ecolobby").setExecutor(new EcoLobbyCommand());
    
            getServer().getPluginManager().registerEvents(new SimpleEventHandler(), this);
        }
    
        @Override
        public void onDisable() {
            System.out.println(ChatColor.RED + "EcoLobby plugin is disabled");
            this.configurations = null;
        }
    
        public void reload() {
            this.configurations.reloadConfigurations();
        }
    }
    Фали конфигурации:
    Код:
    package com.sp3ctr0.ecolobby.configurations;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.*;
    
    import com.google.common.collect.Lists;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    
    public class Configurations
    {
        private Map<String, Map.Entry<FileConfiguration, File>> configurations = new HashMap<> ();
    
        private List<String> configurationsNames;
    
        private Plugin plugin;
    
        public Configurations(Plugin plugin, String... configurationsNames)
        {
            this.plugin = plugin;
    
            this.configurationsNames = Lists.newArrayList(configurationsNames);
    
            this.loadConfigurations();
        }
    
        private File generateDefaultFile(String name)
        {
            File file = new File(this.plugin.getDataFolder(), name);
    
            if (!file.exists())
            {
                this.plugin.saveResource(name, false);
            }
    
            return file;
        }
    
        public void loadConfigurations()
        {
            for (String configurationName : this.configurationsNames)
            {
                if (this.configurations.containsKey(configurationName))
                {
                    continue;
                }
    
                File configurationFile = this.generateDefaultFile(configurationName);
    
                FileConfiguration configuration = YamlConfiguration.loadConfiguration(configurationFile);
    
                this.configurations.put(configurationName, new AbstractMap.SimpleEntry<> (configuration, configurationFile));
            }
        }
    
        public void reloadConfigurations()
        {
            this.configurations.clear();
    
            this.loadConfigurations();
        }
    
        private Optional<Map.Entry<FileConfiguration, File>> getEntry(String configurationName)
        {
            return Optional.ofNullable(this.configurations.get(configurationName));
        }
    
        public FileConfiguration get(String configurationName)
        {
            return this.getEntry(configurationName)
                    .map(Map.Entry::getKey)
                    .orElse(null);
        }
    
        private File getFile(String configurationName)
        {
            return this.getEntry(configurationName)
                    .map(Map.Entry::getValue)
                    .orElse(null);
        }
    
        public void save(String configurationName)
        {
            this.getEntry(configurationName).ifPresent((entry) ->
            {
                try {
                    entry.getKey().save(entry.getValue());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
    Сама ошибка:
    Код:
    [12:45:52 WARN]: Unexpected exception while parsing console command "ecolobby"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ecolobby' in plugin EcoLobby v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[patched_1.12.2.jar:git-Paper-1618]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:152) ~[patched_1.12.2.jar:git-Paper-1618]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:685) ~[patched_1.12.2.jar:git-Paper-1618]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:648) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:463) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:424) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
            at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.NullPointerException
            at com.sp3ctr0.ecolobby.commands.EcoLobbyCommand.reload(EcoLobbyCommand.java:33) ~[?:?]
            at com.sp3ctr0.ecolobby.commands.EcoLobbyCommand.onCommand(EcoLobbyCommand.java:21) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[patched_1.12.2.jar:git-Paper-1618]
            ... 8 more
     
  2. Автор темы
    Вова Петренко

    Вова Петренко Активный участник Пользователь

    Баллы:
    61
    Имя в Minecraft:
    Baraban4ik
    Понял что связана со строками 33 и 21 но не понимаю почему
     
  3. Sturm_PT

    Sturm_PT Активный участник Пользователь

    Баллы:
    76
    в EcoLobbyCommand переменная plugin null, лог вроде ясный
    это в код вставь, конструктор называется
    Код:
    public EcoLobbyCommand(Ecolobby pl) {
      this.plugin = pl;
    }
     
  4. Автор темы
    Вова Петренко

    Вова Петренко Активный участник Пользователь

    Баллы:
    61
    Имя в Minecraft:
    Baraban4ik
    так это я сделал но перезагрузка не работает сообщение
    отправляется но файлы не обновляются
     
  5. Автор темы
    Вова Петренко

    Вова Петренко Активный участник Пользователь

    Баллы:
    61
    Имя в Minecraft:
    Baraban4ik
    Всё починил
     
Статус темы:
Закрыта.

Поделиться этой страницей