I have a config file Homes.yml
that I am using to the store home of users on a server to be able to teleport to between reloads. Currently the /ksethome
command is working and the config file is updated with the proper coordinates.
However, when executing the /khome
command an error is thrown.
Here is a copy of Homes.yml
Homes:
Kalenpw:
==: org.bukkit.Location
yaw: 174.53154
pitch: 18.115198
z: 89.15419690166142
y: 100.0
world: Khalidor
x: 99.43791494211929
and here is a copy of the plugin
//@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;
}
}
finally, a copy of the error:
[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
I've done a bit of googling around and can't seem to figure out what I'm doing wrong. From what I can tell I'm creating the Location object wrong from the Homes.yml because I can switch the code to just use a hashmap and it works fine which also eliminates the plugin.yml as being the issue. Thanks for the help!
Edit:
Line 152 from TestPlugin.java
World homeWorld = Bukkit.getServer().getWorld(worldName);
Edit:
Apparently the issue is every way I've gone about getting my world is returning null. I used Bukkit.broadcastMessage(Bukkit.getWorlds().toString());
to check to make sure that I was using the proper string to get the world from and that broadcasted [CraftWorld{name=Khalidor}, CraftWorld{name=Khalidor_nether}, CraftWorld{name=Khalidor_the_end}]
I'm not in the Nether or End So it appears to me it should be picking up that world.
When I run these String worldName = homesConfig.getString("Homes." + user.getName() + ".world");
Bukkit.broadcastMessage(worldName);
I get a null pointer exception so for whatever reason I'm getting null back instead of the name of my world. Still figuring out why that is going on.
Edit:
So I've figured out the issue is with my YML file instead of using the String from Homes.yml I just entered the string of my world and now it reads the X Y Z locations all as 0 so for some reason it's reading that wrong.
Edit: solved turns out the issue wasn't in the YML file but I had a line of code left in from earlier testing that was causing issues with the YML because I was saving the x y z and then overwriting with a location.
You might want to check to see if "homesConfig.contains("Homes." + user.getName() + )" before you try to use it. I think getting something that the config file doesn't contain will just return null.
Here's how you could check it
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;
}
I haven't tested this code, but it could work. Also pogostick29dev has a lot of bukkit tutorials, one covers settings managers, if you wanted to check him out.