11package dev .objz .commandbridge .velocity ;
22
33import static java .util .stream .Collectors .toUnmodifiableSet ;
4-
5- import java .time .Duration ;
6- import java .util .ArrayList ;
7- import java .util .HashMap ;
8- import java .util .HashSet ;
9- import java .util .List ;
10- import java .util .Map ;
11- import java .util .Objects ;
12- import java .util .Set ;
13- import java .util .concurrent .ConcurrentHashMap ;
14-
154import com .velocitypowered .api .proxy .ProxyServer ;
16-
175import dev .objz .commandbridge .config .model .VelocityConfig ;
186import dev .objz .commandbridge .logging .Log ;
197import dev .objz .commandbridge .net .OutNode ;
2917import dev .objz .commandbridge .velocity .net .out .ctx .RegistrationRequestContext ;
3018import dev .objz .commandbridge .velocity .net .session .ClientSession ;
3119import dev .objz .commandbridge .velocity .net .session .SessionHub ;
20+ import java .time .Duration ;
21+ import java .util .ArrayList ;
22+ import java .util .HashMap ;
23+ import java .util .HashSet ;
24+ import java .util .List ;
25+ import java .util .Map ;
26+ import java .util .Objects ;
27+ import java .util .Set ;
28+ import java .util .concurrent .ConcurrentHashMap ;
3229
3330public final class RegistrationManager {
3431
@@ -40,20 +37,23 @@ public final class RegistrationManager {
4037 private final Map <String , Set <Script >> backendByClient = new ConcurrentHashMap <>();
4138 private final Set <Script > velocityScripts = ConcurrentHashMap .newKeySet ();
4239
43- public RegistrationManager (ProxyServer proxy ,
44- SessionHub sessions ,
45- VelocityConfig config ,
40+ public RegistrationManager (ProxyServer proxy , SessionHub sessions , VelocityConfig config ,
4641 OutNode <Object > outNode ) {
4742 this .sessions = Objects .requireNonNull (sessions );
4843 this .registry = new CommandRegistry (new ArgumentMapper (proxy ));
4944 this .outNode = Objects .requireNonNull (outNode );
50- this .registerTimeout = Duration .ofSeconds (
51- Objects .requireNonNull (config ).timeouts ().registerTimeout ());
45+ this .registerTimeout = Duration .ofSeconds (Objects .requireNonNull (config ).timeouts ().registerTimeout ());
5246 }
5347
5448 public void load (List <Script > scripts ) {
5549 clearState ();
5650
51+ try {
52+ registry .unregisterAll ();
53+ } catch (Exception e ) {
54+ Log .error (e , "Failed to unregister all Velocity commands before reload" );
55+ }
56+
5757 if (scripts == null || scripts .isEmpty ()) {
5858 Log .warn ("No scripts to register" );
5959 return ;
@@ -68,11 +68,8 @@ public void load(List<Script> scripts) {
6868 continue ;
6969 }
7070
71- var locations = s .register ().stream ()
72- .filter (Objects ::nonNull )
73- .map (IdMapping ::location )
74- .filter (Objects ::nonNull )
75- .collect (toUnmodifiableSet ());
71+ var locations = s .register ().stream ().filter (Objects ::nonNull ).map (IdMapping ::location )
72+ .filter (Objects ::nonNull ).collect (toUnmodifiableSet ());
7673
7774 if (locations .contains (Location .VELOCITY )) {
7875 try {
@@ -87,33 +84,35 @@ public void load(List<Script> scripts) {
8784 }
8885
8986 if (locations .contains (Location .BACKEND )) {
90- s .register ().stream ()
91- .filter (m -> m != null && m .location () == Location .BACKEND )
92- .map (IdMapping ::id )
93- .filter (Objects ::nonNull )
87+ s .register ().stream ().filter (m -> m != null && m .location () == Location .BACKEND )
88+ .map (IdMapping ::id ).filter (Objects ::nonNull )
9489 .forEach (backendId -> backendStaging
9590 .computeIfAbsent (backendId , k -> new ArrayList <>())
9691 .add (s ));
9792 }
9893 }
9994
10095 backendStaging .forEach ((backendId , list ) -> backendByClient .put (backendId , new HashSet <>(list )));
96+
10197 int ok = velocityCollector .ok ;
10298 if (ok > 0 ) {
103- Log .success (true , "Registered '{}' " + Log .plural (ok , "Velocity command" , "Velocity commands" ),
99+ Log .success (true ,
100+ "Registered '{}' " + Log .plural (ok , "Velocity command" , "Velocity commands" ),
104101 ok );
105102 }
106103 if (!velocityCollector .errors .isEmpty ()) {
107- Log .error ("Failed to register '{}' " + Log .plural (velocityCollector .errors .size (),
108- "Velocity command" , "Velocity commands" ),
104+ Log .error ("Failed to register '{}' "
105+ + Log .plural (velocityCollector .errors .size (), "Velocity command" ,
106+ "Velocity commands" ),
109107 velocityCollector .errors .size ());
110108 }
111109
112110 if (!backendByClient .isEmpty ()) {
113111 int total = backendByClient .values ().stream ().mapToInt (Set ::size ).sum ();
114112 int clients = backendByClient .size ();
115- Log .success (true , "Prepared '{}' " + Log .plural (total , "backend command" , "backend commands" )
116- + " for '{}' " + Log .plural (clients , "client" , "clients" ),
113+ Log .success (true ,
114+ "Prepared '{}' " + Log .plural (total , "backend command" , "backend commands" )
115+ + " for '{}' " + Log .plural (clients , "client" , "clients" ),
117116 total , clients );
118117 }
119118 }
@@ -130,8 +129,7 @@ public void onClientAuthenticated(ClientSession session) {
130129 return ;
131130 }
132131
133- outNode .send (
134- MessageType .REGISTER_COMMANDS ,
132+ outNode .send (MessageType .REGISTER_COMMANDS ,
135133 new RegistrationRequestContext (session , scripts , registerTimeout ));
136134 }
137135
@@ -140,8 +138,7 @@ public void reload() {
140138 if (s .status () == AuthStatus .AUTH_OK && s .ch () != null && s .ch ().isOpen ()) {
141139 var set = backendByClient .get (s .id ());
142140 if (set != null && !set .isEmpty ()) {
143- outNode .send (
144- MessageType .REGISTER_COMMANDS ,
141+ outNode .send (MessageType .REGISTER_COMMANDS ,
145142 new RegistrationRequestContext (s , set , registerTimeout ));
146143 }
147144 }
0 commit comments