Skip to content

Commit

Permalink
- [X] Implement Caching
Browse files Browse the repository at this point in the history
- [X] Other users are able to use the thing
- [X] Add support for multiple pronouns and flags
- [X] Use SendPayloadBroadcast
- [X] DEHDOS SCHUTZ
  • Loading branch information
HerXayah committed Sep 9, 2024
1 parent 08ae8a4 commit 8f16d4b
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 205 deletions.
47 changes: 15 additions & 32 deletions core/src/main/java/com/funkeln/pronouns/PronounAddon.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.funkeln.pronouns;

import com.funkeln.pronouns.event.BroadcastEventListener;
import com.funkeln.pronouns.event.ServerJoinListener;
import com.funkeln.pronouns.nametag.FlagNameTag;
import com.funkeln.pronouns.utils.Pridetags;
import com.funkeln.pronouns.profile.ProfileRepository;
import com.google.gson.JsonObject;
import net.labymod.api.Laby;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.component.Component;
Expand All @@ -10,9 +13,6 @@
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> {
Expand All @@ -38,34 +38,17 @@ protected void enable() {
INSTANCE = this;
this.registerSettingCategory();
if(this.configuration().enabled().get()) {
if(configuration().name().get().isEmpty()) {
displayMessage("Please set the name in the config");
} else {
meow = configuration().name().get().trim();
}
PronounsAPI.addProfileFetchListener(meow, new ProfileFetchListener() {
@Override
public void onProfileFetched(Profile profile) {
log.info("Fetched profile for " + meow);
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
PronounsAPI.getProfile(meow);

// Wait to ensure the async task completes (for demonstration purposes)
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
labyAPI().eventBus().registerListener(new BroadcastEventListener());
labyAPI().taskExecutor().getScheduledPool().scheduleAtFixedRate(
() -> {
publishNameUpdate();
ProfileRepository.updateProfiles(false);
},
30000,
30000,
java.util.concurrent.TimeUnit.MILLISECONDS
);
labyAPI().eventBus().registerListener(new ServerJoinListener(this));
}
this.logger().info("Enabled the Addon");
this.labyAPI().tagRegistry().register(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.funkeln.pronouns.event;

import com.funkeln.pronouns.profile.ProfileRepository;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.labymod.api.event.Subscribe;
import net.labymod.api.event.labymod.labyconnect.session.LabyConnectBroadcastEvent;
import java.util.UUID;

/**
* @author https://github.com/PrincessAkira (Sarah) Today is the 9/9/2024 @9:58 PM This project is
* named labymod4-addon-template
* @description Another day of Insanity
*/
public class BroadcastEventListener {
@Subscribe
public void onBroadcastReceive(LabyConnectBroadcastEvent event) {
if (!event.getKey().equals("pronouns")) {
return;
}
JsonElement payload = event.getPayload();
if (!payload.isJsonObject()) {
return;
}
JsonObject jsonObject = payload.getAsJsonObject();
UUID sender = event.getSender();
String name = jsonObject.get("name").getAsJsonPrimitive().getAsString();
ProfileRepository.enterName(sender, name);
ProfileRepository.updateProfiles(true);
}
}
65 changes: 65 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/flag/FlagResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.funkeln.pronouns.flag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import net.labymod.api.Laby;
import net.labymod.api.client.gui.icon.Icon;

/**
* @author https://github.com/PrincessAkira (Sarah) Today is the 9/9/2024 @10:30 PM This project is
* named labymod4-addon-template
* @description Another day of Insanity
*/
public class FlagResolver {
private final static Map<String, Icon> cache = new HashMap<>();
private static final String FLAGS_URL = "https://en.pronouns.page/flags/";

// Name zu Icon
public static void iconFromName(
String name,
Consumer<Icon> lazyReturn
) {
if (name.contains("?") || name.contains("&") || name.contains("/") || name.contains(".")) {
return;
}
if (cache.containsKey(name)) {
lazyReturn.accept(cache.get(name));
}
Laby.labyAPI().taskExecutor().getPool().submit(() -> {
Icon icon = Icon.url(FLAGS_URL + name + ".png");
cache.put(name, icon);
lazyReturn.accept(icon);
});
}

// Namen zu Icons
public static void iconsFromName(
Consumer<Icon[]> lazyReturn,
String... names
) {
int size = names.length;
AtomicLong remaining = new AtomicLong(size);
Icon[] results = new Icon[size];

for (int i = 0; i < names.length; i++) {
String name = names[i];
int finalI = i;
iconFromName(name, icon -> {
results[finalI] = icon;
if (remaining.decrementAndGet() == 0) {
lazyReturn.accept(results);
}
});
}
}

public static void iconsFromName(
List<String> names,
Consumer<Icon[]> lazyReturn
) {
iconsFromName(lazyReturn, names.toArray(new String[0]));
}
}
10 changes: 7 additions & 3 deletions core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public FlagNameTag(RectangleRenderer rectangleRenderer) {

@Override
protected @Nullable RenderableComponent getRenderableComponent() {
if (!(this.entity instanceof Player) || this.entity.isCrouching() || !Laby.labyAPI().minecraft().getClientPlayer().getName().equals(((Player) this.entity).getName())) {
if (!(this.entity instanceof Player) || this.entity.isCrouching()) {
return null;
}
HorizontalAlignment alignment;
alignment = HorizontalAlignment.CENTER;

if (ProfileRepository.find(this.entity.getUniqueId()).isPresent()) {
HorizontalAlignment alignment = HorizontalAlignment.CENTER;
PronounAddon addon = PronounAddon.getInstance();

if (!addon.configuration().enabled().get()) {
Expand All @@ -58,7 +58,10 @@ public FlagNameTag(RectangleRenderer rectangleRenderer) {
}

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

@Override
protected void renderText(
Expand All @@ -80,6 +83,7 @@ protected void renderText(
for (Icon flag : flags) {

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

// Move the x position to the right for the next flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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.profile.Profile;
import org.jetbrains.annotations.Nullable;

/**
Expand Down
154 changes: 154 additions & 0 deletions core/src/main/java/com/funkeln/pronouns/profile/Profile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.funkeln.pronouns.profile;


import com.funkeln.pronouns.flag.FlagResolver;
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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @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 {
public static final String API_URL = "https://en.pronouns.page/api/";
public static long PROFILE_EXPIRY = 5 * 60 * 1000L;
public static long PRONOUN_EXPIRY = 5 * 60 * 1000L;

private final UUID id;
private String websiteName = "";
private String pronoun;
public Icon[] flags;

private boolean requestedUpdate;
private boolean firstRequestSpent = false;
private long lastNameUpdate = System.currentTimeMillis();
private long lastPronounUpdate = System.currentTimeMillis();

public Profile(UUID id) {
this.id = id;
}

public void updateName(String name) {
if (!websiteName.equals(name)) {
this.websiteName = name;
pronoun = null;
flags = null;
requestedUpdate = true;
}
lastNameUpdate = System.currentTimeMillis();
}

public void update() {
firstRequestSpent = true;
requestedUpdate = false;
Laby.labyAPI().taskExecutor().getPool().submit(() -> {
// async
try {
URL url = new URI(API_URL + "profile/get/" + websiteName + "?version=2").toURL();
JsonObject profile = null;
try (InputStream stream = url.openStream()) {
JsonElement element = JsonParser.parseReader(new InputStreamReader(stream));
profile = element.getAsJsonObject();
}
updatePronouns(pronounFromJson(profile));
List<String> flagNames = flagNamesFromJson(profile);
FlagResolver.iconsFromName(flagNames, this::updateFlags);
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
});
}

public boolean updateRequired() {
return System.currentTimeMillis() - lastPronounUpdate > PRONOUN_EXPIRY || requestedUpdate;
}

public boolean requiresUpdateNow() {
return !firstRequestSpent;
}

public static String pronounFromJson(JsonObject profile) {
JsonArray pronounsArray = profile.getAsJsonObject("profiles").getAsJsonObject("en").getAsJsonArray("pronouns");
if (pronounsArray == null || pronounsArray.isEmpty()) return null;
List<String> outputPronounList = new ArrayList<>();
for(JsonElement pronoun : pronounsArray) {
outputPronounList.add(pronoun.getAsJsonObject().get("value").getAsString());
if(outputPronounList.size() > 3) {
break;
}
}
return String.join(" & ", outputPronounList);
}

public static List<String> flagNamesFromJson(JsonObject profile) {
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<String> flagNamesList = new ArrayList<>();
for (JsonElement flag : flagsArray) {
if(flagNamesList.size() > 3) {
break;
}
flagNamesList.add(flag.getAsString());
}
return flagNamesList;
}

public boolean expired() {
return System.currentTimeMillis() - lastNameUpdate > PROFILE_EXPIRY
;
}

public boolean pronounsAvailable() {
return pronoun != null;
}

public boolean flagsAvailable() {
return flags != null;
}

public Icon[] flags() {
return flags;
}

public void updatePronouns(String pronoun) {
this.pronoun = pronoun;
lastPronounUpdate = System.currentTimeMillis();
}

public void updateFlags(Icon[] flags) {
this.flags = flags;
lastPronounUpdate = System.currentTimeMillis();
}

public String username() {
return websiteName;
}

public String pronoun() {
return pronoun;
}
}
22 changes: 0 additions & 22 deletions core/src/main/java/com/funkeln/pronouns/utils/Pridetags.java

This file was deleted.

Loading

0 comments on commit 8f16d4b

Please sign in to comment.