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.OfflinePlayer; |
10 |
import org.bukkit.command.Command; |
11 |
import org.bukkit.command.CommandExecutor; |
12 |
import org.bukkit.command.CommandSender; |
13 |
import org.bukkit.entity.Player; |
14 |
import org.bukkit.plugin.Plugin; |
15 |
import org.bukkit.configuration.Configuration; |
16 |
import org.bukkit.configuration.file.YamlConfiguration; |
17 |
|
18 |
public class HomeCommand implements CommandExecutor { |
19 |
|
20 |
Plugin plugin; |
21 |
|
22 |
YamlConfiguration config; |
23 |
|
24 |
File file; |
25 |
|
26 |
public HomeCommand(Plugin plugin) { |
27 |
this.plugin = plugin; |
28 |
|
29 |
file = new File( plugin.getDataFolder(), "homes.yml"); |
30 |
|
31 |
config = new YamlConfiguration(); |
32 |
try { |
33 |
config.load(file); |
34 |
} catch (Exception e) { |
35 |
System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() ); |
36 |
} |
37 |
} |
38 |
|
39 |
Configuration getConfig() { |
40 |
return config; |
41 |
} |
42 |
|
43 |
|
44 |
@Override |
45 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
46 |
|
47 |
if (!(sender instanceof Player)) { |
48 |
return false; |
49 |
} |
50 |
final Player p = (Player) sender; |
51 |
|
52 |
String name = p.getName(); |
53 |
|
54 |
if ( command.getName().equals("home") ) { |
55 |
tpUser(p, name); |
56 |
} |
57 |
if ( command.getName().equals("ophome") ) { |
58 |
if (args.length != 1) { |
59 |
p.sendMessage("Usage: /ophome <player>"); |
60 |
return true; |
61 |
} |
62 |
if ( p.isOp() ) { |
63 |
OfflinePlayer op = sender.getServer().getOfflinePlayer(args[0]); |
64 |
if (op.hasPlayedBefore() == false && op.isOnline() == false) { |
65 |
sender.sendMessage("Player not found"); |
66 |
} else { |
67 |
p.sendMessage("Sending you to " + op.getName() + "'s home"); |
68 |
tpUser(p, op.getName() ); |
69 |
} |
70 |
} else { |
71 |
p.sendMessage("Only operators may use this command"); |
72 |
} |
73 |
} |
74 |
|
75 |
if (command.getName().equals("sethome") ) { |
76 |
Location loc = p.getLocation(); |
77 |
config.set( name + ".yaw", loc.getYaw() ); |
78 |
config.set( name + ".pitch", loc.getPitch() ); |
79 |
config.set( name + ".world", loc.getWorld().getName() ); |
80 |
config.set( name + ".x", loc.getX() ); |
81 |
config.set( name + ".y", loc.getY() ); |
82 |
config.set( name + ".z", loc.getZ() ); |
83 |
|
84 |
try { |
85 |
config.save(file); |
86 |
p.sendMessage(ChatColor.YELLOW + "home is set"); |
87 |
} catch (IOException e) { |
88 |
System.out.println("[HoerupUtils] : /sethome save threw an IO exception: " + e.getMessage() ); |
89 |
} |
90 |
} |
91 |
|
92 |
return true; |
93 |
} |
94 |
|
95 |
private void tpUser(final Player p, String name) { |
96 |
String worldName = config.getString( name + ".world"); |
97 |
if (worldName != null) { |
98 |
double yaw = config.getDouble( name + ".yaw", 0.0); |
99 |
double pitch = config.getDouble( name + ".pitch", 0.0); |
100 |
double x = config.getDouble( name + ".x", 0.0); |
101 |
double y = config.getDouble( name + ".y", 0.0); |
102 |
double z = config.getDouble( name + ".z", 0.0); |
103 |
|
104 |
World world = p.getServer().getWorld(worldName); |
105 |
int blockX = (int) Math.floor(x); |
106 |
int blockZ = (int) Math.floor(z); |
107 |
world.loadChunk(blockX, blockZ); |
108 |
|
109 |
final Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); |
110 |
|
111 |
Runnable r = new Runnable() { |
112 |
public void run() { |
113 |
p.teleport(loc); |
114 |
} |
115 |
}; |
116 |
int tickCount = 2; // 2 ticks = 100ms |
117 |
|
118 |
p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount); |
119 |
|
120 |
} else { |
121 |
p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); |
122 |
} |
123 |
|
124 |
} |
125 |
} |