diff --git a/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerRegisteredEvent.java b/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerRegisteredEvent.java new file mode 100644 index 000000000..754492a60 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerRegisteredEvent.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 Velocity Contributors + * + * The Velocity API is licensed under the terms of the MIT License. For more details, + * reference the LICENSE file in the api top-level directory. + */ + +package com.velocitypowered.api.event.proxy.server; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import com.velocitypowered.api.proxy.server.ServerInfo; +import org.jetbrains.annotations.NotNull; + +/** + * This event is fired by the proxy after a backend server is registered to the server map. + * Currently, it may occur when a server is registered dynamically at runtime or when a server is + * replaced due to configuration reload. + * + * @see com.velocitypowered.api.proxy.ProxyServer#registerServer(ServerInfo) + * + * @param registeredServer A {@link RegisteredServer} that has been registered. + * @since 3.3.0 + */ +@Beta +public record ServerRegisteredEvent(@NotNull RegisteredServer registeredServer) { + public ServerRegisteredEvent { + Preconditions.checkNotNull(registeredServer, "registeredServer"); + } +} diff --git a/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerUnregisteredEvent.java b/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerUnregisteredEvent.java new file mode 100644 index 000000000..36b4023bb --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/proxy/server/ServerUnregisteredEvent.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 Velocity Contributors + * + * The Velocity API is licensed under the terms of the MIT License. For more details, + * reference the LICENSE file in the api top-level directory. + */ + +package com.velocitypowered.api.event.proxy.server; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import com.velocitypowered.api.proxy.server.ServerInfo; +import org.jetbrains.annotations.NotNull; + +/** + * This event is fired by the proxy after a backend server is unregistered from the server map. + * Currently, it may occur when a server is unregistered dynamically at runtime + * or when a server is replaced due to configuration reload. + * + * @see com.velocitypowered.api.proxy.ProxyServer#unregisterServer(ServerInfo) + * + * @param unregisteredServer A {@link RegisteredServer} that has been unregistered. + * @since 3.3.0 + */ +@Beta +public record ServerUnregisteredEvent(@NotNull RegisteredServer unregisteredServer) { + public ServerUnregisteredEvent { + Preconditions.checkNotNull(unregisteredServer, "unregisteredServer"); + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/server/ServerMap.java b/proxy/src/main/java/com/velocitypowered/proxy/server/ServerMap.java index d3fc5431f..267b78740 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/ServerMap.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/ServerMap.java @@ -19,6 +19,8 @@ package com.velocitypowered.proxy.server; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.event.proxy.server.ServerRegisteredEvent; +import com.velocitypowered.api.event.proxy.server.ServerUnregisteredEvent; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerInfo; import com.velocitypowered.proxy.VelocityServer; @@ -84,6 +86,10 @@ public class ServerMap { throw new IllegalArgumentException( "Server with name " + serverInfo.getName() + " already registered"); } else if (existing == null) { + if (server != null) { + server.getEventManager().fireAndForget(new ServerRegisteredEvent(rs)); + } + return rs; } else { return existing; @@ -107,5 +113,9 @@ public class ServerMap { "Trying to remove server %s with differing information", serverInfo.getName()); Preconditions.checkState(servers.remove(lowerName, rs), "Server with name %s replaced whilst unregistering", serverInfo.getName()); + + if (server != null) { + server.getEventManager().fireAndForget(new ServerUnregisteredEvent(rs)); + } } }