Spigot - 在 Command 类中创建、写入和读取 YAML 文件

问题描述 投票:0回答:1

我是一名14岁的中级程序员。我谦虚地请求你用我能理解的语言解释你的答案,不要批评我问了一个看似显而易见的问题。至此,这就是我遇到的问题:

这就是 IM 构建内容:我正在构建一个 Minecraft Java 版插件,当提供命令

/dui create <name>
时,它将创建一个具有相应 UI 的 YAML 文件。我所说的 UI 基本上是类似箱子的界面,每行中的项目都具有特殊属性(在 YAML 文件中编辑)。这些项目将导致不同的界面,每个界面都有一些很酷的命令和功能。

可能有用的信息:

我正在使用 spigot-api-1.8.8 作为我的依赖项。我在访问他们的网站时使用的是最新版本的paper 1.8.8。

Spigot API 版本:1.8 (.8) 服务器:Paper(本地主机) IDE:IntelliJ 社区版 Java版本:1.8(JDK 8)(推荐Java版本)

目录:

DecentUI
 - src
   - com.kanske.decentui
     - commands
       - DecentUICommands.java
     DecentUI.java
   - plugin.yml

(如果您不需要每个文件的内容,我很抱歉) 每个文件的内容:

// commands/DecentUICommands.java

package com.kanske.decentui.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;

public class DecentUICommands implements CommandExecutor {
    private File customConfigFile;
    private FileConfiguration customConfig;

    Plugin plugin;

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player player = (Player) sender;
        if (command.getName().equalsIgnoreCase("dui")) {
            if (args[0].equalsIgnoreCase("create")) {
                if (args.length == 2) {
                    createCustomConfig(args[1]);
                    plugin.getConfig().set("title", "this_is_working");
                    plugin.saveConfig();
                    plugin.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + plugin.getConfig().getString("title"));
                }
            }
        }
        return true;
    }

    public FileConfiguration getCustomConfig() {
        return this.customConfig;
    }

    private void createCustomConfig(String name) {
        customConfigFile = new File(plugin.getDataFolder(), name+".yml");
        customConfig = new YamlConfiguration();
        try {
            customConfig.load(customConfigFile);
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }
}
// DecentUI.java

package com.kanske.decentui;

import com.kanske.decentui.commands.DecentUICommands;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public class DecentUI extends JavaPlugin {

    @Override
    public void onEnable() {
        getCommand("dui").setExecutor(new DecentUICommands());
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "SERVER ENABLED");
    }

    @Override
    public void onDisable() {
        getServer().getConsoleSender().sendMessage(ChatColor.RED + "SERVER DISABLED");
    }
}

# plugin.yml

name: DecentUI
version: 0.1
author: kanske
main: com.kanske.decentui.DecentUI
api-version: 1.8
commands:
  create:
    description: create a GUI menu that is saved in config_files.
    usage: /<command> create

每次,我都会构建工件,然后使用

Server
文件夹中的 run.bat 文件运行服务器,其内部内容为:

java -Xmx2048M -jar server.jar
(server.jar是纸质的)

希望这对您来说是足够的信息。谢谢您的宝贵时间:)

这是 Java 调试错误(尽管我知道它可能没有多大意义(这对我来说当然没有意义)):

java.lang.NullPointerException

我编译了没有

DecentUICommands.java
的代码,它工作得很好,因为服务器启动时没有任何错误。

java yaml bukkit spigot
1个回答
0
投票

错误似乎出在

DecentUI.class
上,在
onEnable
方法中。

产生错误的行是

getCommand("dui").setExecutor(new DecentUICommands());
。为什么?

  1. plugin.yml
    中声明了
    create
    命令,应将其用作
    /create
    。但你从来没有使用过它。
  2. 您正在尝试加载
    /gui
    命令,该命令从未在插件 yaml 文件中声明。

plugin.yml
应该是这样的:

name: DecentUI
version: 0.1
author: kanske
main: com.kanske.decentui.DecentUI
api-version: 1.8
commands:
  create:
    description: create a GUI menu that is saved in config_files.
    usage: /<command> create
  dui:
    description: create a GUI menu that is saved in config_files.
    usage: /<command> create

onEnable()
方法中,它会像这样:

    @Override
    public void onEnable() {
        getCommand("dui").setExecutor(new DecentUICommands());
        getCommand("create").setExecutor(new CreateCommands());
        getLogger().info("Server enabled");
    }
© www.soinside.com 2019 - 2024. All rights reserved.