Archived
0

Code cleanup batch 1

This commit is contained in:
David Panić
2018-11-10 22:42:49 +01:00
parent fe80557b8a
commit 0b1c2c69fd
13 changed files with 1046 additions and 1206 deletions

View File

@@ -29,84 +29,73 @@ 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.
/**
* The ChatGroups module. Allows people to have private sub-chats that can be
* accessed via a single char prefix or a toggle.
*
* @author Pepich */
* @author Pepich
*/
@Commands(CommandHolderType.File)
@AutoRegisterListener
@Version(major = 4, minor = 1, revision = 0, compatible = 4)
public class Chatgroups implements Module, Listener
{
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()
{
public boolean onEnable() {
groups = JsonManager.getObject(groupsLocation);
if (groups == null)
{
if (groups == null) {
groups = new JSONObject();
saveGroups();
}
keys = JsonManager.getObject(keysLocation);
if (keys == null)
{
if (keys == null) {
keys = new JSONObject();
saveKeys();
}
cgtoggled = new ArrayList<>();
return true;
}
@Override
public void onDisable()
{
public void onDisable() {
saveKeys();
saveGroups();
}
/** Prints chatgroup info (like players in the group, groupname) to the sender.
/**
* Prints chatgroup info (like players in the group, groupname) to the sender.
*
* @param sender the issuer of the command.
* @return true. */
* @return true.
*/
@SuppressWarnings("unchecked")
@Command(hook = "cginfo")
public boolean cgInfo(CommandSender sender)
{
public boolean cgInfo(CommandSender sender) {
String group = getGroup(sender);
if (group == null)
getLogger().message(sender, true, "You are not in a chatgroup!");
else
{
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())
{
while (iter.hasNext()) {
String id = iter.next();
if (((String) groups.get(id)).equals(group))
{
if (!id.equals("CONSOLE"))
{
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);
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)
{
for (String player : players) {
sb.append(player);
sb.append("&7, &9");
}
@@ -116,32 +105,31 @@ public class Chatgroups implements Module, Listener
}
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.
/**
* Prints a Players cgkey to their chat.
*
* @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. */
*/
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.");
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)
{
if (key == null || key.length() == 0) {
getCgKey(sender);
return true;
}
@@ -150,251 +138,226 @@ public class Chatgroups implements Module, Listener
saveKeys();
return true;
}
/** Let's a Player toggle their auto-cg status to allow for automatically sending chat messages to their chatgroup.
/**
* 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. */
* @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!");
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.
/**
* Lets a CommandSender leave their group.
*
* @param sender the command issuer.
* @return true. */
* @return true.
*/
@Command(hook = "cgleave")
public boolean cgLeave(CommandSender sender)
{
public boolean cgLeave(CommandSender sender) {
String group = removeGroup(sender);
if (group == null)
{
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());
if (sender instanceof Player) cgtoggled.remove(((Player) sender).getUniqueId());
return true;
}
/** Lets a CommandSender join a group.
/**
* Lets a CommandSender join a group.
*
* @param sender the command issuer.
* @param name the name of the group.
* @return true. */
* @param name the name of the group.
* @return true.
*/
@Command(hook = "cgjoin")
public boolean cgJoin(CommandSender sender, String name)
{
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
{
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!");
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.
/**
* 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 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. */
* @return true.
*/
@Command(hook = "cgsay")
public boolean cgSay(CommandSender sender, String message)
{
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!");
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.
/**
* Deals with chat events to allow for cgkeys and cgtoggle.
*
* @param event the chat event containing the player and the message. */
* @param event the chat event containing the player and the message.
*/
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event)
{
public void onPlayerChat(AsyncPlayerChatEvent event) {
String group = getGroup(event.getPlayer());
Player player = event.getPlayer();
if (group != null)
{
if (event.getMessage().startsWith(getKey(player)))
{
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()))
{
} else if (cgtoggled.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
sendToGroup(event.getPlayer(), event.getMessage());
}
}
}
/** Finds the group of a CommandSender.
/**
* 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");
* @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.
/**
* Sets the group of the CommandSender.
*
* @param target the CommandSender to set the group of.
* @param group the name of the group to join. */
* @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);
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
/**
* 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)
{
* @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");
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.
/**
* 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)
{
* @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.
/**
* 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)
{
* @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()
{
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)
{
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 (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()
{
if (ModuleLoader.getModule("Socialspy") != null) {
Socialspy.spyBroadcast(sender, "§e" + group + " §a(cg)", message, "/cg", new BroadcastFilter() {
@Override
public boolean sendTo(CommandSender recipient)
{
public boolean sendTo(CommandSender recipient) {
return getGroup(recipient) == null || !getGroup(recipient).equals(group);
}
});
}
if (getGroup(Bukkit.getConsoleSender()) == null || !getGroup(Bukkit.getConsoleSender()).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.
/**
* 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)
{
* @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()
{
Utils.broadcast(null, message, new BroadcastFilter() {
@Override
public boolean sendTo(CommandSender recipient)
{
public boolean sendTo(CommandSender recipient) {
String rgroup = getGroup(recipient);
if (rgroup != null)
return rgroup.equals(group);
else
return false;
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 (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))
{
if (getGroup(Bukkit.getConsoleSender()) == null || !getGroup(Bukkit.getConsoleSender()).equals(group)) {
getLogger().info("In " + group + ": " + message + " §8(hidden)");
}
}
/** Saves the groups. */
private void saveGroups()
{
private void saveGroups() {
JsonManager.save(groups, groupsLocation);
}
/** Saves the keys. */
private void saveKeys()
{
private void saveKeys() {
JsonManager.save(keys, keysLocation);
}
}