Human pass over storage

This commit is contained in:
Mariell Hoversholm committed 2020-09-09 22:16:57 +02:00
1 parent 14b2cbbd73
commit 8bb7446da8
8 files changed
+175 -96

No files matched your search

@@ -11,75 +11,101 @@ import java.util.UUID;
public interface ChatUser extends Audience {
@Nullable Player asPlayer();
@NonNull OfflinePlayer asOfflinePlayer();
@Nullable
Player asPlayer();
@NonNull
OfflinePlayer asOfflinePlayer();
@NonNull
UUID getUUID();
boolean isOnline();
@Nullable String getNickname();
void setNickname(String nickname, boolean fromRemote);
default void setNickname(String nickname) {
@Nullable
String getNickname();
default void setNickname(@NonNull String nickname) {
this.setNickname(nickname, false);
}
void setNickname(@Nullable String nickname, boolean fromRemote);
@Nullable
ChatChannel getSelectedChannel();
void setSelectedChannel(ChatChannel channel, boolean fromRemote);
default void setSelectedChannel(ChatChannel channel) {
default void setSelectedChannel(@NonNull ChatChannel channel) {
this.setSelectedChannel(channel, false);
}
void setSelectedChannel(@NonNull ChatChannel channel, boolean fromRemote);
void clearSelectedChannel();
UserChannelSettings getChannelSettings(ChatChannel channel);
@NonNull
UserChannelSettings getChannelSettings(@NonNull ChatChannel channel);
boolean isSpyingWhispers();
void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote);
default void setSpyingWhispers(boolean spyingWhispers) {
this.setSpyingWhispers(spyingWhispers, false);
}
void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote);
boolean isMuted();
void setMuted(boolean muted, boolean fromRemote);
default void setMuted(boolean muted) {
this.setMuted(muted, false);
}
void setMuted(boolean muted, boolean fromRemote);
boolean isShadowMuted();
void setShadowMuted(boolean shadowMuted, boolean fromRemote);
default void setShadowMuted(boolean shadowMuted) {
this.setShadowMuted(shadowMuted, false);
}
@Nullable @Nullable UUID getReplyTarget();
void setReplyTarget(@Nullable UUID target, boolean fromRemote);
void setShadowMuted(boolean shadowMuted, boolean fromRemote);
@Nullable
UUID getReplyTarget();
default void setReplyTarget(@Nullable UUID target) {
this.setReplyTarget(target, false);
}
default void setReplyTarget(@Nullable ChatUser user, boolean fromRemote) {
this.setReplyTarget(user.getUUID(), fromRemote);
}
default void setReplyTarget(@Nullable ChatUser user) {
this.setReplyTarget(user.getUUID(), false);
}
boolean isIgnoringUser(UUID uuid);
void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote);
default void setIgnoringUser(UUID uuid, boolean ignoring) {
void setReplyTarget(@Nullable UUID target, boolean fromRemote);
default void setReplyTarget(@Nullable ChatUser user, boolean fromRemote) {
this.setReplyTarget(user.getUUID(), fromRemote);
}
boolean isIgnoringUser(@NonNull UUID uuid);
void setIgnoringUser(@NonNull UUID uuid, boolean ignoring, boolean fromRemote);
default void setIgnoringUser(@NonNull UUID uuid, boolean ignoring) {
this.setIgnoringUser(uuid, ignoring, false);
}
default boolean isIgnoringUser(@NonNull ChatUser user) {
return this.isIgnoringUser(user.getUUID());
}
default void setIgnoringUser(@NonNull ChatUser user, boolean ignoring, boolean fromRemote) {
this.setIgnoringUser(user.getUUID(), ignoring, fromRemote);
}
default void setIgnoringUser(@NonNull ChatUser user, boolean ignoring) {
this.setIgnoringUser(user.getUUID(), ignoring, false);
}
void sendMessage(ChatUser sender, String message);
void sendMessage(@NonNull ChatUser sender, @NonNull String message);
}
@@ -6,24 +6,30 @@ import java.util.List;
public class CommandSettings {
private boolean enabled;
private String name;
private List<String> aliases;
private final boolean enabled;
public CommandSettings(boolean enabled, String name, List<String> aliases) {
@NonNull
private final String name;
@NonNull
private final List<@NonNull String> aliases;
public CommandSettings(boolean enabled, @NonNull String name, @NonNull List<@NonNull String> aliases) {
this.enabled = enabled;
this.name = name;
this.aliases = aliases;
}
public String @NonNull [] getAliasesArray() {
public @NonNull String @NonNull [] getAliasesArray() {
return aliases.toArray(new String[0]);
}
public List<String> getAliases() {
@NonNull
public List<@NonNull String> getAliases() {
return aliases;
}
@NonNull
public String getName() {
return name;
}
@@ -6,21 +6,28 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public interface UserChannelSettings {
boolean isSpying();
void setSpying(boolean spying, boolean fromRemote);
default void setSpying(boolean spying) {
this.setSpying(spying, false);
}
void setSpying(boolean spying, boolean fromRemote);
boolean isIgnored();
void setIgnoring(boolean ignored, boolean fromRemote);
default void setIgnoring(boolean ignored) {
this.setIgnoring(ignored, false);
}
@Nullable @Nullable TextColor getColor();
void setColor(@Nullable TextColor color, boolean fromRemote);
@Nullable
TextColor getColor();
default void setColor(@Nullable TextColor color) {
this.setColor(color, false);
}
void setColor(@Nullable TextColor color, boolean fromRemote);
}
@@ -8,23 +8,30 @@ import java.util.UUID;
public interface UserService {
@Nullable ChatUser wrap(String name);
@Nullable ChatUser wrap(OfflinePlayer player);
@Nullable ChatUser wrap(UUID uuid);
@Nullable
ChatUser wrap(String name);
@Nullable
ChatUser wrap(OfflinePlayer player);
@Nullable
ChatUser wrap(UUID uuid);
@Nullable
default ChatUser wrapIfLoaded(@NonNull OfflinePlayer player) {
return wrapIfLoaded(player.getUniqueId());
}
@Nullable @Nullable
ChatUser wrapIfLoaded(UUID uuid);
@Nullable
ChatUser wrapIfLoaded(@NonNull UUID uuid);
@Nullable ChatUser refreshUser(UUID uuid);
@Nullable
ChatUser refreshUser(@NonNull UUID uuid);
void onDisable();
void invalidate(ChatUser user);
void validate(ChatUser user);
void invalidate(@NonNull ChatUser user);
void validate(@NonNull ChatUser user);
}
@@ -15,50 +15,58 @@ 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.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import javax.annotation.CheckForNull;
import java.util.*;
public class CarbonChatUser implements ChatUser, ForwardingAudience {
private final transient @Nullable @NonNull CarbonChat carbonChat;
@NonNull
private final transient CarbonChat carbonChat;
@NonNull
private final Map<@NonNull String, @NonNull SimpleUserChannelSettings> channelSettings = new HashMap<>();
@NonNull
private final List<@NonNull UUID> ignoredUsers = new ArrayList<>();
@MonotonicNonNull // @NonNull but not initialised in all constructors.
private UUID uuid;
private @Nullable String selectedChannel = null;
private final Map<String, SimpleUserChannelSettings> channelSettings = new HashMap<>();
private final List<UUID> ignoredUsers = new ArrayList<>();
@Nullable
private String selectedChannel = null;
private boolean muted = false;
private boolean shadowMuted = false;
private boolean spyingWhispers = false;
private @Nullable String nickname = null;
@Nullable
private String nickname = null;
private transient @Nullable UUID replyTarget = null;
@Nullable
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");
public CarbonChatUser(@NonNull UUID uuid) {
this();
this.uuid = uuid;
}
@Override
public @NonNull Iterable<? extends Audience> audiences() {
@NonNull
public Iterable<@NonNull ? extends Audience> audiences() {
return Collections.singleton(carbonChat.getAdventureManager().getAudiences().player(uuid));
}
@Override
public @Nullable Player asPlayer() {
@Nullable
public Player asPlayer() {
return Bukkit.getPlayer(uuid);
}
@Override
public @NonNull OfflinePlayer asOfflinePlayer() {
@NonNull
public OfflinePlayer asOfflinePlayer() {
return Bukkit.getOfflinePlayer(uuid);
}
@@ -68,12 +76,14 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
}
@Override
@NonNull
public UUID getUUID() {
return uuid;
}
@Override
public @Nullable String getNickname() {
@Nullable
public String getNickname() {
return nickname;
}
@@ -106,6 +116,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
}
@Override
@Nullable
public ChatChannel getSelectedChannel() {
return carbonChat.getChannelManager().getChannelOrDefault(selectedChannel);
}
@@ -140,7 +151,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
}
@Override
public boolean isIgnoringUser(UUID uuid) {
public boolean isIgnoringUser(@NonNull UUID uuid) {
return ignoredUsers.contains(uuid);
}
@@ -161,7 +172,8 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
}
}
public @NonNull List<UUID> getIgnoredUsers() {
@NonNull
public List<@NonNull UUID> getIgnoredUsers() {
return ignoredUsers;
}
@@ -197,14 +209,14 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
return shadowMuted;
}
@CheckForNull
@Override
@Nullable
public UUID getReplyTarget() {
return replyTarget;
}
@Override
public void setReplyTarget(@NonNull UUID target, boolean fromRemote) {
public void setReplyTarget(@Nullable UUID target, boolean fromRemote) {
this.replyTarget = target;
if (!fromRemote) {
@@ -216,13 +228,15 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
}
@Override
@NonNull
public UserChannelSettings getChannelSettings(@NonNull ChatChannel channel) {
return channelSettings.computeIfAbsent(channel.getKey(), (name) -> {
return new SimpleUserChannelSettings(uuid, channel.getKey());
});
}
public @NonNull Map<String, ? extends UserChannelSettings> getChannelSettings() {
@NonNull
public Map<@NonNull String, @NonNull ? extends UserChannelSettings> getChannelSettings() {
return channelSettings;
}
@@ -242,7 +256,8 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
return spyingWhispers;
}
public void sendMessage(@NonNull ChatUser sender, String message) {
@Override
public void sendMessage(@NonNull ChatUser sender, @NonNull String message) {
if (isIgnoringUser(sender) || sender.isIgnoringUser(this)) {
return;
}
@@ -1,6 +1,9 @@
package net.draycia.carbon.storage.impl;
import com.google.common.cache.*;
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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
@@ -25,15 +28,17 @@ import java.util.concurrent.ExecutionException;
public class JSONUserService implements UserService {
private final @NonNull CarbonChat carbonChat;
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
@NonNull
private final CarbonChat carbonChat;
@NonNull
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
@NonNull
private final Type userType = new TypeToken<CarbonChatUser>() {}.getType();
@NonNull
private final LoadingCache<@NonNull UUID, @NonNull 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(@NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
@@ -47,7 +52,8 @@ public class JSONUserService implements UserService {
}
@Override
public @Nullable ChatUser wrap(@NonNull String name) {
@Nullable
public ChatUser wrap(@NonNull String name) {
Player player = Bukkit.getPlayer(name);
if (player != null) {
@@ -58,12 +64,14 @@ public class JSONUserService implements UserService {
}
@Override
public @Nullable ChatUser wrap(@NonNull OfflinePlayer player) {
@Nullable
public ChatUser wrap(@NonNull OfflinePlayer player) {
return wrap(player.getUniqueId());
}
@Override
public @Nullable ChatUser wrap(@NonNull UUID uuid) {
@Nullable
public ChatUser wrap(@NonNull UUID uuid) {
try {
return userCache.get(uuid);
} catch (ExecutionException e) {
@@ -79,7 +87,8 @@ public class JSONUserService implements UserService {
}
@Override
public @Nullable ChatUser refreshUser(@NonNull UUID uuid) {
@Nullable
public ChatUser refreshUser(@NonNull UUID uuid) {
userCache.invalidate(uuid);
return this.wrap(uuid);
@@ -92,9 +101,10 @@ public class JSONUserService implements UserService {
@Override
public void validate(@NonNull ChatUser user) {
userCache.put(user.getUUID(), (CarbonChatUser)user);
userCache.put(user.getUUID(), (CarbonChatUser) user);
}
@NonNull
private CarbonChatUser loadUser(@NonNull UUID uuid) {
File userFile = new File(carbonChat.getDataFolder(), "users/" + uuid.toString() + ".json");
ensureFileExists(userFile);
@@ -112,7 +122,7 @@ public class JSONUserService implements UserService {
return new CarbonChatUser(uuid);
}
private void saveUser(@NonNull RemovalNotification<UUID, CarbonChatUser> notification) {
private void saveUser(@NonNull RemovalNotification<@NonNull UUID, @NonNull CarbonChatUser> notification) {
File userFile = new File(carbonChat.getDataFolder(), "users/" + notification.getKey().toString() + ".json");
ensureFileExists(userFile);
@@ -27,13 +27,15 @@ import java.util.concurrent.ExecutionException;
public class MySQLUserService implements UserService {
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
@NonNull
private final CarbonChat carbonChat;
@NonNull
private final Database database;
@NonNull
private final LoadingCache<@NonNull UUID, @NonNull CarbonChatUser> userCache = CacheBuilder.newBuilder()
.removalListener(this::saveUser)
.build(CacheLoader.from(this::loadUser));
private final @NonNull CarbonChat carbonChat;
private final @NonNull Database database;
public MySQLUserService(@NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
@@ -61,7 +63,8 @@ public class MySQLUserService implements UserService {
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) {}
} catch (SQLSyntaxErrorException ignored) {
}
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))");
@@ -79,7 +82,8 @@ public class MySQLUserService implements UserService {
}
@Override
public @Nullable ChatUser wrap(@NonNull String name) {
@Nullable
public ChatUser wrap(@NonNull String name) {
Player player = Bukkit.getPlayer(name);
if (player != null) {
@@ -90,12 +94,14 @@ public class MySQLUserService implements UserService {
}
@Override
public @Nullable ChatUser wrap(@NonNull OfflinePlayer player) {
@Nullable
public ChatUser wrap(@NonNull OfflinePlayer player) {
return wrap(player.getUniqueId());
}
@Override
public @Nullable ChatUser wrap(@NonNull UUID uuid) {
@Nullable
public ChatUser wrap(@NonNull UUID uuid) {
try {
return userCache.get(uuid);
} catch (ExecutionException e) {
@@ -111,7 +117,8 @@ public class MySQLUserService implements UserService {
}
@Override
public @Nullable ChatUser refreshUser(@NonNull UUID uuid) {
@Nullable
public ChatUser refreshUser(@NonNull UUID uuid) {
userCache.invalidate(uuid);
return this.wrap(uuid);
@@ -124,10 +131,11 @@ public class MySQLUserService implements UserService {
@Override
public void validate(@NonNull ChatUser user) {
userCache.put(user.getUUID(), (CarbonChatUser)user);
userCache.put(user.getUUID(), (CarbonChatUser) user);
}
private @NonNull CarbonChatUser loadUser(@NonNull UUID uuid) {
@NonNull
private CarbonChatUser loadUser(@NonNull UUID uuid) {
CarbonChatUser user = new CarbonChatUser(uuid);
try (DbStatement statement = database.query("SELECT * from sc_users WHERE uuid = ?;")) {
@@ -185,13 +193,9 @@ public class MySQLUserService implements UserService {
return user;
}
private void saveUser(@NonNull RemovalNotification<UUID, CarbonChatUser> notification) {
private void saveUser(@NonNull RemovalNotification<@NonNull UUID, @NonNull CarbonChatUser> notification) {
CarbonChatUser user = notification.getValue();
if (user == null) {
return;
}
database.createTransaction(stm -> {
// Save user general data
String selectedName = null;
@@ -4,6 +4,7 @@ import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.storage.UserChannelSettings;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -11,28 +12,31 @@ import java.util.UUID;
public class SimpleUserChannelSettings implements UserChannelSettings {
@NonNull
private final transient CarbonChat carbonChat;
private boolean spying;
private boolean ignored;
private @Nullable String color;
@Nullable
private String color;
@MonotonicNonNull // @NonNull but not initialised in all constructors.
private UUID uuid;
@MonotonicNonNull // @NonNull but not initialised in all constructors.
private String channel;
private final transient @Nullable @NonNull CarbonChat carbonChat;
private SimpleUserChannelSettings() {
carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
}
public SimpleUserChannelSettings(UUID uuid, String channel) {
public SimpleUserChannelSettings(@NonNull UUID uuid, @NonNull String channel) {
this.uuid = uuid;
this.channel = channel;
carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
}
private @NonNull CarbonChatUser getUser() {
return (CarbonChatUser)carbonChat.getUserService().wrap(uuid);
@NonNull
private CarbonChatUser getUser() {
return (CarbonChatUser) carbonChat.getUserService().wrap(uuid);
}
@Override