Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
CommandSpy: refactor spy system
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Dec 3, 2023
1 parent 2d2ad73 commit c57bc0d
Show file tree
Hide file tree
Showing 55 changed files with 338 additions and 279 deletions.
10 changes: 10 additions & 0 deletions src/main/java/net/flectone/chat/manager/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public void checkMigration() {
isLess420 = true;
}

if (compareVersions(fileVersion, "4.3.0") == -1) {
List<String> spyList = commands.getStringList("spy.list");
if (spyList.contains("msg")) {
spyList.remove("msg");
spyList.add("tell");
commands.set("spy.list", spyList);
commands.save();
}
}

config.set("plugin.version", projectVersion);
config.save();

Expand Down
18 changes: 8 additions & 10 deletions src/main/java/net/flectone/chat/model/player/FPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.flectone.chat.model.mail.Mail;
import net.flectone.chat.model.sound.FSound;
import net.flectone.chat.module.FModule;
import net.flectone.chat.module.commands.CommandSpy;
import net.flectone.chat.module.integrations.IntegrationsModule;
import net.flectone.chat.module.player.afkTimeout.AfkTimeoutModule;
import net.flectone.chat.module.player.nameTag.NameTagModule;
Expand Down Expand Up @@ -162,13 +163,6 @@ public void unregisterTeam() {
} catch (IllegalStateException ignore) {}
}

public void registerWorldPrefix() {
FModule fModule = moduleManager.get(WorldModule.class);
if (fModule instanceof WorldModule worldModule) {
setWorldPrefix(worldModule.getPrefix(player, player.getWorld()));
}
}

public void toDatabase() {
database.toDatabase(this);
}
Expand Down Expand Up @@ -315,17 +309,19 @@ public boolean isHaveCooldown(@NotNull String action) {
return isHaveCooldown;
}

