mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Initial MySQL support
This commit is contained in:
1 parent
47a0fe98a8
commit
1a8c43a870
6 files changed
+171
-16
No files matched your search
@@ -7,6 +7,7 @@ import net.draycia.simplechat.listeners.chat.*;
|
||||
import net.draycia.simplechat.managers.*;
|
||||
import net.draycia.simplechat.storage.UserService;
|
||||
import net.draycia.simplechat.storage.impl.JSONUserService;
|
||||
import net.draycia.simplechat.storage.impl.MySQLUserService;
|
||||
import net.draycia.simplechat.util.ItemStackUtils;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
@@ -55,7 +56,16 @@ public final class SimpleChat extends JavaPlugin {
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
|
||||
userService = new JSONUserService(this); // TODO: SQLUserService
|
||||
String storageType = getConfig().getString("storage.type");
|
||||
|
||||
if (storageType.equalsIgnoreCase("mysql")) {
|
||||
userService = new MySQLUserService(this);
|
||||
} else if (storageType.equalsIgnoreCase("json")) {
|
||||
userService = new JSONUserService(this);
|
||||
} else {
|
||||
getLogger().warning("Invalid storage type selected! Falling back to JSON.");
|
||||
userService = new JSONUserService(this);
|
||||
}
|
||||
|
||||
// Setup listeners
|
||||
setupListeners();
|
||||
@@ -97,13 +107,17 @@ public final class SimpleChat extends JavaPlugin {
|
||||
}
|
||||
|
||||
public ChatChannel getChannel(String name) {
|
||||
if (name == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
public List<ChatChannel> getChannels() {
|
||||
|
||||
@@ -33,7 +33,6 @@ public class JSONUserService extends UserService {
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private Gson gson;
|
||||
|
||||
private Type userType = new TypeToken<SimpleChatUser>() {}.getType();
|
||||
|
||||
public JSONUserService(SimpleChat simpleChat) {
|
||||
@@ -41,9 +40,7 @@ public class JSONUserService extends UserService {
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(simpleChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
|
||||
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
|
||||
builder.registerTypeAdapter(new TypeToken<ChatChannel>() {}.getType(), new ChatChannelAdapter(simpleChat));
|
||||
gson = builder.create();
|
||||
gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package net.draycia.simplechat.storage.impl;
|
||||
|
||||
import co.aikar.idb.BukkitDB;
|
||||
import co.aikar.idb.Database;
|
||||
import co.aikar.idb.DbRow;
|
||||
import co.aikar.idb.DbStatement;
|
||||
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.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.storage.UserService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MySQLUserService extends UserService {
|
||||
|
||||
private final LoadingCache<UUID, SimpleChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.removalListener(this::saveUser)
|
||||
.build(CacheLoader.from(this::loadUser));
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private Database database;
|
||||
|
||||
private MySQLUserService() { }
|
||||
|
||||
public MySQLUserService(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("storage");
|
||||
|
||||
String username = section.getString("username");
|
||||
String password = section.getString("password");
|
||||
String dbname = section.getString("database");
|
||||
String hostandport = section.getString("host") + ":" + section.getString("port");
|
||||
|
||||
database = BukkitDB.createHikariDatabase(simpleChat, username, password, dbname, hostandport);
|
||||
|
||||
try {
|
||||
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_ignored_users (uuid CHAR(36), user CHAR(36))");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(simpleChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
@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
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
userCache.cleanUp();
|
||||
}
|
||||
|
||||
private SimpleChatUser loadUser(UUID uuid) {
|
||||
SimpleChatUser user = new SimpleChatUser(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> ignoredChannels = database.getResults("SELECT * from sc_ignored_channels 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.setMuted(users.<Boolean>get("muted"));
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
|
||||
|
||||
for (DbRow ignoredChannel : ignoredChannels) {
|
||||
user.setIgnoringChannel(simpleChat.getChannel(ignoredChannel.getString("channel")), true);
|
||||
}
|
||||
|
||||
for (DbRow ignoredUser : ignoredUsers) {
|
||||
user.setIgnoringUser(UUID.fromString(ignoredUser.getString("user")), true);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
|
||||
SimpleChatUser user = notification.getValue();
|
||||
|
||||
try {
|
||||
database.executeUpdate("SET @uuid = ?, @channel = ?, @muted = ?, @shadowmuted = ? " +
|
||||
"INSERT INTO db_users (uuid, channel, muted, shadowmuted) VALUES (@uuid, @channel, @muted, @shadowmuted) " +
|
||||
"ON DUPLICATE KEY UPDATE uuid = @uuid, channel = @channel, muted = @muted, shadowmuted = @shadowmuted;",
|
||||
user.getUUID().toString(), user.getSelectedChannel().getName(), user.isMuted(), user.isShadowMuted());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,9 +12,11 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
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;
|
||||
|
||||
@@ -42,8 +44,8 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return simpleChat.getAudiences().player(uuid);
|
||||
public @NonNull Iterable<? extends Audience> audiences() {
|
||||
return Collections.singleton(simpleChat.getAudiences().player(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,10 +164,10 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
String toPlayerFormat = simpleChat.getConfig().getString("language.message-to-other");
|
||||
String fromPlayerFormat = simpleChat.getConfig().getString("language.message-from-other");
|
||||
|
||||
Component toPlayerComponent = MiniMessage.instance().parse(toPlayerFormat, "message", message,
|
||||
Component toPlayerComponent = MiniMessage.get().parse(toPlayerFormat, "message", message,
|
||||
"target", this.asOfflinePlayer().getName());
|
||||
|
||||
Component fromPlayerComponent = MiniMessage.instance().parse(fromPlayerFormat, "message", message,
|
||||
Component fromPlayerComponent = MiniMessage.get().parse(fromPlayerFormat, "message", message,
|
||||
"sender", sender.asOfflinePlayer().getName());
|
||||
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
Reference in new issue
Block a user