-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMCommand.java
More file actions
94 lines (81 loc) · 3.7 KB
/
Copy pathPMCommand.java
File metadata and controls
94 lines (81 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.mrsans.pmlogger;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class PMCommand implements CommandExecutor {
// tracks who each player last messaged, for /r and /reply
private static final Map<UUID, UUID> lastConversation = new HashMap<>();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
Player player = (Player) sender;
String cmd = command.getName().toLowerCase();
Player target;
String message;
if (cmd.equals("r") || cmd.equals("reply")) {
UUID lastUuid = lastConversation.get(player.getUniqueId());
if (lastUuid == null) {
player.sendMessage(ChatColor.RED + "You have nobody to reply to.");
return true;
}
target = Bukkit.getPlayer(lastUuid);
if (target == null || !target.isOnline()) {
player.sendMessage(ChatColor.RED + "That player is no longer online.");
return true;
}
if (args.length < 1) {
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <message>");
return true;
}
message = String.join(" ", args);
} else {
if (args.length < 2) {
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <player> <message>");
return true;
}
target = Bukkit.getPlayer(args[0]);
if (target == null || !target.isOnline()) {
player.sendMessage(ChatColor.RED + "That player is not online.");
return true;
}
if (target.getUniqueId().equals(player.getUniqueId())) {
player.sendMessage(ChatColor.RED + "You cannot message yourself.");
return true;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i > 1) sb.append(" ");
sb.append(args[i]);
}
message = sb.toString();
}
// Send in vanilla-style formatting
player.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[me -> " + target.getName() + "] " + ChatColor.RESET + message);
target.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "[" + player.getName() + " -> me] " + ChatColor.RESET + message);
// Track conversation both ways so either side can /r
lastConversation.put(player.getUniqueId(), target.getUniqueId());
lastConversation.put(target.getUniqueId(), player.getUniqueId());
// Log it
PMLoggerPlugin.getInstance().logMessage(player.getName(), target.getName(), message);
// Deliver to spies (excluding the two people already in the conversation)
String spyLine = ChatColor.DARK_GRAY + "[PM] " + player.getName() + " -> " + target.getName() + ": " + ChatColor.GRAY + message;
for (UUID spyId : PMLoggerPlugin.getInstance().getSpyPlayers()) {
Player spy = Bukkit.getPlayer(spyId);
if (spy != null && spy.isOnline()
&& !spy.getUniqueId().equals(player.getUniqueId())
&& !spy.getUniqueId().equals(target.getUniqueId())) {
spy.sendMessage(spyLine);
}
}
return true;
}
}