Added ignore ? (ignore list) and sync ignores

This commit is contained in:
Teriuihi 2021-08-01 04:21:33 +02:00
parent dfcf8de34e
commit e51c893649
4 changed files with 106 additions and 21 deletions

View File

@ -438,26 +438,39 @@ public class Queries {
public static String getDisplayName(UUID uuid) {
String nickname = getNickname(uuid);
if (nickname != null) return nickname;
HashSet<String> userNames = getUserNames(List.of(uuid));
return userNames.isEmpty() ? null : userNames.iterator().next();
}
// View has been created.
String query = "SELECT Username FROM utility_users WHERE uuid = ?";
public static HashSet<String> getUserNames(List<UUID> uuid) {
StringBuilder query = new StringBuilder("SELECT Username FROM utility_users WHERE uuid IN (");
if (uuid.isEmpty()) return new HashSet<>();
query.append("?, ".repeat(uuid.size()));
query.delete(query.length() - 2, query.length());
query.append(")");
HashSet<String> userNames = new HashSet<>();
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
PreparedStatement statement = connection.prepareStatement(query.toString());
statement.setString(1, uuid.toString());
for (int i = 0; i < uuid.size(); i++) {
statement.setString(i + 1, uuid.get(i).toString());
}
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getString("Username");
while (resultSet.next()) {
userNames.add(resultSet.getString("Username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
return userNames;
}
}

View File

@ -31,11 +31,15 @@ public final class ChatUserManager {
return user;
}
}
ChatUser user = Queries.loadChatUser(uuid);
if(user == null) user = new ChatUser(uuid, -1, false, false);
Queries.saveUser(user);
chatUsers.add(user);
return user;
ChatUser chatUser = Queries.loadChatUser(uuid);
if (chatUser != null) {
ChatUserManager.addUser(chatUser);
}
return chatUser;
// if(user == null) user = new ChatUser(uuid, -1, false, false);
// Queries.saveUser(user);
// chatUsers.add(user);
// return user;
}
public List<Mail> getUnReadMail(ChatUser user) {

View File

@ -1,9 +1,13 @@
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.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -11,41 +15,83 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
public class Ignore implements CommandExecutor {
private final ChatPlugin plugin;
public Ignore() {
plugin = ChatPlugin.getInstance();
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player)) { // must be a player
if(!(sender instanceof Player player)) { // must be a player
return true;
}
if(args.length > 1) return false; // todo error message or command info
String targetName = args[0];
if (targetName.equals("?")) {
new BukkitRunnable() {
@Override
public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
HashSet<String> userNames = Queries.getUserNames(chatUser.getIgnoredPlayers());
StringBuilder ignoredMessage = new StringBuilder();
if (userNames.isEmpty()) {
player.sendMessage(MiniMessage.get().parse("You don't have anyone ignored!")); //TODO load from config
return;
}
ignoredMessage.append("You have the following users ignored:\n");
userNames.forEach(username -> ignoredMessage.append(username).append("\n"));
ignoredMessage.delete(ignoredMessage.length() - 1, ignoredMessage.length());
player.sendMessage(MiniMessage.get().parse(ignoredMessage.toString()));
}
}.runTaskAsynchronously(plugin);
return false;
}
Player targetPlayer = Bukkit.getPlayer(targetName);
if(targetPlayer == null) { // can't ignore offline players
sender.sendMessage("You can't ignore offline players");
//sender.sendMessage("Target not found..."); // TODO load from config and minimessage
return false;
}
if(targetPlayer.hasPermission("chat.ignorebypass")) {
UUID target = targetPlayer.getUniqueId();
if(targetPlayer.hasPermission("chat.ignorebypass") || target.equals(player.getUniqueId())) {
sender.sendMessage("You can't ignore this player"); // TODO load from config and minimessage
return false;
}
UUID target = targetPlayer.getUniqueId();
new BukkitRunnable() {
@Override
public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(((Player) sender).getUniqueId());
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if(!chatUser.getIgnoredPlayers().contains(target)) {
chatUser.addIgnoredPlayers(target);
Queries.ignoreUser(((Player) sender).getUniqueId(), target);
sender.sendMessage("You have ignored " + targetName + "."); // TODO load from config and minimessage
sendPluginMessage("ignore", player, target);
} else {
sender.sendMessage("You have ignored " + targetName + "."); // TODO load from config and minimessage
sender.sendMessage("You already have " + targetName + " ignored, to unignore use /unignore " + targetName + "."); // TODO load from config and minimessage
}
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}.runTaskAsynchronously(plugin);
return false;
}
private void sendPluginMessage(String channel, Player player, UUID target) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(target.toString());
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
}

View File

@ -1,13 +1,21 @@
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.listeners.PluginMessage;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@ -15,9 +23,15 @@ import java.util.UUID;
public class Unignore implements CommandExecutor {
private final ChatPlugin plugin;
public Unignore() {
plugin = ChatPlugin.getInstance();
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player)) { // must be a player
if(!(sender instanceof Player player)) { // must be a player
return true;
}
if(args.length > 1) return false; // todo error message or command info
@ -30,11 +44,12 @@ public class Unignore implements CommandExecutor {
new BukkitRunnable() {
@Override
public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(((Player) sender).getUniqueId());
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if(chatUser.getIgnoredPlayers().contains(target)) {
chatUser.removeIgnoredPlayers(target);
Queries.unIgnoreUser(((Player) sender).getUniqueId(), target);
Queries.unIgnoreUser(player.getUniqueId(), target);
sender.sendMessage("You no longer ignore " + targetName + "."); // TODO load from config and minimessage
sendPluginMessage("unignore", player, target);
} else {
sender.sendMessage("You don't have " + targetName + " ignored."); // TODO load from config and minimessage
}
@ -43,4 +58,11 @@ public class Unignore implements CommandExecutor {
return false;
}
private void sendPluginMessage(String channel, Player player, UUID target) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(target.toString());
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
}