22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
5- import com .rtm516 .mcxboxbroadcast .core .FriendUtils ;
65import com .rtm516 .mcxboxbroadcast .core .Logger ;
76import com .rtm516 .mcxboxbroadcast .core .SessionInfo ;
87import com .rtm516 .mcxboxbroadcast .core .SessionManager ;
1716import org .geysermc .geyser .GeyserImpl ;
1817import org .geysermc .geyser .api .command .Command ;
1918import org .geysermc .geyser .api .command .CommandSource ;
20- import org .geysermc .geyser .api .connection .GeyserConnection ;
2119import org .geysermc .geyser .api .event .lifecycle .GeyserDefineCommandsEvent ;
2220import org .geysermc .geyser .api .event .lifecycle .GeyserPostInitializeEvent ;
2321import org .geysermc .geyser .api .extension .Extension ;
@@ -45,8 +43,6 @@ public class MCXboxBroadcastExtension implements Extension {
4543 SessionManager sessionManager ;
4644 SessionInfo sessionInfo ;
4745 ExtensionConfig config ;
48- private ScheduledFuture <?> updateTimerScheduledFuture ;
49- private ScheduledFuture <?> friendTimerScheduledFuture ;
5046
5147 @ Subscribe
5248 public void onCommandDefine (GeyserDefineCommandsEvent event ) {
@@ -60,14 +56,7 @@ public void onCommandDefine(GeyserDefineCommandsEvent event) {
6056 return ;
6157 }
6258
63- sessionManager .stopSession ();
64-
65- if (updateTimerScheduledFuture != null ) {
66- updateTimerScheduledFuture .cancel (true );
67- }
68- if (friendTimerScheduledFuture != null ) {
69- friendTimerScheduledFuture .cancel (true );
70- }
59+ sessionManager .shutdown ();
7160
7261 sessionManager = new SessionManager (this .dataFolder ().toString (), logger );
7362
@@ -95,34 +84,8 @@ public void onPostInitialize(GeyserPostInitializeEvent event) {
9584 logger = new ExtensionLoggerImpl (this .logger ());
9685 sessionManager = new SessionManager (this .dataFolder ().toString (), logger );
9786
98- File configFile = this .dataFolder ().resolve ("config.yml" ).toFile ();
99-
100- // Create the config file if it doesn't exist
101- if (!configFile .exists ()) {
102- try (FileWriter writer = new FileWriter (configFile )) {
103- try (FileSystem fileSystem = FileSystems .newFileSystem (new File (MCXboxBroadcastExtension .class .getProtectionDomain ().getCodeSource ().getLocation ().toURI ()).toPath (), Collections .emptyMap ())) {
104- try (InputStream input = Files .newInputStream (fileSystem .getPath ("config.yml" ))) {
105- byte [] bytes = new byte [input .available ()];
106-
107- input .read (bytes );
108-
109- writer .write (new String (bytes ).toCharArray ());
110-
111- writer .flush ();
112- }
113- }
114- } catch (IOException | URISyntaxException e ) {
115- logger .error ("Failed to create config" , e );
116- return ;
117- }
118- }
119-
120- try {
121- config = new ObjectMapper (new YAMLFactory ()).readValue (configFile , ExtensionConfig .class );
122- } catch (IOException e ) {
123- logger .error ("Failed to load config" , e );
124- return ;
125- }
87+ // Load the config file
88+ config = ConfigLoader .load (this , MCXboxBroadcastExtension .class , ExtensionConfig .class );
12689
12790 // Pull onto another thread so we don't hang the main thread
12891 new Thread (() -> {
@@ -172,33 +135,24 @@ public void onPostInitialize(GeyserPostInitializeEvent event) {
172135 private void createSession () {
173136 // Create the Xbox session
174137 try {
175- logger .info ("Setting up Xbox session..." );
176- sessionManager .createSession (sessionInfo );
177- sessionManager .updatePresence ();
178- logger .info ("Created Xbox session!" );
138+ sessionManager .init (sessionInfo );
179139 } catch (SessionCreationException | SessionUpdateException e ) {
180140 logger .error ("Failed to create xbox session!" , e );
181141 return ;
182142 }
183143
184- // Start the update timer
185- updateTimerScheduledFuture = GeyserImpl . getInstance ().getScheduledThread (). scheduleWithFixedDelay ( this :: tick , config .updateInterval (), config . updateInterval (), TimeUnit . SECONDS ); // TODO Find API equivalent
144+ // Set up the auto friend sync
145+ sessionManager . friendManager ().initAutoFriend ( config .friendSync ());
186146
187- // Start a timer for the friend sync if enabled
188- if (config .friendSync ().autoFollow () || config .friendSync ().autoUnfollow ()) {
189- friendTimerScheduledFuture = GeyserImpl .getInstance ().getScheduledThread ().scheduleAtFixedRate (() -> FriendUtils .autoFriend (sessionManager , logger , config .friendSync ()), config .friendSync ().updateInterval (), config .friendSync ().updateInterval (), TimeUnit .SECONDS );
190- }
147+ // Start the update timer
148+ sessionManager .scheduledThread ().scheduleWithFixedDelay (this ::tick , config .updateInterval (), config .updateInterval (), TimeUnit .SECONDS );
191149 }
192150
193151 private void tick () {
194- // Make sure the connection is still active
195- sessionManager .checkConnection ();
196-
197152 // Update the player count for the session
198153 try {
199154 sessionInfo .setPlayers (this .geyserApi ().onlineConnections ().size ());
200155 sessionManager .updateSession (sessionInfo );
201- sessionManager .updatePresence ();
202156 } catch (SessionUpdateException e ) {
203157 logger .error ("Failed to update session information!" , e );
204158 }
@@ -209,7 +163,7 @@ private void tick() {
209163 && this .geyserApi ().platformType () == PlatformType .SPIGOT // TODO Find API equivalent
210164 && config .whitelistFriends ()) {
211165 try {
212- for (FollowerResponse .Person person : sessionManager .getXboxFriends ()) {
166+ for (FollowerResponse .Person person : sessionManager .friendManager (). get ()) {
213167 if (WhitelistUtils .addPlayer (Utils .getJavaUuid (person .xuid ), "unknown" )) {
214168 logger .info ("Added xbox friend " + person .displayName + " to whitelist" );
215169 }
0 commit comments