Skip to content

Commit

Permalink
v0.9.2.1
Browse files Browse the repository at this point in the history
- The API clients now have a configurable thread count for the HTTP CLients so the Plugin doesnt create them into the infinite
- configmanager now keeps previous made changes from the config while adding new things
- description and url added to the Plugin meta data
- added shutdown() to the API Clients to proberly shutdown the HTTP clients on Proxy shutdown
- added shutdown message on proxy shutdown
- version bump

Took 1 hour 16 minutes
  • Loading branch information
TubYoub committed Oct 4, 2024
1 parent 2e875a3 commit 0fcde5a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tubyoub</groupId>
<artifactId>VelocityPteroPower</artifactId>
<version>0.9.2</version>
<version>0.9.2.1</version>
<packaging>jar</packaging>

<name>VelocityPteroPower</name>
Expand All @@ -18,7 +18,7 @@

<profiles>
<profile>
<id>beta</id>
<id>alpha</id>
<build>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings;
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import dev.dejvokep.boostedyaml.settings.updater.MergeRule;
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
import org.slf4j.Logger;

Expand All @@ -55,6 +56,7 @@ public class ConfigurationManager {
private PanelType panel;
private boolean checkUpdate;
private int startupJoinDelay;
private int apiThreads;
private final VelocityPteroPower plugin;
private final Logger logger;
private Map<String, PteroServerInfo> serverInfoMap;
Expand Down Expand Up @@ -82,11 +84,16 @@ public void loadConfig(){
LoaderSettings.builder().setAutoUpdate(true).build(),
DumperSettings.DEFAULT,
UpdaterSettings.builder().setVersioning(new BasicVersioning("fileversion"))
.setOptionSorting(UpdaterSettings.OptionSorting.SORT_BY_DEFAULTS).build());
.setOptionSorting(UpdaterSettings.OptionSorting.SORT_BY_DEFAULTS)
.setMergeRule(MergeRule.MAPPINGS, true)
.setMergeRule(MergeRule.MAPPING_AT_SECTION, true)
.setMergeRule(MergeRule.SECTION_AT_MAPPING, true)
.setKeepAll(true)
.build());


checkUpdate = (boolean) config.get("checkUpdate");

