Skip to content

Commit

Permalink
Started working on spotify compat
Browse files Browse the repository at this point in the history
  • Loading branch information
tttsaurus committed Jan 6, 2025
1 parent 7907bde commit 5a0b7fd
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.tttsaurus.ingameinfo.common.api.internal.InternalMethods;
import com.tttsaurus.ingameinfo.common.impl.gui.layout.MainGroup;
import com.tttsaurus.ingameinfo.common.impl.serialization.GuiLayoutDeserializer;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -37,6 +39,8 @@ private List<Element> getElements(ElementGroup group, String uid)
return list;
}

public String getDefaultIxml() { return ""; }

// searching the file under ./config/ingameinfo/
// and .ixml is the suffix
public abstract String getIxmlFileName();
Expand All @@ -45,15 +49,29 @@ private GuiLayout init()
{
try
{
RandomAccessFile file = new RandomAccessFile("config/ingameinfo/" + getIxmlFileName() + ".ixml", "rw");
File directory = new File("config/ingameinfo");
if (!directory.exists()) directory.mkdirs();

String filePath = "config/ingameinfo/" + getIxmlFileName() + ".ixml";
File testFile = new File(filePath);
boolean writeDefault = !testFile.exists();

RandomAccessFile file = new RandomAccessFile(filePath, "rw");
StringBuilder builder = new StringBuilder();

String line = file.readLine();
while (line != null)
if (writeDefault)
{
file.write(getDefaultIxml().getBytes(StandardCharsets.UTF_8));
builder.append(getDefaultIxml().replace("\n", ""));
}
else
{
builder.append(line);
line = file.readLine();
String line = file.readLine();
while (line != null)
{
builder.append(line);
line = file.readLine();
}
}

GuiLayoutDeserializer deserializer = new GuiLayoutDeserializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class Texture2D
{
private int glTextureID;
private int glTextureID = 0;
private final int width;
private final int height;
private boolean isGlBound;
Expand Down Expand Up @@ -35,7 +35,7 @@ public Texture2D(int width, int height, ByteBuffer byteBuffer)

public void dispose()
{
GL11.glDeleteTextures(glTextureID);
if (glTextureID != 0) GL11.glDeleteTextures(glTextureID);
glTextureID = 0;
isGlBound = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public static List<Tuple<String, String>> splitParams(String param)
value = param.substring(endIndex + 1, endIndex2);
endIndex2++;
}
else if (c == '{')
{
int braceCount = 1;
endIndex2 += 1;

while (endIndex2 < param.length() && braceCount > 0)
{
c = param.charAt(endIndex2);
if (c == '{')
braceCount++;
else if (c == '}')
braceCount--;
endIndex2++;
}

value = param.substring(endIndex, endIndex2);
}
else
{
while (endIndex2 + 1 < param.length() && (c != ' '))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyOAuthUtils;
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.TrackPlaying;
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyUserInfo;
import com.tttsaurus.ingameinfo.common.impl.render.renderer.URLImageRenderer;
import com.tttsaurus.ingameinfo.common.impl.render.renderer.UrlImageRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.util.text.Style;
Expand All @@ -14,7 +14,7 @@
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public final class InGameCommandHandler
public final class SpotifyCommandHandler
{
@SubscribeEvent
public static void onChatReceived(ClientChatReceivedEvent event)
Expand Down Expand Up @@ -85,7 +85,7 @@ else if (message.startsWith("#spotify-ui-display"))
TrackPlaying trackPlaying = SpotifyAccessUtils.getCurrentlyPlaying(SpotifyUserInfo.token.accessToken);
player.sendMessage(new TextComponentString(TextFormatting.AQUA + "[SpotifyBot]" + TextFormatting.RESET + " " + trackPlaying.trackName));
trackPlaying.artists.forEach(str -> player.sendMessage(new TextComponentString(TextFormatting.AQUA + "[SpotifyBot]" + TextFormatting.RESET + " " + str)));
URLImageRenderer.SHARED.updateURL(trackPlaying.albumImage640by640);
UrlImageRenderer.SHARED.updateURL(trackPlaying.albumImage640by640);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify;

import com.tttsaurus.ingameinfo.common.api.mvvm.view.View;

public class SpotifyView extends View
{
@Override
public String getDefaultIxml()
{
return
"""
<ImageUrl uid = "albumImage" width = 20 height = 20>
""";
}

@Override
public String getIxmlFileName()
{
return "spotify";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify;

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;

public class SpotifyViewModel extends ViewModel<SpotifyView>
{
@Reactive(targetUid = "albumImage", property = "url", initiativeSync = true)
public ReactiveObject<String> albumImageUrl = new ReactiveObject<>(){};

@Override
public void start()
{
albumImageUrl.set("https://media.forgecdn.net/avatars/thumbnails/1071/348/256/256/638606872011907048.png");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.tttsaurus.ingameinfo.common.api.gui.registry.RegisterElement;
import com.tttsaurus.ingameinfo.common.api.gui.style.StyleProperty;

// for those who don't implicitly have a size
@RegisterElement(constructable = false)
public abstract class Sized extends Element
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.tttsaurus.ingameinfo.common.impl.gui.control;

import com.tttsaurus.ingameinfo.common.api.gui.layout.Rect;
import com.tttsaurus.ingameinfo.common.api.gui.registry.RegisterElement;
import com.tttsaurus.ingameinfo.common.api.gui.style.StyleProperty;
import com.tttsaurus.ingameinfo.common.api.gui.style.StylePropertyCallback;
import com.tttsaurus.ingameinfo.common.impl.render.renderer.UrlImageRenderer;

@RegisterElement
public class UrlImage extends Sized
{
private final UrlImageRenderer urlImageRenderer = new UrlImageRenderer();

@StylePropertyCallback
public void setUrlCallback()
{
urlImageRenderer.updateURL(url);
}
@StyleProperty(setterCallbackPost = "setUrlCallback")
public String url;

@Override
public void calcRenderPos(Rect contextRect)
{
super.calcRenderPos(contextRect);
urlImageRenderer.setX(rect.x);
urlImageRenderer.setY(rect.y);
}

@Override
public void calcWidthHeight()
{
super.calcWidthHeight();
urlImageRenderer.setWidth(width);
urlImageRenderer.setHeight(height);
}

@Override
public void onFixedUpdate(double deltaTime)
{

}

@Override
public void onRenderUpdate(boolean focused)
{
urlImageRenderer.render();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tttsaurus.ingameinfo.common.impl.mvvm.registry;

import com.tttsaurus.ingameinfo.common.api.event.MvvmRegisterEvent;
import com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify.SpotifyViewModel;
import com.tttsaurus.ingameinfo.plugin.crt.impl.CrtMvvm;
import com.tttsaurus.ingameinfo.plugin.crt.impl.CrtViewModel;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
Expand All @@ -17,5 +18,7 @@ public static void onMvvmRegister(MvvmRegisterEvent event)
crtViewModel.runtimeMvvm = mvvm;
MvvmRegistry.setIgiGuiContainer(mvvm, crtViewModel);
}

MvvmRegistry.autoRegister("spotify", SpotifyViewModel.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ImageRenderer implements IRenderer
protected float y = 0;
protected float width = 0;
protected float height = 0;
protected float scale = 1f;

//<editor-fold desc="getters & setters">
public Texture2D getTexture() { return texture; }
Expand All @@ -36,15 +35,9 @@ public class ImageRenderer implements IRenderer

public float getHeight() { return height; }
public void setHeight(float height) { this.height = height; }

public float getScale() { return scale; }
public void setSclae(float scale) { this.scale = scale; }
//</editor-fold>

protected ImageRenderer()
{

}
protected ImageRenderer() { }
public ImageRenderer(Texture2D texture)
{
this.texture = texture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.net.URL;
import java.nio.ByteBuffer;

public class URLImageRenderer extends ImageRenderer
public class UrlImageRenderer extends ImageRenderer
{
public static final URLImageRenderer SHARED = new URLImageRenderer("https://media.forgecdn.net/avatars/thumbnails/1071/348/256/256/638606872011907048.png");
public static final UrlImageRenderer SHARED = new UrlImageRenderer("https://media.forgecdn.net/avatars/thumbnails/1071/348/256/256/638606872011907048.png");

public URLImageRenderer(String url)
public UrlImageRenderer() { }

public UrlImageRenderer(String url)
{
BufferedImage image = downloadImage(url);
if (image != null) texture = createTexture(image);
Expand All @@ -23,7 +25,7 @@ public void updateURL(String url)
BufferedImage image = downloadImage(url);
if (image != null)
{
texture.dispose();
if (texture != null) texture.dispose();
texture = createTexture(image);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ public class PaddingDeserializer implements IDeserializer<Padding>
@Override
public Padding deserialize(String raw, String protocol)
{
if (protocol.equals("ixml"))
{

}
else if (protocol.equals("json"))
if (protocol.equals("json") || protocol.equals("ixml"))
{
String top = RawJsonUtils.extractValue(raw, "top");
String bottom = RawJsonUtils.extractValue(raw, "bottom");
String left = RawJsonUtils.extractValue(raw, "left");
String right = RawJsonUtils.extractValue(raw, "right");
Padding padding = new Padding(0, 0, 0, 0);

if (!top.isEmpty())
try { padding.top = Integer.parseInt(top); } catch (Exception ignored) { }
try { padding.top = Float.parseFloat(top); } catch (Exception ignored) { }
if (!bottom.isEmpty())
try { padding.bottom = Integer.parseInt(bottom); } catch (Exception ignored) { }
try { padding.bottom = Float.parseFloat(bottom); } catch (Exception ignored) { }
if (!left.isEmpty())
try { padding.left = Integer.parseInt(left); } catch (Exception ignored) { }
try { padding.left = Float.parseFloat(left); } catch (Exception ignored) { }
if (!right.isEmpty())
try { padding.right = Integer.parseInt(right); } catch (Exception ignored) { }
try { padding.right = Float.parseFloat(right); } catch (Exception ignored) { }
return padding;
}
return null;
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/tttsaurus/ingameinfo/proxy/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.tttsaurus.ingameinfo.common.api.reflection.TypeUtils;
import com.tttsaurus.ingameinfo.common.api.serialization.IDeserializer;
import com.tttsaurus.ingameinfo.common.impl.gui.IgiGuiLifeCycle;
import com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify.InGameCommandHandler;
import com.tttsaurus.ingameinfo.common.impl.appcommunication.spotify.SpotifyCommandHandler;
import com.tttsaurus.ingameinfo.common.impl.gui.registry.ElementRegistry;
import com.tttsaurus.ingameinfo.common.impl.mvvm.registry.MvvmRegisterEventHandler;
import com.tttsaurus.ingameinfo.plugin.crt.impl.CrtEventManager;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void init(FMLInitializationEvent event, Logger logger)
MinecraftForge.EVENT_BUS.register(CrtEventManager.Handler.class);

// app communication
MinecraftForge.EVENT_BUS.register(InGameCommandHandler.class);
MinecraftForge.EVENT_BUS.register(SpotifyCommandHandler.class);

String myPackage = "com.tttsaurus.ingameinfo";
ElementRegistry.register();
Expand Down

0 comments on commit 5a0b7fd

Please sign in to comment.