Skip to content

Commit

Permalink
Merge pull request #19 from Matt-MX/dev-2.0
Browse files Browse the repository at this point in the history
Dev 2.0
  • Loading branch information
Matt-MX authored Sep 18, 2024
2 parents af7ff46 + 528c2a5 commit de48a83
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 76 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Will simply build your gradle project.
name: Build Gradle Project

# This action will run every time you push to the "dev" branch.
on:
push:
branches:
- "dev*"

jobs:
build-project:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 21
- name: Checkout project sources
uses: actions/checkout@v3
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Run build with Gradle Wrapper
run: ./gradlew build
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
build
common/build
gameserver/build
proxy/build
proxy/build
run/
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ plugins folder in your Velocity server.

Simply alter anything in the `config.yml` that is generated located in `./plugins/reconnect/config.yml`.

To allow users to reconnect, make sure they have the permission `velocity.reconnect`!

<p align="right">(<a href="#top">back to top</a>)</p>

<!-- Storage -->
Expand Down
9 changes: 1 addition & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ tasks {
mergeServiceFiles()
}
runVelocity {
// velocityVersion(libs.versions.velocity.api.get())
velocityVersion(libs.versions.velocity.get())
}
// withType(xyz.jpenilla.runtask.task.AbstractRun::class) {
// javaLauncher.set(javaToolchains.launcherFor {
// languageVersion = JavaLanguageVersion.of(17)
// vendor = JvmVendorSpec.JETBRAINS
// })
// jvmArgs("-XX:+AllowEnhancedClassRedefinition", "-XX:+AllowRedefinitionToAddDeleteMethods")
// }
}
13 changes: 11 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ pluginManagement {
}

plugins {
// add toolchain resolver
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
id("com.gradle.enterprise") version("3.15")
}

gradleEnterprise {
if (System.getenv("CI") != null) {
buildScan {
publishAlways()
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
}
}
}
11 changes: 8 additions & 3 deletions src/main/java/com/mattmx/reconnect/ReconnectConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.mattmx.reconnect;

import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

import java.util.List;

@ConfigSerializable
public class ReconnectConfig {

@Comment("Do we want to send players a message when they are reconnected to a server?")
Expand Down Expand Up @@ -47,12 +49,14 @@ public class ReconnectConfig {
""")
public StorageOptions storage = new StorageOptions();

@ConfigSerializable
public static class StorageOptions {
public String method = "yaml";

public StorageOptionsData data = new StorageOptionsData();
}

@ConfigSerializable
public static class StorageOptionsData {
public String address = "localhost:3306";
@Comment("For sqlite or yaml storage types this will be the file location.")
Expand All @@ -64,12 +68,13 @@ public static class StorageOptionsData {
Advanced connection pool settings. Most users will not need to change these.
https://github.com/brettwooldridge/HikariCP/blob/dev/README.md#gear-configuration-knobs-baby
""")
public AdvancedConnectionOptions connectionOptions = new AdvancedConnectionOptions();
public AdvancedConnectionParams connectionParameters = new AdvancedConnectionParams();
}

