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.
- [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
Showing
9 changed files
with
273 additions
and
205 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
31 changes: 31 additions & 0 deletions
31
core/src/main/java/com/funkeln/pronouns/event/BroadcastEventListener.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,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
65
core/src/main/java/com/funkeln/pronouns/flag/FlagResolver.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,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])); | ||
} | ||
} |
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
154 changes: 154 additions & 0 deletions
154
core/src/main/java/com/funkeln/pronouns/profile/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,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
22
core/src/main/java/com/funkeln/pronouns/utils/Pridetags.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.