public void sendCDMessage(@NotNull String action) {
public void sendCDMessage(@NotNull String alias, @NotNull String action) {
String message = locale.getVaultString(player, "commands.cooldown");
message = message
.replace("<alias>", action)
.replace("<alias>", alias)
.replace("<time>", TimeUtil.convertTime(player, cooldownTime - TimeUtil.getCurrentTime()));
message = MessageUtil.formatAll(player, message);

player.sendMessage(message);

CommandSpy.send(player, action, new ArrayList<>(), CommandSpy.Type.ERROR, message);
}

public void sendMutedMessage() {
public void sendMutedMessage(@NotNull String action) {
String message = locale.getVaultString(player, "commands.muted");

Moderation mute = getMute();
Expand All @@ -337,6 +333,8 @@ public void sendMutedMessage() {
message = MessageUtil.formatAll(player, message);

player.sendMessage(message);

CommandSpy.send(player, action, new ArrayList<>(), CommandSpy.Type.ERROR, message);
}

public void playSound(@NotNull Location location, @NotNull String action) {
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/net/flectone/chat/module/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import net.flectone.chat.model.player.FPlayer;
import net.flectone.chat.model.player.Settings;
import net.flectone.chat.model.sound.FSound;
import net.flectone.chat.module.commands.CommandSpy;
import net.flectone.chat.module.integrations.IntegrationsModule;
import net.flectone.chat.module.sounds.SoundsModule;
import net.flectone.chat.util.CommandsUtil;
import net.flectone.chat.util.MessageUtil;
import net.md_5.bungee.api.chat.BaseComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
Expand Down Expand Up @@ -109,13 +111,33 @@ public void sendUsageMessage(@NotNull CommandSender commandSender, @NotNull Stri
message = MessageUtil.formatAll(player, message);

commandSender.sendMessage(message);
sendToSpy(player, new ArrayList<>(), CommandSpy.Type.USAGE, message);
}

public void sendMessage(@NotNull CommandSender commandSender, @NotNull String string) {
public void sendErrorMessage(@NotNull CommandSender commandSender, @NotNull String string) {
String message = locale.getVaultString(commandSender, string);
message = MessageUtil.formatAll(null, message);

commandSender.sendMessage(message);
sendToSpy(commandSender, new ArrayList<>(), CommandSpy.Type.ERROR, message);
}

public void sendDefaultMessage(@NotNull CommandSender commandSender, @NotNull String string) {
String message = locale.getVaultString(commandSender, string);
message = MessageUtil.formatAll(null, message);

commandSender.sendMessage(message);
sendToSpy(commandSender, new ArrayList<>(), CommandSpy.Type.DEFAULT, message);
}

public void sendFormattedMessage(@NotNull CommandSender commandSender, @NotNull String string) {
commandSender.sendMessage(string);
sendToSpy(commandSender, new ArrayList<>(), CommandSpy.Type.DEFAULT, string);
}

public void sendFormattedMessage(@NotNull CommandSender commandSender, @NotNull BaseComponent... components) {
commandSender.spigot().sendMessage(components);
sendToSpy(commandSender, new ArrayList<>(), CommandSpy.Type.DEFAULT, "");
}

public void sendGlobalMessage(@Nullable Player player, @Nullable ItemStack itemStack, @NotNull String format,
Expand All @@ -134,6 +156,7 @@ public void sendGlobalMessage(@NotNull Collection<Player> recipients, @Nullable

if (player != null) {
message = IntegrationsModule.interactiveChatMark(message, player.getUniqueId());
sendToSpy(player, recipients, CommandSpy.Type.DEFAULT, message);
}

MessageBuilder messageBuilder = new MessageBuilder(player, itemStack, message, getFeatures());
Expand All @@ -147,6 +170,12 @@ public void sendGlobalMessage(@NotNull Collection<Player> recipients, @Nullable
});
}

private void sendToSpy(@Nullable CommandSender commandSender, @NotNull Collection<Player> recipients,
@NotNull CommandSpy.Type type, @NotNull String message) {
if (!(commandSender instanceof Player player)) return;
CommandSpy.send(player, command.getName(), recipients, type, message);
}

@NotNull
public Collection<Player> getRecipientsList(@Nullable Player player) {
return getRecipientsList(player, (Collection<Player>) Bukkit.getOnlinePlayers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
@NotNull String[] strings) {
CmdSettings commandCmdSettings = processCommand(commandSender, command);
if (commandCmdSettings.isConsole()) {
sendMessage(commandSender, getModule() + ".console");
sendErrorMessage(commandSender, getModule() + ".console");
return true;
}

if (commandCmdSettings.isHaveCooldown()) {
commandCmdSettings.getFPlayer().sendCDMessage(s);
commandCmdSettings.getFPlayer().sendCDMessage(s, command.getName());
return true;
}

Expand All @@ -59,7 +59,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
fPlayer.setAfkSuffix(MessageUtil.formatAll(player, afkSuffix));

String afkMessage = locale.getVaultString(player, "commands.afk." + isAfk + "-message");
player.sendMessage(MessageUtil.formatAll(player, MessageUtil.formatPlayerString(player, afkMessage)));
sendFormattedMessage(player, MessageUtil.formatAll(player, MessageUtil.formatPlayerString(player, afkMessage)));

fPlayer.playSound(player, player, this.toString());
fPlayer.updateTeam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
CmdSettings cmdSettings = processCommand(commandSender, command);

if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

if (cmdSettings.isMuted()) {
cmdSettings.getFPlayer().sendMutedMessage();
cmdSettings.getFPlayer().sendMutedMessage(command.getName());
return true;
}

if (cmdSettings.isDisabled()) {
sendMessage(commandSender, getModule() + ".you-disabled");
sendErrorMessage(commandSender, getModule() + ".you-disabled");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
String banPlayer = args[0];
FPlayer banFPlayer = playerManager.getOffline(banPlayer);
if (banFPlayer == null) {
sendMessage(commandSender, getModule() + ".null-player");
sendErrorMessage(commandSender, getModule() + ".null-player");
return true;
}

int banTime = stringToTime(time);
if (banTime < -1) {
sendMessage(commandSender, getModule() + ".long-number");
sendErrorMessage(commandSender, getModule() + ".long-number");
return true;
}

CmdSettings cmdSettings = new CmdSettings(commandSender, command);
if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ public void asyncOnCommand(@NotNull CommandSender commandSender, @NotNull Comman

int bansCount = database.getCountRow("bans");
if (bansCount == 0) {
sendMessage(commandSender, this + ".empty");
sendErrorMessage(commandSender, this + ".empty");
return;
}

int lastPage = (int) Math.ceil((double) bansCount / perPage);
if (args.length != 0 &&
(!StringUtils.isNumeric(args[0]) || Integer.parseInt(args[0]) < 1 || Integer.parseInt(args[0]) > lastPage)) {
sendMessage(commandSender, this + ".page-not-exist");
sendErrorMessage(commandSender, this + ".page-not-exist");
return;
}

CmdSettings cmdSettings = new CmdSettings(commandSender, command);

if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return;
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public void asyncOnCommand(@NotNull CommandSender commandSender, @NotNull Comman
FComponentBuilder fComponentBuilder = getfComponentBuilder(pageLine, page, cmdSettings);

componentBuilder.append(fComponentBuilder.build(cmdSettings.getSender(), cmdSettings.getSender()));
commandSender.spigot().sendMessage(componentBuilder.create());
sendFormattedMessage(commandSender, componentBuilder.create());

if (!cmdSettings.isConsole()) {
cmdSettings.getFPlayer().playSound(cmdSettings.getSender(), cmdSettings.getSender(), this.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

CmdSettings cmdSettings = processCommand(commandSender, command);
if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

if (cmdSettings.isMuted()) {
cmdSettings.getFPlayer().sendMutedMessage();
cmdSettings.getFPlayer().sendMutedMessage(command.getName());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
CmdSettings cmdSettings = new CmdSettings(commandSender, command);

if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

Expand All @@ -64,7 +64,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
}

if (cmdSettings.isConsole()) {
sendMessage(commandSender, getModule() + ".console");
sendErrorMessage(commandSender, getModule() + ".console");
return true;
}

Expand All @@ -80,7 +80,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
if (args.length < minimumColors) {
String minimumMessage = locale.getVaultString(commandSender, this + ".minimum-message");
minimumMessage = minimumMessage.replace("<minimum>", String.valueOf(minimumColors));
commandSender.sendMessage(MessageUtil.formatAll(cmdSettings.getSender(), minimumMessage));
sendFormattedMessage(commandSender, MessageUtil.formatAll(cmdSettings.getSender(), minimumMessage));
return true;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public void setColors(@NotNull FPlayer fPlayer, @NotNull HashMap<String, String>
}

message = message.replace("<colors>", colorsMessage);
player.sendMessage(MessageUtil.formatAll(player, player, message, true));
sendFormattedMessage(player, MessageUtil.formatAll(player, player, message, true));

fPlayer.playSound(fPlayer.getPlayer(), fPlayer.getPlayer(), this.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

CmdSettings cmdSettings = new CmdSettings(commandSender, command);
if (cmdSettings.isConsole()) {
sendMessage(commandSender, getModule() + ".console");
sendErrorMessage(commandSender, getModule() + ".console");
return true;
}

Expand All @@ -46,7 +46,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
if (args.length == 1 && args[0].equals("save")) {

if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

Expand All @@ -70,7 +70,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
List<String> possibleChangeList = commands.getStringList(getName() + ".change-list");
if (!possibleChangeList.contains(name)
|| !commandSender.hasPermission("flectonechat.commands.chatsettings." + name)) {
sendMessage(commandSender, this + ".not-available");
sendErrorMessage(commandSender, this + ".not-available");
return true;
}

Expand All @@ -85,7 +85,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

FComponent fComponent = new FComponent(MessageUtil.formatAll(cmdSettings.getSender(), message));
fComponent.addRunCommand("/chatsettings save");
cmdSettings.getSender().spigot().sendMessage(fComponent.get());
sendFormattedMessage(cmdSettings.getSender(), fComponent.get());
return true;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command


Player player = fPlayer.getPlayer();
player.spigot().sendMessage(fComponentBuilder.build(player, player));
sendFormattedMessage(player, fComponentBuilder.build(player, player));

fPlayer.playSound(player, player, this.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command

CmdSettings cmdSettings = new CmdSettings(commandSender, command);
if (cmdSettings.isHaveCooldown()) {
cmdSettings.getFPlayer().sendCDMessage(alias);
cmdSettings.getFPlayer().sendCDMessage(alias, command.getName());
return true;
}

Expand All @@ -42,7 +42,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
}

commandSender.sendMessage(CLEARED_STRING);
sendMessage(commandSender, this + ".message");
sendDefaultMessage(commandSender, this + ".message");

if (!cmdSettings.isConsole()) {
cmdSettings.getFPlayer().playSound(cmdSettings.getSender(), cmdSettings.getSender(), this.toString());
Expand Down
Loading

0 comments on commit c57bc0d

Please sign in to comment.