Skip to content

Commit

Permalink
save track by button
Browse files Browse the repository at this point in the history
* L by default
  • Loading branch information
erruqie committed Nov 20, 2024
1 parent cb6065b commit 7a2e66e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/one/clownless/blockify/BlockifyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BlockifyMain implements ModInitializer
private static KeyBinding nextKey;
private static KeyBinding prevKey;
private static KeyBinding forceKey;
private static KeyBinding addTrackKey;
private static KeyBinding hideKey;
private static KeyBinding increaseVolumeKey;
private static KeyBinding decreaseVolumeKey;
Expand All @@ -34,6 +35,7 @@ public class BlockifyMain implements ModInitializer
private boolean nextKeyPrevState = false;
private boolean prevKeyPrevState = false;
private boolean forceKeyPrevState = false;
private boolean addTrackKeyPrevState = false;
private boolean hideKeyPrevState = false;
private boolean increaseVolumeKeyPrevState = false;
private boolean decreaseVolumeKeyPrevState = false;
Expand Down Expand Up @@ -110,6 +112,15 @@ public void run()
)
);

addTrackKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"blockify.key.addTrack",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_L,
"Blockify"
)
);

forceKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"blockify.key.force",
Expand Down Expand Up @@ -164,6 +175,7 @@ public void run()
nextKeyHandler(nextKey.isPressed());
prevKeyHandler(prevKey.isPressed());
forceKeyHandler(forceKey.isPressed());
addTrackKeyHandler(addTrackKey.isPressed());
hideKeyHandler(hideKey.isPressed());
increaseVolumeKeyHandler(increaseVolumeKey.isPressed());
decreaseVolumeKeyHandler(decreaseVolumeKey.isPressed());
Expand Down Expand Up @@ -230,6 +242,16 @@ public void prevKeyHandler(boolean currPressState)
prevKeyPrevState = currPressState;
}

public void addTrackKeyHandler(boolean currPressState)
{
if (currPressState && !addTrackKeyPrevState)
{
LOGGER.info("Add Track Key Pressed");
SpotifyUtil.addTrack();
}
addTrackKeyPrevState = currPressState;
}

public void forceKeyHandler(boolean currPressState)
{
if (currPressState && !forceKeyPrevState)
Expand Down
64 changes: 62 additions & 2 deletions src/main/java/one/clownless/blockify/util/SpotifyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static String authorize()
authURI.append("&response_type=code");
authURI.append("&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fcallback");
authURI.append("&scope=user-read-playback-state%20user-read-currently-playing");
authURI.append("%20user-modify-playback-state");
authURI.append("%20user-modify-playback-state%20user-library-modify");
authURI.append("&code_challenge_method=S256");
verifier = PKCEUtil.generateCodeVerifier();
challenge = PKCEUtil.generateCodeChallenge(verifier);
Expand Down Expand Up @@ -243,6 +243,57 @@ public static void refreshActiveSession()
LOGGER.info("Successfully refreshed active session");
}

public static void addTrack(String track_id)
{
try
{
HttpRequest putReq = HttpRequest.newBuilder(new URI("https://api.spotify.com/v1/me/tracks?ids=" + track_id))
.PUT(HttpRequest.BodyPublishers.ofString(""))
.header("Authorization", "Bearer " + accessToken).build();
HttpResponse<String> putRes = client.send(putReq, HttpResponse.BodyHandlers.ofString());
LOGGER.info("Add Track Request (" + track_id + "): " + putRes.statusCode());
if (putRes.statusCode() == 200)
{
MinecraftClient.getInstance().player.sendMessage(Text.of("Track Saved!"), true);
}
else if (putRes.statusCode() == 404)
{
refreshActiveSession();
LOGGER.info("Retrying put request...");
putRes = client.send(putReq, HttpResponse.BodyHandlers.ofString());
LOGGER.info("Add Track Request (" + track_id + "): " + putRes.statusCode());
}
else if (putRes.statusCode() == 403)
{
MinecraftClient.getInstance().player.sendMessage(Text.of("Spotify Premium is required for this feature."), false);
}
else if (putRes.statusCode() == 401)
{
if (refreshAccessToken())
{
addTrack(track_id);
}
else
{
isAuthorized = false;
}
}
} catch (IOException | InterruptedException | URISyntaxException e)
{
if (e instanceof IOException && e.getMessage().equals("Connection reset"))
{
LOGGER.info("Attempting to retry put request...");
addTrack(track_id);
LOGGER.info("Successfully sent put request");
}
else
{
LOGGER.error(e.getMessage());
}
}

}

public static void putRequest(String type)
{
try
Expand Down Expand Up @@ -344,6 +395,13 @@ public static void nextSong()
});
}

public static void addTrack()
{
EXECUTOR_SERVICE.execute(() -> {
addTrack(getPlaybackInfo()[7]);
});
}

public static void prevSong()
{
EXECUTOR_SERVICE.execute(() -> {
Expand Down Expand Up @@ -382,7 +440,7 @@ public static void playPause()

public static String[] getPlaybackInfo()
{
String[] results = new String[7];
String[] results = new String[8];
try
{
playbackResponse = client.send(playbackRequest, HttpResponse.BodyHandlers.ofString());
Expand All @@ -401,6 +459,7 @@ public static String[] getPlaybackInfo()
results[2] = json.get("progress_ms").getAsString();
results[3] = json.get("item").getAsJsonObject().get("duration_ms").getAsString();
results[4] = json.get("item").getAsJsonObject().get("images").getAsJsonArray().get(1).getAsJsonObject().get("url").getAsString();
results[5] = json.get("item").getAsJsonObject().get("id").getAsString();
return results;
}
results[0] = json.get("item").getAsJsonObject().get("name").getAsString();
Expand Down Expand Up @@ -433,6 +492,7 @@ public static String[] getPlaybackInfo()
}
results[5] = json.get("item").getAsJsonObject().get("external_urls").getAsJsonObject().get("spotify").getAsString();
results[6] = json.get("device").getAsJsonObject().get("volume_percent").getAsString();
results[7] = json.get("item").getAsJsonObject().get("id").getAsString();
isPlaying = json.get("is_playing").getAsBoolean();
}
else if (playbackResponse.statusCode() == 401)
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/blockify/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"blockify.key.next": "Next Song",
"blockify.key.prev": "Previous Song",
"blockify.key.force": "Force Update",
"blockify.key.addTrack": "Save track",
"blockify.key.hide": "Hide Spotify",
"blockify.key.increaseVolume": "Increase Volume",
"blockify.key.decreaseVolume": "Decrease Volume",
Expand Down

0 comments on commit 7a2e66e

Please # to comment.