Added debug messages

Added a way to clear all locks on a server when it was empty when a user joins
This commit is contained in:
Teriuihi 2022-05-15 22:38:37 +02:00
parent 780249933c
commit a88fbe139e
4 changed files with 61 additions and 2 deletions

View File

@ -30,7 +30,7 @@ public final class Config {
public static File CONFIG_PATH;
public static void init() { // todo setup share for the config
CONFIG_PATH = new File(DataLock.getDataDirectory().toAbsolutePath() + File.separator + "DataLock");
CONFIG_PATH = new File(DataLock.getDataDirectory().toAbsolutePath().toString());
File configFile = new File(CONFIG_PATH, "config.yml");
configLoader = YAMLConfigurationLoader.builder()
@ -152,8 +152,12 @@ public final class Config {
* ONLY EDIT ANYTHING BELOW THIS LINE
**/
public static List<String> PLUGIN_MESSAGE_CHANNELS = new ArrayList<>(List.of("example_plugin:table_1"));
public static boolean DEBUG = false;
private static void loadGroups() {
private static void loadSettings() {
PLUGIN_MESSAGE_CHANNELS = getList("settings.channels", new ArrayList<>(List.of("example_plugin:table_1")));
DEBUG = getBoolean("settings.debug", DEBUG);
if (DEBUG)
Logger.info("DEBUG: on");
}
}

View File

@ -37,6 +37,7 @@ public class DataLock {
public void onProxyInitialization(ProxyInitializeEvent event) {
reloadConfig();
server.getEventManager().register(this, EventListener.getInstance());
server.getEventManager().register(this, new PlayerListener());
new Reload(server);
}

View File

@ -8,6 +8,7 @@ import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.ChannelRegistrar;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer;
@ -29,6 +30,7 @@ public class EventListener {
if (instance == null)
instance = new EventListener();
EventListener.channelIdentifierList.clear();
ChannelRegistrar channelRegistrar = DataLock.getServer().getChannelRegistrar();
for (String s : Config.PLUGIN_MESSAGE_CHANNELS) {
String[] split = s.split(":");
if (split.length != 2) {
@ -40,13 +42,34 @@ public class EventListener {
Logger.warn("Duplicate message channel [%] in config.", s);
continue;
}
if (Config.DEBUG)
Logger.info("Loaded entry [%] as [%].", s, minecraftChannelIdentifier.asKey().asString());
EventListener.channelIdentifierList.add(minecraftChannelIdentifier);
channelRegistrar.register(minecraftChannelIdentifier);
}
}
public void clearServer(int hashCode) {
channelLockMap.forEach((identifier, value) -> {
HashSet<Lock> temp = new HashSet<>();
for (Lock lock : value) {
if (lock.getServerHash() == hashCode)
temp.add(lock);
}
for (Lock lock : temp) {
value.remove(lock);
queueNextLock(value, lock, identifier);
if (Config.DEBUG)
Logger.info("Clearing % from % due to clear server being called for the server that lock is on", lock.getData(), identifier.getId());
}
});
}
@Subscribe
public void onPluginMessageEvent(PluginMessageEvent event) {
ChannelIdentifier identifier = event.getIdentifier();
if (Config.DEBUG)
Logger.info("Received message on [%].", identifier.getId());
if (!EventListener.channelIdentifierList.contains(identifier))
return;
@ -81,6 +104,9 @@ public class EventListener {
return;
}
if (Config.DEBUG)
Logger.info("Plugin message channel: [%]", channel.toLowerCase());
switch (channel.toLowerCase()) {
case "try-lock" -> tryLock(identifier, hashLock, data, serverConnection);
case "check-lock" -> checkLock(identifier, hashLock, data, serverConnection);

View File

@ -0,0 +1,28 @@
package com.alttd.datalock;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import java.util.Optional;
public class PlayerListener {
@Subscribe
void onPlayerConnect(ServerConnectedEvent event) {
Player player = event.getPlayer();
ServerInfo serverInfo = event.getServer().getServerInfo();
if (event.getServer().getPlayersConnected().stream().filter(p -> p.equals(player)).findAny().isEmpty())
EventListener.getInstance().clearServer(serverInfo.hashCode());
Optional<RegisteredServer> previousServer = event.getPreviousServer();
if (previousServer.isEmpty())
return;
serverInfo = previousServer.get().getServerInfo();
if (event.getServer().getPlayersConnected().stream().filter(p -> p.equals(player)).findAny().isEmpty())
EventListener.getInstance().clearServer(serverInfo.hashCode());
}
}