Skip to content

Commit

Permalink
Improve shutdown and update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Dec 28, 2019
1 parent ba90c22 commit 61696bf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void restartHttpServer() throws Exception {
public void shutdown() {
getLogger().info("Shutting down scheduler");
scheduler.shutdown();
try {
scheduler.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
getLogger().warn("Error while shutting down scheduler", e);
}

getLogger().info("Stopping HTTP server");
httpServer.stopSafe();
Expand Down Expand Up @@ -162,6 +167,10 @@ public HttpClient getHttpClient() {
return httpClient;
}

public HttpServer getHttpServer() {
return httpServer;
}

public Gson getGson() {
return gson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public void execute(CommandSender sender, String[] args) {
return;
}

if (port < 1 || port > 65535) {
sender.sendMessage("§cThe port must be between 1 and 65535");
return;
}

plugin.getConfig().setHttpPort(port);

plugin.getPlatform().executeAsync(() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.azuriom.azlink.common.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WebsiteResponse {

private final Map<String, String> commands = new HashMap<>();
private final Map<String, List<String>> commands = new HashMap<>();

public WebsiteResponse(Map<String, String> commands) {
public WebsiteResponse(Map<String, List<String>> commands) {
this.commands.putAll(commands);
}

public Map<String, String> getCommands() {
public Map<String, List<String>> getCommands() {
return commands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public HttpServer(AzLinkPlugin plugin) {
}

public void start() throws Exception {
plugin.getLogger().info("Stating HTTP server");

int port = plugin.getConfig().getHttpPort();

if (port < 1 || port > 65535) {
Expand All @@ -51,10 +53,6 @@ public void startSafe() {
}

public void stop() throws Exception {
if (channelFuture != null) {
channelFuture.channel().closeFuture().sync();
}

bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -17,20 +18,25 @@ public class FetcherTask implements Runnable {
private final AzLinkPlugin plugin;

private Instant lastFullDataSent = Instant.MIN;
private Instant lastRequest = Instant.MIN;

public FetcherTask(AzLinkPlugin plugin) {
this.plugin = plugin;
}

@Override
public void run() {
if (!plugin.getConfig().isValid()) {
Instant now = Instant.now();

if (!plugin.getConfig().isValid() || lastRequest.isAfter(now.minusSeconds(5))) {
return;
}

lastRequest = now;

plugin.getPlatform().executeSync(() -> {
LocalDateTime now = LocalDateTime.now();
boolean sendFullData = now.getMinute() % 15 == 0 && lastFullDataSent.isBefore(Instant.now().minusSeconds(60));
LocalDateTime currentTime = LocalDateTime.now();
boolean sendFullData = currentTime.getMinute() % 15 == 0 && lastFullDataSent.isBefore(now.minusSeconds(60));

ServerData data = plugin.getServerData(sendFullData);

Expand All @@ -46,21 +52,29 @@ private void sendData(ServerData data, boolean sendFullData) {
return;
}

plugin.getLogger().info("Dispatching " + response.getCommands() + " commands.");
plugin.getLogger().info("Dispatching commands to " + response.getCommands().size() + " players.");

Map<String, CommandSender> players = plugin.getPlatform()
.getOnlinePlayers()
.collect(Collectors.toMap(CommandSender::getName, Function.identity()));
.collect(Collectors.toMap(cs -> cs.getName().toLowerCase(), Function.identity()));

for (Map.Entry<String, List<String>> entry : response.getCommands().entrySet()) {
String playerName = entry.getKey();
List<String> commands = entry.getValue();
CommandSender player = players.get(playerName.toLowerCase());

if (player != null) {
playerName = player.getName();
}

for (Map.Entry<String, String> entry : response.getCommands().entrySet()) {
CommandSender player = players.get(entry.getKey());
String command = entry.getValue()
.replace("{name}", player.getName())
.replace("{uuid}", player.getUuid().toString());
for (String command : commands) {
command = command.replace("{name}", playerName)
.replace("{uuid}", player != null ? player.getUuid().toString() : "?");

plugin.getLogger().info("Dispatching command for player " + player.getName() + ": " + command);
plugin.getLogger().info("Dispatching command for player " + playerName + ": " + command);

plugin.getPlatform().dispatchConsoleCommand(command);
plugin.getPlatform().dispatchConsoleCommand(command);
}
}

if (sendFullData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStoppedEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.scheduler.Task;

Expand Down Expand Up @@ -61,6 +62,11 @@ public void onGamePreInitialization(GamePreInitializationEvent event) {
Task.builder().intervalTicks(1).execute(tpsTask).submit(this);
}

@Listener
public void onGameStop(GameStoppedEvent event) {
plugin.shutdown();
}

@Override
public AzLinkPlugin getPlugin() {
return plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
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.ProxyServer;
Expand Down Expand Up @@ -51,6 +52,11 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
server.getCommandManager().register(new VelocityCommandExecutor(plugin), "gazlink", "gazuriomlink");
}

@Subscribe
public void onProxyShutdown(ProxyShutdownEvent event) {
plugin.shutdown();
}

@Override
public AzLinkPlugin getPlugin() {
return plugin;
Expand Down

0 comments on commit 61696bf

Please sign in to comment.