forked from unimatrix/web
Preserve one central pack offer per proxy connection
This commit is contained in:
parent
7f19cc85ce
commit
88fb1c28bc
3 changed files with 125 additions and 1 deletions
|
|
@ -9,6 +9,11 @@ repositories {
|
|||
dependencies {
|
||||
compileOnly("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
|
||||
annotationProcessor("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
|
||||
testImplementation("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
|
||||
testImplementation("org.junit.jupiter:junit-jupiter:5.11.4")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
}
|
||||
java { toolchain.languageVersion.set(JavaLanguageVersion.of(21)) }
|
||||
tasks.withType<JavaCompile> { options.encoding = "UTF-8" }
|
||||
|
||||
tasks.test { useJUnitPlatform() }
|
||||
|
|
|
|||
|
|
@ -58,7 +58,13 @@ public final class CentralPackPlugin {
|
|||
ResourcePackInfo current=pack;
|
||||
Player player=event.getPlayer();
|
||||
if(current!=null&&player.isActive()&&offered.put(player.getUniqueId(),player)!=player){
|
||||
player.sendResourcePackOffer(current);
|
||||
try{
|
||||
player.sendResourcePackOffer(current);
|
||||
}catch(RuntimeException failure){
|
||||
// No offer was queued. Permit a later attempt on this connection.
|
||||
offered.remove(player.getUniqueId(),player);
|
||||
logger.warn("Could not queue the central resource pack offer",failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,113 @@
|
|||
package com.dunerealms.proxy;
|
||||
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||
import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent;
|
||||
import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent.Status;
|
||||
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.player.ResourcePackInfo;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CentralPackPluginTest {
|
||||
@TempDir Path directory;
|
||||
|
||||
@Test void packStatusNeverReoffersDuringTransfersButReconnectGetsOneNewOffer() throws Exception {
|
||||
var backend = proxy(RegisteredServer.class, (mock, method, args) -> null);
|
||||
for (Status[] replies : new Status[][] {
|
||||
{Status.ACCEPTED, Status.DOWNLOADED, Status.SUCCESSFUL},
|
||||
{Status.DECLINED},
|
||||
{Status.ACCEPTED, Status.FAILED_DOWNLOAD}
|
||||
}) {
|
||||
var plugin = initializedPlugin();
|
||||
var first = new Connection(UUID.randomUUID());
|
||||
dispatch(plugin, new ServerPostConnectEvent(first.player, null));
|
||||
for (Status reply : replies) {
|
||||
dispatch(plugin, new PlayerResourcePackStatusEvent(first.player, UUID.fromString("a7b5ab7d-d8d8-5b51-b016-71a5f4c00008"), reply, null));
|
||||
dispatch(plugin, new ServerPostConnectEvent(first.player, backend));
|
||||
}
|
||||
assertEquals(1, first.offers);
|
||||
dispatch(plugin, new DisconnectEvent(first.player, DisconnectEvent.LoginStatus.SUCCESSFUL_LOGIN));
|
||||
var reconnect = new Connection(first.id);
|
||||
dispatch(plugin, new ServerPostConnectEvent(reconnect.player, null));
|
||||
dispatch(plugin, new DisconnectEvent(first.player, DisconnectEvent.LoginStatus.SUCCESSFUL_LOGIN));
|
||||
dispatch(plugin, new ServerPostConnectEvent(reconnect.player, null));
|
||||
assertEquals(1, reconnect.offers);
|
||||
}
|
||||
}
|
||||
|
||||
@Test void aSynchronousQueueFailureDoesNotConsumeTheConnectionOffer() throws Exception {
|
||||
var plugin = initializedPlugin();
|
||||
var connection = new Connection(UUID.randomUUID());
|
||||
connection.failOnce = true;
|
||||
dispatch(plugin, new ServerPostConnectEvent(connection.player, null));
|
||||
assertEquals(0, connection.offers);
|
||||
dispatch(plugin, new ServerPostConnectEvent(connection.player, null));
|
||||
dispatch(plugin, new ServerPostConnectEvent(connection.player, null));
|
||||
assertEquals(2, connection.attempts);
|
||||
assertEquals(1, connection.offers);
|
||||
}
|
||||
|
||||
private CentralPackPlugin initializedPlugin() throws Exception {
|
||||
Files.writeString(directory.resolve("pack.properties"),
|
||||
"url=https://www.dunerealms.com/downloads/DuneRealms-Central-v2.zip\nsha1=" + "1".repeat(40) + "\n");
|
||||
var pack = proxy(ResourcePackInfo.class, (mock, method, args) -> null);
|
||||
var builder = proxy(ResourcePackInfo.Builder.class, (mock, method, args) ->
|
||||
method.getName().equals("build") ? pack : mock);
|
||||
var server = proxy(ProxyServer.class, (mock, method, args) -> builder);
|
||||
var log = proxy(Logger.class, (mock, method, args) -> null);
|
||||
var plugin = new CentralPackPlugin(server, log, directory);
|
||||
dispatch(plugin, new ProxyInitializeEvent());
|
||||
return plugin;
|
||||
}
|
||||
|
||||
private static void dispatch(CentralPackPlugin plugin, Object event) throws Exception {
|
||||
for (var method : CentralPackPlugin.class.getDeclaredMethods())
|
||||
if (method.isAnnotationPresent(Subscribe.class) && method.getParameterTypes()[0].isInstance(event))
|
||||
method.invoke(plugin, event);
|
||||
}
|
||||
|
||||
private static <T> T proxy(Class<T> type, InvocationHandler handler) {
|
||||
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
|
||||
}
|
||||
|
||||
private static final class Connection {
|
||||
final UUID id;
|
||||
final Player player;
|
||||
int attempts;
|
||||
int offers;
|
||||
boolean failOnce;
|
||||
|
||||
Connection(UUID id) {
|
||||
this.id = id;
|
||||
this.player = proxy(Player.class, (mock, method, args) -> switch (method.getName()) {
|
||||
case "isActive" -> true;
|
||||
case "getUniqueId" -> this.id;
|
||||
case "equals" -> mock == args[0];
|
||||
case "hashCode" -> System.identityHashCode(mock);
|
||||
case "sendResourcePackOffer" -> {
|
||||
attempts++;
|
||||
if (failOnce) {
|
||||
failOnce = false;
|
||||
throw new IllegalStateException("fixture queue failure");
|
||||
}
|
||||
offers++;
|
||||
yield null;
|
||||
}
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff