Java Bukkit插件从config.yml获取位置
我正在尝试创建插件,当有人打开箱子时,它会检查箱子的位置是否与我的config.yml中的任何位置不匹配。所以我想这样做:Java Bukkit插件从config.yml获取位置,java,plugins,minecraft,bukkit,Java,Plugins,Minecraft,Bukkit,我正在尝试创建插件,当有人打开箱子时,它会检查箱子的位置是否与我的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(Loca
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...
}
}
但当我右击胸部时,消息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
您可以执行以下操作: 将您的位置保存在配置中,如下所示:
# 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=new ArrayList();
for(字符串键:getConfig().getKeys(false)){
//我们正在从config.yml文件中获取所有密钥
ConfigurationSection location=getConfig().getConfigurationSection(键);
intx=location.getInt(key+“.x”);
inty=location.getInt(key+“.y”);
intz=location.getInt(key+“.z”);
字符串世界=location.getString(key+“.world”);
位置l=新位置(Bukkit.getWorld(world),x,y,z);
增加(l);
}
现在,您应该将该配置文件中的所有位置都添加到ArrayList listLocations中,并可以从中进行检查 您不需要从配置中获取每个单独的值(x、y、z、world),因为: ,这意味着您可以只使用
config#set(“path.to.location”,yourLocation)
和yourLocation=(Location)config#get(“path.to.Location”)代码>
现在,除了你之外:
我建议你,因为这对你将来会很有帮助
从以下几行:
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)代码>(间接),因为world在这里是空的stringworld=location.getString(key+“.world”)代码>
检查您的配置路径(但是,我建议您只使用我上面提到的位置)您似乎知道您想要什么:“类似这样的东西”。显然这还不够,你需要更具体的东西。你必须更具体地向我们解释你到底想要什么。请说得更清楚。你能告诉我们哪一行是Listeners 47吗?但仍然有一个问题,我已经编辑了我的问题。原因是:java.lang.IllegalArgumentException:Name不能为null |在示例中的第47行。你能给我们那个代码吗?当我试图找到第47行时,我得到一个“}”
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) ~[?:?]