55 * with this project.
66 */
77
8- package com . onarandombox . MultiverseSignPortals ;
8+ package org . mvplugins . multiverse . signportals ;
99
1010import com .dumptruckman .minecraft .util .Logging ;
11- import com . onarandombox . MultiverseCore .MultiverseCore ;
12- import com . onarandombox . MultiverseCore . api .MVPlugin ;
13- import com . onarandombox . MultiverseSignPortals . listeners . MVSPBlockListener ;
14- import com . onarandombox . MultiverseSignPortals . listeners . MVSPPlayerListener ;
15- import com . onarandombox . MultiverseSignPortals . listeners . MVSPPluginListener ;
16- import com . onarandombox . MultiverseSignPortals . listeners . MVSPVersionListener ;
17- import com . onarandombox . MultiverseSignPortals . utils . PortalDetector ;
18- import org .bukkit . plugin . PluginManager ;
11+ import org . mvplugins . multiverse . core .MultiverseCore ;
12+ import org . mvplugins . multiverse . core . api .config . MVCoreConfig ;
13+ import org . mvplugins . multiverse . core . inject . PluginServiceLocator ;
14+ import org . mvplugins . multiverse . core . submodules . MVCore ;
15+ import org . mvplugins . multiverse . core . submodules . MVPlugin ;
16+ import org . mvplugins . multiverse . external . jvnet . hk2 . annotations . Service ;
17+ import org . mvplugins . multiverse . external . vavr . control . Option ;
18+ import org .mvplugins . multiverse . external . vavr . control . Try ;
1919import org .bukkit .plugin .java .JavaPlugin ;
20+ import org .mvplugins .multiverse .signportals .listeners .SignPortalsListener ;
2021
2122import java .util .logging .Level ;
2223
24+ @ Service
2325public class MultiverseSignPortals extends JavaPlugin implements MVPlugin {
2426
25- protected MultiverseCore core ;
26- protected MVSPPlayerListener playerListener ;
27- protected MVSPPluginListener pluginListener ;
28- protected MVSPBlockListener blockListener ;
29- private final static int requiresProtocol = 24 ;
27+ private MultiverseCore core ;
28+ private PluginServiceLocator serviceLocator ;
3029
31- private PortalDetector portalDetector ;
30+ private final static int requiresProtocol = 50 ;
31+
32+ /** This fires before I get Enabled. */
33+ public void onLoad () {
34+ Logging .init (this );
35+ this .getDataFolder ().mkdirs ();
36+ }
3237
3338 public void onEnable () {
3439 Logging .init (this );
@@ -50,44 +55,60 @@ public void onEnable() {
5055 return ;
5156 }
5257
53- Logging .setDebugLevel (core .getMVConfig ().getGlobalDebug ());
58+ initializeDependencyInjection ();
59+ registerEvents ();
60+ Logging .setDebugLevel (serviceLocator .getActiveService (MVCoreConfig .class ).getGlobalDebug ());
5461
5562 this .core .incrementPluginCount ();
56-
57- // Init our listeners
58- this .pluginListener = new MVSPPluginListener (this );
59- this .playerListener = new MVSPPlayerListener (this );
60- this .blockListener = new MVSPBlockListener (this );
61-
62- // Init our events
63- PluginManager pm = this .getServer ().getPluginManager ();
64- pm .registerEvents (this .pluginListener , this );
65- pm .registerEvents (this .playerListener , this );
66- pm .registerEvents (this .blockListener , this );
67- pm .registerEvents (new MVSPVersionListener (this ), this );
68-
69- this .portalDetector = new PortalDetector (this );
70-
7163 Logging .log (true , Level .INFO , " Enabled - By %s" , getAuthors ());
7264 }
7365
7466 public void onDisable () {
7567 // The Usual
68+ shutdownDependencyInjection ();
7669 Logging .info ("- Disabled" );
7770 }
7871
79- /** This fires before I get Enabled. */
80- public void onLoad () {
81- Logging .init (this );
82- this .getDataFolder ().mkdirs ();
72+ private void initializeDependencyInjection () {
73+ serviceLocator = core .getServiceLocatorFactory ()
74+ .registerPlugin (new MultiverseSignPortalsPluginBinder (this ), core .getServiceLocator ())
75+ .flatMap (PluginServiceLocator ::enable )
76+ .getOrElseThrow (exception -> {
77+ Logging .severe ("Failed to initialize dependency injection!" );
78+ getServer ().getPluginManager ().disablePlugin (this );
79+ return new RuntimeException (exception );
80+ });
81+ }
82+
83+ private void shutdownDependencyInjection () {
84+ Option .of (serviceLocator )
85+ .peek (PluginServiceLocator ::disable )
86+ .peek (ignore -> serviceLocator = null );
87+ }
88+
89+ /**
90+ * Function to Register all the Events needed.
91+ */
92+ private void registerEvents () {
93+ var pluginManager = getServer ().getPluginManager ();
94+
95+ Try .run (() -> serviceLocator .getAllServices (SignPortalsListener .class ).forEach (
96+ listener -> {
97+ Logging .info (listener .toString ());
98+ pluginManager .registerEvents (listener , this );
99+ }))
100+ .onFailure (e -> {
101+ throw new RuntimeException ("Failed to register listeners. Terminating..." , e );
102+ });
83103 }
84104
85105 /**
86106 * Parse the Authors Array into a readable String with ',' and 'and'.
87107 *
88108 * @return An comma separated string of authors
89109 */
90- private String getAuthors () {
110+ @ Override
111+ public String getAuthors () {
91112 String authors = "" ;
92113 for (int i = 0 ; i < this .getDescription ().getAuthors ().size (); i ++) {
93114 if (i == this .getDescription ().getAuthors ().size () - 1 ) {
@@ -99,55 +120,22 @@ private String getAuthors() {
99120 return authors .substring (2 );
100121 }
101122
102- /**
103- * {@inheritDoc}
104- *
105- * @deprecated This is now deprecated, nobody needs it any longer.
106- * All logging is now done with {@link Logging}.
107- */
108- @ Override
109- @ Deprecated
110- public void log (Level level , String msg ) {
111- Logging .log (level , msg );
112- }
113-
114- // No longer using, use getVersionInfo instead.
115123 @ Override
116- @ Deprecated
117- public String dumpVersionInfo (String buffer ) {
118- buffer += logAndAddToPasteBinBuffer (this .getVersionInfo ());
119- return buffer ;
124+ public PluginServiceLocator getServiceLocator () {
125+ return serviceLocator ;
120126 }
121127
122128 public String getVersionInfo () {
123129 return "[Multiverse-SignPortals] Multiverse-SignPortals Version: " + this .getDescription ().getVersion () + '\n' ;
124130 }
125131
126- // No longer using, use getVersionInfo instead.
127- @ Deprecated
128- private String logAndAddToPasteBinBuffer (String string ) {
129- Logging .info (string );
130- return Logging .getPrefixedMessage (string , false );
131- }
132-
133132 @ Override
134- public MultiverseCore getCore () {
133+ public MVCore getCore () {
135134 return this .core ;
136135 }
137136
138- @ Override
139- public void setCore (MultiverseCore core ) {
140- this .core = core ;
141- }
142-
143137 @ Override
144138 public int getProtocolVersion () {
145139 return 1 ;
146140 }
147-
148- public PortalDetector getPortalDetector () {
149- return this .portalDetector ;
150- }
151-
152-
153141}
0 commit comments