generated from LabyMod/addon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
317 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
16 changes: 13 additions & 3 deletions
16
...rg/example/core/ExampleConfiguration.java → ...unkeln/pronouns/PronounConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
core/src/main/java/com/funkeln/pronouns/utils/Pridetags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
core/src/main/java/com/funkeln/pronouns/utils/Profile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
core/src/main/java/com/funkeln/pronouns/utils/PronounsAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.