Bukkit插件从config.yml

时间:2016-08-06 11:13:14

标签: java plugins minecraft bukkit

我有这个代码,我想当有人点击胸部并且它的位置与点击的阻止位置匹配时向玩家发送消息:

public class Listeners implements Listener {

public Core plugin;
public Listeners(Core core) {
    this.plugin = core;
}

@EventHandler
private void onPlayerInteract(PlayerInteractEvent e) {
    if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if(plugin.createMode) {
            if (e.getClickedBlock().getType() == Material.CHEST) {
                e.setCancelled(true);
                Location loc = e.getClickedBlock().getLocation();
                String name = plugin.name;
                plugin.getConfig().set(name + ".world", loc.getWorld().getName());
                plugin.getConfig().set(name + ".x", loc.getBlockX());
                plugin.getConfig().set(name + ".y", loc.getBlockY());
                plugin.getConfig().set(name + ".z", loc.getBlockZ());
                plugin.saveConfig();
                e.getPlayer().sendMessage(ChatColor.GOLD + "[ChestTreasure] " + ChatColor.RESET + "Treasury chest successfully created!");
                plugin.createMode = false;
            }
        } else {
            if(e.getClickedBlock().getType() == Material.CHEST) {
                plugin.chests.clear();
                for (String key : plugin.getConfig().getKeys(false) ){
                    //We are getting every key from our config.yml file
                    ConfigurationSection location = plugin.getConfig().getConfigurationSection(key);
                    String world = location.getString(key + ".world");
                    int x = location.getInt(key + ".x");
                    int y = location.getInt(key + ".y");
                    int z = location.getInt(key + ".z");
                    e.getPlayer().sendMessage("Hondnota x je " + String.valueOf(x));
                    Location l = new Location(Bukkit.getWorld(world), x, y, z);
                    plugin.chests.add(l);
                }
                for(Location l : plugin.chests) {
                    e.getPlayer().sendMessage(String.valueOf(e.getClickedBlock().getLocation().getX()));
                    if(l == e.getClickedBlock().getLocation()) {
                        e.getPlayer().sendMessage("Jeej");
                    }
                }
            }
        }
    }
}

} 但当我右键单击胸部时,消息jeej没有出现,所有出现的内容都是消息Hodnota x je 0.但是我的配置中有几个键,x在任何地方都不是0。在控制台中出现此错误:

[12:25:13 ERROR]: Could not pass event PlayerInteractEvent to ChestTreasure v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:492) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:890) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:55) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:11) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.lang.IllegalArgumentException: Name cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getWorld(CraftServer.java:1023) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.Bukkit.getWorld(Bukkit.java:500) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at me.sudoman281.chestTreasure.Listeners.onPlayerInteract(Listeners.java:47) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        ... 17 more

1 个答案:

答案 0 :(得分:1)

Caused by: java.lang.IllegalArgumentException: Name cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getWorld(CraftServer.java:1023) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.Bukkit.getWorld(Bukkit.java:500) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at me.sudoman281.chestTreasure.Listeners.onPlayerInteract(Listeners.java:47) ~[?:?]

我没有必要进一步阅读以了解你的onPlayerInteract中的某些内容是null,并且它与getWorld有关,因此,没有更多信息,我很确定这行是一个问题:

Location l = new Location(Bukkit.getWorld(world), x, y, z);

我猜你的变量world为空,并且由于它是从配置中获取的,这很可能意味着它在配置中不存在。 这些行很可能是问题所在:

for (String key : plugin.getConfig().getKeys(false) ){
                    //We are getting every key from our config.yml file
                    ConfigurationSection location = plugin.getConfig().getConfigurationSection(key)

您正在为每个密钥获取ConfigurationSection。但那些很可能不是ConfigurationSections。我相信你有一个名为location或者其他东西的ConfigurationSection,如果是这样的话,你应该这样做:

ConfigurationSection location = plugin.getConfig().getConfigurationSection("location")

代替。

我不确定您的配置部分是否称为位置,但这是我的猜测,只需将地址替换为您实际部分的名称。

在不知道配置的外观的情况下,这是我能回答的最好的