1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.bukkit.ChatColor; |
7 |
import org.bukkit.Location; |
8 |
import org.bukkit.World; |
9 |
import org.bukkit.command.Command; |
10 |
import org.bukkit.command.CommandExecutor; |
11 |
import org.bukkit.command.CommandSender; |
12 |
import org.bukkit.entity.Player; |
13 |
import org.bukkit.plugin.Plugin; |
14 |
import org.bukkit.configuration.file.YamlConfiguration; |
15 |
|
16 |
public class HomeCommand implements CommandExecutor { |
17 |
|
18 |
Plugin plugin; |
19 |
|
20 |
YamlConfiguration config; |
21 |
|
22 |
File file; |
23 |
|
24 |
public HomeCommand(Plugin plugin) { |
25 |
this.plugin = plugin; |
26 |
|
27 |
file = new File( plugin.getDataFolder(), "homes.yml"); |
28 |
|
29 |
config = new YamlConfiguration(); |
30 |
try { |
31 |
config.load(file); |
32 |
} catch (Exception e) { |
33 |
System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() ); |
34 |
} |
35 |
} |
36 |
|
37 |
|
38 |
@Override |
39 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
40 |
|
41 |
if (!(sender instanceof Player)) { |
42 |
return false; |
43 |
} |
44 |
Player p = (Player) sender; |
45 |
|
46 |
String name = p.getName(); |
47 |
|
48 |
if ( command.getName().equals("home") ) { |
49 |
|
50 |
String worldName = config.getString( name + ".world"); |
51 |
if (worldName != null) { |
52 |
double yaw = config.getDouble( name + ".yaw", 0.0); |
53 |
double pitch = config.getDouble( name + ".pitch", 0.0); |
54 |
double x = config.getDouble( name + ".x", 0.0); |
55 |
double y = config.getDouble( name + ".y", 0.0); |
56 |
double z = config.getDouble( name + ".z", 0.0); |
57 |
|
58 |
World world = p.getServer().getWorld(worldName); |
59 |
|
60 |
Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); |
61 |
|
62 |
p.teleport(loc); |
63 |
} else { |
64 |
p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); |
65 |
} |
66 |
} |
67 |
|
68 |
if (command.getName().equals("sethome") ) { |
69 |
Location loc = p.getLocation(); |
70 |
config.set( name + ".yaw", loc.getYaw() ); |
71 |
config.set( name + ".pitch", loc.getPitch() ); |
72 |
config.set( name + ".world", loc.getWorld().getName() ); |
73 |
config.set( name + ".x", loc.getX() ); |
74 |
config.set( name + ".y", loc.getY() ); |
75 |
config.set( name + ".z", loc.getZ() ); |
76 |
|
77 |
try { |
78 |
config.save(file); |
79 |
p.sendMessage(ChatColor.YELLOW + "home is set"); |
80 |
} catch (IOException e) { |
81 |
System.out.println("[HoerupUtils] : /sethome save threw an IO exception: " + e.getMessage() ); |
82 |
} |
83 |
} |
84 |
|
85 |
return true; |
86 |
} |
87 |
} |