Skip to content

Commit 6b63ab6

Browse files
committed
feat: Initial implementation
1 parent 3623f67 commit 6b63ab6

13 files changed

Lines changed: 243 additions & 66 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# VelocityPluginTemplate
1+
# AuthmeVelocity LastServer Addon
22

33
[![Discord](https://img.shields.io/discord/899740810956910683?color=7289da&label=Discord)](https://discord.gg/5NMMzK5mAn)
44

5-
Description Here
5+
a
66

77
## Installation
8-
- Download YourPlugin from Modrinth
8+
- Download LastServer Addon from Modrinth
99
- Drag and drop on your plugins folder
1010
- Start the server

build.gradle.kts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
java
33
alias(libs.plugins.runvelocity)
44
alias(libs.plugins.blossom)
5+
alias(libs.plugins.shadow)
56
}
67

78
repositories {
@@ -11,12 +12,20 @@ repositories {
1112
dependencies {
1213
compileOnly(libs.velocity)
1314
annotationProcessor(libs.velocity)
15+
compileOnly(libs.authmevelocity)
16+
implementation("com.zaxxer:HikariCP:5.0.1") {
17+
isTransitive = false
18+
}
19+
implementation(libs.sqlite)
1420
}
1521

1622
tasks {
23+
build {
24+
dependsOn(shadowJar)
25+
}
1726
compileJava {
1827
options.encoding = Charsets.UTF_8.name()
19-
options.release.set(11)
28+
options.release.set(17)
2029
}
2130
clean {
2231
// Deletes the directory that is generated by the runVelocity plugin
@@ -28,8 +37,8 @@ tasks {
2837
}
2938

3039
blossom {
31-
replaceTokenIn("src/main/java/io/github/_4drian3d/Constants.java")
40+
replaceTokenIn("src/main/java/io/github/_4drian3d/authmevelocity/lastserver/Constants.java")
3241
replaceToken("{version}", project.version)
3342
}
3443

35-
java.toolchain.languageVersion.set(JavaLanguageVersion.of(11))
44+
java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
group = your.package.here
1+
group = io.github.4drian3d
22
version = 1.0.0-SNAPSHOT
3-
description = Description Here
3+
description = LastServer Addon for AuthMeVelocity

gradle/libs.versions.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ configurate = "4.1.2"
1717
libby = "43d25ade72"
1818
bstats = "3.0.0"
1919

20-
# Required by Configurate 4
21-
geantyref = "1.3.13"
20+
sqlite = "3.42.0.0"
2221

2322
[libraries]
2423

2524
# Compile Only Dependencies
2625
velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity" }
2726
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" }
27+
authmevelocity = { module = "io.github.4drian3d:authmevelocity-api-velocity", version = "4.0.2" }
2828

2929
bstats = { module = "org.bstats:bstats-velocity", version.ref = "bstats" }
3030

3131
configurate-hocon = { module = "org.spongepowered:configurate-hocon", version.ref = "configurate" }
3232
configurate-kotlin = { module = "org.spongepowered:configurate-extra-kotlin", version.ref = "configurate" }
3333

3434
libby = { group = "com.github.AlessioDP.libby", name = "libby-velocity", version.ref = "libby" }
35+
sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" }
3536

3637
[plugins]
3738

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
44
networkTimeout=10000
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = "PluginName"
1+
rootProject.name = "AuthMeVelocity-LastServerAddon"

src/main/java/io/github/_4drian3d/VelocityPlugin.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/main/java/io/github/_4drian3d/Constants.java renamed to src/main/java/io/github/_4drian3d/authmevelocity/lastserver/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github._4drian3d;
1+
package io.github._4drian3d.authmevelocity.lastserver;
22

33
public final class Constants {
44
public static final String VERSION = "{version}";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github._4drian3d.authmevelocity.lastserver;
2+
3+
import com.google.inject.Inject;
4+
import com.google.inject.Injector;
5+
import com.velocitypowered.api.event.Subscribe;
6+
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
7+
import com.velocitypowered.api.plugin.Dependency;
8+
import com.velocitypowered.api.plugin.Plugin;
9+
import io.github._4drian3d.authmevelocity.lastserver.database.Database;
10+
import io.github._4drian3d.authmevelocity.lastserver.listener.DisconnectListener;
11+
import io.github._4drian3d.authmevelocity.lastserver.listener.LastLoginListener;
12+
import io.github._4drian3d.authmevelocity.lastserver.listener.PreSendOnLoginListener;
13+
import org.slf4j.Logger;
14+
15+
import java.util.stream.Stream;
16+
17+
@Plugin(
18+
id = "authmevelocity-lastlogin-addon",
19+
name = "AuthMeVelocity-LastLogin-Addon",
20+
description = "LastLogin Addon",
21+
version = Constants.VERSION,
22+
authors = { "4drian3d" },
23+
dependencies = {@Dependency(id = "authmevelocity")}
24+
)
25+
public final class LastServerAddon {
26+
@Inject
27+
private Injector injector;
28+
@Inject
29+
private Logger logger;
30+
@Inject
31+
private Database database;
32+
33+
@Subscribe
34+
void onProxyInitialization(final ProxyInitializeEvent event) {
35+
this.database.initDatabase();
36+
Stream.of(PreSendOnLoginListener.class, DisconnectListener.class)
37+
.map(this.injector::getInstance)
38+
.forEach(LastLoginListener::register);
39+
this.logger.info("Started AuthMeVelocity LastServer Addon");
40+
}
41+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package io.github._4drian3d.authmevelocity.lastserver.database;
2+
3+
import com.google.inject.Inject;
4+
import com.google.inject.Singleton;
5+
import com.velocitypowered.api.plugin.annotation.DataDirectory;
6+
import com.zaxxer.hikari.HikariConfig;
7+
import com.zaxxer.hikari.HikariDataSource;
8+
import org.slf4j.Logger;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.sql.Connection;
14+
import java.sql.PreparedStatement;
15+
import java.sql.ResultSet;
16+
import java.sql.SQLException;
17+
18+
@Singleton
19+
public final class Database {
20+
private static final String CREATE_SENTENCE = """
21+
CREATE TABLE IF NOT EXISTS last_server\
22+
(\
23+
`player` VARCHAR(20) NOT NULL PRIMARY KEY, \
24+
`server` VARCHAR(20) NOT NULL\
25+
);
26+
""";
27+
private static final String SELECT_BY_PLAYER = "SELECT `server` FROM last_server WHERE `player` = ?;";
28+
private static final String INSERT_DATA = "INSERT INTO last_server(`player`, `server`) VALUES (?, ?);";
29+
30+
private final HikariDataSource source;
31+
private final Logger logger;
32+
@Inject
33+
public Database(
34+
final @DataDirectory Path dataDirectory,
35+
final Logger logger
36+
) throws IOException {
37+
if (Files.notExists(dataDirectory)) {
38+
Files.createDirectory(dataDirectory);
39+
}
40+
final Path databasePath = dataDirectory.resolve("database.db").toAbsolutePath();
41+
if (Files.notExists(databasePath)) {
42+
Files.createFile(databasePath);
43+
}
44+
final HikariConfig hikariConfig = new HikariConfig();
45+
hikariConfig.setDriverClassName("org.h2.Driver");
46+
hikariConfig.setJdbcUrl("jdbc:h2:"+databasePath);
47+
48+
this.source = new HikariDataSource(hikariConfig);
49+
this.logger = logger;
50+
}
51+
52+
public String lastServerOf(final String playerName) {
53+
try (final Connection connection = this.source.getConnection();
54+
final PreparedStatement statement = fromPlayer(playerName, connection);
55+
final ResultSet rs = statement.executeQuery()
56+
) {
57+
return rs.getString("server");
58+
} catch (final SQLException e) {
59+
return null;
60+
}
61+
}
62+
63+
public void setLastServer(final String playerName, final String server) {
64+
try (final Connection connection = this.source.getConnection();
65+
final PreparedStatement statement = connection.prepareStatement(INSERT_DATA);
66+
) {
67+
statement.setString(1, playerName);
68+
statement.setString(2, server);
69+
statement.executeUpdate();
70+
} catch (final SQLException e) {
71+
this.logger.warn("An error occurred updating last server information of player {}", playerName);
72+
}
73+
}
74+
75+
private PreparedStatement fromPlayer(final String playerName, final Connection connection) throws SQLException {
76+
final PreparedStatement statement = connection.prepareStatement(SELECT_BY_PLAYER);
77+
statement.setString(1, playerName);
78+
return statement;
79+
}
80+
81+
public void initDatabase() {
82+
try (final Connection connection = this.source.getConnection();
83+
final PreparedStatement statement = connection.prepareStatement(CREATE_SENTENCE)
84+
) {
85+
statement.executeUpdate();
86+
} catch (final Exception e) {
87+
this.logger.warn("An error occurred loading database", e);
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)