Skip to content

Commit

Permalink
add ignore command, add unignore command, add ignorelist command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex2go authored Aug 25, 2021
1 parent a9a09dd commit c0c0e6a
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/main/java/eu/rex2go/chat2go/Chat2Go.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import eu.rex2go.chat2go.command.broadcast.BroadcastCommand;
import eu.rex2go.chat2go.command.chat.ChatCommand;
import eu.rex2go.chat2go.command.force.ForceCommand;
import eu.rex2go.chat2go.command.ignore.IgnoreCommand;
import eu.rex2go.chat2go.command.ignorelist.IgnoreListCommand;
import eu.rex2go.chat2go.command.msg.MsgCommand;
import eu.rex2go.chat2go.command.mute.MuteCommand;
import eu.rex2go.chat2go.command.reply.ReplyCommand;
import eu.rex2go.chat2go.command.unignore.UnignoreCommand;
import eu.rex2go.chat2go.command.unmute.UnmuteCommand;
import eu.rex2go.chat2go.config.ChatConfig;
import eu.rex2go.chat2go.config.MessageConfig;
Expand Down Expand Up @@ -179,6 +182,9 @@ private boolean setupChat() {
private void setupCommands() {
new BroadcastCommand();
new ChatCommand();
new IgnoreCommand();
new UnignoreCommand();
new IgnoreListCommand();
new ForceCommand();
new MsgCommand();
new MuteCommand();
Expand Down Expand Up @@ -247,6 +253,11 @@ private void setupTables() {
ps.execute();
ps.close();

// ignore table
ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `ignore` ( user_uuid VARCHAR(32) NOT NULL, ignore_uuid VARCHAR(32) NOT NULL, FOREIGN KEY (user_uuid) REFERENCES user(uuid), FOREIGN KEY (ignore_uuid) REFERENCES user(uuid) );");
ps.execute();
ps.close();

connection.close();

} catch (SQLException exception) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/eu/rex2go/chat2go/ChatPermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum ChatPermission {
COMMAND_CHAT_RELOAD,
COMMAND_CHAT_CLEAR,
COMMAND_CHAT_BADWORD,
COMMAND_IGNORE,
COMMAND_FORCE,
COMMAND_MUTE,
NOTIFY_FILTER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public void execute(CommandSender sender, User user, String label, String[] args
sender.sendMessage(ChatColor.WHITE + "- " + color + "/force <player> <command|message> " + ChatColor.WHITE + " | "
+ ChatColor.GRAY + " force a player");

sender.sendMessage(ChatColor.WHITE + "- " + color + "/ignore <player> " + ChatColor.WHITE + " | "
+ ChatColor.GRAY + " ignore a player");

sender.sendMessage(ChatColor.WHITE + "- " + color + "/unignore <player> " + ChatColor.WHITE + " | "
+ ChatColor.GRAY + " unignore a player");

sender.sendMessage(ChatColor.WHITE + "- " + color + "/ignorelist " + ChatColor.WHITE + " | "
+ ChatColor.GRAY + " list ignored players");

sender.sendMessage(ChatColor.GRAY + "--- ---");
}
}
49 changes: 49 additions & 0 deletions src/main/java/eu/rex2go/chat2go/command/ignore/IgnoreCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package eu.rex2go.chat2go.command.ignore;

import eu.rex2go.chat2go.Chat2Go;
import eu.rex2go.chat2go.ChatPermission;
import eu.rex2go.chat2go.command.WrappedCommandExecutor;
import eu.rex2go.chat2go.command.exception.CommandException;
import eu.rex2go.chat2go.command.exception.PlayerNotFoundCommandException;
import eu.rex2go.chat2go.user.User;
import org.bukkit.command.CommandSender;

public class IgnoreCommand extends WrappedCommandExecutor {

public IgnoreCommand() {
super("ignore");
}

@Override
protected void execute(CommandSender sender, User user, String label, String... args) throws CommandException {

checkPermission(sender, ChatPermission.COMMAND_IGNORE.getPermission());

if (args.length < 1) {
getTranslator().sendMessage(sender, "§7/ignore <{player}>");
return;
}

String targetName = args[0];

if(targetName.equalsIgnoreCase(sender.getName())) {
user.sendMessage("command.ignore.ignore_yourself", false);
return;
}

User target = Chat2Go.getUserManager().loadUser(null, targetName, false);

if (target == null) {
throw new PlayerNotFoundCommandException(targetName);
}

if (user.getIgnored().contains(target.getUuid())) {
user.sendMessage("command.ignore.already_ignoring", false, target.getName());
} else {
user.ignore(target);
user.sendMessage("command.ignore.ignored", false, target.getName());
}

Chat2Go.getUserManager().unloadOffline(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package eu.rex2go.chat2go.command.ignorelist;

import eu.rex2go.chat2go.Chat2Go;
import eu.rex2go.chat2go.ChatPermission;
import eu.rex2go.chat2go.command.WrappedCommandExecutor;
import eu.rex2go.chat2go.command.exception.CommandException;
import eu.rex2go.chat2go.user.User;
import eu.rex2go.chat2go.util.MathUtil;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class IgnoreListCommand extends WrappedCommandExecutor {

public IgnoreListCommand() {
super("ignorelist");
}

@Override
protected void execute(CommandSender sender, User user, String label, String... args) throws CommandException {

checkPermission(sender, ChatPermission.COMMAND_IGNORE.getPermission());

getTranslator().sendMessage(sender, "§7-§b-§7- §f{ignore_list} §7-§b-§7-");

List<String> ignoredList = new ArrayList<>();

for (UUID ignored : user.getIgnored()) {
User ignoredUser = Chat2Go.getUserManager().loadUser(ignored, null, false);
ignoredList.add(ignoredUser.getName());
}

int entriesPerPage = 8;
int entryCount = ignoredList.size();
int pages = entryCount / entriesPerPage + (entryCount % entriesPerPage == 0 ? 0 : 1);
int page = 1;

if (entryCount == 0) {
user.sendMessage("command.ignorelist.no_ignored", false);
return;
}

if (args.length > 2) {
String pageStr = args[2];

if (MathUtil.isNumber(pageStr)) {
page = Integer.parseInt(pageStr);

if (page > pages) page = pages;
if (page < 1) page = 1;
}
}

int offset = entriesPerPage * (page - 1);

for (int i = offset; i < offset + entriesPerPage; i++) {
if (entryCount <= i) break;

String ignore = ignoredList.get(i);
String deleteTranslation = getTranslator().getTranslation("unignore");
String deleteHoverTranslation = getTranslator().getTranslation("command.ignorelist.unignore_hover", ignore);

BaseComponent[] deleteComponents = new ComponentBuilder("§c[" + deleteTranslation + "]")
.event(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new Text(deleteHoverTranslation)))
.event(new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/unignore " + ignore))
.create();

BaseComponent[] components = new ComponentBuilder("§7- §f" + ignore + " ")
.append(deleteComponents)
.create();

sender.spigot().sendMessage(components);
}

if (pages > 1) {
ComponentBuilder componentBuilder = new ComponentBuilder();

if (page != 1) {
componentBuilder.append(
new ComponentBuilder(" §f[<]")
.event(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new Text(getTranslator().getTranslation("previous_page"))))
.event(new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/ignorelist " + (page - 1)))
.create()
);
}

if (pages != page) {
componentBuilder.append(
new ComponentBuilder(" §f[>]")
.event(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new Text(getTranslator().getTranslation("next_page"))))
.event(new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/ignorelist " + (page + 1)))
.create()
);
}

String translation = getTranslator().getTranslation(
"pagination",
String.valueOf(page),
String.valueOf(pages));


BaseComponent[] components = new ComponentBuilder(translation).append(componentBuilder.create()).create();

sender.spigot().sendMessage(components);
}

Chat2Go.getUserManager().unloadOffline(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package eu.rex2go.chat2go.command.unignore;

import eu.rex2go.chat2go.Chat2Go;
import eu.rex2go.chat2go.ChatPermission;
import eu.rex2go.chat2go.command.WrappedCommandExecutor;
import eu.rex2go.chat2go.command.exception.CommandException;
import eu.rex2go.chat2go.command.exception.PlayerNotFoundCommandException;
import eu.rex2go.chat2go.user.User;
import org.bukkit.command.CommandSender;

public class UnignoreCommand extends WrappedCommandExecutor {

public UnignoreCommand() {
super("unignore");
}

@Override
protected void execute(CommandSender sender, User user, String label, String... args) throws CommandException {

checkPermission(sender, ChatPermission.COMMAND_IGNORE.getPermission());

if (args.length < 1) {
getTranslator().sendMessage(sender, "§7/unignore <{player}>");
return;
}

String targetName = args[0];
User target = Chat2Go.getUserManager().loadUser(null, targetName, false);

if (target == null) {
throw new PlayerNotFoundCommandException(targetName);
}

if (!user.getIgnored().contains(target.getUuid())) {
user.sendMessage("command.unignore.not_ignoring", false, target.getName());
} else {
user.unignore(target);
user.sendMessage("command.unignore.unignored", false, target.getName());
}

Chat2Go.getUserManager().unloadOffline(user);
}
}
2 changes: 1 addition & 1 deletion src/main/java/eu/rex2go/chat2go/config/MessageConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class MessageConfig extends RexConfig {

public MessageConfig() {
super(Chat2Go.getInstance(), "messages.yml", 2);
super(Chat2Go.getInstance(), "messages.yml", 3);
}

@Override
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/eu/rex2go/chat2go/listener/PlayerChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,20 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
Chat2Go.getInstance().getLogger().log(Level.SEVERE, "Your chat format is invalid.");
}

// TODO filter ignore list
// avoid concurrent modification exception
Set<Player> recipients = new HashSet<>(event.getRecipients());

for(Player recipient : recipients) {
User recipientUser = Chat2Go.getUser(recipient);

if(recipientUser.getIgnored().contains(user.getUuid())) {
event.getRecipients().remove(recipient);
}
}

// update
recipients = new HashSet<>(event.getRecipients());

if (ChatConfig.isChatWorldChatEnabled()) {
for (Player recipient : recipients) {
if (!recipient.getWorld().equals(player.getWorld())) {
Expand Down
58 changes: 55 additions & 3 deletions src/main/java/eu/rex2go/chat2go/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
import java.util.*;
import java.util.logging.Level;

public class User {
Expand All @@ -37,6 +35,9 @@ public class User {

private boolean inPrivateChat = false;

@Getter
private List<UUID> ignored = new ArrayList<>();

public User(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
Expand All @@ -46,6 +47,7 @@ public User(UUID uuid, String name) {
if (connectionWrapper == null) return;

this.mute = Chat2Go.getUserManager().loadMute(this, connectionWrapper.getConnection());
Chat2Go.getUserManager().loadIgnoreList(this, connectionWrapper.getConnection());

connectionWrapper.close();
}
Expand Down Expand Up @@ -226,4 +228,54 @@ public void say(String message) {
recipient.sendMessage(formatted);
}
}


public boolean ignore(User user) {
if (ignored.contains(user.getUuid())) return false;

ignored.add(user.getUuid());

ConnectionWrapper connectionWrapper = DatabaseManager.getConnectionWrapper();
PreparedStatement preparedStatement = connectionWrapper.prepareStatement(
"INSERT INTO `ignore` (user_uuid, ignore_uuid) VALUES (?, ?)");

try {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, user.getUuid().toString());

preparedStatement.execute();
preparedStatement.close();
} catch (SQLException exception) {
exception.printStackTrace();
}

connectionWrapper.close();

return true;
}


public boolean unignore(User user) {
if (!ignored.contains(user.getUuid())) return false;

ignored.remove(user.getUuid());

ConnectionWrapper connectionWrapper = DatabaseManager.getConnectionWrapper();
PreparedStatement preparedStatement = connectionWrapper.prepareStatement(
"DELETE FROM `ignore` WHERE user_uuid = ? AND ignore_uuid = ?");

try {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, user.getUuid().toString());

preparedStatement.execute();
preparedStatement.close();
} catch (SQLException exception) {
exception.printStackTrace();
}

connectionWrapper.close();

return true;
}
}
Loading

0 comments on commit c0c0e6a

Please sign in to comment.