summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/modules/chatgroups
diff options
context:
space:
mode:
authorDavid <david@panic.tk>2018-11-07 23:50:06 +0100
committerDavid <david@panic.tk>2018-11-07 23:50:06 +0100
commit604cf01967ede98bf5024e4926bb0777fc4e8eee (patch)
treee2fa63d7e683769ee3bf3eddc75280648e92eb04 /src/main/java/com/redstoner/modules/chatgroups
parente86c52ef7c0e1e33c6af0e8674b038976bec11cc (diff)
Converted Modules to gradle
Diffstat (limited to 'src/main/java/com/redstoner/modules/chatgroups')
-rw-r--r--src/main/java/com/redstoner/modules/chatgroups/Chatgroups.cmd35
-rw-r--r--src/main/java/com/redstoner/modules/chatgroups/Chatgroups.java400
2 files changed, 435 insertions, 0 deletions
diff --git a/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.cmd b/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.cmd
new file mode 100644
index 0000000..33fd0c5
--- /dev/null
+++ b/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.cmd
@@ -0,0 +1,35 @@
+command cgt {
+ [empty] {
+ help Toggles your cgtoggle status.;
+ type player;
+ run cgtoggle;
+ }
+}
+command cgkey {
+ [string:key] {
+ help Sets your chatgroup key.;
+ run setcgkey key;
+ type player;
+ }
+}
+command cgsay {
+ [string:message...] {
+ help Chats in your chatgroup.;
+ run cgsay message;
+ }
+}
+command cg {
+ join [string:group] {
+ help Joins a chatgroup.;
+ run cgjoin group;
+ }
+ leave {
+ help Leaves your chatgroup.;
+ run cgleave;
+ }
+ info {
+ help Displays info about your chatgroup.;
+ run cginfo;
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.java b/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.java
new file mode 100644
index 0000000..457702c
--- /dev/null
+++ b/src/main/java/com/redstoner/modules/chatgroups/Chatgroups.java
@@ -0,0 +1,400 @@
+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 = 4, minor = 1, 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;
+ }
+
+ /** Let's 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);
+ }
+}