Skip to content

Commit

Permalink
default config options, display chat in console config option and inf…
Browse files Browse the repository at this point in the history
…o output formatting
  • Loading branch information
bierdosenhalter committed Aug 31, 2022
1 parent 7812f44 commit 86d2e2a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.zeroBzeroT.chatCo</groupId>
<artifactId>ChatCoPlus</artifactId>
<version>0.27.2</version>
<version>0.27.3</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

Expand Down
73 changes: 35 additions & 38 deletions src/main/java/org/zeroBzeroT/chatCo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@ public class Main extends JavaPlugin {
public static File PermissionConfig;
public static File WhisperLog;
public static File dataFolder;
private static File Configuration;
private static File Help;
public Collection<ChatPlayer> playerList;
public boolean checkForChatDisable;
public boolean checkForIgnores;

public void onDisable() {
playerList.clear();
}

public void onEnable() {
playerList = Collections.synchronizedCollection(new ArrayList<>());
checkForChatDisable = getConfig().getBoolean("ChatCo.chatDisableEnabled", true);
checkForIgnores = getConfig().getBoolean("ChatCo.ignoresEnabled", true);

checkFiles();
readConfig(0);
// Config defaults
getConfig().options().copyDefaults(true);
getConfig().options().copyHeader(true);

saveResourceFiles();
toggleConfigValue(0);

final PluginManager pm = getServer().getPluginManager();

Expand All @@ -47,7 +46,7 @@ public void onEnable() {
pm.registerEvents(new Whispers(this), this);
}

if (getConfig().getBoolean("ChatCo.SpoilersEnabled", false)) {
if (getConfig().getBoolean("ChatCo.spoilersEnabled", false)) {
pm.registerEvents(new Spoilers(), this);
}

Expand All @@ -57,13 +56,13 @@ public void onEnable() {
}
}

private void readConfig(final int change) {
private void toggleConfigValue(final int change) {
switch (change) {
case 3:
getConfig().set("ChatCo.SpoilersEnabled", true);
getConfig().set("ChatCo.spoilersEnabled", true);
break;
case 4:
getConfig().set("ChatCo.SpoilersEnabled", false);
getConfig().set("ChatCo.spoilersEnabled", false);
break;
case 5:
getConfig().set("ChatCo.whisperChangesEnabled", true);
Expand All @@ -90,9 +89,8 @@ private void readConfig(final int change) {
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void checkFiles() {
private void saveResourceFiles() {
Main.dataFolder = getDataFolder();
Main.Configuration = new File(Main.dataFolder, "config.yml");
Main.PermissionConfig = new File(Main.dataFolder, "permissionConfig.yml");
Main.WhisperLog = new File(Main.dataFolder, "whisperlog.txt");
Main.Help = new File(Main.dataFolder, "help.txt");
Expand All @@ -107,9 +105,8 @@ private void checkFiles() {
saveStreamToFile(getResource("help.txt"), Main.Help);
}

if (!Main.Configuration.exists()) {
saveDefaultConfig();
}
// Save the default config file, if it does not exist
saveDefaultConfig();

if (!Main.PermissionConfig.exists()) {
Main.PermissionConfig.getParentFile().mkdirs();
Expand All @@ -121,16 +118,16 @@ public boolean onCommand(final CommandSender sender, final Command cmd, final St
if (sender instanceof Player) {
if (cmd.getName().equalsIgnoreCase("togglechat") && getConfig().getBoolean("toggleChatEnabled", true)) {
if (toggleChat((Player) sender)) {
sender.sendMessage(ChatColor.RED + "Your chat is now disabled until you type /togglechat or relog");
sender.sendMessage(ChatColor.RED + "Your chat is now disabled until you type /togglechat or relog.");
} else {
sender.sendMessage(ChatColor.RED + "Your chat has been re-enabled, type /togglechat to disable it again");
sender.sendMessage(ChatColor.RED + "Your chat has been re-enabled, type /togglechat to disable it again.");
}
return true;
} else if (cmd.getName().equalsIgnoreCase("toggletells")) {
if (toggleTells((Player) sender)) {
sender.sendMessage(ChatColor.RED + "You will no longer receive tells, type /toggletells to see them again");
sender.sendMessage(ChatColor.RED + "You will no longer receive tells, type /toggletells to see them again.");
} else {
sender.sendMessage(ChatColor.RED + "You now receive tells, type /toggletells to disable them again");
sender.sendMessage(ChatColor.RED + "You now receive tells, type /toggletells to disable them again.");
}
return true;
} else if (cmd.getName().equalsIgnoreCase("unignoreall") && getConfig().getBoolean("ignoresEnabled", true)) {
Expand All @@ -143,19 +140,19 @@ public boolean onCommand(final CommandSender sender, final Command cmd, final St
} else if (cmd.getName().equalsIgnoreCase("ignore") && getConfig().getBoolean("ignoresEnabled", true)) {
try {
if (args.length < 1) {
sender.sendMessage(ChatColor.RED + "You forgot to type the name of the player");
sender.sendMessage(ChatColor.RED + "You forgot to type the name of the player.");
return true;
}

if (args[0].length() > 16) {
sender.sendMessage(ChatColor.RED + "You entered an invalid player name");
sender.sendMessage(ChatColor.RED + "You entered an invalid player name.");
return true;
}

final Player ignorable = Bukkit.getServer().getPlayer(args[0]);

if (ignorable == null) {
sender.sendMessage(ChatColor.RED + "You have entered a player who does not exist or is offline");
sender.sendMessage(ChatColor.RED + "You have entered a player who does not exist or is offline.");
return true;
}

Expand All @@ -165,15 +162,15 @@ public boolean onCommand(final CommandSender sender, final Command cmd, final St
e.printStackTrace();
}
} else if (cmd.getName().equalsIgnoreCase("ignorelist") && getConfig().getBoolean("ignoresEnabled", true)) {
sender.sendMessage(ChatColor.WHITE + "Ignored players:");
sender.sendMessage(ChatColor.YELLOW + "Ignored players:");
int i = 0;

for (final String ignores : getChatPlayer((Player) sender).getIgnoreList()) {
sender.sendMessage(ChatColor.WHITE + "" + ChatColor.ITALIC + ignores);
sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.ITALIC + ignores);
++i;
}

sender.sendMessage(ChatColor.WHITE + "" + i + " players ignored");
sender.sendMessage(ChatColor.YELLOW + "" + i + " players ignored.");
return true;
}
}
Expand All @@ -189,40 +186,40 @@ public boolean onCommand(final CommandSender sender, final Command cmd, final St
if (args.length >= 2) {
if (args[0].equalsIgnoreCase("spoilers")) {
if (args[1].equalsIgnoreCase("e")) {
readConfig(3);
toggleConfigValue(3);
sender.sendMessage("Spoilers enabled");
} else if (args[1].equalsIgnoreCase("d")) {
readConfig(4);
toggleConfigValue(4);
sender.sendMessage("Spoilers disabled");
}
}

if (args[0].equalsIgnoreCase("whispers")) {
if (args[1].equalsIgnoreCase("e")) {
readConfig(5);
toggleConfigValue(5);
sender.sendMessage("Whisper changes enabled");
} else if (args[1].equalsIgnoreCase("d")) {
readConfig(6);
toggleConfigValue(6);
sender.sendMessage("Whisper changes disabled");
}
}

if (args[0].equalsIgnoreCase("newcommands")) {
if (args[1].equalsIgnoreCase("e")) {
readConfig(7);
toggleConfigValue(7);
sender.sendMessage("New Whisper commands enabled");
} else if (args[1].equalsIgnoreCase("d")) {
readConfig(8);
toggleConfigValue(8);
sender.sendMessage("New whisper commands disabled");
}
}

if (args[0].equalsIgnoreCase("whisperlog")) {
if (args[1].equalsIgnoreCase("e")) {
readConfig(9);
toggleConfigValue(9);
sender.sendMessage("Whisper logging enabled");
} else if (args[1].equalsIgnoreCase("d")) {
readConfig(10);
toggleConfigValue(10);
sender.sendMessage("Whisper logging disabled");
}
}
Expand Down Expand Up @@ -270,12 +267,12 @@ private boolean toggleTells(final Player p) {
}

private void ignorePlayer(final Player p, final String target) throws IOException {
String message = ChatColor.WHITE + "Chat messages from " + target + " will be ";
String message = ChatColor.YELLOW + "Chat messages from " + target + " will be ";

if (getChatPlayer(p).isIgnored(target)) {
message += "shown";
message += "shown.";
} else {
message += "hidden";
message += "hidden.";
}

p.sendMessage(message);
Expand All @@ -284,7 +281,7 @@ private void ignorePlayer(final Player p, final String target) throws IOExceptio

private void unIgnoreAll(final Player p) throws IOException {
getChatPlayer(p).unIgnoreAll();
String message = ChatColor.WHITE + "Ignore list deleted";
String message = ChatColor.YELLOW + "Ignore list deleted.";
p.sendMessage(message);
}

Expand Down
30 changes: 17 additions & 13 deletions src/main/java/org/zeroBzeroT/chatCo/PublicChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class PublicChat implements Listener {
public PublicChat(final Main plugin) {
this.plugin = plugin;
File customConfig = Main.PermissionConfig;
this.permissionConfig = YamlConfiguration.loadConfiguration(customConfig);
permissionConfig = YamlConfiguration.loadConfiguration(customConfig);
}

public String replacePrefixColors(String message, final Player player) {
for (ChatColor color : ChatColor.values()) {
if (this.plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name()) != null && message.startsWith(this.plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name()))) {
if (plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name()) != null && message.startsWith(plugin.getConfig().getString("ChatCo.chatPrefixes." + color.name()))) {

// check for global or player permission
if (this.permissionConfig.getBoolean("ChatCo.chatPrefixes." + color.name(), false) || player.hasPermission("ChatCo.chatPrefixes." + color.name())) {
if (permissionConfig.getBoolean("ChatCo.chatPrefixes." + color.name(), false) || player.hasPermission("ChatCo.chatPrefixes." + color.name())) {
message = color + message;
}

Expand All @@ -47,9 +47,9 @@ public String replacePrefixColors(String message, final Player player) {

public String replaceInlineColors(String message, final Player player) {
for (ChatColor color : ChatColor.values()) {
if ((this.permissionConfig.getBoolean("ChatCo.chatColors." + color.name(), false) || player.hasPermission("ChatCo.chatColors." + color.name()))
&& this.plugin.getConfig().getString("ChatCo.chatColors." + color.name()) != null) {
message = message.replace(this.plugin.getConfig().getString("ChatCo.chatColors." + color.name()), color.toString());
if ((permissionConfig.getBoolean("ChatCo.chatColors." + color.name(), false) || player.hasPermission("ChatCo.chatColors." + color.name()))
&& plugin.getConfig().getString("ChatCo.chatColors." + color.name()) != null) {
message = message.replace(plugin.getConfig().getString("ChatCo.chatColors." + color.name()), color.toString());
}
}

Expand All @@ -66,8 +66,8 @@ public void onPlayerChat(final AsyncPlayerChatEvent event) {

// Plain message
final Player player = event.getPlayer();
String legacyMessage = this.replacePrefixColors(event.getMessage(), player);
legacyMessage = this.replaceInlineColors(legacyMessage, player);
String legacyMessage = replacePrefixColors(event.getMessage(), player);
legacyMessage = replaceInlineColors(legacyMessage, player);

// Do not send empty messages
if (ChatColor.stripColor(legacyMessage).trim().length() == 0) {
Expand All @@ -91,13 +91,17 @@ public void onPlayerChat(final AsyncPlayerChatEvent event) {
message.addExtra(componentFromLegacyText("> "));
message.addExtra(messageText);

// Send to console
if (plugin.getConfig().getBoolean("ChatCo.chatToConsole", true))
plugin.getLogger().info(message.toLegacyText());

// Send to the players
for (Player recipient : event.getRecipients()) {
try {
ChatPlayer chatPlayer = this.plugin.getChatPlayer(recipient);
ChatPlayer chatPlayer = plugin.getChatPlayer(recipient);

if ((!chatPlayer.chatDisabled || !this.plugin.checkForChatDisable) &&
(!chatPlayer.isIgnored(player.getName()) || !this.plugin.checkForIgnores)) {
if ((!chatPlayer.chatDisabled || !plugin.getConfig().getBoolean("ChatCo.chatDisableEnabled", true)) &&
(!chatPlayer.isIgnored(player.getName()) || !plugin.getConfig().getBoolean("ChatCo.ignoresEnabled", true))) {
recipient.spigot().sendMessage(message);
}

Expand All @@ -109,11 +113,11 @@ public void onPlayerChat(final AsyncPlayerChatEvent event) {

@EventHandler
public void onPlayerQuit(final PlayerQuitEvent e) {
this.plugin.remove(e.getPlayer());
plugin.remove(e.getPlayer());
}

@EventHandler
public void onPlayerKick(final PlayerKickEvent e) {
this.plugin.remove(e.getPlayer());
plugin.remove(e.getPlayer());
}
}
20 changes: 10 additions & 10 deletions src/main/java/org/zeroBzeroT/chatCo/Whispers.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)

if (plugin.getConfig().getBoolean("ChatCo.lastCommand", true) && (args[0].equalsIgnoreCase("/l") || args[0].equalsIgnoreCase("/last"))) {
if (args.length == 1) {
sender.sendMessage(ChatColor.WHITE + "Usage: /l <message>");
sender.sendMessage(ChatColor.YELLOW + "Usage: /l <message>");
event.setCancelled(true);
return;
}
Expand All @@ -47,9 +47,9 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)

if ((target == null && plugin.getChatPlayer(sender).LastReceiver != null)
|| Utils.isVanished(target)) {
sender.sendMessage(ChatColor.RED + "The last person you sent a private message to is offline");
sender.sendMessage(ChatColor.RED + "The last person you sent a private message to is offline.");
} else if (target == null) {
sender.sendMessage(ChatColor.RED + "You have not initiated any private message in this session");
sender.sendMessage(ChatColor.RED + "You have not initiated any private message in this session.");
} else {
String message = Arrays.stream(args).skip(1).collect(Collectors.joining(" "));
sendPrivateMessage(sender, target, message);
Expand All @@ -58,7 +58,7 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
event.setCancelled(true);
} else if (plugin.getConfig().getBoolean("ChatCo.replyCommands", true) && (args[0].equalsIgnoreCase("/r") || args[0].equalsIgnoreCase("/reply"))) {
if (args.length == 1) {
sender.sendMessage(ChatColor.WHITE + "Usage: /r <message>");
sender.sendMessage(ChatColor.YELLOW + "Usage: /r <message>");
event.setCancelled(true);
return;
}
Expand All @@ -67,9 +67,9 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)

if ((target == null && plugin.getChatPlayer(sender).LastMessenger != null)
|| Utils.isVanished(target)) {
sender.sendMessage(ChatColor.RED + "The last person you received a private message from is offline");
sender.sendMessage(ChatColor.RED + "The last person you received a private message from is offline.");
} else if (target == null) {
sender.sendMessage(ChatColor.RED + "You have not received any private messages in this session");
sender.sendMessage(ChatColor.RED + "You have not received any private messages in this session.");
} else {
String message = Arrays.stream(args).skip(1).collect(Collectors.joining(" "));
sendPrivateMessage(sender, target, message);
Expand All @@ -78,15 +78,15 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
event.setCancelled(true);
} else if (args[0].equalsIgnoreCase("/tell") || args[0].equalsIgnoreCase("/msg") || args[0].equalsIgnoreCase("/t") || args[0].equalsIgnoreCase("/w") || args[0].equalsIgnoreCase("/whisper") || args[0].equalsIgnoreCase("/pm")) {
if (args.length < 3) {
sender.sendMessage(ChatColor.WHITE + "Usage: /w <player> <message>");
sender.sendMessage(ChatColor.YELLOW + "Usage: /w <player> <message>");
event.setCancelled(true);
return;
}

final Player target = Bukkit.getPlayerExact(args[1]);

if (target == null || Utils.isVanished(target)) {
sender.sendMessage(ChatColor.RED + args[1] + " is offline");
sender.sendMessage(ChatColor.RED + args[1] + " is offline.");
event.setCancelled(true);
return;
}
Expand Down Expand Up @@ -173,9 +173,9 @@ private void sendPrivateMessage(Player sender, Player receiver, String message)
sender.spigot().sendMessage(senderMessage);

if (isIgnoring && plugin.getConfig().getBoolean("ChatCo.ignoreMessageEnabled", true)) {
sender.sendMessage(ChatColor.RED + receiver.getName() + " is ignoring you");
sender.sendMessage(ChatColor.RED + receiver.getName() + " is ignoring you.");
} else if (doNotSend && plugin.getConfig().getBoolean("ChatCo.chatDisabledMessageEnabled", true)) {
sender.sendMessage(ChatColor.RED + receiver.getName() + "'s chat is disabled");
sender.sendMessage(ChatColor.RED + receiver.getName() + "'s chat is disabled.");
} else if (!doNotSend && !isIgnoring) {
receiver.spigot().sendMessage(receiverMessage);

Expand Down
Loading

0 comments on commit 86d2e2a

Please sign in to comment.