Skip to content

Commit

Permalink
Implement rendering of Flags
Browse files Browse the repository at this point in the history
  • Loading branch information
HerXayah committed Sep 8, 2024
1 parent 0c477db commit 5013d67
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ plugins {
}

group = "de.funkeln.pronouns"
version = System.getenv().getOrDefault("VERSION", "1.0.0")
version = System.getenv().getOrDefault("VERSION", "1.0.0")

labyMod {
defaultPackageName = "de.funkeln.pronouns" //change this to your main package name (used by all modules)
defaultPackageName = "de.funkeln" //change this to your main package name (used by all modules)
addonInfo {
namespace = "pronouns"
displayName = "PronounsDisplay"
Expand Down
16 changes: 10 additions & 6 deletions core/src/main/java/com/funkeln/pronouns/PronounAddon.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.funkeln.pronouns;

import com.funkeln.pronouns.nametag.FlagNameTag;
import net.labymod.api.Laby;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.component.Component;
Expand Down Expand Up @@ -34,6 +35,7 @@ public static PronounAddon getInstance() {

@Override
protected void enable() {
INSTANCE = this;
this.registerSettingCategory();
if(this.configuration().enabled().get()) {
if(configuration().name().get().isEmpty()) {
Expand Down Expand Up @@ -75,14 +77,16 @@ public void onProfileFetchFailed(String username, Exception e) {
Laby.references().renderPipeline().rectangleRenderer()
)
);
this.labyAPI().tagRegistry().register(
"pronouns_flags",
PositionType.ABOVE_NAME,
new FlagNameTag(
Laby.references().renderPipeline(),
Laby.references().renderPipeline().rectangleRenderer()
)
);
}

public void prepareComponent(Profile profile) {
component = Component.text(profile.getPronoun());
logger().info("ara ara: " + component.toString());
}


@Override
protected Class<PronounConfiguration> configurationClass() {
return PronounConfiguration.class;
Expand Down
106 changes: 106 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.funkeln.pronouns.nametag;

import com.funkeln.pronouns.PronounAddon;
import com.funkeln.pronouns.utils.Profile;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.entity.player.Player;
import net.labymod.api.client.entity.player.tag.tags.NameTag;
import net.labymod.api.client.gui.HorizontalAlignment;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.client.render.RenderPipeline;
import net.labymod.api.client.render.draw.RectangleRenderer;
import net.labymod.api.client.render.font.RenderableComponent;
import net.labymod.api.client.render.matrix.Stack;
import org.jetbrains.annotations.Nullable;

import static com.funkeln.pronouns.utils.Profile.flags;

/**
* @author https://github.com/PrincessAkira (Sarah) Today is the 9/8/2024 @2:20 PM This project is
* named labymod4-addon-template
* @description Another day of Insanity
*/
public class FlagNameTag extends NameTag {

private final RectangleRenderer rectangleRenderer;

public FlagNameTag(RenderPipeline renderPipeline, RectangleRenderer rectangleRenderer) {
RenderPipeline renderPipeline1 = Laby.references().renderPipeline();
this.rectangleRenderer = renderPipeline1.rectangleRenderer();
}

public FlagNameTag(RectangleRenderer rectangleRenderer) {
this.rectangleRenderer = rectangleRenderer;
}

@Override
protected @Nullable RenderableComponent getRenderableComponent() {
if (this.entity instanceof Player && !this.entity.isCrouching()) {
HorizontalAlignment alignment;
alignment = HorizontalAlignment.CENTER;

PronounAddon addon = PronounAddon.getInstance();
if (!addon.configuration().enabled().get()) {
return null;
}

Component component = Component.empty();
if (component == null) {
return null;
}

return RenderableComponent.of(component, alignment);
} else {
return null;
}

}
@Override
protected void renderText(
Stack stack,
RenderableComponent component,
boolean discrete,
int textColor,
int backgroundColor,
float x,
float y
) {
x = 0;
float width = this.getWidth();
float height = this.getHeight();

if (Profile.getFlags() != null) {
float padding = 0.5f; // Space between flags
int widtz = 15; // Define the width for each flag
for (Icon flag : flags) {

// Render flag at the updated x position
flag.render(stack, x, +1, 15, height - 1);

// Move the x position to the right for the next flag
x += widtz + padding; // Increment x by the width of the flag and padding
}
}
super.renderText(stack, component, discrete, textColor, 0, width, y + 1);
}

@Override
public float getScale() {
return 0.65F;
}

@Override
public float getWidth() {
if (Profile.getFlags() == null) {
return super.getWidth();
}

return super.getWidth() + 15 * flags.length;
}

@Override
public float getHeight() {
return super.getHeight() + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.labymod.api.client.entity.player.Player;
import net.labymod.api.client.entity.player.tag.tags.NameTag;
import net.labymod.api.client.gui.HorizontalAlignment;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.client.render.RenderPipeline;
import net.labymod.api.client.render.draw.RectangleRenderer;
import net.labymod.api.client.render.font.RenderableComponent;
Expand All @@ -14,6 +15,8 @@
import com.funkeln.pronouns.utils.PronounsAPI;
import org.jetbrains.annotations.Nullable;

import static com.funkeln.pronouns.utils.Profile.flags;

/**
* @author https://github.com/PrincessAkira (Sarah) Today is the 8/16/2024 @7:26 PM This project is
* named labymod4-addon-template
Expand Down Expand Up @@ -71,23 +74,25 @@ protected void renderText(
height,
backgroundColor
);

float textX = x;

super.renderText(stack, component, discrete, textColor, 0, textX, y + 1);
x += + 1;
super.renderText(stack, component, discrete, textColor, 0, x, y + 1);
}

@Override
public float getScale() {
return 0.5F;
return 0.65F;
}

@Override
public float getWidth() {
if (Profile.getFlags() == null) {
return super.getWidth();
}

return super.getWidth();
}


@Override
public float getHeight() {
return super.getHeight() + 1;
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/com/funkeln/pronouns/utils/Profile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.funkeln.pronouns.utils;


import net.labymod.api.client.gui.icon.Icon;

/**
* @author https://github.com/PrincessAkira (Sarah) Today is the 9/8/2024 @2:39 AM This project is
Expand All @@ -12,12 +13,16 @@ public class Profile {
private final String username;
private final String pronoun;

public Profile(String username, String pronoun) {
public static volatile Icon[] flags;

public Profile(String username, String pronoun, Icon[] flags) {
this.username = username;
this.pronoun = pronoun;
this.flags = flags;
}

public String getUsername() { return username; }
public String getPronoun() { return pronoun; }
public static Icon[] getFlags() { return flags; }

}
50 changes: 48 additions & 2 deletions core/src/main/java/com/funkeln/pronouns/utils/PronounsAPI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.funkeln.pronouns.utils;

import com.funkeln.pronouns.PronounAddon;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.labymod.api.Laby;
import net.labymod.api.client.gui.icon.Icon;

import java.io.*;
import java.net.*;
Expand All @@ -15,6 +18,7 @@ public class PronounsAPI {

public static final String API_URL = "https://en.pronouns.page/api/";
public static final String FLAGS_URL = "https://en.pronouns.page/flags/";
private static PronounsAPI instance;

private static final Queue<Consumer<Void>> queues = new ConcurrentLinkedQueue<>();
private static final Map<String, ProfileFetchListener> listeners = new HashMap<>();
Expand All @@ -23,6 +27,14 @@ public static void addProfileFetchListener(String username, ProfileFetchListener
listeners.put(username, listener);
}


public static PronounsAPI getInstance() {
if (instance == null) {
instance = new PronounsAPI();
}
return instance;
}

private static void fetchProfile(String name) {
new Thread(() -> {
try {
Expand All @@ -33,7 +45,8 @@ private static void fetchProfile(String name) {
profile = element.getAsJsonObject();
}
String pronoun = getPronoun(profile);
Profile profileObj = new Profile(name, pronoun);
Icon[] flags = getFlags(profile);
Profile profileObj = new Profile(name, pronoun, flags);
Pridetags.profiles.add(profileObj);

// Notify listener
Expand Down Expand Up @@ -73,4 +86,37 @@ public static String getPronoun(JsonObject profile) {
if (pronounsArray == null || pronounsArray.isEmpty()) return null;
return pronounsArray.get(0).getAsJsonObject().get("value").getAsString();
}
}

public static Icon[] getFlags(JsonObject profile) {
if (PronounAddon.getInstance().configuration().enabled().get()) {
JsonObject profiles = profile.getAsJsonObject("profiles");
if (profiles == null) {
return null;
}
JsonObject enProfile = profiles.getAsJsonObject("en");
if (enProfile == null) {
return null;
}
JsonArray flagsArray = enProfile.getAsJsonArray("flags");
if (flagsArray == null || flagsArray.isEmpty()) {
return null;
}
List<Icon> flagsList = new ArrayList<>();
List<String> flagNamesList = new ArrayList<>();
for (JsonElement flag : flagsArray) {
String url = FLAGS_URL + flag.getAsString() + ".png";
try {
URL iconURL = new URL(url);
Icon icon = Icon.url(String.valueOf(iconURL));
Laby.labyAPI().minecraft().chatExecutor().displayClientMessage(icon.getUrl());
flagsList.add(icon);
flagNamesList.add(flag.getAsString());
} catch (Exception e) {
e.printStackTrace(); // Consider better error handling here
}
}
return flagsList.toArray(new Icon[0]);
}
return null;
}
}

0 comments on commit 5013d67

Please sign in to comment.