Skip to content

Commit c53c5a8

Browse files
refactor: prepare adding file storage
1 parent ff4d523 commit c53c5a8

6 files changed

Lines changed: 197 additions & 109 deletions

File tree

PistonChat/src/main/java/net/pistonmaster/pistonchat/PistonChat.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import net.pistonmaster.pistonchat.commands.whisper.ReplyCommand;
1616
import net.pistonmaster.pistonchat.commands.whisper.WhisperCommand;
1717
import net.pistonmaster.pistonchat.events.ChatEvent;
18+
import net.pistonmaster.pistonchat.storage.PCStorage;
19+
import net.pistonmaster.pistonchat.storage.mysql.MySQLStorage;
1820
import net.pistonmaster.pistonchat.tools.*;
1921
import net.pistonmaster.pistonchat.utils.ConfigManager;
2022
import net.pistonmaster.pistonutils.update.GitHubUpdateChecker;
@@ -28,8 +30,6 @@
2830
import org.mariadb.jdbc.MariaDbPoolDataSource;
2931

3032
import java.io.IOException;
31-
import java.sql.Connection;
32-
import java.sql.SQLException;
3333
import java.util.logging.Logger;
3434

3535
@Getter
@@ -43,7 +43,7 @@ public final class PistonChat extends JavaPlugin {
4343
private final HardIgnoreTool hardIgnoreTool = new HardIgnoreTool(this);
4444
private final CommonTool commonTool = new CommonTool(this);
4545
private final FoliaLib foliaLib = new FoliaLib(this);
46-
private MariaDbPoolDataSource ds;
46+
private PCStorage storage;
4747
private BukkitAudiences adventure;
4848

4949
@Override
@@ -71,30 +71,11 @@ public void onEnable() {
7171
Bukkit.getPluginManager().disablePlugin(this);
7272
}
7373

74-
log.info(ChatColor.DARK_GREEN + "Connecting to database");
75-
ds = new MariaDbPoolDataSource();
76-
FileConfiguration config = configManager.get();
77-
try {
78-
ds.setUser(config.getString("mysql.username"));
79-
ds.setPassword(config.getString("mysql.password"));
80-
ds.setUrl("jdbc:mariadb://" + config.getString("mysql.host") + ":" + config.getInt("mysql.port") +
81-
"/" + config.getString("mysql.database")
82-
+ "?sslMode=disable&serverTimezone=UTC&maxPoolSize=10"
83-
);
84-
85-
try (Connection connection = ds.getConnection()) {
86-
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_settings_chat` (`uuid` VARCHAR(36) NOT NULL," +
87-
"`chat_enabled` tinyint(1) NOT NULL," +
88-
"PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
89-
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_settings_whisper` (`uuid` VARCHAR(36) NOT NULL," +
90-
"`whisper_enabled` tinyint(1) NOT NULL," +
91-
"PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
92-
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_hard_ignores` (`uuid` VARCHAR(36) NOT NULL," +
93-
"`ignored_uuid` VARCHAR(36) NOT NULL," +
94-
"PRIMARY KEY (`uuid`, `ignored_uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
95-
}
96-
} catch (SQLException e) {
97-
e.printStackTrace();
74+
log.info(ChatColor.DARK_GREEN + "Loading storage");
75+
if (configManager.get().getString("storage").equalsIgnoreCase("mysql")) {
76+
storage = new MySQLStorage(log, configManager);
77+
} else if (configManager.get().getString("storage").equalsIgnoreCase("file")) {
78+
9879
}
9980

10081
log.info(ChatColor.DARK_GREEN + "Registering commands");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.pistonmaster.pistonchat.storage;
2+
3+
import java.util.List;
4+
import java.util.UUID;
5+
6+
public interface PCStorage {
7+
void setChatEnabled(UUID uuid, boolean enabled);
8+
9+
boolean isChatEnabled(UUID uuid);
10+
11+
void setWhisperingEnabled(UUID uuid, boolean enabled);
12+
13+
boolean isWhisperingEnabled(UUID uuid);
14+
15+
void hardIgnorePlayer(UUID ignoringReceiver, UUID ignoredChatter);
16+
17+
boolean isHardIgnored(UUID chatter, UUID receiver);
18+
19+
List<UUID> getIgnoredList(UUID uuid);
20+
21+
void clearIgnoredPlayers(UUID player);
22+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package net.pistonmaster.pistonchat.storage.mysql;
2+
3+
import net.md_5.bungee.api.ChatColor;
4+
import net.pistonmaster.pistonchat.storage.PCStorage;
5+
import net.pistonmaster.pistonchat.utils.ConfigManager;
6+
import org.bukkit.configuration.file.FileConfiguration;
7+
import org.mariadb.jdbc.MariaDbPoolDataSource;
8+
9+
import java.sql.Connection;
10+
import java.sql.PreparedStatement;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.util.List;
14+
import java.util.UUID;
15+
import java.util.logging.Logger;
16+
17+
public class MySQLStorage implements PCStorage {
18+
private final MariaDbPoolDataSource ds;
19+
20+
public MySQLStorage(Logger log, ConfigManager configManager) {
21+
log.info(ChatColor.DARK_GREEN + "Connecting to database");
22+
ds = new MariaDbPoolDataSource();
23+
FileConfiguration config = configManager.get();
24+
try {
25+
ds.setUser(config.getString("mysql.username"));
26+
ds.setPassword(config.getString("mysql.password"));
27+
ds.setUrl("jdbc:mariadb://" + config.getString("mysql.host") + ":" + config.getInt("mysql.port") +
28+
"/" + config.getString("mysql.database")
29+
+ "?sslMode=disable&serverTimezone=UTC&maxPoolSize=10"
30+
);
31+
32+
try (Connection connection = ds.getConnection()) {
33+
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_settings_chat` (`uuid` VARCHAR(36) NOT NULL," +
34+
"`chat_enabled` tinyint(1) NOT NULL," +
35+
"PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
36+
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_settings_whisper` (`uuid` VARCHAR(36) NOT NULL," +
37+
"`whisper_enabled` tinyint(1) NOT NULL," +
38+
"PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
39+
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `pistonchat_hard_ignores` (`uuid` VARCHAR(36) NOT NULL," +
40+
"`ignored_uuid` VARCHAR(36) NOT NULL," +
41+
"PRIMARY KEY (`uuid`, `ignored_uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
42+
}
43+
} catch (SQLException e) {
44+
e.printStackTrace();
45+
}
46+
47+
log.info(ChatColor.DARK_GREEN + "Connected to database");
48+
}
49+
50+
@Override
51+
public void setChatEnabled(UUID uuid, boolean enabled) {
52+
try (Connection connection = ds.getConnection()) {
53+
PreparedStatement statement = connection.prepareStatement("INSERT INTO `pistonchat_settings_chat` (`uuid`, `chat_enabled`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `chat_enabled` = ?;");
54+
statement.setString(1, uuid.toString());
55+
statement.setBoolean(2, enabled);
56+
statement.setBoolean(3, enabled);
57+
statement.execute();
58+
} catch (SQLException e) {
59+
throw new RuntimeException(e);
60+
}
61+
}
62+
63+
@Override
64+
public boolean isChatEnabled(UUID uuid) {
65+
try (Connection connection = ds.getConnection()) {
66+
PreparedStatement statement = connection.prepareStatement("SELECT `chat_enabled` FROM `pistonchat_settings_chat` WHERE `uuid` = ?;");
67+
statement.setString(1, uuid.toString());
68+
69+
ResultSet resultSet = statement.executeQuery();
70+
if (!resultSet.next()) {
71+
return true;
72+
}
73+
74+
return resultSet.getBoolean("chat_enabled");
75+
} catch (SQLException e) {
76+
throw new RuntimeException(e);
77+
}
78+
}
79+
80+
@Override
81+
public void setWhisperingEnabled(UUID uuid, boolean enabled) {
82+
try (Connection connection = ds.getConnection()) {
83+
PreparedStatement statement = connection.prepareStatement("INSERT INTO `pistonchat_settings_whisper` (`uuid`, `whisper_enabled`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `whisper_enabled` = ?;");
84+
statement.setString(1, uuid.toString());
85+
statement.setBoolean(2, enabled);
86+
statement.setBoolean(3, enabled);
87+
statement.execute();
88+
} catch (SQLException e) {
89+
throw new RuntimeException(e);
90+
}
91+
}
92+
93+
@Override
94+
public boolean isWhisperingEnabled(UUID uuid) {
95+
try (Connection connection = ds.getConnection()) {
96+
PreparedStatement statement = connection.prepareStatement("SELECT `whisper_enabled` FROM `pistonchat_settings_whisper` WHERE `uuid` = ?;");
97+
statement.setString(1, uuid.toString());
98+
99+
ResultSet resultSet = statement.executeQuery();
100+
if (!resultSet.next()) {
101+
return true;
102+
}
103+
104+
return resultSet.getBoolean("whisper_enabled");
105+
} catch (SQLException e) {
106+
throw new RuntimeException(e);
107+
}
108+
}
109+
110+
@Override
111+
public void hardIgnorePlayer(UUID ignoringReceiver, UUID ignoredChatter) {
112+
113+
}
114+
115+
@Override
116+
public boolean isHardIgnored(UUID chatter, UUID receiver) {
117+
return false;
118+
}
119+
120+
@Override
121+
public List<UUID> getIgnoredList(UUID uuid) {
122+
return List.of();
123+
}
124+
125+
@Override
126+
public void clearIgnoredPlayers(UUID player) {
127+
128+
}
129+
}

PistonChat/src/main/java/net/pistonmaster/pistonchat/tools/TempDataTool.java

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,44 @@
66
import net.pistonmaster.pistonchat.PistonChat;
77
import org.bukkit.entity.Player;
88

9-
import java.sql.Connection;
10-
import java.sql.PreparedStatement;
11-
import java.sql.ResultSet;
12-
import java.sql.SQLException;
139
import java.util.UUID;
1410
import java.util.concurrent.TimeUnit;
1511

1612
@RequiredArgsConstructor
1713
public class TempDataTool {
18-
private final PistonChat plugin;
19-
private final LoadingCache<UUID, Boolean> whisperCache = Caffeine.newBuilder()
20-
.expireAfterWrite(5, TimeUnit.SECONDS)
21-
.build(this::loadIsWhisperingEnabled);
22-
private final LoadingCache<UUID, Boolean> chatCache = Caffeine.newBuilder()
23-
.expireAfterWrite(5, TimeUnit.SECONDS)
24-
.build(this::loadIsChatEnabled);
25-
26-
public void setWhisperingEnabled(Player player, boolean value) {
27-
try (Connection connection = plugin.getDs().getConnection()) {
28-
PreparedStatement statement = connection.prepareStatement("INSERT INTO `pistonchat_settings_whisper` (`uuid`, `whisper_enabled`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `whisper_enabled` = ?;");
29-
statement.setString(1, player.getUniqueId().toString());
30-
statement.setBoolean(2, value);
31-
statement.setBoolean(3, value);
32-
statement.execute();
33-
34-
whisperCache.put(player.getUniqueId(), value);
35-
} catch (SQLException e) {
36-
throw new RuntimeException(e);
37-
}
38-
}
39-
40-
public void setChatEnabled(Player player, boolean value) {
41-
try (Connection connection = plugin.getDs().getConnection()) {
42-
PreparedStatement statement = connection.prepareStatement("INSERT INTO `pistonchat_settings_chat` (`uuid`, `chat_enabled`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `chat_enabled` = ?;");
43-
statement.setString(1, player.getUniqueId().toString());
44-
statement.setBoolean(2, value);
45-
statement.setBoolean(3, value);
46-
statement.execute();
47-
48-
chatCache.put(player.getUniqueId(), value);
49-
} catch (SQLException e) {
50-
throw new RuntimeException(e);
51-
}
52-
}
53-
54-
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
55-
public boolean isWhisperingEnabled(Player player) {
56-
return Boolean.TRUE.equals(whisperCache.get(player.getUniqueId()));
57-
}
58-
59-
private boolean loadIsWhisperingEnabled(UUID uuid) {
60-
try (Connection connection = plugin.getDs().getConnection()) {
61-
PreparedStatement statement = connection.prepareStatement("SELECT `whisper_enabled` FROM `pistonchat_settings_whisper` WHERE `uuid` = ?;");
62-
statement.setString(1, uuid.toString());
63-
64-
ResultSet resultSet = statement.executeQuery();
65-
if (!resultSet.next()) {
66-
return true;
67-
}
68-
69-
return resultSet.getBoolean("whisper_enabled");
70-
} catch (SQLException e) {
71-
throw new RuntimeException(e);
72-
}
73-
}
74-
75-
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
76-
public boolean isChatEnabled(Player player) {
77-
return Boolean.TRUE.equals(chatCache.get(player.getUniqueId()));
78-
}
79-
80-
private boolean loadIsChatEnabled(UUID uuid) {
81-
try (Connection connection = plugin.getDs().getConnection()) {
82-
PreparedStatement statement = connection.prepareStatement("SELECT `chat_enabled` FROM `pistonchat_settings_chat` WHERE `uuid` = ?;");
83-
statement.setString(1, uuid.toString());
84-
85-
ResultSet resultSet = statement.executeQuery();
86-
if (!resultSet.next()) {
87-
return true;
88-
}
89-
90-
return resultSet.getBoolean("chat_enabled");
91-
} catch (SQLException e) {
92-
throw new RuntimeException(e);
93-
}
94-
}
14+
private final PistonChat plugin;
15+
private final LoadingCache<UUID, Boolean> whisperCache = Caffeine.newBuilder()
16+
.expireAfterWrite(5, TimeUnit.SECONDS)
17+
.build(this::loadIsWhisperingEnabled);
18+
private final LoadingCache<UUID, Boolean> chatCache = Caffeine.newBuilder()
19+
.expireAfterWrite(5, TimeUnit.SECONDS)
20+
.build(this::loadIsChatEnabled);
21+
22+
public void setWhisperingEnabled(Player player, boolean value) {
23+
plugin.getStorage().setWhisperingEnabled(player.getUniqueId(), value);
24+
whisperCache.put(player.getUniqueId(), value);
25+
}
26+
27+
public void setChatEnabled(Player player, boolean value) {
28+
plugin.getStorage().setChatEnabled(player.getUniqueId(), value);
29+
chatCache.put(player.getUniqueId(), value);
30+
}
31+
32+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
33+
public boolean isWhisperingEnabled(Player player) {
34+
return Boolean.TRUE.equals(whisperCache.get(player.getUniqueId()));
35+
}
36+
37+
private boolean loadIsWhisperingEnabled(UUID uuid) {
38+
return plugin.getStorage().isWhisperingEnabled(uuid);
39+
}
40+
41+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
42+
public boolean isChatEnabled(Player player) {
43+
return Boolean.TRUE.equals(chatCache.get(player.getUniqueId()));
44+
}
45+
46+
private boolean loadIsChatEnabled(UUID uuid) {
47+
return plugin.getStorage().isChatEnabled(uuid);
48+
}
9549
}

PistonChat/src/main/java/net/pistonmaster/pistonchat/utils/ConfigManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void create() throws IOException {
2323

2424
config = getConfig();
2525
config.setDefaults(getDefaultConfig());
26-
config.options().copyHeader();
26+
config.options().copyHeader(true);
2727

2828
saveConfig(config);
2929
}

PistonChat/src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
# This config is configured to be what 2b2t.org has.
44
# https://hub.spigotmc.org/javadocs/spigot/org/bukkit/ChatColor.html
55
# pistonchat.prefix.green and pistonchat.chatformat.default are given by default.
6+
# storage is either file or mysql
67
whisper:
78
from: "<light_purple><player_name> whispers: <message>"
89
to: "<light_purple>You whisper to <player_name>: <message>"
910
hover-text: "<gold>Message <dark_aqua><player_name>"
1011

12+
storage: "file"
1113
mysql:
1214
host: "localhost"
1315
port: 3306

0 commit comments

Comments
 (0)