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.Configuration; |
15 |
import org.bukkit.configuration.file.YamlConfiguration; |
16 |
|
17 |
public class HomeCommand implements CommandExecutor { |
18 |
|
19 |
Plugin plugin; |
20 |
|
21 |
YamlConfiguration config; |
22 |
|
23 |
File file; |
24 |
|
25 |
public HomeCommand(Plugin plugin) { |
26 |
this.plugin = plugin; |
27 |
|
28 |
file = new File( plugin.getDataFolder(), "homes.yml"); |
29 |
|
30 |
config = new YamlConfiguration(); |
31 |
try { |
32 |
config.load(file); |
33 |
} catch (Exception e) { |
34 |
System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() ); |
35 |
} |
36 |
} |
37 |
|
38 |
Configuration getConfig() { |
39 |
return config; |
40 |
} |
41 |
|
42 |
|
43 |
@Override |
44 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
45 |
|
46 |
if (!(sender instanceof Player)) { |
47 |
return false; |
48 |
} |
49 |
final Player p = (Player) sender; |
50 |
|
51 |
String name = p.getName(); |
52 |
|
53 |
if ( command.getName().equals("home") ) { |
54 |
tpUser(p, name); |
55 |
} |
56 |
if ( command.getName().equals("ophome") ) { |
57 |
if (args.length != 1) { |
58 |
p.sendMessage("Usage: /ophome <player>"); |
59 |
return true; |
60 |
} |
61 |
if ( p.isOp() ) { |
62 |
tpUser(p, args[0]); |
63 |
} else { |
64 |
p.sendMessage("Only operators may use this command"); |
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 |
|
88 |
private void tpUser(final Player p, String name) { |
89 |
String worldName = config.getString( name + ".world"); |
90 |
if (worldName != null) { |
91 |
double yaw = config.getDouble( name + ".yaw", 0.0); |
92 |
double pitch = config.getDouble( name + ".pitch", 0.0); |
93 |
double x = config.getDouble( name + ".x", 0.0); |
94 |
double y = config.getDouble( name + ".y", 0.0); |
95 |
double z = config.getDouble( name + ".z", 0.0); |
96 |
|
97 |
World world = p.getServer().getWorld(worldName); |
98 |
int blockX = (int) Math.floor(x); |
99 |
int blockZ = (int) Math.floor(z); |
100 |
world.loadChunk(blockX, blockZ); |
101 |
|
102 |
final Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); |
103 |
|
104 |
Runnable r = new Runnable() { |
105 |
public void run() { |
106 |
p.teleport(loc); |
107 |
} |
108 |
}; |
109 |
int tickCount = 2; // 2 ticks = 100ms |
110 |
|
111 |
p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount); |
112 |
|
113 |
} else { |
114 |
p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); |
115 |
} |
116 |
|
117 |
} |
118 |
} |