diff --git a/build.gradle b/build.gradle index f28b830..51e9249 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ repositories { } dependencies { + implementation 'org.junit.jupiter:junit-jupiter:5.9.0' compileOnly 'com.velocitypowered:velocity-api:3.1.0' annotationProcessor 'com.velocitypowered:velocity-api:3.1.0' } diff --git a/src/main/java/com/alttd/datalock/EventListener.java b/src/main/java/com/alttd/datalock/EventListener.java index 7b5d660..a047859 100644 --- a/src/main/java/com/alttd/datalock/EventListener.java +++ b/src/main/java/com/alttd/datalock/EventListener.java @@ -13,23 +13,25 @@ import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.server.RegisteredServer; import java.util.*; +import java.util.stream.Collectors; public class EventListener { private final HashMap> queuedLocks = new HashMap<>(); private final HashMap> channelLockMap = new HashMap<>(); - private final static List channelIdentifierList = new ArrayList<>(); + private final List channelIdentifierList = new ArrayList<>(); private static EventListener instance = null; public static EventListener getInstance() { + if (instance == null) + return new EventListener(); return instance; } public static void reload() { - if (instance == null) - instance = new EventListener(); - EventListener.channelIdentifierList.clear(); + instance = getInstance(); + instance.channelIdentifierList.clear(); ChannelRegistrar channelRegistrar = DataLock.getServer().getChannelRegistrar(); for (String s : Config.PLUGIN_MESSAGE_CHANNELS) { String[] split = s.split(":"); @@ -38,13 +40,13 @@ public class EventListener { continue; } 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); continue; } if (Config.DEBUG) Logger.info("Loaded entry [%] as [%].", s, minecraftChannelIdentifier.asKey().asString()); - EventListener.channelIdentifierList.add(minecraftChannelIdentifier); + instance.channelIdentifierList.add(minecraftChannelIdentifier); channelRegistrar.register(minecraftChannelIdentifier); } } @@ -65,14 +67,33 @@ public class EventListener { }); } + private String formatLockMap(HashMap> 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 public void onPluginMessageEvent(PluginMessageEvent event) { ChannelIdentifier identifier = event.getIdentifier(); if (Config.DEBUG) Logger.info("Received message on [%].", identifier.getId()); - if (!EventListener.channelIdentifierList.contains(identifier)) + if (!channelIdentifierList.contains(identifier)) return; + if (Config.DEBUG) + Logger.info("\nCurrent locks:\n%\nQueued locks:\n%", formatLockMap(channelLockMap), formatLockMap(queuedLocks)); + event.setResult(PluginMessageEvent.ForwardResult.handled()); if(event.getSource() instanceof Player) { diff --git a/src/test/java/com/alttd/datalock/LockTest.java b/src/test/java/com/alttd/datalock/LockTest.java new file mode 100644 index 0000000..a5234fd --- /dev/null +++ b/src/test/java/com/alttd/datalock/LockTest.java @@ -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"))); + } +} \ No newline at end of file