mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
Single module setup
This commit is contained in:
1 parent
ad9a794156
commit
66217f6534
70 files changed
+92
-138
No files matched your search
@@ -0,0 +1,334 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.PrivateMessageEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.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 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.*;
|
||||
|
||||
public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
private final transient CarbonChat carbonChat;
|
||||
|
||||
private UUID uuid;
|
||||
|
||||
private String selectedChannel = null;
|
||||
private final Map<String, SimpleUserChannelSettings> channelSettings = new HashMap<>();
|
||||
private final List<UUID> ignoredUsers = new ArrayList<>();
|
||||
private boolean muted = false;
|
||||
private boolean shadowMuted = false;
|
||||
private boolean spyingWhispers = false;
|
||||
|
||||
private String nickname = null;
|
||||
|
||||
private transient UUID replyTarget = null;
|
||||
|
||||
public CarbonChatUser() {
|
||||
this.carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
}
|
||||
|
||||
public CarbonChatUser(UUID uuid) {
|
||||
this.carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Iterable<? extends Audience> audiences() {
|
||||
return Collections.singleton(carbonChat.getAdventureManager().getAudiences().player(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player asPlayer() {
|
||||
return Bukkit.getPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OfflinePlayer asOfflinePlayer() {
|
||||
return Bukkit.getOfflinePlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnline() {
|
||||
return asOfflinePlayer().isOnline();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNickname(String nickname, boolean fromRemote) {
|
||||
if (nickname != null) {
|
||||
Component component = carbonChat.getAdventureManager().processMessage(nickname);
|
||||
nickname = CarbonChat.LEGACY.serialize(component);
|
||||
}
|
||||
|
||||
final String newNickname = nickname;
|
||||
this.nickname = newNickname;
|
||||
|
||||
if (isOnline()) {
|
||||
this.asPlayer().setDisplayName(newNickname);
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
|
||||
this.asPlayer().setPlayerListName(newNickname);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
if (nickname == null) {
|
||||
carbonChat.getMessageManager().sendMessage("nickname-reset", this.getUUID(), (byteArray) -> {});
|
||||
} else {
|
||||
carbonChat.getMessageManager().sendMessage("nickname", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(newNickname);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return carbonChat.getChannelManager().getChannelOrDefault(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel, boolean fromRemote) {
|
||||
String failureMessage = chatChannel.getSwitchFailureMessage();
|
||||
|
||||
ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
if (event.isCancelled()) {
|
||||
sendMessage(carbonChat.getAdventureManager().processMessage(event.getFailureMessage(),
|
||||
"channel", chatChannel.getName()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedChannel = chatChannel.getKey();
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("selected-channel", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(chatChannel.getKey());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
setSelectedChannel(carbonChat.getChannelManager().getDefaultChannel());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnoringUser(UUID uuid) {
|
||||
return ignoredUsers.contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote) {
|
||||
if (ignoring) {
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-user", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(uuid.getMostSignificantBits());
|
||||
byteArray.writeLong(uuid.getLeastSignificantBits());
|
||||
byteArray.writeBoolean(ignoring);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public List<UUID> getIgnoredUsers() {
|
||||
return ignoredUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted(boolean muted, boolean fromRemote) {
|
||||
this.muted = muted;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(muted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted() {
|
||||
return muted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShadowMuted(boolean shadowMuted, boolean fromRemote) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("shadow-muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(shadowMuted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShadowMuted() {
|
||||
return shadowMuted;
|
||||
}
|
||||
|
||||
@CheckForNull
|
||||
@Override
|
||||
public UUID getReplyTarget() {
|
||||
return replyTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReplyTarget(UUID target, boolean fromRemote) {
|
||||
this.replyTarget = target;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("reply-target", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(target.getMostSignificantBits());
|
||||
byteArray.writeLong(target.getLeastSignificantBits());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserChannelSettings getChannelSettings(ChatChannel channel) {
|
||||
return channelSettings.computeIfAbsent(channel.getKey(), (name) -> {
|
||||
return new SimpleUserChannelSettings(uuid, channel.getKey());
|
||||
});
|
||||
}
|
||||
|
||||
public Map<String, ? extends UserChannelSettings> getChannelSettings() {
|
||||
return channelSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("spying-whispers", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(spyingWhispers);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpyingWhispers() {
|
||||
return spyingWhispers;
|
||||
}
|
||||
|
||||
public void sendMessage(ChatUser sender, String message) {
|
||||
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String toPlayerFormat = carbonChat.getConfig().getString("language.message-to-other");
|
||||
String fromPlayerFormat = carbonChat.getConfig().getString("language.message-from-other");
|
||||
|
||||
String senderName = sender.asOfflinePlayer().getName();
|
||||
String senderOfflineName = senderName;
|
||||
|
||||
String targetName = this.asOfflinePlayer().getName();
|
||||
String targetOfflineName = targetName;
|
||||
|
||||
if (sender.isOnline()) {
|
||||
senderName = sender.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
if (this.isOnline()) {
|
||||
targetName = this.asPlayer().getDisplayName();
|
||||
}
|
||||
|
||||
Component toPlayerComponent = carbonChat.getAdventureManager().processMessage(toPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
Component fromPlayerComponent = carbonChat.getAdventureManager().processMessage(fromPlayerFormat, "br", "\n",
|
||||
"message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName);
|
||||
|
||||
PrivateMessageEvent event = new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isOnline()) {
|
||||
if (sender.isOnline()) {
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendMessage(fromPlayerComponent);
|
||||
|
||||
sender.setReplyTarget(this);
|
||||
this.setReplyTarget(sender);
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("pings.on-whisper")) {
|
||||
Key key = Key.of(carbonChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
this.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
} else if (sender.isOnline()) {
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
// TODO: make sure this works lol
|
||||
carbonChat.getMessageManager().sendMessage("whisper-component", sender.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(this.getUUID().getMostSignificantBits());
|
||||
byteArray.writeLong(this.getUUID().getLeastSignificantBits());
|
||||
byteArray.writeUTF(carbonChat.getAdventureManager().getAudiences().gsonSerializer().serialize(fromPlayerComponent));
|
||||
});
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser user = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (!user.isSpyingWhispers()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.getUUID().equals(sender.getUUID()) || user.getUUID().equals(getUUID())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(carbonChat.getConfig().getString("language.spy-whispers"),
|
||||
"br", "\n", "message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import com.google.common.cache.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class JSONUserService implements UserService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final Type userType = new TypeToken<CarbonChatUser>() {}.getType();
|
||||
|
||||
public JSONUserService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(carbonChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(String name) {
|
||||
Player player = Bukkit.getPlayer(name);
|
||||
|
||||
if (player != null) {
|
||||
return wrap(player);
|
||||
}
|
||||
|
||||
return wrap(Bukkit.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
|
||||
return this.wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate(ChatUser user) {
|
||||
userCache.invalidate(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ChatUser user) {
|
||||
userCache.put(user.getUUID(), (CarbonChatUser)user);
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
File userFile = new File(carbonChat.getDataFolder(), "users/" + uuid.toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
try (JsonReader reader = gson.newJsonReader(new FileReader(userFile))) {
|
||||
CarbonChatUser user = gson.fromJson(reader, userType);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new CarbonChatUser(uuid);
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, CarbonChatUser> notification) {
|
||||
File userFile = new File(carbonChat.getDataFolder(), "users/" + notification.getKey().toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userFile))) {
|
||||
gson.toJson(notification.getValue(), userType, writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureFileExists(File file) {
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import co.aikar.idb.*;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.cache.RemovalNotification;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLSyntaxErrorException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class MySQLUserService implements UserService {
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final Database database;
|
||||
|
||||
public MySQLUserService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
ConfigurationSection section = carbonChat.getConfig().getConfigurationSection("storage");
|
||||
|
||||
if (section == null) {
|
||||
throw new IllegalStateException("Missing Database Credentials!");
|
||||
}
|
||||
|
||||
String username = section.getString("username", "username");
|
||||
String password = section.getString("password", "password");
|
||||
String dbname = section.getString("database", "0");
|
||||
String hostandport = section.getString("hostname", "hostname") + ":" + section.getString("port", "0");
|
||||
|
||||
database = new HikariPooledDatabase(BukkitDB.getRecommendedOptions(carbonChat, username, password, dbname, hostandport));
|
||||
|
||||
DB.setGlobalDatabase(database);
|
||||
|
||||
try {
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_users (uuid CHAR(36) PRIMARY KEY," +
|
||||
"channel VARCHAR(16), muted BOOLEAN, shadowmuted BOOLEAN, spyingwhispers BOOLEAN," +
|
||||
"nickname VARCHAR(512))");
|
||||
|
||||
// Ignore the exception, it's just saying the column already exists
|
||||
try {
|
||||
database.executeUpdate("ALTER TABLE sc_users ADD COLUMN spyingwhispers BOOLEAN DEFAULT false");
|
||||
database.executeUpdate("ALTER TABLE sc_users ADD COLUMN nickname VARCHAR(512) DEFAULT false");
|
||||
} catch (SQLSyntaxErrorException syntaxErrorException) {}
|
||||
|
||||
database.executeUpdate("CREATE TABLE IF NOT EXISTS sc_channel_settings (uuid CHAR(36), channel CHAR(16), 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), PRIMARY KEY (uuid, user))");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
database.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(String name) {
|
||||
Player player = Bukkit.getPlayer(name);
|
||||
|
||||
if (player != null) {
|
||||
return wrap(player);
|
||||
}
|
||||
|
||||
return wrap(Bukkit.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(OfflinePlayer player) {
|
||||
return wrap(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser wrap(UUID uuid) {
|
||||
try {
|
||||
return userCache.get(uuid);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatUser refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
|
||||
return this.wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate(ChatUser user) {
|
||||
userCache.invalidate(user.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ChatUser user) {
|
||||
userCache.put(user.getUUID(), (CarbonChatUser)user);
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
CarbonChatUser user = new CarbonChatUser(uuid);
|
||||
|
||||
try (DbStatement statement = database.query("SELECT * from sc_users WHERE uuid = ?;")) {
|
||||
statement.execute(uuid.toString());
|
||||
|
||||
DbRow users = statement.getNextRow();
|
||||
|
||||
if (users == null) {
|
||||
return user;
|
||||
}
|
||||
|
||||
List<DbRow> channelSettings = 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());
|
||||
|
||||
ChatChannel channel = carbonChat.getChannelManager().getChannelOrDefault(users.getString("channel"));
|
||||
|
||||
if (channel != null) {
|
||||
user.setSelectedChannel(channel, true);
|
||||
}
|
||||
|
||||
String nickname = users.getString("nickname");
|
||||
|
||||
if (nickname != null) {
|
||||
user.setNickname(nickname, true);
|
||||
}
|
||||
|
||||
user.setMuted(users.<Boolean>get("muted"), true);
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"), true);
|
||||
user.setSpyingWhispers(users.<Boolean>get("spyingwhispers"), true);
|
||||
|
||||
for (DbRow channelSetting : channelSettings) {
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(channelSetting.getString("channel"));
|
||||
|
||||
if (chatChannel != null) {
|
||||
UserChannelSettings settings = user.getChannelSettings(chatChannel);
|
||||
|
||||
settings.setSpying(channelSetting.<Boolean>get("spying"), true);
|
||||
settings.setIgnoring(channelSetting.<Boolean>get("ignored"), true);
|
||||
|
||||
String color = channelSetting.getString("color");
|
||||
|
||||
if (color != null) {
|
||||
settings.setColor(TextColor.fromHexString(color), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DbRow ignoredUser : ignoredUsers) {
|
||||
user.setIgnoringUser(UUID.fromString(ignoredUser.getString("user")), true, true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, CarbonChatUser> notification) {
|
||||
CarbonChatUser user = notification.getValue();
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
database.createTransaction(stm -> {
|
||||
// Save user general data
|
||||
String selectedName = null;
|
||||
|
||||
if (user.getSelectedChannel() != null) {
|
||||
selectedName = user.getSelectedChannel().getKey();
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Saving user data!");
|
||||
stm.executeUpdateQuery("INSERT INTO sc_users (uuid, channel, muted, shadowmuted, spyingwhispers, nickname) VALUES (?, ?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?, spyingwhispers = ?, nickname =?",
|
||||
user.getUUID().toString(), selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(), user.getNickname(),
|
||||
selectedName, user.isMuted(), user.isShadowMuted(), user.isSpyingWhispers(), user.getNickname());
|
||||
|
||||
carbonChat.getLogger().info("Saving user channel settings!");
|
||||
// Save user channel settings
|
||||
for (Map.Entry<String, ? extends UserChannelSettings> entry : user.getChannelSettings().entrySet()) {
|
||||
UserChannelSettings value = entry.getValue();
|
||||
|
||||
String colorString = null;
|
||||
|
||||
if (value.getColor() != null) {
|
||||
colorString = value.getColor().asHexString();
|
||||
}
|
||||
|
||||
stm.executeUpdateQuery("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(), colorString,
|
||||
value.isSpying(), value.isIgnored(), colorString);
|
||||
}
|
||||
|
||||
carbonChat.getLogger().info("Saving user ignores!");
|
||||
// Save user ignore list (remove old entries then add new ones)
|
||||
// TODO: keep DB up to date with settings as settings are mutated
|
||||
stm.executeUpdateQuery("DELETE FROM sc_ignored_users WHERE uuid = ?", user.getUUID().toString());
|
||||
|
||||
for (UUID entry : user.getIgnoredUsers()) {
|
||||
stm.executeUpdateQuery("INSERT INTO sc_ignored_users (uuid, user) VALUES (?, ?)",
|
||||
user.getUUID().toString(), entry.toString());
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
private final UUID uuid;
|
||||
private final String channel;
|
||||
|
||||
private transient CarbonChat carbonChat;
|
||||
|
||||
public SimpleUserChannelSettings(UUID uuid, String channel) {
|
||||
this.uuid = uuid;
|
||||
this.channel = channel;
|
||||
|
||||
carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
}
|
||||
|
||||
private CarbonChatUser getUser() {
|
||||
return (CarbonChatUser)carbonChat.getUserService().wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpying() {
|
||||
return this.spying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpying(boolean spying, boolean fromRemote) {
|
||||
this.spying = spying;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("spying-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(spying);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIgnored() {
|
||||
return ignored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoring(boolean ignored, boolean fromRemote) {
|
||||
this.ignored = ignored;
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(ignored);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor getColor() {
|
||||
if (color == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return TextColor.fromHexString(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(@Nullable TextColor color, boolean fromRemote) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
} else {
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("channel-color", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(color.asHexString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user