Refactor settings and fix stuff

This commit is contained in:
Draycia authored and Josh (Vicarious) committed 2020-07-07 05:05:11 -07:00
1 parent 2fb43df8ad
commit 6530c8d026
10 files changed
+182 -48

No files matched your search

@@ -156,9 +156,10 @@ public class SimpleChatChannel extends ChatChannel {
} }
// Get formatted message // Get formatted message
TextComponent formattedMessage = (TextComponent)MiniMessage.get().parse(formatEvent.getFormat(), "color", "<" + color.toString() + ">", TextComponent formattedMessage = (TextComponent)MiniMessage.get().parse(formatEvent.getFormat(),
"phase", Long.toString(System.currentTimeMillis() % 25), "server", "color", "<" + color.toString() + ">",
simpleChat.getConfig().getString("server-name", "Server"), "phase", Long.toString(System.currentTimeMillis() % 25),
"server", simpleChat.getConfig().getString("server-name", "Server"),
"message", formatEvent.getMessage()); "message", formatEvent.getMessage());
// Call custom chat event // Call custom chat event
@@ -168,7 +169,19 @@ public class SimpleChatChannel extends ChatChannel {
// Send message as normal // Send message as normal
for (ChatUser chatUser : componentEvent.getRecipients()) { for (ChatUser chatUser : componentEvent.getRecipients()) {
chatUser.sendMessage(componentEvent.getComponent()); TextColor userColor = chatUser.getChannelSettings(this).getColor();
if (userColor == null) {
chatUser.sendMessage(componentEvent.getComponent());
} else {
TextComponent newFormat = (TextComponent)MiniMessage.get().parse(formatEvent.getFormat(),
"color", "<" + userColor.toString() + ">",
"phase", Long.toString(System.currentTimeMillis() % 25),
"server", simpleChat.getConfig().getString("server-name", "Server"),
"message", formatEvent.getMessage());
chatUser.sendMessage(newFormat);
}
} }
// Log message to console // Log message to console
@@ -269,7 +282,7 @@ public class SimpleChatChannel extends ChatChannel {
return false; return false;
} }
if (target.ignoringChannel(this)) { if (target.getChannelSettings(this).isIgnored()) {
return false; return false;
} }
} }
@@ -0,0 +1,29 @@
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.CommandPermission;
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.kyori.adventure.text.format.TextColor;
@CommandAlias("chcolor|channelcolor")
@CommandPermission("simplechat.setcolor")
public class ChannelColorCommand extends BaseCommand {
private SimpleChat simpleChat;
public ChannelColorCommand(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
}
@Default
@CommandCompletion("@chatchannel @chatcolors")
public void baseCommand(ChatUser user, ChatChannel chatChannel, String color) {
user.getChannelSettings(chatChannel).setColor(TextColor.fromHexString(color));
}
}
@@ -5,6 +5,7 @@ import co.aikar.commands.annotation.*;
import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel; import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser; import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -25,11 +26,13 @@ public class ToggleCommand extends BaseCommand {
String message; String message;
if (user.ignoringChannel(channel)) { UserChannelSettings settings = user.getChannelSettings(channel);
user.setIgnoringChannel(channel, true);
if (settings.isIgnored()) {
settings.setIgnoring(true);
message = channel.getToggleOffMessage(); message = channel.getToggleOffMessage();
} else { } else {
user.setIgnoringChannel(channel, false); settings.setIgnoring(false);
message = channel.getToggleOnMessage(); message = channel.getToggleOnMessage();
} }
@@ -19,6 +19,8 @@ public class CommandManager {
commandManager = new BukkitCommandManager(simpleChat); commandManager = new BukkitCommandManager(simpleChat);
commandManager.enableUnstableAPI("help");
commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> { commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
ChatUser user = simpleChat.getUserService().wrap(context.getPlayer()); ChatUser user = simpleChat.getUserService().wrap(context.getPlayer());
@@ -71,6 +73,7 @@ public class CommandManager {
manager.registerCommand(new MessageCommand(simpleChat)); manager.registerCommand(new MessageCommand(simpleChat));
manager.registerCommand(new ReplyCommand(simpleChat)); manager.registerCommand(new ReplyCommand(simpleChat));
manager.registerCommand(new MuteCommand(simpleChat)); manager.registerCommand(new MuteCommand(simpleChat));
manager.registerCommand(new ChannelColorCommand(simpleChat));
} }
public co.aikar.commands.CommandManager getCommandManager() { public co.aikar.commands.CommandManager getCommandManager() {
@@ -4,8 +4,8 @@ import net.draycia.simplechat.channels.ChatChannel;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import javax.annotation.CheckForNull;
import java.util.UUID; import java.util.UUID;
public interface ChatUser extends Audience { public interface ChatUser extends Audience {
@@ -19,8 +19,7 @@ public interface ChatUser extends Audience {
ChatChannel getSelectedChannel(); ChatChannel getSelectedChannel();
void setSelectedChannel(ChatChannel channel); void setSelectedChannel(ChatChannel channel);
boolean ignoringChannel(ChatChannel chatChannel); UserChannelSettings getChannelSettings(ChatChannel channel);
void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring);
void setMuted(boolean muted); void setMuted(boolean muted);
boolean isMuted(); boolean isMuted();
@@ -28,10 +27,9 @@ public interface ChatUser extends Audience {
void setShadowMuted(boolean shadowMuted); void setShadowMuted(boolean shadowMuted);
boolean isShadowMuted(); boolean isShadowMuted();
@CheckForNull @Nullable UUID getReplyTarget();
UUID getReplyTarget(); void setReplyTarget(@Nullable UUID target);
void setReplyTarget(UUID target); void setReplyTarget(@Nullable ChatUser user);
void setReplyTarget(ChatUser user);
boolean isIgnoringUser(UUID uuid); boolean isIgnoringUser(UUID uuid);
boolean isIgnoringUser(ChatUser user); boolean isIgnoringUser(ChatUser user);
@@ -0,0 +1,17 @@
package net.draycia.simplechat.storage;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.Nullable;
public interface UserChannelSettings {
boolean isSpying();
void setSpying(boolean spying);
boolean isIgnored();
void setIgnoring(boolean ignored);
@Nullable TextColor getColor();
void setColor(@Nullable TextColor color);
}
@@ -7,10 +7,8 @@ import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser; import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserService; import net.draycia.simplechat.storage.UserService;
import net.draycia.simplechat.util.ChatChannelAdapter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@@ -6,14 +6,18 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalNotification; import com.google.common.cache.RemovalNotification;
import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser; import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.draycia.simplechat.storage.UserService; import net.draycia.simplechat.storage.UserService;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -46,9 +50,9 @@ public class MySQLUserService extends UserService {
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_users (uuid CHAR(36) PRIMARY KEY," + database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_users (uuid CHAR(36) PRIMARY KEY," +
"channel TINYTEXT , muted BOOLEAN, shadowmuted BOOLEAN)"); "channel TINYTEXT , muted BOOLEAN, shadowmuted BOOLEAN)");
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_ignored_channels (uuid CHAR(36), channel TINYTEXT)"); database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_channel_settings (uuid CHAR(36), channel TINYTEXT, spying BOOLEAN, ignored BOOLEAN, color TINYTEXT, PRIMARY KEY (uuid, channel))");
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_ignored_users (uuid CHAR(36), user CHAR(36))"); database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_ignored_users (uuid CHAR(36), user CHAR(36), PRIMARY KEY (uuid, user)");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -91,7 +95,7 @@ public class MySQLUserService extends UserService {
return user; return user;
} }
List<DbRow> ignoredChannels = database.getResults("SELECT * from sc_ignored_channels WHERE uuid = ?;", uuid.toString()); List<DbRow> ignoredChannels = 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()); List<DbRow> ignoredUsers = database.getResults("SELECT * from sc_ignored_users WHERE uuid = ?;", uuid.toString());
user.setSelectedChannel(simpleChat.getChannel(users.getString("channel"))); user.setSelectedChannel(simpleChat.getChannel(users.getString("channel")));
@@ -99,7 +103,12 @@ public class MySQLUserService extends UserService {
user.setShadowMuted(users.<Boolean>get("shadowmuted")); user.setShadowMuted(users.<Boolean>get("shadowmuted"));
for (DbRow ignoredChannel : ignoredChannels) { for (DbRow ignoredChannel : ignoredChannels) {
user.setIgnoringChannel(simpleChat.getChannel(ignoredChannel.getString("channel")), true); ChatChannel chatChannel = simpleChat.getChannel(ignoredChannel.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")));
} }
for (DbRow ignoredUser : ignoredUsers) { for (DbRow ignoredUser : ignoredUsers) {
@@ -115,14 +124,34 @@ public class MySQLUserService extends UserService {
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) { private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
SimpleChatUser user = notification.getValue(); SimpleChatUser user = notification.getValue();
try { database.createTransaction(stm -> {
database.executeUpdate("INSERT INTO sc_users (uuid, channel, muted, shadowmuted) VALUES (?, ?, ?, ?) " + // Save user general data
"ON DUPLICATE KEY UPDATE uuid = ?, channel = ?, muted = ?, shadowmuted = ?", stm.executeUpdate("INSERT INTO sc_users (uuid, channel, muted, shadowmuted) VALUES (?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?",
user.getUUID().toString(), user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted(), user.getUUID().toString(), user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted(),
user.getUUID().toString(), user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted()); user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted());
} catch (SQLException e) {
e.printStackTrace(); // Save user channel settings
} for (Map.Entry<String, UserChannelSettings> entry : user.getChannelSettings().entrySet()) {
UserChannelSettings value = entry.getValue();
stm.executeUpdate("INSERT INTO sc_channel_settings (uuid, channel, spying, ignored, color) VALUES (?, ?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE spying = ?, ignored = ?, color = ?",
user.getUUID().toString(), entry.getKey(), value.isSpying(), value.isIgnored(), value.getColor().asHexString(),
value.isSpying(), value.isIgnored(), value.getColor().asHexString());
}
// Save user ignore list (remove old entries then add new ones)
// TODO: keep DB up to date with settings as settings are mutated
stm.executeUpdate("DELETE FROM sc_ignored_users WHERE uuid = ?", user.getUUID().toString());
for (UUID entry : user.getIgnoredUsers()) {
stm.executeUpdate("INSERT INTO sc_ignored_users (uuid, user) VALUES (?, ?)",
user.getUUID().toString(), entry.toString());
}
return true;
});
} }
} }
@@ -3,23 +3,20 @@ package net.draycia.simplechat.storage.impl;
import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel; import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser; import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience; import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class SimpleChatUser implements ChatUser, ForwardingAudience { public class SimpleChatUser implements ChatUser, ForwardingAudience {
@@ -28,7 +25,7 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
private UUID uuid; private UUID uuid;
private String selectedChannel = null; private String selectedChannel = null;
private List<String> ignoredChannels = new ArrayList<>(); private Map<String, UserChannelSettings> channelSettings = new HashMap<>();
private List<UUID> ignoredUsers = new ArrayList<>(); private List<UUID> ignoredUsers = new ArrayList<>();
private boolean muted = false; private boolean muted = false;
private boolean shadowMuted = false; private boolean shadowMuted = false;
@@ -79,20 +76,6 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
this.selectedChannel = chatChannel.getName(); this.selectedChannel = chatChannel.getName();
} }
@Override
public boolean ignoringChannel(ChatChannel chatChannel) {
return ignoredChannels.contains(chatChannel.getName());
}
@Override
public void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring) {
if (ignoring) {
ignoredChannels.add(chatChannel.getName());
} else {
ignoredChannels.remove(chatChannel.getName());
}
}
@Override @Override
public boolean isIgnoringUser(UUID uuid) { public boolean isIgnoringUser(UUID uuid) {
return ignoredUsers.contains(uuid); return ignoredUsers.contains(uuid);
@@ -121,6 +104,10 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
} }
} }
public List<UUID> getIgnoredUsers() {
return ignoredUsers;
}
@Override @Override
public void setMuted(boolean muted) { public void setMuted(boolean muted) {
this.muted = muted; this.muted = muted;
@@ -157,6 +144,15 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
this.replyTarget = user.getUUID(); this.replyTarget = user.getUUID();
} }
@Override
public UserChannelSettings getChannelSettings(ChatChannel channel) {
return channelSettings.computeIfAbsent(channel.getName(), (name) -> new SimpleUserChannelSettings());
}
public Map<String, UserChannelSettings> getChannelSettings() {
return channelSettings;
}
public void sendMessage(ChatUser sender, String message) { public void sendMessage(ChatUser sender, String message) {
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) { if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
return; return;
@@ -0,0 +1,48 @@
package net.draycia.simplechat.storage.impl;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.Nullable;
public class SimpleUserChannelSettings implements UserChannelSettings {
private boolean spying;
private boolean ignored;
private String color;
@Override
public boolean isSpying() {
return this.spying;
}
@Override
public void setSpying(boolean spying) {
this.spying = spying;
}
@Override
public boolean isIgnored() {
return ignored;
}
@Override
public void setIgnoring(boolean ignored) {
this.ignored = ignored;
}
@Override
public @Nullable TextColor getColor() {
return TextColor.fromHexString(color);
}
@Override
public void setColor(@Nullable TextColor color) {
if (color == null) {
this.color = null;
return;
}
this.color = color.asHexString();
}
}