1 |
torben |
1950 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
|
|
3 |
|
|
import java.util.HashMap; |
4 |
|
|
|
5 |
|
|
import org.bukkit.ChatColor; |
6 |
|
|
import org.bukkit.Location; |
7 |
|
|
import org.bukkit.command.Command; |
8 |
|
|
import org.bukkit.command.CommandExecutor; |
9 |
|
|
import org.bukkit.command.CommandSender; |
10 |
|
|
import org.bukkit.entity.Player; |
11 |
|
|
import org.bukkit.event.EventHandler; |
12 |
|
|
import org.bukkit.event.Listener; |
13 |
|
|
import org.bukkit.event.player.PlayerQuitEvent; |
14 |
|
|
import org.bukkit.plugin.Plugin; |
15 |
|
|
|
16 |
|
|
public class WayPoint implements CommandExecutor, Listener { |
17 |
|
|
|
18 |
|
|
Plugin plugin; |
19 |
|
|
|
20 |
|
|
HashMap<String,Location> waypoints = new HashMap<String,Location>(); |
21 |
|
|
|
22 |
|
|
public WayPoint(Plugin plugin) { |
23 |
|
|
this.plugin = plugin; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
@Override |
27 |
|
|
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
28 |
|
|
|
29 |
|
|
if (!(sender instanceof Player)) { |
30 |
|
|
return false; |
31 |
|
|
} |
32 |
|
|
final Player p = (Player) sender; |
33 |
|
|
|
34 |
|
|
String name = p.getName(); |
35 |
|
|
|
36 |
|
|
if ( command.getName().equals("waypoint") ) { |
37 |
|
|
tpUser(p, name); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
if (command.getName().equals("setwaypoint") ) { |
41 |
|
|
Location loc = p.getLocation().clone(); |
42 |
|
|
waypoints.put(name, loc); |
43 |
|
|
p.sendMessage("Waypoint is set"); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
return true; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
private void tpUser(final Player p, String name) { |
50 |
|
|
|
51 |
|
|
final Location loc = waypoints.get(name); |
52 |
|
|
if (loc != null) { |
53 |
|
|
Runnable r = new Runnable() { |
54 |
|
|
public void run() { |
55 |
|
|
p.teleport(loc); |
56 |
|
|
} |
57 |
|
|
}; |
58 |
|
|
int tickCount = 2; // 2 ticks = 100ms |
59 |
|
|
|
60 |
|
|
p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount); |
61 |
|
|
|
62 |
|
|
} else { |
63 |
|
|
p.sendMessage(ChatColor.YELLOW + "You don't have an active waypoint"); |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
@EventHandler |
68 |
|
|
public void onPlayerQuit(PlayerQuitEvent e) { |
69 |
|
|
Player p = e.getPlayer(); |
70 |
|
|
|
71 |
|
|
if (waypoints.containsKey( p.getName() )) { |
72 |
|
|
waypoints.remove( p.getName() ); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
} |
76 |
|
|
} |