-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
241 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.tttsaurus.ingameinfo; | ||
|
||
import net.minecraftforge.common.config.Configuration; | ||
|
||
public class IgiConfig | ||
{ | ||
public static String SPOTIFY_CLIENT_ID; | ||
public static String SPOTIFY_CLIENT_SECRET; | ||
|
||
public static Configuration CONFIG; | ||
|
||
public static void loadConfig() | ||
{ | ||
try | ||
{ | ||
CONFIG.load(); | ||
|
||
SPOTIFY_CLIENT_ID = CONFIG.getString("Spotify Client Id", "spotify", "", "Input client id of your spotify app \nDeclaration: this mod doesn't record or share your client id \nand it's not recommended for you to share your client id \nGuide: you have to create a spotify app to get client id & secrete \nhttps://developer.spotify.com/documentation/web-api/concepts/apps \nRedirect URI should be set to http://localhost:8888 for this mod to listen"); | ||
SPOTIFY_CLIENT_SECRET = CONFIG.getString("Spotify Client Secret", "spotify", "", "Input client secret of your spotify app \nDeclaration: this mod doesn't record or share your client secret \nand it's not recommended for you to share your client secret"); | ||
} | ||
catch (Exception ignored) { } | ||
finally | ||
{ | ||
if (CONFIG.hasChanged()) CONFIG.save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/main/java/com/tttsaurus/ingameinfo/common/api/appcommunication/spotify/Token.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package com.tttsaurus.ingameinfo.common.api.appcommunication.spotify; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
||
public class Token | ||
{ | ||
public String accessToken; | ||
public String refreshToken; | ||
|
||
public int expiresIn; | ||
public LocalDate start; | ||
public LocalDateTime start; | ||
|
||
public Token(String accessToken, String refreshToken, int expiresIn) | ||
{ | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
this.expiresIn = expiresIn; | ||
this.start = LocalDate.now(); | ||
this.start = LocalDateTime.now(); | ||
} | ||
|
||
public Token() | ||
{ | ||
accessToken = ""; | ||
refreshToken = ""; | ||
expiresIn = 0; | ||
start = LocalDate.now(); | ||
start = LocalDateTime.now(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 92 additions & 1 deletion
93
.../java/com/tttsaurus/ingameinfo/common/impl/appcommunication/spotify/SpotifyViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,108 @@ | ||
package com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify; | ||
|
||
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyAccessUtils; | ||
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyOAuthUtils; | ||
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyUserInfo; | ||
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.TrackPlaying; | ||
import com.tttsaurus.ingameinfo.common.api.mvvm.binding.Reactive; | ||
import com.tttsaurus.ingameinfo.common.api.mvvm.binding.ReactiveObject; | ||
import com.tttsaurus.ingameinfo.common.api.mvvm.viewmodel.ViewModel; | ||
import com.tttsaurus.ingameinfo.common.impl.igievent.EventCenter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class SpotifyViewModel extends ViewModel<SpotifyView> | ||
{ | ||
@Reactive(targetUid = "albumImage", property = "url", initiativeSync = true) | ||
public ReactiveObject<String> albumImageUrl = new ReactiveObject<>(){}; | ||
|
||
@Reactive(targetUid = "trackTitle", property = "text", initiativeSync = true) | ||
public ReactiveObject<String> trackTitleText = new ReactiveObject<>(){}; | ||
|
||
@Override | ||
public void start() | ||
{ | ||
albumImageUrl.set("https://media.forgecdn.net/avatars/thumbnails/1071/348/256/256/638606872011907048.png"); | ||
activeSetter.invoke(false); | ||
EventCenter.spotifyOverlayEvent.addListener((flag) -> | ||
{ | ||
if (flag) | ||
{ | ||
activeSetter.invoke(true); | ||
trackTitleText.set("Please wait..."); | ||
CompletableFuture.supplyAsync(() -> | ||
{ | ||
try | ||
{ | ||
TrackPlaying trackPlaying = SpotifyAccessUtils.getCurrentlyPlaying(SpotifyUserInfo.token.accessToken); | ||
albumImageUrl.set(trackPlaying.albumImage300by300); | ||
trackTitleText.set(trackPlaying.trackName); | ||
} | ||
catch (Exception ignored) { } | ||
return null; | ||
}); | ||
} | ||
else | ||
activeSetter.invoke(false); | ||
}); | ||
|
||
// read refresh token and refresh | ||
File directory = new File("config/ingameinfo/cache"); | ||
if (!directory.exists()) directory.mkdirs(); | ||
try | ||
{ | ||
RandomAccessFile file = new RandomAccessFile("config/ingameinfo/cache/spotify_refresh_token.txt", "rw"); | ||
StringBuilder builder = new StringBuilder(); | ||
String line = file.readLine(); | ||
while (line != null) | ||
{ | ||
builder.append(line); | ||
line = file.readLine(); | ||
} | ||
|
||
String refreshToken = builder.toString(); | ||
if (!refreshToken.isEmpty()) | ||
{ | ||
try | ||
{ | ||
SpotifyUserInfo.token.refreshToken = refreshToken; | ||
SpotifyOAuthUtils.refreshAccessToken(SpotifyUserInfo.token); | ||
} | ||
catch (IOException e) | ||
{ | ||
file.setLength(0); | ||
} | ||
} | ||
|
||
file.close(); | ||
} | ||
catch (Exception ignored) { } | ||
} | ||
|
||
private float refreshTokenTimer = 0; | ||
@Override | ||
public void onFixedUpdate(double deltaTime) | ||
{ | ||
refreshTokenTimer += (float)deltaTime; | ||
if (refreshTokenTimer > 1f) | ||
{ | ||
refreshTokenTimer -= 1f; | ||
long timeSpan = Duration.between(SpotifyUserInfo.token.start, LocalDateTime.now()).getSeconds(); | ||
if (timeSpan >= (SpotifyUserInfo.token.expiresIn - 5)) | ||
{ | ||
CompletableFuture.supplyAsync(() -> | ||
{ | ||
try | ||
{ | ||
SpotifyOAuthUtils.refreshAccessToken(SpotifyUserInfo.token); | ||
} | ||
catch (Exception ignored) { } | ||
return null; | ||
}); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.