35
35
import com .velocitypowered .api .proxy .messages .ChannelIdentifier ;
36
36
import com .velocitypowered .api .proxy .messages .MinecraftChannelIdentifier ;
37
37
import com .zaxxer .hikari .HikariDataSource ;
38
+ import de .chojo .sadu .databases .Database ;
38
39
import de .chojo .sadu .databases .PostgreSql ;
40
+ import de .chojo .sadu .databases .SqLite ;
39
41
import de .chojo .sadu .datasource .DataSourceCreator ;
42
+ import de .chojo .sadu .datasource .stage .ConfigurationStage ;
43
+ import de .chojo .sadu .jdbc .RemoteJdbcConfig ;
40
44
import de .chojo .sadu .updater .SqlUpdater ;
41
45
import de .chojo .sadu .wrapper .QueryBuilderConfig ;
42
46
import de .jvstvshd .velocitypunishment .api .VelocityPunishment ;
56
60
57
61
import java .io .IOException ;
58
62
import java .nio .file .Path ;
59
- import java .nio .file .Paths ;
63
+ import java .sql .Connection ;
64
+ import java .sql .PreparedStatement ;
60
65
import java .sql .SQLException ;
61
66
import java .util .concurrent .ExecutorService ;
62
67
import java .util .concurrent .Executors ;
@@ -68,12 +73,14 @@ public class VelocityPunishmentPlugin implements VelocityPunishment {
68
73
private final Logger logger ;
69
74
private final ConfigurationManager configurationManager ;
70
75
private final ExecutorService service = Executors .newCachedThreadPool ();
76
+ private final Path dataDirectory ;
71
77
public static final ChannelIdentifier MUTE_DATA_CHANNEL_IDENTIFIER = MinecraftChannelIdentifier .from (MuteData .MUTE_DATA_CHANNEL_IDENTIFIER );
72
78
private PunishmentManager punishmentManager ;
73
79
private HikariDataSource dataSource ;
74
80
private PlayerResolver playerResolver ;
75
81
private MessageProvider messageProvider ;
76
82
83
+
77
84
private static final String MUTES_DISABLED_STRING = """
78
85
Since 1.19.1, cancelling chat messages on proxy is not possible anymore. Therefore, we have to listen to the chat event on the actual game server. This means
79
86
that there has to be a spigot/paper extension to this plugin which is not yet available unless there's a possibility. Therefore all mute related features won't work at the moment.
@@ -92,14 +99,15 @@ public class VelocityPunishmentPlugin implements VelocityPunishment {
92
99
public VelocityPunishmentPlugin (ProxyServer server , Logger logger , @ DataDirectory Path dataDirectory ) {
93
100
this .server = server ;
94
101
this .logger = logger ;
95
- this .configurationManager = new ConfigurationManager (Paths . get ( dataDirectory .toAbsolutePath (). toString (), "config.json " ));
102
+ this .configurationManager = new ConfigurationManager (dataDirectory .resolve ( "config.yml " ));
96
103
this .playerResolver = new DefaultPlayerResolver (server );
97
104
this .communicator = new MessagingChannelCommunicator (server , logger );
105
+ this .dataDirectory = dataDirectory ;
98
106
}
99
107
100
108
@ Subscribe
101
109
public void onProxyInitialization (ProxyInitializeEvent event ) {
102
- Thread .setDefaultUncaughtExceptionHandler ((t , e ) -> logger .error ("An error occurred in thread " + t .getName (), e ));
110
+ Thread .setDefaultUncaughtExceptionHandler ((t , e ) -> logger .error ("An error occurred in thread {}" , t .getName (), e ));
103
111
try {
104
112
configurationManager .load ();
105
113
if (configurationManager .getConfiguration ().isWhitelistActivated ()) {
@@ -114,9 +122,9 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
114
122
punishmentManager = new DefaultPunishmentManager (server , dataSource , this );
115
123
try {
116
124
updateDatabase ();
117
- // initDataSource();
125
+ initDataSource ();
118
126
} catch (SQLException | IOException e ) {
119
- logger .error ("Could not create table velocity_punishment in database " + dataSource .getDataSourceProperties ().get ("dataSource.databaseName" ), e );
127
+ logger .error ("Could not create table velocity_punishment in database {}" , dataSource .getDataSourceProperties ().get ("dataSource.databaseName" ), e );
120
128
}
121
129
setup (server .getCommandManager (), server .getEventManager ());
122
130
logger .info ("Velocity Punishment Plugin v1.0.0 has been loaded" );
@@ -139,28 +147,59 @@ private void setup(CommandManager commandManager, EventManager eventManager) {
139
147
commandManager .register (WhitelistCommand .whitelistCommand (this ));
140
148
}
141
149
150
+ @ SuppressWarnings ({"unchecked" , "UnstableApiUsage" })
142
151
private HikariDataSource createDataSource () {
143
152
var dbData = configurationManager .getConfiguration ().getDataBaseData ();
144
- //TODO add config option for sql type
145
- return DataSourceCreator .create (PostgreSql .get ())
146
- .configure (jdbcConfig -> jdbcConfig .host (dbData .getHost ())
147
- .port (dbData .getPort ())
148
- .database (dbData .getDatabase ())
149
- .user (dbData .getUsername ())
150
- .password (dbData .getPassword ())
151
- .applicationName ("Velocity Punishment Plugin" ))
152
- .create ().withMaximumPoolSize (dbData .getMaxPoolSize ())
153
+ ConfigurationStage stage = switch (dbData .sqlType ().name ().toLowerCase ()) {
154
+ case "sqlite" -> DataSourceCreator .create (SqLite .get ())
155
+ .configure (sqLiteJdbc -> sqLiteJdbc .path (dataDirectory .resolve ("punishment.db" )))
156
+ .create ();
157
+ case "postgresql" ->
158
+ DataSourceCreator .create (PostgreSql .get ()).configure (jdbcConfig -> jdbcConfig .host (dbData .getHost ())
159
+
160
+ .port (dbData .getPort ())
161
+ .database (dbData .getDatabase ())
162
+ .user (dbData .getUsername ())
163
+ .password (dbData .getPassword ())
164
+ )
165
+ .create ();
166
+
167
+ default ->
168
+ DataSourceCreator .create ((Database <RemoteJdbcConfig <?>, ?>) dbData .sqlType ()).configure (jdbcConfig -> jdbcConfig .host (dbData .getHost ())
169
+ .port (dbData .getPort ())
170
+ .database (dbData .getDatabase ())
171
+ .user (dbData .getUsername ())
172
+ .password (dbData .getPassword ()))
173
+ .create ();
174
+ };
175
+ return stage .withMaximumPoolSize (dbData .getMaxPoolSize ())
153
176
.withMinimumIdle (dbData .getMinIdle ())
154
177
.withPoolName ("velocity-punishment-hikari" )
178
+ .forSchema (dbData .getPostgresSchema ())
155
179
.build ();
156
180
}
157
181
182
+ @ SuppressWarnings ("UnstableApiUsage" )
158
183
private void updateDatabase () throws IOException , SQLException {
159
- //TODO config option for sql type
160
- SqlUpdater .builder (dataSource , PostgreSql .get ())
161
- .setSchemas ("punishment" )
162
- .execute ();
184
+ if (configurationManager .getConfiguration ().getDataBaseData ().sqlType ().name ().equalsIgnoreCase ("postgresql" )) {
185
+ SqlUpdater .builder (dataSource , PostgreSql .get ())
186
+ .setSchemas (configurationManager .getConfiguration ().getDataBaseData ().getPostgresSchema ())
187
+ .execute ();
188
+ } else {
189
+ logger .warn ("Database type is not (yet) supported for automatic updates. Please update the database manually." );
190
+ }
191
+ }
163
192
193
+ private void initDataSource () throws SQLException {
194
+ try (Connection connection = dataSource .getConnection (); PreparedStatement statement =
195
+ connection .prepareStatement ("CREATE TABLE IF NOT EXISTS velocity_punishment (uuid VARCHAR (36), name VARCHAR (16), type VARCHAR (1000), expiration DATETIME (6), " +
196
+ "reason VARCHAR (1000), punishment_id VARCHAR (36))" )) {
197
+ statement .execute ();
198
+ }
199
+ try (Connection connection = dataSource .getConnection (); PreparedStatement statement =
200
+ connection .prepareStatement ("CREATE TABLE IF NOT EXISTS velocity_punishment_whitelist (uuid VARCHAR (36))" )) {
201
+ statement .execute ();
202
+ }
164
203
}
165
204
166
205
@ Override
@@ -219,4 +258,8 @@ public Logger getLogger() {
219
258
public boolean whitelistActive () {
220
259
return configurationManager .getConfiguration ().isWhitelistActivated ();
221
260
}
261
+
262
+ public ConfigurationManager getConfig () {
263
+ return configurationManager ;
264
+ }
222
265
}
0 commit comments