Skip to content

Commit

Permalink
Add /spy command
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Jul 7, 2024
1 parent dc9e8a5 commit 32c96c0
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 5 deletions.
25 changes: 21 additions & 4 deletions spigot/src/main/java/ru/brikster/chatty/Chatty.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cloud.commandframework.CommandManager.ManagerSettings;
import cloud.commandframework.CommandTree.Node;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.standard.BooleanArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.bukkit.BukkitCommandManager;
import cloud.commandframework.exceptions.ArgumentParseException;
Expand All @@ -23,6 +24,7 @@
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
Expand All @@ -38,6 +40,7 @@
import ru.brikster.chatty.command.ProxyingCommandHandler;
import ru.brikster.chatty.command.ProxyingCommandSuggestionsProvider;
import ru.brikster.chatty.command.handler.ClearChatCommandHandler;
import ru.brikster.chatty.command.handler.SpyCommandHandler;
import ru.brikster.chatty.config.file.MessagesConfig;
import ru.brikster.chatty.config.file.PmConfig;
import ru.brikster.chatty.config.file.SettingsConfig;
Expand Down Expand Up @@ -203,13 +206,16 @@ private void initialize() throws Exception {
ClearChatCommandHandler clearChatCommandHandler = injector.getInstance(ClearChatCommandHandler.class);
registerProxyingHandler("clearchat", clearChatCommandHandler);

SpyCommandHandler spyCommandHandler = injector.getInstance(SpyCommandHandler.class);
registerProxyingHandler("spy", spyCommandHandler);

if (this.asyncCommandManager == null) {
initAsyncCommandManager();
if (pmConfig.isEnable()) {
registerPmCommands(commandSuggestionsProvider);
}
registerIgnoreCommand(commandSuggestionsProvider);
registerClearChatCommand();
registerMiscCommands();
}

ChattyApiImpl.updateInstance(new ChattyApiImpl(injector.getInstance(ChatRegistry.class).getChats()));
Expand Down Expand Up @@ -265,13 +271,24 @@ private void initAsyncCommandManager() throws Exception {
asyncCommandManager.setSetting(ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
}

private void registerClearChatCommand() {
var command = asyncCommandManager
private void registerMiscCommands() {
var clearChatCommand = asyncCommandManager
.commandBuilder("clearchat")
.permission("chatty.command.clearchat")
.handler(proxyingCommandHandlerMap.get("clearchat"))
.build();
asyncCommandManager.command(command);
asyncCommandManager.command(clearChatCommand);

var spyCommand = asyncCommandManager
.commandBuilder("spy")
.senderType(Player.class)
.permission("chatty.command.spy")
.argument(BooleanArgument.<CommandSender>builder("state")
.withLiberal(true)
.build())
.handler(proxyingCommandHandlerMap.get("spy"))
.build();
asyncCommandManager.command(spyCommand);
}

private void registerIgnoreCommand(CommandSuggestionsProvider<CommandSender> pmSuggestionsProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public final class SpyModeStrategy implements MessageTransformStrategy<String> {
if (context.getChat().isEnableSpy()) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.hasPermission("chatty.spy." + context.getChat().getId())
&& !recipients.contains(onlinePlayer)) {
&& repository.isEnableSpy(onlinePlayer.getUniqueId())
&& !recipients.contains(onlinePlayer)) {
recipients.add(onlinePlayer);
spies.add(onlinePlayer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.brikster.chatty.command.handler;

import cloud.commandframework.context.CommandContext;
import cloud.commandframework.execution.CommandExecutionHandler;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import ru.brikster.chatty.config.file.MessagesConfig;
import ru.brikster.chatty.repository.player.PlayerDataRepository;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
@RequiredArgsConstructor(onConstructor_ = {@Inject})
public final class SpyCommandHandler implements CommandExecutionHandler<CommandSender> {

private final PlayerDataRepository playerDataRepository;
private final MessagesConfig messagesConfig;
private final BukkitAudiences audiences;

@Override
public void execute(@NonNull CommandContext<CommandSender> commandContext) {
boolean state = commandContext.get("state");
playerDataRepository.setEnableSpy(((Player) commandContext.getSender()).getUniqueId(), state);
if (state) {
audiences.sender(commandContext.getSender())
.sendMessage(messagesConfig.getSpyCommandSpyIsNowEnabled());
} else {
audiences.sender(commandContext.getSender())
.sendMessage(messagesConfig.getSpyCommandSpyIsNowDisabled());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ public class MessagesConfig extends OkaeriConfig {

private Component reloadCommandSuccess = MINI_MESSAGE.deserialize("<green>Plugin successfully reloaded!");
private Component clearchatCommandSuccess = MINI_MESSAGE.deserialize("<green>Chat cleared.");
private Component spyCommandSpyIsNowEnabled = MINI_MESSAGE.deserialize("<green>Spy mode is now enabled.");
private Component spyCommandSpyIsNowDisabled = MINI_MESSAGE.deserialize("<red>Spy mode is now disabled.");

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void handleCommand(@NotNull CommandContext<@NotNull CommandSender> comman
sender, target, message);
audiences.filter(spyCandidate -> spyCandidate.hasPermission("chatty.spy.pm")
&& !(spyCandidate instanceof ConsoleCommandSender)
&& playerDataRepository.isEnableSpy(((Player) spyCandidate).getUniqueId())
&& spyCandidate != sender
&& (!target.isOnline() || spyCandidate != target.asCommandSender()))
.sendMessage(spyComponentFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ru.brikster.chatty.proxy.data.ChatMessage;
import ru.brikster.chatty.proxy.data.PrivateMessage;
import ru.brikster.chatty.proxy.data.ProxyPlayer;
import ru.brikster.chatty.repository.player.PlayerDataRepository;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -55,6 +56,7 @@ public ProxyServiceImpl(Config redissonConfig,
ChatRegistry chatRegistry,
PmConfig pmConfig,
ChatStylePlayerGrouper stylePlayerGrouper,
PlayerDataRepository playerDataRepository,
Plugin plugin) {
RedissonClient redissonClient = Redisson.create(redissonConfig);
this.playersCache = redissonClient.getMapCache("chatty_players");
Expand Down Expand Up @@ -115,6 +117,7 @@ public ProxyServiceImpl(Config redissonConfig,
audiences.filter(spyCandidate ->
spyCandidate.hasPermission("chatty.spy.pm")
&& !(spyCandidate instanceof ConsoleCommandSender)
&& playerDataRepository.isEnableSpy(((Player) spyCandidate).getUniqueId())
&& !spyCandidate.getName().equalsIgnoreCase(redisMessage.getTargetName()))
.sendMessage(spyMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ public boolean isIgnoredPlayer(@NotNull UUID playerUuid, @NotNull UUID uuid) {
}
}

@Override
public boolean isEnableSpy(@NotNull UUID playerUuid) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"SELECT spy " +
"FROM chatty_users " +
"WHERE uuid = ?")) {
statement.setString(1, playerUuid.toString());

ResultSet resultSet = statement.executeQuery();
return resultSet.getBoolean("spy");
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot check player spy mode", sqlException);
}
}

@Override
public void setEnableSpy(@NotNull UUID playerUuid, boolean spy) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"UPDATE chatty_users SET spy = ? WHERE uuid = ?")) {
statement.setBoolean(1, spy);
statement.setString(2, playerUuid.toString());
statement.executeUpdate();
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot update player spy mode", sqlException);
}
}

@Override
public void close() {
dataSource.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public interface PlayerDataRepository extends Closeable {

boolean isIgnoredPlayer(@NotNull UUID playerUuid, @NotNull UUID uuid);

boolean isEnableSpy(@NotNull UUID playerUuid);

void setEnableSpy(@NotNull UUID playerUuid, boolean spy);

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ public boolean isIgnoredPlayer(@NotNull UUID playerUuid, @NotNull UUID uuid) {
}
}

@Override
public boolean isEnableSpy(@NotNull UUID playerUuid) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"SELECT spy " +
"FROM chatty_users " +
"WHERE uuid = ?")) {
statement.setString(1, playerUuid.toString());

ResultSet resultSet = statement.executeQuery();
return resultSet.getBoolean("spy");
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot check player spy mode", sqlException);
}
}

@Override
public void setEnableSpy(@NotNull UUID playerUuid, boolean spy) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"UPDATE chatty_users SET spy = ? WHERE uuid = ?")) {
statement.setBoolean(1, spy);
statement.setString(2, playerUuid.toString());
statement.executeUpdate();
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot update player spy mode", sqlException);
}
}

@Override
public void close() {
dataSource.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,35 @@ public boolean isIgnoredPlayer(@NotNull UUID playerUuid, @NotNull UUID uuid) {
}
}

@Override
public boolean isEnableSpy(@NotNull UUID playerUuid) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"SELECT spy " +
"FROM users " +
"WHERE uuid = ?")) {
statement.setBytes(1, SqliteUtil.fromUUID(playerUuid));

ResultSet resultSet = statement.executeQuery();
return resultSet.getBoolean("spy");
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot check player spy mode", sqlException);
}
}

@Override
public void setEnableSpy(@NotNull UUID playerUuid, boolean spy) {
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(
"UPDATE users SET spy = ? WHERE uuid = ?")) {
statement.setBoolean(1, spy);
statement.setBytes(2, SqliteUtil.fromUUID(playerUuid));
statement.executeUpdate();
} catch (SQLException sqlException) {
throw new IllegalStateException("Cannot update player spy mode", sqlException);
}
}

@Override
public void close() {
dataSource.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE chatty_users
ADD COLUMN spy BOOLEAN NOT NULL DEFAULT TRUE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE chatty_users
ADD COLUMN spy BOOLEAN NOT NULL DEFAULT TRUE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
ADD COLUMN spy BOOLEAN NOT NULL DEFAULT TRUE;

0 comments on commit 32c96c0

Please sign in to comment.