我有一个配置文件Homes.yml
,我将其用于服务器上用户的商店主页,以便能够在重新加载之间传送。目前/ksethome
命令正在运行,配置文件使用正确的坐标进行更新。
但是,执行/khome
命令时会抛出错误。
以下是Homes.yml
Homes:
Kalenpw:
==: org.bukkit.Location
yaw: 174.53154
pitch: 18.115198
z: 89.15419690166142
y: 100.0
world: Khalidor
x: 99.43791494211929
这是插件的副本
//@kalenpw
package com.Khalidor.testplugin;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.PlayerInventory;
public final class TestPlugin extends JavaPlugin implements Listener {
MyConfigManager manager;
MyConfig homesConfig;
HashMap<String, Location> playerHomes = new HashMap<String, Location>();
@Override
public void onEnable() {
getLogger().info("onEable has been invoked!");
getServer().getPluginManager().registerEvents(this, this);
manager = new MyConfigManager(this);
homesConfig = manager.getNewConfig("Homes.yml");
// TODO add stuff to hashmap onEnable()
}
@Override
public void onDisable() {
// when plugin is disabled
getLogger().info("onDisable has been invoked");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player user = (Player) sender;
// Sethome
if (label.equalsIgnoreCase("KSetHome")) {
playerHomes.put(user.getName(), user.getLocation());
homesConfig.set("Homes." + user.getName(), user.getLocation());
homesConfig.saveConfig();
user.sendMessage(ChatColor.GOLD + "Set home!");
}
// TP home
if (label.equalsIgnoreCase("KHome")) {
// Location homeLocation = playerHomes.get(user.getName());
/*
* Location homeLocation = homesConfig.get("Homes." +
* user.getName(), );
*/
String worldName = homesConfig.getString("Homes." + user.getName() + ".world");
World homeWorld = Bukkit.getServer().getWorld(worldName);
double homeX = homesConfig.getDouble("Homes." + user.getName() + ".x");
double homeY = homesConfig.getDouble("Homes." + user.getName() + ".y");
double homeZ = homesConfig.getDouble("Homes." + user.getName() + ".z");
Location homeLocation = new Location(homeWorld, homeX, homeY, homeZ);
user.teleport(homeLocation);
user.sendMessage(ChatColor.GOLD + "Teleported home!");
}
return true;
}
// Not executed by player
return false;
}
}
最后,错误的副本:
[21:25:01 INFO]: Kalenpw issued server command: /ksethome
[21:25:04 INFO]: Kalenpw issued server command: /khome
[21:25:04 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'khome' in plugin TestPlugin v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_85]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_85]
at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at java.lang.Thread.run(Thread.java:745) [?:1.7.0_85]
Caused by: java.lang.IllegalArgumentException: Name cannot be null
at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at org.bukkit.craftbukkit.v1_9_R1.CraftServer.getWorld(CraftServer.java:1022) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
at com.Khalidor.testplugin.TestPlugin.onCommand(TestPlugin.java:152) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
... 15 more
我做了一些谷歌搜索,似乎无法弄清楚我做错了什么。从我可以告诉我,我正在从Homes.yml创建错误的Location对象,因为我可以将代码切换为只使用一个hashmap并且它工作正常,这也消除了plugin.yml作为问题。谢谢你的帮助!
编辑:
来自TestPlugin.java的第152行
World homeWorld = Bukkit.getServer().getWorld(worldName);
编辑:
显然,这个问题是关于让我的世界返回null的各种方式。我使用Bukkit.broadcastMessage(Bukkit.getWorlds().toString());
来检查以确保我使用正确的字符串来获取世界,并且广播[CraftWorld{name=Khalidor}, CraftWorld{name=Khalidor_nether}, CraftWorld{name=Khalidor_the_end}]
我不在虚空或结束所以在我看来它应该是采摘那个世界。
当我运行这些String worldName = homesConfig.getString("Homes." + user.getName() + ".world");
Bukkit.broadcastMessage(worldName);
我得到一个空指针异常,所以无论出于何种原因我都得到null而不是我的世界的名字。还在弄清楚为什么会这样。
编辑:
所以我发现问题出在我的YML文件中而不是使用来自Homes.yml的字符串我刚刚输入了我的世界字符串,现在它将XYZ位置全部读为0所以出于某种原因,它正在读取错。
编辑:已解决该问题不在YML文件中,但我在早期测试中留下了一行代码,导致YML出现问题,因为我正在保存xyz,然后用位置覆盖。 / p>
答案 0 :(得分:1)
您可能想查看&#34; homesConfig.contains(&#34; Homes。&#34; + user.getName()+)&#34;在你尝试使用它之前。我认为获取配置文件不包含的内容只会返回null。
这是你怎么检查的
if (!homesConfig.contains("Homes." + user.getName() + ".world") || <just copy the first condition but for the different elements, like x, y, z, ect>) {
user.sendMessage(ChatColor.RED + "No home set");
return true;
}
我还没有测试过这段代码,但它可以运行。另外pogostick29dev有很多bukkit教程,一个涵盖设置管理员,如果你想检查他。
答案 1 :(得分:0)
(未经测试)
第152行:
String worldName = homesConfig.getString("Homes." + user.getName() + ".world");
应该是
World homeWorld = ((Location) getConfig().get("Homes." + user.getName())).getWorld();
为什么?
您将位置存储在行
中homesConfig.set(&#34; Homes。&#34; + user.getName(), user.getLocation());
所以你必须检索位置。然后,您就可以获得该位置的世界。
此外,您不应该使用播放器的名称,因为您可以在mojangs网站上更改您的名字。请改用播放器的UID。