Add basic /spy support

This commit is contained in:
Draycia authored and Josh (Vicarious) committed 2020-07-08 20:50:06 -07:00
1 parent 53d9086939
commit b3dded7852
10 files changed
+237 -27

No files matched your search

@@ -109,7 +109,7 @@ public abstract class ChatChannel {
*/
public abstract boolean canPlayerUse(ChatUser user);
public abstract boolean canPlayerSee(ChatUser sender, ChatUser target);
public abstract boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
public abstract List<ChatUser> getAudience(ChatUser user);
@@ -133,6 +133,8 @@ public abstract class ChatChannel {
public abstract void processDiscordMessage(MessageCreateEvent event);
public String processPlaceholders(ChatUser user, String input) { return input; }
public static abstract class Builder {
public abstract ChatChannel build(SimpleChat simpleChat);
public abstract ChatChannel.Builder setColor(TextColor color);
@@ -18,8 +18,14 @@ public class AllianceChatChannel extends SimpleChatChannel {
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target) {
if (super.canPlayerSee(sender, target) && sender != null) {
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (super.canPlayerSee(sender, target, false) && sender != null) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
Resident targetResident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
@@ -19,8 +19,33 @@ public class NationChatChannel extends SimpleChatChannel {
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target) {
if (super.canPlayerSee(sender, target) && sender != null) {
public String processPlaceholders(ChatUser user, String input) {
return input.replace("<nation>", getNation(user));
}
private String getNation(ChatUser user) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
if (resident.hasNation()) {
return resident.getTown().getNation().getFormattedName();
}
} catch (NotRegisteredException e) {
e.printStackTrace();
}
return "";
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (super.canPlayerSee(sender, target, false) && sender != null) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
@@ -52,6 +77,11 @@ public class NationChatChannel extends SimpleChatChannel {
return false;
}
@Override
public boolean isNationChat() {
return true;
}
public static NationChatChannel.Builder nationBuilder(String name) {
return new NationChatChannel.Builder(name);
}
@@ -16,8 +16,27 @@ public class PartyChatChannel extends SimpleChatChannel {
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target) {
if (super.canPlayerSee(sender, target) && sender != null) {
public String processPlaceholders(ChatUser user, String input) {
System.out.println("??????");
String partyName = PartyAPI.getPartyName(user.asPlayer());
if (partyName == null) {
partyName = "NotLoaded";
}
return input.replace("<party>", partyName);
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (super.canPlayerSee(sender, target, false) && sender != null) {
return PartyAPI.inSameParty(sender.asPlayer(), target.asPlayer());
}
@@ -33,6 +52,11 @@ public class PartyChatChannel extends SimpleChatChannel {
return false;
}
@Override
public boolean isPartyChat() {
return true;
}
public static PartyChatChannel.Builder partyBuilder(String name) {
return new PartyChatChannel.Builder(name);
}
@@ -99,7 +99,7 @@ public class SimpleChatChannel extends ChatChannel {
List<ChatUser> audience = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
if (canPlayerSee(user, simpleChat.getUserService().wrap(player))) {
if (canPlayerSee(user, simpleChat.getUserService().wrap(player), false)) {
audience.add(simpleChat.getUserService().wrap(player));
}
}
@@ -166,18 +166,49 @@ public class SimpleChatChannel extends ChatChannel {
Bukkit.getPluginManager().callEvent(componentEvent);
// Send message as normal
for (ChatUser chatUser : componentEvent.getRecipients()) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
ChatUser chatUser = simpleChat.getUserService().wrap(onlinePlayer);
TextColor userColor = chatUser.getChannelSettings(this).getColor();
if (userColor == null) {
chatUser.sendMessage(componentEvent.getComponent());
String prefix;
if (isNationChat()) {
prefix = simpleChat.getConfig().getString("spy-prefix-nation");
} else if (isTownChat()) {
prefix = simpleChat.getConfig().getString("spy-prefix-town");
} else if (isPartyChat()) {
prefix = simpleChat.getConfig().getString("spy-prefix-mcmmo");
} else {
TextComponent newFormat = (TextComponent)MiniMessage.get().parse(formatEvent.getFormat(),
prefix = simpleChat.getConfig().getString("spy-prefix");
}
prefix = processPlaceholders(user, prefix);
if (userColor == null) {
if (isUserSpying(user, chatUser)) {
prefix = prefix.replace("<color>", "<color:" + getColor() + ">");
chatUser.sendMessage(MiniMessage.get().parse(prefix).append(componentEvent.getComponent()));
} else if (componentEvent.getRecipients().contains(chatUser)) {
chatUser.sendMessage(componentEvent.getComponent());
}
} else {
prefix = prefix.replace("<color>", "<color:" + chatUser.getChannelSettings(this).getColor().asHexString() + ">");
String format = formatEvent.getFormat();
TextComponent newFormat = (TextComponent)MiniMessage.get().parse(format,
"color", "<color:" + userColor.toString() + ">",
"phase", Long.toString(System.currentTimeMillis() % 25),
"server", simpleChat.getConfig().getString("server-name", "Server"),
"message", formatEvent.getMessage());
if (isUserSpying(user, chatUser)) {
newFormat = (TextComponent)MiniMessage.get().parse(prefix).append(newFormat);
} else if (!componentEvent.getRecipients().contains(chatUser)) {
return;
}
ChatComponentEvent newEvent = new ChatComponentEvent(user, this, newFormat, formatEvent.getMessage(), Collections.singletonList(chatUser));
Bukkit.getPluginManager().callEvent(newEvent);
@@ -187,10 +218,8 @@ public class SimpleChatChannel extends ChatChannel {
}
// Log message to console
//String sm = user.isShadowMuted() ? "[SM] " : "";
//System.out.println(sm + LegacyComponentSerializer.legacy().serialize(componentEvent.getComponent()));
//System.out.println(GsonComponentSerializer.gson().serialize(componentEvent.getComponent()));
String sm = user.isShadowMuted() ? "[SM] " : "";
System.out.println(sm + LegacyComponentSerializer.legacy().serialize(componentEvent.getComponent()));
// Route message to bungee / discord (if message originates from this server)
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
@@ -206,6 +235,16 @@ public class SimpleChatChannel extends ChatChannel {
}
}
private boolean isUserSpying(ChatUser sender, ChatUser target) {
if (!canPlayerSee(sender, target, false)) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
return false;
}
@Override
public void sendComponent(ChatUser player, Component component) {
for (ChatUser user : getAudience(player)) {
@@ -268,9 +307,15 @@ public class SimpleChatChannel extends ChatChannel {
return getFormat(group);
}
public boolean canPlayerSee(ChatUser sender, ChatUser target) {
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
Player targetPlayer = target.asPlayer();
if (checkSpying && targetPlayer.hasPermission("simplechat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (!targetPlayer.hasPermission("simplechat.channels." + getName() + ".see")) {
return false;
}
@@ -18,8 +18,33 @@ public class TownChatChannel extends SimpleChatChannel {
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target) {
if (super.canPlayerSee(sender, target) && sender != null) {
public String processPlaceholders(ChatUser user, String input) {
return input.replace("<town>", getTown(user));
}
private String getTown(ChatUser user) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
if (resident.hasTown()) {
return resident.getTown().getFormattedName();
}
} catch (NotRegisteredException e) {
e.printStackTrace();
}
return "";
}
@Override
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (super.canPlayerSee(sender, target, false) && sender != null) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
@@ -47,6 +72,11 @@ public class TownChatChannel extends SimpleChatChannel {
return false;
}
@Override
public boolean isTownChat() {
return true;
}
public static TownChatChannel.Builder townBuilder(String name) {
return new TownChatChannel.Builder(name);
}
@@ -0,0 +1,45 @@
package net.draycia.simplechat.commands;
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Default;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player;
@CommandAlias("spych|spychannel|spy")
public class SpyChannelCommand extends BaseCommand {
private SimpleChat simpleChat;
public SpyChannelCommand(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
}
@Default
@CommandCompletion("@channel")
public void baseCommand(Player player, @Conditions("exists:true") ChatChannel channel) {
ChatUser user = simpleChat.getUserService().wrap(player);
String message;
UserChannelSettings settings = user.getChannelSettings(channel);
if (settings.isSpying()) {
settings.setSpying(false);
message = simpleChat.getConfig().getString("language.spy-toggled-off");
} else {
settings.setSpying(true);
message = simpleChat.getConfig().getString("language.spy-toggled-on");
}
user.sendMessage(MiniMessage.get().parse(message,
"color", "<color:" + channel.getColor().toString() + ">", "channel", channel.getName()));
}
}
@@ -35,6 +35,16 @@ public class CommandManager {
return completions;
});
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
ArrayList<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
completions.add(chatChannel.getName());
}
return completions;
});
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
String name = context.popFirstArg();
@@ -63,6 +73,12 @@ public class CommandManager {
}
});
commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> {
if (value == null) {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
}
});
setupCommands(commandManager);
}
@@ -79,6 +95,7 @@ public class CommandManager {
manager.registerCommand(new MuteCommand(simpleChat));
manager.registerCommand(new ChannelColorCommand(simpleChat));
manager.registerCommand(new SetColorCommand(simpleChat));
manager.registerCommand(new SpyChannelCommand(simpleChat));
}
public co.aikar.commands.CommandManager getCommandManager() {
@@ -97,20 +97,25 @@ public class MySQLUserService extends UserService {
return user;
}
List<DbRow> ignoredChannels = database.getResults("SELECT * from sc_channel_settings WHERE uuid = ?;", uuid.toString());
List<DbRow> channelSettings = database.getResults("SELECT * from sc_channel_settings WHERE uuid = ?;", uuid.toString());
List<DbRow> ignoredUsers = database.getResults("SELECT * from sc_ignored_users WHERE uuid = ?;", uuid.toString());
user.setSelectedChannel(simpleChat.getChannel(users.getString("channel")));
user.setMuted(users.<Boolean>get("muted"));
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
for (DbRow ignoredChannel : ignoredChannels) {
ChatChannel chatChannel = simpleChat.getChannel(ignoredChannel.getString("channel"));
for (DbRow channelSetting : channelSettings) {
ChatChannel chatChannel = simpleChat.getChannel(channelSetting.getString("channel"));
UserChannelSettings settings = user.getChannelSettings(chatChannel);
settings.setSpying(ignoredChannel.<Boolean>get("spying"));
settings.setIgnoring(ignoredChannel.<Boolean>get("ignored"));
settings.setColor(TextColor.fromHexString(ignoredChannel.getString("color")));
settings.setSpying(channelSetting.<Boolean>get("spying"));
settings.setIgnoring(channelSetting.<Boolean>get("ignored"));
String color = channelSetting.getString("color");
if (color != null) {
settings.setColor(TextColor.fromHexString(color));
}
}
for (DbRow ignoredUser : ignoredUsers) {
@@ -127,7 +132,6 @@ public class MySQLUserService extends UserService {
SimpleChatUser user = notification.getValue();
if (user == null) {
System.out.println("null");
return;
}