Jump to content

Recommended Posts

Posted

I am trying to create a HUD "Now Playing" element that displays the current song, artist, album, and corresponding album art live based on what the user is listening to using the Spotify API. Sort of like a widget, for now, that appears on the top left corner of the user's screen. Here's a visual representation of where I am currently (see top left).

In that square-like space to the left of the song details, I want to squeeze in the Album Art. I have an object that holds the image URL of the currently playing album, so I figured I would attempt something like this:

	private static String nowPlayingAlbum = spoticraftInstance.nowPlaying.item.album.images.get(1).url;
	
	private static ResourceLocation COVER_ART = new ResourceLocation(SpoticraftMod.MODID, nowPlayingAlbum == null ? "" : nowPlayingAlbum);

FYI, spoticraftInstance is a direct Gson mapping of the JSON response from Spotify's Get Currently Playing Track API. This updates every second on a separate thread so spoticraftInstance always contains the latest song information.

However upon launching the game it crashes with the following message:

Caused by: net.minecraft.ResourceLocationException: Non [a-z0-9/._-] character in path of location: spoticraft:https://i.scdn.co/image/ab67616d00001e028dc0d801766a5aa6a33cbe37

This was a surprise, so I am trying to think of how to get around this properly. Should I download the image first, display it, then delete it after that song is no longer playing? Or is there a better way I am unaware of that will allow me to draw the image live as the URL changes? Here is the entire declaration of my HUD element for reference as of now:

import static com.example.spoticraft.SpoticraftCommon.literalOrEllipse;
import static com.example.spoticraft.SpoticraftMod.spoticraftInstance;

public class SpoticraftDrawing {
	private static String nowPlayingAlbum = spoticraftInstance.nowPlaying.item.album.images.get(1).url;
	
	private static ResourceLocation COVER_ART = new ResourceLocation(SpoticraftMod.MODID, nowPlayingAlbum == null ? "" : nowPlayingAlbum);
	private static final ResourceLocation BACKGROUND = new ResourceLocation(SpoticraftMod.MODID, "textures/background.png");
	
	public static final IGuiOverlay SPOTIFCRAFT_OVERLAY = ((gui, poseStack, partialTick, width, height) -> {
		int x = 5;
		int y = 5;
		int backgroundWidth = 200;
		int backgroundHeight = 50;
		
		RenderSystem.setShader(GameRenderer::getPositionTexShader);
		RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
		RenderSystem.setShaderTexture(0, BACKGROUND);
		NowPlaying currentSong = spoticraftInstance.nowPlaying;
		Font font = gui.getFont();
		
		GuiComponent.blit(poseStack, x, y, 0, 0, backgroundWidth, backgroundHeight, 0, 0);
		
		int textX = 50;
		
		if (currentSong != null) {
			RenderSystem.setShaderTexture(0, COVER_ART);
			GuiComponent.blit(poseStack, x + 5, 5, 0, 0, 50, 50, 0, 0);
			GuiComponent.drawString(poseStack, font, literalOrEllipse("§f§l" + currentSong.item.name), textX, 12, 0);
			GuiComponent.drawString(poseStack, font, literalOrEllipse("§e" + currentSong.item.artists.get(0).name), textX, 25, 0);
			GuiComponent.drawString(poseStack, font, literalOrEllipse("§6§o" + currentSong.item.album.name), textX, 38, 0);
		} else {
			if (SpoticraftMod.spoticraftInstance.getRefreshToken() == null) {
				GuiComponent.drawString(poseStack, font, "§f/spoticraft init to see your music", 20, 12, 0);				
			} else {
				GuiComponent.drawString(poseStack, font, "§6§oNothing Playing", 20, 12, 0);
			}
		}
	});
}

Thank you!

Posted

As it says, that is not a valid ResourceLocation. A ResourceLocation is not a URL.

For textures it represents a binding in the TextureManager that is typically populated during resource pack loading.

If you want something more dynamic try looking at what the SkinManager does, it downloads player skins from Mojang's website.

You can also use search where you will find variations of your question asked many times (along with many misunderstandings like yours).

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted

Ok, so I have successfully created a Java class that saves the latest user's Spotify Session's album to a file named album_art.png. Here is where I am at so far:

public class SpoticraftArtDownloader {
	public static String imageURL = null;
	
	private static void downloadImage() throws Exception {
		if (imageURL == null) return;
		URL url = new URL(imageURL);
		InputStream in = new BufferedInputStream(url.openStream());
		OutputStream out = new BufferedOutputStream(new FileOutputStream("./album_art.png"));

		for ( int i; (i = in.read()) != -1; ) {
		    out.write(i);
		}
		in.close();
		out.close();
	}
	
	private static void deleteImage() {
		File file = new File("./album_art.png");
		if (file.exists()) file.delete();
	}
	
	public static void rotateImage(String url) throws Exception {
		deleteImage();
		imageURL = url;
		downloadImage();
	}
}

However, this is saving the image into the run directory, which from my understanding is inaccessible from the mod, with something like

ResourceLocation nowPlayingArt = new ResourceLocation(SpoticraftMod.MODID, "textures/album_art.png");
RenderSystem.setShaderTexture(0, nowPlayingArt);

How can I point SpoticraftArtDownloader to download the images into the textures folder?

Posted
On 4/20/2023 at 10:56 AM, warjort said:

... A ResourceLocation is not a URL.

For textures it represents a binding in the TextureManager ...

... try looking at what the SkinManager does,

 

i.e. its use of skinsDirectory and TextureManager.register()

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.