4747import java .util .concurrent .CompletableFuture ;
4848import java .util .stream .Collectors ;
4949
50+ import static valandur .webapi .util .Constants .*;
51+
5052/**
5153 * The web hook service provides access to the Web-API web hooks.
5254 */
@@ -94,18 +96,13 @@ public void init() {
9496 " Minecraft/" + mc +
9597 " Java/" + System .getProperty ("java.version" );
9698
97- // Load filters
98- logger .info ("Loading filters..." );
99-
10099 filters .clear ();
101100
102- // Add some default filters
101+ // Add filters
103102 filters .put (BlockTypeFilter .name , BlockTypeFilter .class );
104103 filters .put (PlayerFilter .name , PlayerFilter .class );
105104 filters .put (ItemTypeFilter .name , ItemTypeFilter .class );
106105
107- logger .info ("Done loading filters" );
108-
109106 // Load config
110107 Path configPath = WebAPI .getConfigPath ().resolve (configFileName ).normalize ();
111108 HookConfig config = BaseConfig .load (configPath , new HookConfig ());
@@ -115,41 +112,88 @@ public void init() {
115112 customHooks .clear ();
116113 commandHooks .clear ();
117114
115+ // Calculate max width of any hook text for printing
116+ int maxAddressLength = 0 ;
117+ for (CommandWebHook cmdHook : config .command .values ()) {
118+ maxAddressLength = Math .max (
119+ maxAddressLength ,
120+ cmdHook .getHooks ().stream ().map (h -> h .getAddress ().length ()).max (Comparator .comparingInt (a -> a )).orElse (0 )
121+ );
122+ }
123+ for (List <WebHook > hooks : config .events .asMap ().values ()) {
124+ maxAddressLength = Math .max (
125+ maxAddressLength ,
126+ hooks .stream ().map (h -> h .getAddress ().length ()).max (Comparator .comparingInt (a -> a )).orElse (0 )
127+ );
128+ }
129+ for (List <WebHook > hooks : config .custom .values ()) {
130+ maxAddressLength = Math .max (
131+ maxAddressLength ,
132+ hooks .stream ().map (h -> h .getAddress ().length ()).max (Comparator .comparingInt (a -> a )).orElse (0 )
133+ );
134+ }
135+
118136 // Add command hooks
119137 for (Map .Entry <String , CommandWebHook > entry : config .command .entrySet ()) {
120- if (!entry .getValue ().isEnabled ())
121- continue ;
122- commandHooks .put (entry .getKey (), entry .getValue ());
138+ String cmd = entry .getKey ();
139+ CommandWebHook cmdHook = entry .getValue ();
140+
141+ String separator = String .join ("" , Collections .nCopies (maxAddressLength - cmd .length () - 7 , " " ));
142+ logger .info (" Command: " + cmd + separator + " [" + (cmdHook .isEnabled () ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED" ) + ANSI_RESET + "]" );
143+ for (WebHook hook : cmdHook .getHooks ()) {
144+ separator = String .join ("" , Collections .nCopies (maxAddressLength - hook .getAddress ().length (), " " ));
145+ logger .info (" " + hook .getAddress () + separator + " [" + (hook .isEnabled () ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED" ) + ANSI_RESET + "]" );
146+ }
147+
148+ if (cmdHook .isEnabled ()) {
149+ commandHooks .put (cmd , cmdHook );
150+ }
123151 }
124152
125153 // Add event hooks
126154 for (Map .Entry <WebHookType , List <WebHook >> entry : config .events .asMap ().entrySet ()) {
127- eventHooks .put (
128- entry .getKey (),
129- entry .getValue ().stream ().filter (WebHook ::isEnabled ).collect (Collectors .toList ())
130- );
155+ WebHookType type = entry .getKey ();
156+ List <WebHook > hooks = entry .getValue ();
157+
158+ eventHooks .put (type , hooks .stream ().filter (WebHook ::isEnabled ).collect (Collectors .toList ()));
159+
160+ if (hooks .size () == 0 ) {
161+ continue ;
162+ }
163+
164+ logger .info (" Event: " + type );
165+ for (WebHook hook : hooks ) {
166+ String separator = String .join ("" , Collections .nCopies (maxAddressLength - hook .getAddress ().length (), " " ));
167+ logger .info (" " + hook .getAddress () + separator + " [" + (hook .isEnabled () ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED" ) + ANSI_RESET + "]" );
168+ }
131169 }
132170
133171 // Add custom event hooks
134172 for (Map .Entry <String , List <WebHook >> entry : config .custom .entrySet ()) {
135173 String className = entry .getKey ();
174+ List <WebHook > hooks = entry .getValue ();
136175
137176 try {
138177 Class c = Class .forName (className );
139- if (!Event .class .isAssignableFrom (c ))
178+ if (!Event .class .isAssignableFrom (c )) {
140179 throw new InvalidClassException ("Class " + c .toString () + " must be a subclass of " +
141180 Event .class .toString () + " so that it can be used as a custom web hook" );
181+ }
142182 Class <? extends Event > clazz = (Class <? extends Event >) c ;
143183
144184 WebHookEventListener listener = new WebHookEventListener (clazz );
145- List <WebHook > hooks = entry .getValue ().stream ().filter (WebHook ::isEnabled ).collect (Collectors .toList ());
146-
147185 Sponge .getEventManager ().registerListener (WebAPI .getInstance (), clazz , listener );
148- customHooks .put (clazz , new Tuple <>(hooks , listener ));
186+ customHooks .put (clazz , new Tuple <>(hooks .stream ().filter (WebHook ::isEnabled ).collect (Collectors .toList ()), listener ));
187+
188+ logger .info (" Custom Event: " + c .getName ());
189+ for (WebHook hook : hooks ) {
190+ String separator = String .join ("" , Collections .nCopies (maxAddressLength - hook .getAddress ().length (), " " ));
191+ logger .info (" " + hook .getAddress () + separator + " [" + (hook .isEnabled () ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED" ) + ANSI_RESET + "]" );
192+ }
149193 } catch (ClassNotFoundException e ) {
150- logger .error ("Could not find class for custom web hook: " + className );
194+ logger .error (" Could not find class for custom web hook: " + className );
151195 } catch (InvalidClassException e ) {
152- logger .error (e .getMessage ());
196+ logger .error (" " + e .getMessage ());
153197 }
154198 }
155199 }
@@ -166,8 +210,18 @@ public Optional<Class<? extends BaseWebHookFilter>> getFilter(String name) {
166210 public void notifyHooks (WebHookType type , Object data ) {
167211 Timings .WEBHOOK_NOTIFY .startTimingIfSync ();
168212
169- List <WebHook > notifyHooks = new ArrayList <>(eventHooks .get (type ));
170- notifyHooks .addAll (eventHooks .get (WebHookType .ALL ));
213+ List <WebHook > notifyHooks = new ArrayList <>();
214+
215+ List <WebHook > origHooks = eventHooks .get (type );
216+ if (origHooks != null ) {
217+ notifyHooks .addAll (origHooks );
218+ }
219+
220+ List <WebHook > allHooks = eventHooks .get (WebHookType .ALL );
221+ if (allHooks != null ) {
222+ notifyHooks .addAll (allHooks );
223+ }
224+
171225 for (WebHook hook : notifyHooks ) {
172226 notifyHook (hook , type , null , data );
173227 }
@@ -192,7 +246,7 @@ public void notifyHook(CommandWebHook cmdHook, String source, Map<String, Object
192246 public void notifyHooks (Class <? extends Event > clazz , Object data ) {
193247 Timings .WEBHOOK_NOTIFY .startTimingIfSync ();
194248
195- List <WebHook > notifyHooks = new ArrayList <>( customHooks .get (clazz ).getFirst () );
249+ List <WebHook > notifyHooks = customHooks .get (clazz ).getFirst ();
196250 for (WebHook hook : notifyHooks ) {
197251 notifyHook (hook , WebHookType .CUSTOM_EVENT , null , data );
198252 }
0 commit comments