Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HerXayah committed Sep 8, 2024
1 parent d120ad2 commit aa8049e
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 101 deletions.
12 changes: 6 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ plugins {
id("net.labymod.gradle.addon")
}

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

labyMod {
defaultPackageName = "org.example" //change this to your main package name (used by all modules)
defaultPackageName = "de.funkeln.pronouns" //change this to your main package name (used by all modules)
addonInfo {
namespace = "example"
displayName = "ExampleAddon"
author = "Example Author"
description = "Example Description"
namespace = "pronouns"
displayName = "PronounsDisplay"
author = "funkeln"
description = "Display your Pronouns from pronouns.page ingame"
minecraftVersion = "*"
version = getVersion().toString()
}
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {

dependencies {
api(project(":api"))
maven(mavenCentral(), "org.apache.httpcomponents:httpclient:4.5.14")
}

labyModProcessor {
Expand Down
90 changes: 90 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/PronounAddon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.funkeln.pronouns;

import net.labymod.api.Laby;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.entity.player.tag.PositionType;
import net.labymod.api.models.addon.annotation.AddonMain;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.funkeln.pronouns.nametag.PronounNameTag;
import com.funkeln.pronouns.utils.Profile;
import com.funkeln.pronouns.utils.PronounsAPI;
import com.funkeln.pronouns.utils.PronounsAPI.ProfileFetchListener;

@AddonMain
public class PronounAddon extends LabyAddon<PronounConfiguration> {


private static final Log log = LogFactory.getLog(PronounAddon.class);
public static PronounAddon INSTANCE;
public String pronoun;
public volatile Component component;
public String meow;


public PronounAddon() {
INSTANCE = this;
}

public static PronounAddon getInstance() {
return INSTANCE;
}


@Override
protected void enable() {
this.registerSettingCategory();
if(this.configuration().enabled().get()) {
if(configuration().name().get().isEmpty()) {
meow = labyAPI().minecraft().getClientPlayer().getName();
} else {
meow = configuration().name().get().trim();
}
displayMessage(meow);
PronounsAPI.addProfileFetchListener(meow, new ProfileFetchListener() {
@Override
public void onProfileFetched(Profile profile) {
pronoun = profile.getPronoun().trim();
}

@Override
public void onProfileFetchFailed(String username, Exception e) {
log.error("Failed to fetch profile for " + username, e);
e.printStackTrace();
}
});

// Request the profile
// if name is empty, set it to the ingame user
PronounsAPI.getProfile(meow);

// Wait to ensure the async task completes (for demonstration purposes)
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.logger().info("Enabled the Addon");
this.labyAPI().tagRegistry().register(
"pronouns",
PositionType.BELOW_NAME,
new PronounNameTag(
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package org.example.core;
package com.funkeln.pronouns;

import net.labymod.api.addon.AddonConfig;
import net.labymod.api.client.gui.screen.widget.widgets.input.SwitchWidget.SwitchSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.TextFieldWidget.TextFieldSetting;
import net.labymod.api.configuration.loader.annotation.ConfigName;
import net.labymod.api.configuration.loader.annotation.SpriteSlot;
import net.labymod.api.configuration.loader.property.ConfigProperty;

@ConfigName("settings")
public class ExampleConfiguration extends AddonConfig {

public class PronounConfiguration extends AddonConfig {
@SpriteSlot(size = 32, x = 1)
@SwitchSetting
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);

@Override
public ConfigProperty<Boolean> enabled() {
return this.enabled;
}

@TextFieldSetting
private final ConfigProperty<String> name = new ConfigProperty<>("");


public ConfigProperty<String> name() {
return this.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.funkeln.pronouns.nametag;

import com.funkeln.pronouns.PronounAddon;
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.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 com.funkeln.pronouns.utils.Profile;
import com.funkeln.pronouns.utils.PronounsAPI;
import org.jetbrains.annotations.Nullable;

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

private final RectangleRenderer rectangleRenderer;

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

@Override
protected @Nullable RenderableComponent getRenderableComponent() {
if (!(this.entity instanceof Player) || this.entity.isCrouching()) {
return null;
}

HorizontalAlignment alignment;
alignment = HorizontalAlignment.CENTER;


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

Component component = Component.text(PronounAddon.getInstance().pronoun);
if (component == null) {
return null;
}

return RenderableComponent.of(component, alignment);
}

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

float textX = x;

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

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

@Override
public float getWidth() {

return super.getWidth();
}

@Override
public float getHeight() {
return super.getHeight() + 1;
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/utils/Pridetags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.funkeln.pronouns.utils;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public final class Pridetags {

public static Set<Profile> profiles = ConcurrentHashMap.newKeySet();

}
23 changes: 23 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/utils/Profile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.funkeln.pronouns.utils;



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

private final String username;
private final String pronoun;

public Profile(String username, String pronoun) {
this.username = username;
this.pronoun = pronoun;
}

public String getUsername() { return username; }
public String getPronoun() { return pronoun; }

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

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;

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 final Queue<Consumer<Void>> queues = new ConcurrentLinkedQueue<>();
private static final Map<String, ProfileFetchListener> listeners = new HashMap<>();

public static void addProfileFetchListener(String username, ProfileFetchListener listener) {
listeners.put(username, listener);
}

private static void fetchProfile(String name) {
new Thread(() -> {
try {
URL url = new URI(API_URL + "profile/get/" + name + "?version=2").toURL();
JsonObject profile = null;
try (InputStream stream = url.openStream()) {
JsonElement element = JsonParser.parseReader(new InputStreamReader(stream));
profile = element.getAsJsonObject();
}
String pronoun = getPronoun(profile);
Profile profileObj = new Profile(name, pronoun);
Pridetags.profiles.add(profileObj);

// Notify listener
ProfileFetchListener listener = listeners.get(name);
if (listener != null) {
listener.onProfileFetched(profileObj);
}
} catch (URISyntaxException | IOException e) {
// Notify listener of failure
ProfileFetchListener listener = listeners.get(name);
if (listener != null) {
listener.onProfileFetchFailed(name, e);
}
}
}).start();
}

public interface ProfileFetchListener {
void onProfileFetched(Profile profile);
void onProfileFetchFailed(String username, Exception e);
}


public static Profile getProfile(String name) {
Optional<Profile> maybeProfile = Pridetags.profiles.stream()
.filter(profile -> profile.getUsername().equals(name))
.findFirst();

if (maybeProfile.isPresent()) return maybeProfile.get();

fetchProfile(name);
return null;
}

public static String getPronoun(JsonObject profile) {
JsonArray pronounsArray = profile.getAsJsonObject("profiles").getAsJsonObject("en").getAsJsonArray("pronouns");
if (pronounsArray == null || pronounsArray.isEmpty()) return null;
return pronounsArray.get(0).getAsJsonObject().get("value").getAsString();
}
}
25 changes: 0 additions & 25 deletions core/src/main/java/org/example/core/ExampleAddon.java

This file was deleted.

Loading

0 comments on commit aa8049e

Please sign in to comment.