Added testing/debug stuff

This commit is contained in:
Teriuihi 2022-08-22 22:15:04 +02:00
parent a88fbe139e
commit 01456bfc9f
3 changed files with 56 additions and 7 deletions

View File

@ -16,6 +16,7 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.9.0'
compileOnly 'com.velocitypowered:velocity-api:3.1.0' compileOnly 'com.velocitypowered:velocity-api:3.1.0'
annotationProcessor 'com.velocitypowered:velocity-api:3.1.0' annotationProcessor 'com.velocitypowered:velocity-api:3.1.0'
} }

View File

@ -13,23 +13,25 @@ import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class EventListener { public class EventListener {
private final HashMap<ChannelIdentifier, HashSet<Lock>> queuedLocks = new HashMap<>(); private final HashMap<ChannelIdentifier, HashSet<Lock>> queuedLocks = new HashMap<>();
private final HashMap<ChannelIdentifier, HashSet<Lock>> channelLockMap = new HashMap<>(); private final HashMap<ChannelIdentifier, HashSet<Lock>> channelLockMap = new HashMap<>();
private final static List<ChannelIdentifier> channelIdentifierList = new ArrayList<>(); private final List<ChannelIdentifier> channelIdentifierList = new ArrayList<>();
private static EventListener instance = null; private static EventListener instance = null;
public static EventListener getInstance() { public static EventListener getInstance() {
if (instance == null)
return new EventListener();
return instance; return instance;
} }
public static void reload() public static void reload()
{ {
if (instance == null) instance = getInstance();
instance = new EventListener(); instance.channelIdentifierList.clear();
EventListener.channelIdentifierList.clear();
ChannelRegistrar channelRegistrar = DataLock.getServer().getChannelRegistrar(); ChannelRegistrar channelRegistrar = DataLock.getServer().getChannelRegistrar();
for (String s : Config.PLUGIN_MESSAGE_CHANNELS) { for (String s : Config.PLUGIN_MESSAGE_CHANNELS) {
String[] split = s.split(":"); String[] split = s.split(":");
@ -38,13 +40,13 @@ public class EventListener {
continue; continue;
} }
MinecraftChannelIdentifier minecraftChannelIdentifier = MinecraftChannelIdentifier.create(split[0], split[1]); MinecraftChannelIdentifier minecraftChannelIdentifier = MinecraftChannelIdentifier.create(split[0], split[1]);
if (EventListener.channelIdentifierList.contains(minecraftChannelIdentifier)) { if (instance.channelIdentifierList.contains(minecraftChannelIdentifier)) {
Logger.warn("Duplicate message channel [%] in config.", s); Logger.warn("Duplicate message channel [%] in config.", s);
continue; continue;
} }
if (Config.DEBUG) if (Config.DEBUG)
Logger.info("Loaded entry [%] as [%].", s, minecraftChannelIdentifier.asKey().asString()); Logger.info("Loaded entry [%] as [%].", s, minecraftChannelIdentifier.asKey().asString());
EventListener.channelIdentifierList.add(minecraftChannelIdentifier); instance.channelIdentifierList.add(minecraftChannelIdentifier);
channelRegistrar.register(minecraftChannelIdentifier); channelRegistrar.register(minecraftChannelIdentifier);
} }
} }
@ -65,14 +67,33 @@ public class EventListener {
}); });
} }
private String formatLockMap(HashMap<ChannelIdentifier, HashSet<Lock>> map) {
StringBuilder stringBuilder = new StringBuilder();
for (ChannelIdentifier plugin : map.keySet()) {
stringBuilder
.append(plugin)
.append("\n")
.append(
map.get(plugin)
.stream()
.map(lock -> lock.getData() + " : " + lock.getServerHash())
.collect(Collectors.joining(", ")))
.append("\n---\n");
}
return stringBuilder.toString();
}
@Subscribe @Subscribe
public void onPluginMessageEvent(PluginMessageEvent event) { public void onPluginMessageEvent(PluginMessageEvent event) {
ChannelIdentifier identifier = event.getIdentifier(); ChannelIdentifier identifier = event.getIdentifier();
if (Config.DEBUG) if (Config.DEBUG)
Logger.info("Received message on [%].", identifier.getId()); Logger.info("Received message on [%].", identifier.getId());
if (!EventListener.channelIdentifierList.contains(identifier)) if (!channelIdentifierList.contains(identifier))
return; return;
if (Config.DEBUG)
Logger.info("\nCurrent locks:\n%\nQueued locks:\n%", formatLockMap(channelLockMap), formatLockMap(queuedLocks));
event.setResult(PluginMessageEvent.ForwardResult.handled()); event.setResult(PluginMessageEvent.ForwardResult.handled());
if(event.getSource() instanceof Player) { if(event.getSource() instanceof Player) {

View File

@ -0,0 +1,27 @@
package com.alttd.datalock;
import static org.junit.jupiter.api.Assertions.*;
class LockTest {
@org.junit.jupiter.api.Test
void testEquals() {
assertEquals(new Lock(123, "test"), new Lock(123, "test"));
assertNotEquals(new Lock(123, "test1"), new Lock(123, "test2"));
assertEquals(new Lock(123, "test"), new Lock(-123, "test"));
}
@org.junit.jupiter.api.Test
void testHashCode() {
assertEquals(new Lock(123, "test").hashCode(), new Lock(123, "test").hashCode());
assertNotEquals(new Lock(123, "test1").hashCode(), new Lock(123, "test2").hashCode());
assertEquals(new Lock(123, "test").hashCode(), new Lock(-123, "test").hashCode());
}
@org.junit.jupiter.api.Test
void compareTo() {
assertEquals(0, new Lock(123, "test").compareTo(new Lock(123, "test")));
assertNotEquals(0, new Lock(123, "test1").compareTo(new Lock(123, "test")));
assertNotEquals(0, new Lock(123, "test").compareTo(new Lock(-123, "test")));
}
}