public static class AdvancedConnectionOptions {
@ConfigSerializable
public static class AdvancedConnectionParams {
public boolean useJdbcString = false;
public String jdbcString = "jdbc:mysql://host:3306/db";
public String jdbcString = "jdbc:mysql://localhost:3306/db";
public long connectionTimeout = 30000;
public long idleTimeout = 600000;
public long keepAliveTime = 0;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/mattmx/reconnect/ReconnectListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public void onChooseInitialServer(@NotNull PlayerChooseInitialServerEvent event)

Player player = event.getPlayer();

// Check if they have the basic permission node
if (!player.hasPermission("velocity.reconnect")) return;

String previousServerName = plugin.getStorageManager()
.getStorageMethod()
.getLastServer(player.getUniqueId());
Expand Down
49 changes: 31 additions & 18 deletions src/main/java/com/mattmx/reconnect/ReconnectVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.inject.Inject;
import com.mattmx.reconnect.storage.*;
import com.mattmx.reconnect.util.updater.UpdateChecker;
import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
Expand All @@ -15,6 +14,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;

import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -41,6 +43,8 @@ public class ReconnectVelocity {
private final @Nullable Logger logger;
private final @Nullable Path dataDirectory;

private final File configLocation;
private final YamlConfigurationLoader loader;
private @Nullable ReconnectConfig config;
private @Nullable StorageManager storage;
private UpdateChecker checker;
Expand All @@ -51,6 +55,12 @@ public ReconnectVelocity(@Nullable ProxyServer server, @Nullable Logger logger,
this.logger = logger;
this.dataDirectory = dataDirectory;

configLocation = getDataDirectory().resolve("config.yml").toFile();

loader = YamlConfigurationLoader.builder()
.file(configLocation)
.build();

instance = this;

saveDefaultConfig();
Expand All @@ -59,12 +69,12 @@ public ReconnectVelocity(@Nullable ProxyServer server, @Nullable Logger logger,
StorageManager.registerStorageMethod(new MariaDbStorage());
StorageManager.registerStorageMethod(new SQLiteStorage());
StorageManager.registerStorageMethod(new YamlStorage());
StorageManager.registerStorageMethod(new LuckPermsStorage());
if (proxy.getPluginManager().isLoaded("luckperms")) {
StorageManager.registerStorageMethod(new LuckPermsStorage());
}

ReconnectCommand.register(this);

loadStorage();

checker = new UpdateChecker();

if (checker.get("https://api.github.com/repos/Matt-MX/ReconnectVelocity/releases/latest")
Expand All @@ -82,38 +92,41 @@ public void loadStorage() {
Objects.requireNonNull(method, "That storage method is invalid!");

// Shutdown current manager
getStorageManager().end();
if (storage != null) {
storage.end();
}

storage = StorageManager.createStorageManager(method);

getLogger().info("Using {} as storage method!", storage.getStorageMethod().getId());
}

@SuppressWarnings({"ResultOfMethodCallIgnored"})
public void saveDefaultConfig() {
File filePath = getDataDirectory()
.resolve("config.toml")
.toFile();

if (!filePath.exists()) {
filePath.getParentFile().mkdirs();
if (!configLocation.exists()) {
configLocation.getParentFile().mkdirs();
try {
filePath.createNewFile();

try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(new TomlWriter().write(new ReconnectConfig()));
}
ConfigurationNode node = loader.load();
node.set(ReconnectConfig.class, new ReconnectConfig());
loader.save(node);

} catch (IOException e) {
throw new RuntimeException(e);
}
}

this.config = new Toml()
.read(filePath)
.to(ReconnectConfig.class);
try {
this.config = loader.load().get(ReconnectConfig.class);
} catch (ConfigurateException e) {
throw new RuntimeException(e);
}
}

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
loadStorage();

getProxy().getEventManager().register(this, new ReconnectListener(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mattmx.reconnect.storage;

import com.mattmx.reconnect.ReconnectCommand;
import com.mattmx.reconnect.ReconnectVelocity;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User;
Expand All @@ -18,6 +19,7 @@ public void init() {
ReconnectVelocity.get().getLogger().warn("LuckPerms is not installed!");
exception.printStackTrace();
}
ReconnectVelocity.get().getLogger().info("LuckPerms found!");
}

@Override
Expand Down Expand Up @@ -55,6 +57,6 @@ public String getLastServer(String uuid) {

@Override
public String getMethod() {
return null;
return "luckperms";
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/mattmx/reconnect/storage/MariaDbStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public void init() {

HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(org.mariadb.jdbc.Driver.class.getName());
if (config.storage.data.connectionOptions.useJdbcString) {
hikariConfig.setJdbcUrl(config.storage.data.connectionOptions.jdbcString);
if (config.storage.data.connectionParameters.useJdbcString) {
hikariConfig.setJdbcUrl(config.storage.data.connectionParameters.jdbcString);
} else {
hikariConfig.setJdbcUrl("jdbc:mariadb://" + config.storage.data.address + "/" + config.storage.data.database);
}
hikariConfig.setUsername(config.storage.data.username);
hikariConfig.setPassword(config.storage.data.password);
hikariConfig.setConnectionTimeout(config.storage.data.connectionOptions.connectionTimeout);
hikariConfig.setIdleTimeout(config.storage.data.connectionOptions.idleTimeout);
hikariConfig.setKeepaliveTime(config.storage.data.connectionOptions.keepAliveTime);
hikariConfig.setMaxLifetime(config.storage.data.connectionOptions.maxLifetime);
hikariConfig.setMinimumIdle(config.storage.data.connectionOptions.minimumIdle);
hikariConfig.setMaximumPoolSize(config.storage.data.connectionOptions.maximumPoolSize);
hikariConfig.setConnectionTimeout(config.storage.data.connectionParameters.connectionTimeout);
hikariConfig.setIdleTimeout(config.storage.data.connectionParameters.idleTimeout);
hikariConfig.setKeepaliveTime(config.storage.data.connectionParameters.keepAliveTime);
hikariConfig.setMaxLifetime(config.storage.data.connectionParameters.maxLifetime);
hikariConfig.setMinimumIdle(config.storage.data.connectionParameters.minimumIdle);
hikariConfig.setMaximumPoolSize(config.storage.data.connectionParameters.maximumPoolSize);
hikariConfig.setPoolName("reconnect");

ds = new HikariDataSource(hikariConfig);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/mattmx/reconnect/storage/MySqlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public void init() {

HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(org.mariadb.jdbc.Driver.class.getName());
if (config.storage.data.connectionOptions.useJdbcString) {
hikariConfig.setJdbcUrl(config.storage.data.connectionOptions.jdbcString);
if (config.storage.data.connectionParameters.useJdbcString) {
hikariConfig.setJdbcUrl(config.storage.data.connectionParameters.jdbcString);
} else {
hikariConfig.setJdbcUrl("jdbc:mariadb://" + config.storage.data.address + "/" + config.storage.data.database);
}
hikariConfig.setUsername(config.storage.data.username);
hikariConfig.setPassword(config.storage.data.password);
hikariConfig.setConnectionTimeout(config.storage.data.connectionOptions.connectionTimeout);
hikariConfig.setIdleTimeout(config.storage.data.connectionOptions.idleTimeout);
hikariConfig.setKeepaliveTime(config.storage.data.connectionOptions.keepAliveTime);
hikariConfig.setMaxLifetime(config.storage.data.connectionOptions.maxLifetime);
hikariConfig.setMinimumIdle(config.storage.data.connectionOptions.minimumIdle);
hikariConfig.setMaximumPoolSize(config.storage.data.connectionOptions.maximumPoolSize);
hikariConfig.setConnectionTimeout(config.storage.data.connectionParameters.connectionTimeout);
hikariConfig.setIdleTimeout(config.storage.data.connectionParameters.idleTimeout);
hikariConfig.setKeepaliveTime(config.storage.data.connectionParameters.keepAliveTime);
hikariConfig.setMaxLifetime(config.storage.data.connectionParameters.maxLifetime);
hikariConfig.setMinimumIdle(config.storage.data.connectionParameters.minimumIdle);
hikariConfig.setMaximumPoolSize(config.storage.data.connectionParameters.maximumPoolSize);
hikariConfig.setPoolName("reconnect");

ds = new HikariDataSource(hikariConfig);
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/com/mattmx/reconnect/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,31 @@

public class StorageManager {
private static final HashMap<String, StorageMethod> methods = new HashMap<>();
private @Nullable StorageMethod currentStorageMethod;
private final @NotNull StorageMethod currentStorageMethod;

public StorageManager(@NotNull StorageMethod method) {
this.currentStorageMethod = method;
}

public void init() {
Objects.requireNonNull(currentStorageMethod, "Storage not initialized! Can't call init until it is initialized.").init();
currentStorageMethod.init();
}

public void end() {
Objects.requireNonNull(currentStorageMethod, "Storage not initialized! Can't call end until it is initialized.").save();
currentStorageMethod.save();
}

public @NotNull StorageMethod getStorageMethod() {
if (this.currentStorageMethod == null) {
throw new RuntimeException("There has been no Storage Method set!");
}

return this.currentStorageMethod;
}

public void setCurrentStorageMethod(@NotNull StorageMethod currentStorageMethod) {
this.currentStorageMethod = currentStorageMethod;
}

public static void registerStorageMethod(@NotNull StorageMethod method) {
methods.put(method.getId(), method);
}

public static StorageManager createStorageManager(@NotNull StorageMethod method) {
StorageManager manager = new StorageManager();
manager.setCurrentStorageMethod(method);

StorageManager manager = new StorageManager(method);
manager.init();

return manager;
}

Expand Down
Loading

0 comments on commit de48a83

Please sign in to comment.