This commit is contained in:
destro174 2021-07-30 03:16:41 +02:00
parent ead360ee1c
commit 9d8d4b3853
14 changed files with 127 additions and 97 deletions

View File

@ -68,8 +68,8 @@ public final class ServerConfig {
/** DO NOT EDIT ANYTHING ABOVE **/
public boolean GLOBALCHAT = true; // TODO - @teri idk what servers need to have this enabled
public boolean JOINLEAVEMSSAGES = true; // TODO set to false on lobby
public boolean GLOBALCHAT = true;
public boolean JOINLEAVEMSSAGES = true;
private void ServerSettings() {
GLOBALCHAT = getBoolean("global-chat-enabled", GLOBALCHAT);
JOINLEAVEMSSAGES = getBoolean("joinleave-messages-enabled", JOINLEAVEMSSAGES);

View File

@ -340,7 +340,8 @@ public class Queries {
statement.setString(1, user.getUuid().toString());
statement.setInt(2, user.getPartyId());
statement.setInt(3, user.toggledPartyChat() ? 1 : 0);
statement.setInt(5, user.isGcOn() ? 1 : 0);
statement.setInt(4, 0);
// statement.setInt(5, user.isGcOn() ? 1 : 0);
statement.execute();
} catch (SQLException e) {
@ -423,10 +424,10 @@ public class Queries {
statement.setString(1, user.getUuid().toString());
statement.setInt(2, user.getPartyId());
statement.setInt(3, user.toggledPartyChat() ? 1 : 0);
statement.setInt(4, user.isGcOn() ? 1 : 0);
statement.setInt(4, 0);
statement.setInt(5, user.getPartyId());
statement.setInt(6, user.toggledPartyChat() ? 1 : 0);
statement.setInt(7, user.isGcOn() ? 1 : 0);
statement.setInt(7, 0);
statement.execute();
} catch (SQLException e) {

View File

@ -27,7 +27,7 @@ public final class ChatUserManager {
public static ChatUser getChatUser(UUID uuid) {
for(ChatUser user : chatUsers) {
if(uuid.compareTo(user.getUuid()) == 0) {
if(uuid.equals(user.getUuid())) {
return user;
}
}

View File

@ -17,7 +17,7 @@ public class ChatUser {
// private Component prefix; // doesn't need saving, we get this from luckperms
// private Component staffPrefix; // doesn't need saving, we get this from luckperms
// private Component prefixAll; // doesn't need saving, we get this from luckperms
private boolean toggleGc; // should be saved, this toggles if the player can see and use global chat
//private boolean toggleGc; // should be saved, this toggles if the player can see and use global chat
private String replyTarget; // reply target for use in /msg i don't mind setting this to null on login, feedback?
private long gcCooldown; // the time when they last used gc, is used for the cooldown, i wouldn't save this, but setting this to the login time means they can't use gc for 30 seconds after logging in
@ -41,7 +41,7 @@ public class ChatUser {
//
// prefixAll = Utility.getPrefix(uuid, false);
this.toggleGc = toggleGc;
//this.toggleGc = toggleGc;
replyTarget = null;
gcCooldown = System.currentTimeMillis(); // players can't use gc for 30 seconds after logging in if we use this?
mails = Queries.getMails(uuid);
@ -89,14 +89,6 @@ public class ChatUser {
return Utility.getPrefix(uuid, false);
}
public void toggleGc() {
toggleGc = !toggleGc;
}
public boolean isGcOn() {
return toggleGc;
}
public String getReplyTarget() {
return replyTarget;
}

View File

@ -10,13 +10,10 @@ import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.luckperms.api.node.Node;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import java.util.*;
public class Utility {
@ -58,22 +55,17 @@ public class Utility {
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid);
if(user == null) return Component.empty();
if(single) {
Group group = luckPerms.getGroupManager().getGroup(user.getPrimaryGroup());
if(group != null)
prefix.append(group.getCachedData().getMetaData().getPrefix());
// Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
// inheritedGroups.stream()
// .sorted(Comparator.comparingInt(o -> o.getWeight().orElse(0)))
// .distinct()
// .forEach(group -> {
// if (Config.PREFIXGROUPS.contains(group.getName())) {
// prefix.append("[").append(group.getCachedData().getMetaData().getPrefix()).append("]");
// }
// });
} else {
prefix.append(user.getCachedData().getMetaData().getPrefix());
if(!single) {
Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
inheritedGroups.stream()
.sorted(Comparator.comparingInt(o -> o.getWeight().orElse(0)))
.forEach(group -> {
if (Config.PREFIXGROUPS.contains(group.getName())) {
prefix.append(group.getCachedData().getMetaData().getPrefix());
}
});
}
prefix.append(user.getCachedData().getMetaData().getPrefix());
return applyColor(prefix.toString());
}
@ -98,6 +90,21 @@ public class Utility {
return user.getUsername();
}
public static void flipPermission(UUID uuid, String permission) {
ChatAPI.get().getLuckPerms().getUserManager().modifyUser(uuid, user -> {
// Add the permission
user.data().add(Node.builder(permission)
.value(user.getCachedData().getPermissionData().checkPermission(permission).asBoolean()).build());
});
}
public static boolean hasPermission(UUID uuid, String permission) {
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid);
if(user == null) return false;
return user.getCachedData().getPermissionData().checkPermission(permission).asBoolean();
}
public static Component applyColor(String message) {
String hexColor1 = "";
String hexColor2 = "";
@ -172,23 +179,4 @@ public class Utility {
: miniMessage.parse(stringBuilder.toString());
}
public static void sendBlockedNotification(String prefix, Player player, String input, String target) {
MiniMessage miniMessage = MiniMessage.get();
Bukkit.getOnlinePlayers().forEach(a ->{
Component blockedNotification = miniMessage.parse("<red>[" + prefix + "] "
+ getDisplayName(player.getUniqueId())
+ (target.isEmpty() ? " tried to say: " : " -> " + target + ": ")
+ input + "</red>");
if (a.hasPermission("chat.alert-blocked")) {
a.sendMessage(blockedNotification);//TODO make configurable (along with all the messages)
}
});
player.sendMessage(miniMessage.parse("<red>The language you used in your message is not allowed, " +
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
}
public static void sendBlockedNotification(String prefix, Player player, Component input, String target) {
sendBlockedNotification(prefix, player, PlainComponentSerializer.plain().serialize(input), target);
}
}

View File

@ -2,12 +2,14 @@ package com.alttd.chat;
import com.alttd.chat.commands.*;
import com.alttd.chat.config.Config;
import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.handler.ChatHandler;
import com.alttd.chat.listeners.ChatListener;
import com.alttd.chat.listeners.PlayerListener;
import com.alttd.chat.listeners.PluginMessage;
import com.alttd.chat.util.ALogger;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
@ -20,6 +22,7 @@ public class ChatPlugin extends JavaPlugin {
private ChatHandler chatHandler;
private String messageChannel;
private ServerConfig serverConfig;
@Override
public void onEnable() {
@ -28,9 +31,12 @@ public class ChatPlugin extends JavaPlugin {
chatAPI = new ChatImplementation();
chatHandler = new ChatHandler();
DatabaseConnection.initialize();
serverConfig = new ServerConfig(Bukkit.getServerName());
registerListener(new PlayerListener(), new ChatListener());
registerCommand("globalchat", new GlobalChat());
registerCommand("toggleglobalchat", new ToggleGlobalChat());
if(serverConfig.GLOBALCHAT) {
registerCommand("globalchat", new GlobalChat());
registerCommand("toggleglobalchat", new ToggleGlobalChat());
}
registerCommand("message", new Message());
registerCommand("reply", new Reply());
registerCommand("ignore", new Ignore());
@ -67,4 +73,8 @@ public class ChatPlugin extends JavaPlugin {
public ChatHandler getChatHandler() {
return chatHandler;
}
public boolean serverGlobalChatEnabled() {
return serverConfig.GLOBALCHAT;
}
}

View File

@ -1,9 +1,12 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import jdk.jshell.execution.Util;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
@ -13,6 +16,7 @@ import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Objects;
import java.util.UUID;
public class ToggleGlobalChat implements CommandExecutor {
@ -24,10 +28,12 @@ public class ToggleGlobalChat implements CommandExecutor {
new BukkitRunnable() {
@Override
public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(((Player) sender).getUniqueId());
chatUser.toggleGc();
Queries.setGlobalChatState(chatUser.isGcOn(), chatUser.getUuid());
sender.sendMessage(MiniMessage.get().parse("You have turned globalchat " + (chatUser.isGcOn() ? "<green>on." : "<red>off."))); // TODO load from config and minimessage
UUID uuid = ((Player) sender).getUniqueId();
ChatUser chatUser = ChatUserManager.getChatUser(uuid);
//chatUser.toggleGc();
Utility.flipPermission(uuid, Config.GCPERMISSION);
//Queries.setGlobalChatState(chatUser.isGcOn(), chatUser.getUuid());
sender.sendMessage(MiniMessage.get().parse("You have turned globalchat " + (Utility.hasPermission(uuid, Config.GCPERMISSION) ? "<green>on." : "<red>off."))); // TODO load from config and minimessage
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
return false;

View File

@ -6,6 +6,7 @@ import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.alttd.chat.util.Utils;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component;
@ -40,7 +41,7 @@ public class ChatHandler {
user.setReplyTarget(target);
String updatedMessage = RegexManager.replaceText(message); // todo a better way for this
if(updatedMessage == null) {
Utility.sendBlockedNotification("DM Language", player, message, target);
Utils.sendBlockedNotification("DM Language", player, message, target);
return; // the message was blocked
}
@ -53,22 +54,29 @@ public class ChatHandler {
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", updatedMessage),
Template.of("sendername", player.getName()),
Template.of("receivername", target),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
Component component = miniMessage.parse("<message>", templates);
sendPrivateMessage(player, target, "privatemessage", component);
Component spymessage = miniMessage.parse(Config.MESSAGESPY, templates);
for(Player pl : Bukkit.getOnlinePlayers()) {
if(pl.hasPermission("chat.social-spy")) { // todo add a toggle for social spy
pl.sendMessage(spymessage);
}
}
}
public void globalChat(Player player, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if(user == null) return;
if(!user.isGcOn()) {
if(!Utility.hasPermission(player.getUniqueId(), Config.GCPERMISSION)) {
player.sendMessage(GCNOTENABLED);// GC IS OFF INFORM THEM ABOUT THIS and cancel
return;
}
long timeLeft = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - user.getGcCooldown());
if(timeLeft <= Config.GCCOOLDOWN || player.hasPermission("chat.globalchat.cooldownbypass")) { // player is on cooldown and should wait x seconds
if(timeLeft <= Config.GCCOOLDOWN && !player.hasPermission("chat.globalchat.cooldownbypass")) { // player is on cooldown and should wait x seconds
player.sendMessage(miniMessage.parse(Config.GCONCOOLDOWN, Template.of("cooldown", Config.GCCOOLDOWN-timeLeft+"")));
return;
}
@ -78,7 +86,7 @@ public class ChatHandler {
String updatedMessage = RegexManager.replaceText(message); // todo a better way for this
if(updatedMessage == null) {
Utility.sendBlockedNotification("GC Language", player, message, "");
Utils.sendBlockedNotification("GC Language", player, message, "");
return; // the message was blocked
}

View File

@ -6,6 +6,7 @@ import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.alttd.chat.util.Utils;
import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience;
@ -39,7 +40,7 @@ public class ChatListener implements Listener, ChatRenderer {
message = RegexManager.replaceText(message); // todo a better way for this
if(message == null) {
event.setCancelled(true);
Utility.sendBlockedNotification("Language", player, input, "");
Utils.sendBlockedNotification("Language", player, input, "");
return; // the message was blocked
}

View File

@ -5,6 +5,7 @@ import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.alttd.chat.util.Utils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import org.bukkit.event.EventHandler;
@ -46,7 +47,7 @@ public class PlayerListener implements Listener {
message = RegexManager.replaceText(message); // todo a better way for this
if (message == null) {
Utility.sendBlockedNotification("Sign Language" ,event.getPlayer(), PlainComponentSerializer.plain().serialize(component), "");
Utils.sendBlockedNotification("Sign Language" ,event.getPlayer(), PlainComponentSerializer.plain().serialize(component), "");
}
component = message == null ? Component.empty() : Component.text(message);

View File

@ -24,35 +24,20 @@ public class PluginMessage implements PluginMessageListener {
}
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
String subChannel = in.readUTF();
UUID uuid;String target; Player p;
switch (subChannel) {
case "privatemessagesend":
uuid = UUID.fromString(in.readUTF());
target = in.readUTF();
p = Bukkit.getPlayer(uuid);
case "privatemessage":
UUID uuid = UUID.fromString(in.readUTF());
String target = in.readUTF();
Player p = Bukkit.getPlayer(uuid);
if(p != null) {
p.sendMessage(GsonComponentSerializer.gson().deserialize(in.readUTF()));
ChatUser user = ChatUserManager.getChatUser(uuid);
user.setReplyTarget(target);
p.sendMessage(GsonComponentSerializer.gson().deserialize(in.readUTF()));
Component spymessage = GsonComponentSerializer.gson().deserialize(in.readUTF());
for(Player pl : Bukkit.getOnlinePlayers()) {
if(pl.hasPermission("chat.social-spy")) { // todo add a toggle for social spy
pl.sendMessage(spymessage);
}
}
}
break;
case "privatemessagesreceived":
uuid = UUID.fromString(in.readUTF());
target = in.readUTF();
p = Bukkit.getPlayer(uuid);
if(p != null) {
ChatUser user = ChatUserManager.getChatUser(uuid);
user.setReplyTarget(target);
p.sendMessage(GsonComponentSerializer.gson().deserialize(in.readUTF()));
}
break;
case "globalchat":
if(ChatPlugin.getInstance().serverGlobalChatEnabled())
Bukkit.broadcast(GsonComponentSerializer.gson().deserialize(in.readUTF()), Config.GCPERMISSION);
break;
default:
break;

View File

@ -0,0 +1,30 @@
package com.alttd.chat.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class Utils {
public static void sendBlockedNotification(String prefix, Player player, String input, String target) {
MiniMessage miniMessage = MiniMessage.get();
Bukkit.getOnlinePlayers().forEach(a ->{
Component blockedNotification = miniMessage.parse("<red>[" + prefix + "] "
+ Utility.getDisplayName(player.getUniqueId())
+ (target.isEmpty() ? " tried to say: " : " -> " + target + ": ")
+ input + "</red>");
if (a.hasPermission("chat.alert-blocked")) {
a.sendMessage(blockedNotification);//TODO make configurable (along with all the messages)
}
});
player.sendMessage(miniMessage.parse("<red>The language you used in your message is not allowed, " +
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
}
public static void sendBlockedNotification(String prefix, Player player, Component input, String target) {
sendBlockedNotification(prefix, player, PlainComponentSerializer.plain().serialize(input), target);
}
}

View File

@ -47,24 +47,23 @@ public class ChatHandler {
ServerConnection serverConnection;
if(player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
// redirect to the sender
String spyMessage = GsonComponentSerializer.gson().serialize(miniMessage.parse(Config.MESSAGESPY, templates));
serverConnection = player.getCurrentServer().get();
Component component = miniMessage.parse(Config.MESSAGESENDER, templates);
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessagesend");
buf.writeUTF("privatemessage");
buf.writeUTF(player.getUniqueId().toString());
buf.writeUTF(player2.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
buf.writeUTF(spyMessage);
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
//redirect to the receiver
serverConnection = player2.getCurrentServer().get();
component = miniMessage.parse(Config.MESSAGERECIEVER, templates);
buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessagesreceived");
buf.writeUTF("privatemessage");
buf.writeUTF(player2.getUniqueId().toString());
buf.writeUTF(player.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
}

View File

@ -3,6 +3,8 @@ package com.alttd.chat.handlers;
import com.alttd.chat.VelocityChat;
import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.data.ServerWrapper;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component;
@ -38,11 +40,18 @@ public class ServerHandler {
}
public void sendGlobalChat(String message) {
Component component = GsonComponentSerializer.gson().deserialize(message);
// Component component = GsonComponentSerializer.gson().deserialize(message);
servers.stream()
.filter(serverWrapper -> serverWrapper.globalChat())
.forEach(serverWrapper -> serverWrapper.getRegisteredServer().sendMessage(component));
.map(ServerWrapper::getRegisteredServer)
.forEach(registeredServer -> {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("globalchat");
buf.writeUTF(message);
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
});
// .filter(serverWrapper -> serverWrapper.globalChat())
// .forEach(serverWrapper -> serverWrapper.getRegisteredServer().sendMessage(component));
}
public List<ServerWrapper> getServers()