【问题标题】:Bukkit plugin get locations from config.ymlBukkit 插件从 config.yml 获取位置
【发布时间】:2016-08-05 20:12:47
【问题描述】:

我正在尝试创建插件,如果箱子的位置与我的 config.yml 中的任何位置都不匹配,它将检查何时有人打开箱子。所以我想做这样的事情:

ArratList<Location> list = new ArrayList();
foreach(get world, x, y, z from each key in section Locations in my config) {
    list.add(new Location(world, x, y, z));
    foreach(Location l : list) {
    if(l == p.location...
}
}

编辑: 现在我有了这个代码:

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

【问题讨论】:

  • 你似乎知道你想要什么:“类似这样的东西”。显然这还不够,你想要更具体的东西。您必须更具体地向我们解释您到底想要什么。
  • 请说清楚一点。
  • 你能告诉我们哪一行是 Listeners 47 吗?

标签: java plugins minecraft bukkit


【解决方案1】:

您可以执行以下操作: 将您的位置保存在配置中,如下所示:

# CONFIG.YML #
location1:
 x: 0
 y: 0
 z: 0
 world: "world"
location2:
 x: ....
 .
 .

然后,获取所有配置:

ArrayList<Location> listLocations = new ArrayList<Location>();
for (String key : getConfig().getKeys(false) ){
    //We are getting every key from our config.yml file
    ConfigurationSection location = getConfig().getConfigurationSection(key);
    int x = location.getInt(key + ".x");
    int y = location.getInt(key + ".y");
    int z = location.getInt(key + ".z");
    String world = location.getString(key + ".world");
    Location l = new Location(Bukkit.getWorld(world), x, y, z);
    listLocations.add(l);
}

您现在应该将该配置文件中的所有位置都添加到 ArrayList listLocations 中,并且可以从那里进行检查!

【讨论】:

  • 但还是有问题,我已经编辑了我的问题。
  • 引起:java.lang.IllegalArgumentException:名称不能为空|例如第 47 行。当我试图找到第 47 行时,你能给我们那个代码吗?我得到一个“}”
【解决方案2】:

您不需要从配置中获取每个单独的值(x、y、z、世界),因为:

Location implements ConfigurationSerializable,这意味着你可以只使用config#set("path.to.location", yourLocation);yourLocation = (Location) config#get("path.to.location");


现在,你的例外:

我建议你learn how to read stacktraces,因为这对你以后会很有帮助。

从这些方面:

       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) ~[?:?]

我知道你打电话给Bukkit.getWorld(null);(间接地),因为这里的世界是空的String world = location.getString(key + ".world"); 检查您的配置路径,(但是,我建议您只使用上面提到的位置)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多