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

@@ -4,8 +4,8 @@ import net.draycia.simplechat.channels.ChatChannel;
import net.kyori.adventure.audience.Audience;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import javax.annotation.CheckForNull;
import java.util.UUID;
public interface ChatUser extends Audience {
@@ -19,8 +19,7 @@ public interface ChatUser extends Audience {
ChatChannel getSelectedChannel();
void setSelectedChannel(ChatChannel channel);
boolean ignoringChannel(ChatChannel chatChannel);
void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring);
UserChannelSettings getChannelSettings(ChatChannel channel);
void setMuted(boolean muted);
boolean isMuted();
@@ -28,10 +27,9 @@ public interface ChatUser extends Audience {
void setShadowMuted(boolean shadowMuted);
boolean isShadowMuted();
@CheckForNull
UUID getReplyTarget();
void setReplyTarget(UUID target);
void setReplyTarget(ChatUser user);
@Nullable UUID getReplyTarget();
void setReplyTarget(@Nullable UUID target);
void setReplyTarget(@Nullable ChatUser user);
boolean isIgnoringUser(UUID uuid);
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.JsonWriter;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserService;
import net.draycia.simplechat.util.ChatChannelAdapter;
import org.bukkit.Bukkit;
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.RemovalNotification;
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.draycia.simplechat.storage.UserService;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
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," +
"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) {
e.printStackTrace();
}
@@ -91,7 +95,7 @@ public class MySQLUserService extends UserService {
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());
user.setSelectedChannel(simpleChat.getChannel(users.getString("channel")));
@@ -99,7 +103,12 @@ public class MySQLUserService extends UserService {
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
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) {
@@ -115,14 +124,34 @@ public class MySQLUserService extends UserService {
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
SimpleChatUser user = notification.getValue();
try {
database.executeUpdate("INSERT INTO sc_users (uuid, channel, muted, shadowmuted) VALUES (?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE uuid = ?, channel = ?, muted = ?, shadowmuted = ?",
database.createTransaction(stm -> {
// Save user general data
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());
} catch (SQLException e) {
e.printStackTrace();
}
user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted());
// 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.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.storage.UserChannelSettings;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
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.OfflinePlayer;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import javax.annotation.CheckForNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;
public class SimpleChatUser implements ChatUser, ForwardingAudience {
@@ -28,7 +25,7 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
private UUID uuid;
private String selectedChannel = null;
private List<String> ignoredChannels = new ArrayList<>();
private Map<String, UserChannelSettings> channelSettings = new HashMap<>();
private List<UUID> ignoredUsers = new ArrayList<>();
private boolean muted = false;
private boolean shadowMuted = false;
@@ -79,20 +76,6 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
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
public boolean isIgnoringUser(UUID uuid) {
return ignoredUsers.contains(uuid);
@@ -121,6 +104,10 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
}
}
public List<UUID> getIgnoredUsers() {
return ignoredUsers;
}
@Override
public void setMuted(boolean muted) {
this.muted = muted;
@@ -157,6 +144,15 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
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) {
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
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();
}
}