apiThreads = (int) config.get("apiThreads", 10);
Section startupJoinSection = config.getSection("startupJoin");
Map<String, Object> startupJoin = new HashMap<>();
if (startupJoinSection != null) {
Expand Down Expand Up @@ -226,4 +233,8 @@ public int getStartupJoinDelay() {
public PanelType getPanelType(){
return panel;
}

public int getApiThreads() {
return apiThreads;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
Expand Down Expand Up @@ -56,9 +57,9 @@
* Main class for the VelocityPteroPower plugin.
* This class handles the initialization of the plugin and the registration of commands and events.
*/
@Plugin(id = "velocity-ptero-power", name = "VelocityPteroPower", version = "0.9", authors = {"TubYoub"})
@Plugin(id = "velocity-ptero-power", name = "VelocityPteroPower", version = "0.9.2.1", authors = {"TubYoub"}, description = "A plugin for Velocity that allows you to manage your Pterodactyl/Pelican servers from the Velocity console.", url = "https://github.com/TubYoub/VelocityPteroPower")
public class VelocityPteroPower {
private final String version = "0.9.2";
private final String version = "0.9.2.1";
private final int pluginId = 21465;
private final ProxyServer proxyServer;
private final ComponentLogger logger;
Expand Down Expand Up @@ -306,4 +307,8 @@ public PanelAPIClient getAPIClient() {
public ConfigurationManager getConfigurationManager() {
return configurationManager;
}
public void onProxyShutdown(ProxyShutdownEvent event) {
apiClient.shutdown();
logger.info("Shutting down VelocityPteroPower... Goodbye");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public interface PanelAPIClient {
void powerServer(String serverId, String signal);
boolean isServerOnline(String serverId);
boolean isServerEmpty(String serverName);
void shutdown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class PelicanAPIClient implements PanelAPIClient {
public final Logger logger;
public final ConfigurationManager configurationManager;
public final ProxyServer proxyServer;
private final ObjectMapper objectMapper = new ObjectMapper();

private final HttpClient httpClient;
private final ExecutorService executorService;

public PelicanAPIClient(VelocityPteroPower plugin) {
this.logger = plugin.getLogger();
this.configurationManager = plugin.getConfigurationManager();
this.proxyServer = plugin.getProxyServer();

this.executorService = Executors.newFixedThreadPool(10); // Limit to 10 threads
this.httpClient = HttpClient.newBuilder()
.executor(executorService)
.build();
}

@Override
Expand Down Expand Up @@ -72,4 +83,8 @@ public boolean isServerEmpty(String serverName) {
Optional<RegisteredServer> server = proxyServer.getServer(serverName);
return server.map(value -> value.getPlayersConnected().isEmpty()).orElse(true);
}

public void shutdown() {
executorService.shutdownNow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -53,6 +55,10 @@ public class PterodactylAPIClient implements PanelAPIClient{
public final ConfigurationManager configurationManager;
public final ProxyServer proxyServer;

private final HttpClient httpClient;
private final ExecutorService executorService;


/**
* Constructor for the PterodactylAPIClient class.
* It initializes the logger, configuration manager, and proxy server from the provided plugin instance.
Expand All @@ -64,6 +70,11 @@ public PterodactylAPIClient(VelocityPteroPower plugin){
this.logger = plugin.getLogger();
this.configurationManager = plugin.getConfigurationManager();
this.proxyServer = plugin.getProxyServer();

this.executorService = Executors.newFixedThreadPool(configurationManager.getApiThreads());
this.httpClient = HttpClient.newBuilder()
.executor(executorService)
.build();
}

/**
Expand All @@ -75,16 +86,15 @@ public PterodactylAPIClient(VelocityPteroPower plugin){
@Override
public void powerServer(String serverId, String signal) {
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(configurationManager.getPterodactylUrl() + "api/client/servers/" + serverId + "/power"))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + configurationManager.getPterodactylApiKey())
.POST(HttpRequest.BodyPublishers.ofString("{\"signal\": \"" + signal + "\"}"))
.build();
.uri(URI.create(configurationManager.getPterodactylUrl() + "api/client/servers/" + serverId + "/power"))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + configurationManager.getPterodactylApiKey())
.POST(HttpRequest.BodyPublishers.ofString("{\"signal\": \"" + signal + "\"}"))
.build();

client.send(request, HttpResponse.BodyHandlers.ofString());
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (Exception e) {
logger.error("Error powering server.", e);
}
Expand All @@ -100,29 +110,25 @@ public void powerServer(String serverId, String signal) {
@Override
public boolean isServerOnline(String serverId) {
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(configurationManager.getPterodactylUrl() + "api/client/servers/" + serverId + "/resources"))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + configurationManager.getPterodactylApiKey())
.GET()
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody = response.body();
if (response.statusCode() == 200) {
if (responseBody.contains("{\"object\":\"stats\",\"attributes\":{\"current_state\":\"running\"")) {
return true;
}
if (response.statusCode() == 200) {
return responseBody.contains("{\"object\":\"stats\",\"attributes\":{\"current_state\":\"running\"");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}

/**
Expand All @@ -136,4 +142,8 @@ public boolean isServerEmpty(String serverName) {
Optional<RegisteredServer> server = proxyServer.getServer(serverName);
return server.map(value -> value.getPlayersConnected().isEmpty()).orElse(true);
}

public void shutdown() {
executorService.shutdownNow();
}
}
9 changes: 8 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
################################

# Version of the configuration file
fileversion: 2
fileversion: 3

# Check for updates
# If true, the plugin will check for updates on startup.
# If a new version is available, a message will be sent to the console and to players with the permission "ptero.reload".
checkUpdate: true

# How many Threads should be used to check the server status
# The more threads the more requests can be handled at the same time
# more threads = more resources used
# restart the server so changes take effect
# default: 10
apiThreads: 10

# This is used to check the server status to transfer players after the server starts
startupJoin:
# Once the server is pingable, wait the specified amount of seconds before sending the player to the server
Expand Down

0 comments on commit 0fcde5a

Please sign in to comment.