summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.java
blob: 66097864e28e00ee5e2ceadb2d7b08e08ac9c46a (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.redstoner.modules.chatgroups;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.json.simple.JSONObject;

import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader;
import com.redstoner.misc.BroadcastFilter;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.misc.JsonManager;
import com.redstoner.misc.Main;
import com.redstoner.misc.Utils;
import com.redstoner.modules.Module;
import com.redstoner.modules.ignore.Ignore;
import com.redstoner.modules.socialspy.Socialspy;

import net.nemez.chatapi.ChatAPI;

/**
 * The ChatGroups module. Allows people to have private sub-chats that can be
 * accessed via a single char prefix or a toggle.
 * 
 * @author Pepich
 */
@Commands(CommandHolderType.File)
@AutoRegisterListener
@Version(major = 5, minor = 0, revision = 0, compatible = 4)
public class Chatgroups implements Module, Listener {
	private static final char defaultKey = ':';
	private static final File groupsLocation = new File(Main.plugin.getDataFolder(), "chatgroups.json");
	private static final File keysLocation = new File(Main.plugin.getDataFolder(), "chatgroup_keys.json");
	private ArrayList<UUID> cgtoggled;
	private static JSONObject groups, keys;

	@Override
	public boolean onEnable() {
		groups = JsonManager.getObject(groupsLocation);
		if (groups == null) {
			groups = new JSONObject();
			saveGroups();
		}
		keys = JsonManager.getObject(keysLocation);
		if (keys == null) {
			keys = new JSONObject();
			saveKeys();
		}
		cgtoggled = new ArrayList<>();
		return true;
	}

	@Override
	public void onDisable() {
		saveKeys();
		saveGroups();
	}

	/**
	 * Prints chatgroup info (like players in the group, groupname) to the sender.
	 * 
	 * @param sender the issuer of the command.
	 * @return true.
	 */
	@SuppressWarnings("unchecked")
	@Command(hook = "cginfo")
	public boolean cgInfo(CommandSender sender) {
		String group = getGroup(sender);
		if (group == null) getLogger().message(sender, true, "You are not in a chatgroup!");
		else {
			ArrayList<String> message = new ArrayList<>();
			message.add("§7Your current chatgroup is: §6" + group);
			ArrayList<String> players = new ArrayList<>();
			Iterator<String> iter = groups.keySet().iterator();
			while (iter.hasNext()) {
				String id = iter.next();
				if (((String) groups.get(id)).equals(group)) {
					if (!id.equals("CONSOLE")) {
						UUID uuid = UUID.fromString(id);
						Player p = Bukkit.getPlayer(uuid);
						if (p != null) players.add(p.getDisplayName());
						else players.add(Bukkit.getOfflinePlayer(UUID.fromString(id)).getName());
					} else players.add(id);
				}
			}
			StringBuilder sb = new StringBuilder("&6Other players in this group: &9");
			for (String player : players) {
				sb.append(player);
				sb.append("&7, &9");
			}
			sb.delete(sb.length() - 2, sb.length());
			message.add(sb.toString());
			getLogger().message(sender, message.toArray(new String[] {}));
		}
		return true;
	}

	/**
	 * Prints a Players cgkey to their chat.
	 * 
	 * @param sender the issuer of the command.
	 */
	public void getCgKey(CommandSender sender) {
		getLogger().message(sender, "Your current cgkey is §6" + getKey((Player) sender));
	}

	/**
	 * Sets the cgkey of a Player.
	 * 
	 * @param sender the issuer of the command.
	 * @param key    the key to be set. Set to NULL or "" to get your current key.
	 * @return true.
	 */
	@SuppressWarnings("unchecked")
	@Command(hook = "setcgkey")
	public boolean setCgKey(CommandSender sender, String key) {
		if (key.length() > 1) {
			getLogger().message(sender, true, "Could not set your key to §6" + key + " §7, it can be at most one char.");
			return true;
		}
		if (key == null || key.length() == 0) {
			getCgKey(sender);
			return true;
		}
		getLogger().message(sender, "Set your key to §6" + key);
		keys.put(((Player) sender).getUniqueId().toString(), key + "");
		saveKeys();
		return true;
	}

	/**
	 * Lets a Player toggle their auto-cg status to allow for automatically sending
	 * chat messages to their chatgroup.
	 * 
	 * @param sender the issuer of the command.
	 * @return true.
	 */
	@Command(hook = "cgtoggle")
	public boolean cgToggleCommand(CommandSender sender) {
		if (getGroup(sender) != null) if (cgtoggled.contains(((Player) sender).getUniqueId())) {
			cgtoggled.remove(((Player) sender).getUniqueId());
			getLogger().message(sender, "CGT now §cdisabled");
		} else {
			cgtoggled.add(((Player) sender).getUniqueId());
			getLogger().message(sender, "CGT now §aenabled");
		}
		else getLogger().message(sender, true, "You are not in a chatgroup!");
		return true;
	}

	/**
	 * Lets a CommandSender leave their group.
	 * 
	 * @param sender the command issuer.
	 * @return true.
	 */
	@Command(hook = "cgleave")
	public boolean cgLeave(CommandSender sender) {
		String group = removeGroup(sender);
		if (group == null) {
			getLogger().message(sender, true, "You were not in a chatgroup!");
			return true;
		}
		String name = Utils.getName(sender);
		sendToGroup(group, "&9" + name + " &7left the group!");
		getLogger().message(sender, "Successfully removed you from your group!");
		if (sender instanceof Player) cgtoggled.remove(((Player) sender).getUniqueId());
		return true;
	}

	/**
	 * Lets a CommandSender join a group.
	 * 
	 * @param sender the command issuer.
	 * @param name   the name of the group.
	 * @return true.
	 */
	@Command(hook = "cgjoin")
	public boolean cgJoin(CommandSender sender, String name) {
		String pname = Utils.getName(sender);
		String group = getGroup(sender);
		if (group != null && group.equals(name)) getLogger().message(sender, true, "You were already in group §6" + name);
		else {
			setGroup(sender, null);
			if (group != null) sendToGroup(group, "&9" + pname + " &7left the group!");
			sendToGroup(name, "&9" + pname + " &7joined the group!");
			setGroup(sender, name);
			getLogger().message(sender, "Successfully joined group §6" + name);
		}
		return true;
	}

	/**
	 * Sends a message to a group.
	 * 
	 * @param sender  the sender of the message - the message will be sent to the
	 *                group of the sender.
	 * @param message the message to be sent.
	 * @return true.
	 */
	@Command(hook = "cgsay")
	public boolean cgSay(CommandSender sender, String message) {
		String group = getGroup(sender);
		if (group != null) sendToGroup(sender, message);
		else getLogger().message(sender, true, "You are not in a chatgroup right now!");
		return true;
	}

	/**
	 * Deals with chat events to allow for cgkeys and cgtoggle.
	 * 
	 * @param event the chat event containing the player and the message.
	 */
	@EventHandler
	public void onPlayerChat(AsyncPlayerChatEvent event) {
		String group = getGroup(event.getPlayer());
		Player player = event.getPlayer();
		if (group != null) {
			if (event.getMessage().startsWith(getKey(player))) {
				event.setCancelled(true);
				sendToGroup(event.getPlayer(), event.getMessage().substring(1));
			} else if (cgtoggled.contains(event.getPlayer().getUniqueId())) {
				event.setCancelled(true);
				sendToGroup(event.getPlayer(), event.getMessage());
			}
		}
	}

	/**
	 * Finds the group of a CommandSender.
	 * 
	 * @param target the CommandSender to get the group of.
	 * @return the group of the target or NULL if he doesn't have one.
	 */
	public static String getGroup(CommandSender target) {
		if (target instanceof Player) return (String) groups.get(((Player) target).getUniqueId().toString());
		else return (String) groups.get("CONSOLE");
	}

	/**
	 * Sets the group of the CommandSender.
	 * 
	 * @param target the CommandSender to set the group of.
	 * @param group  the name of the group to join.
	 */
	@SuppressWarnings("unchecked")
	private void setGroup(CommandSender target, String group) {
		if (target instanceof Player) groups.put(((Player) target).getUniqueId().toString(), group);
		else groups.put("CONSOLE", group);
		saveGroups();
	}

	/**
	 * Removes a CommandSender from their chatgroup. Will also save the groups after
	 * finishing
	 * 
	 * @param target the CommandSender to get their group removed.
	 */
	private String removeGroup(CommandSender target) {
		String group;
		if (target instanceof Player) group = (String) groups.remove(((Player) target).getUniqueId().toString());
		else group = (String) groups.remove("CONSOLE");
		saveGroups();
		return group;
	}

	/**
	 * This method will find the ChatgGroup key of any player.
	 * 
	 * @param player the player to get the key from.
	 * @return the key.
	 */
	public static String getKey(Player player) {
		String key = (String) keys.get(player.getUniqueId().toString());
		return (key == null ? "" + defaultKey : key);
	}

	/**
	 * This method sends a message to a chatgroup.
	 * 
	 * @param sender  the sender of the message. Also defines which group the
	 *                message will be sent to.
	 * @param message the message to be sent.
	 */
	private void sendToGroup(CommandSender sender, String message) {
		String name = Utils.getName(sender);
		String group = getGroup(sender);
		message = ChatAPI.colorify(null, message);

		BroadcastFilter ignore = ModuleLoader.exists("Ignore") ? Ignore.getIgnoredBy(sender) : null;
		Utils.broadcast("§8[§bCG§8] §9", name + "§8: §6" + message, new BroadcastFilter() {
			@Override
			public boolean sendTo(CommandSender recipient) {

				String rgroup = getGroup(recipient);
				if (rgroup != null && (ignore == null ? true : ignore.sendTo(recipient))) return rgroup.equals(group);
				else return false;
			}
		});
		if (ModuleLoader.getModule("Socialspy") != null) {
			Socialspy.spyBroadcast(sender, "§e" + group + " §a(cg)", message, "/cg", new BroadcastFilter() {
				@Override
				public boolean sendTo(CommandSender recipient) {
					return getGroup(recipient) == null || !getGroup(recipient).equals(group);
				}
			});
		}
		if (getGroup(Bukkit.getConsoleSender()) == null || !getGroup(Bukkit.getConsoleSender()).equals(group)) {
			getLogger().info(name + " in " + group + ": " + message + " §8(hidden)");
		}
	}

	/**
	 * This method sends a message to a chatgroup.
	 * 
	 * @param sender  the sender of the message. Also defines which group the
	 *                message will be sent to.
	 * @param message the message to be sent.
	 */
	private void sendToGroup(String group, String message) {
		message = ChatAPI.colorify(null, message);
		Utils.broadcast(null, message, new BroadcastFilter() {
			@Override
			public boolean sendTo(CommandSender recipient) {
				String rgroup = getGroup(recipient);
				if (rgroup != null) return rgroup.equals(group);
				else return false;
			}
		});
		if (ModuleLoader.getModule("Socialspy") != null) {
			Socialspy.spyBroadcast(Bukkit.getConsoleSender(), "§e" + group + " §a(cg)", message, "/cg", new BroadcastFilter() {
				@Override
				public boolean sendTo(CommandSender recipient) {
					return getGroup(recipient) == null || !getGroup(recipient).equals(group);
				}
			});
		}
		if (getGroup(Bukkit.getConsoleSender()) == null || !getGroup(Bukkit.getConsoleSender()).equals(group)) {
			getLogger().info("In " + group + ": " + message + " §8(hidden)");
		}
	}

	/** Saves the groups. */
	private void saveGroups() {
		JsonManager.save(groups, groupsLocation);
	}

	/** Saves the keys. */
	private void saveKeys() {
		JsonManager.save(keys, keysLocation);
	}
}