From 1fb52e03e38f8bdd06bc4e04a40180d64ce7b08d Mon Sep 17 00:00:00 2001 From: Sarah Engel Date: Mon, 9 Sep 2024 23:46:13 +0200 Subject: [PATCH] Changes by uwu --- .../com/funkeln/pronouns/PronounAddon.java | 38 ++++++++----- .../pronouns/PronounConfiguration.java | 1 - .../pronouns/event/ServerJoinListener.java | 23 ++++++++ .../funkeln/pronouns/nametag/FlagNameTag.java | 42 +++++++++----- .../pronouns/nametag/PronounNameTag.java | 57 ++++++++++++------- .../pronouns/profile/ProfileRepository.java | 45 +++++++++++++++ 6 files changed, 153 insertions(+), 53 deletions(-) create mode 100644 core/src/main/java/com/funkeln/pronouns/event/ServerJoinListener.java create mode 100644 core/src/main/java/com/funkeln/pronouns/profile/ProfileRepository.java diff --git a/core/src/main/java/com/funkeln/pronouns/PronounAddon.java b/core/src/main/java/com/funkeln/pronouns/PronounAddon.java index 96a930f..0b71639 100644 --- a/core/src/main/java/com/funkeln/pronouns/PronounAddon.java +++ b/core/src/main/java/com/funkeln/pronouns/PronounAddon.java @@ -16,15 +16,12 @@ @AddonMain public class PronounAddon extends LabyAddon { - - 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; } @@ -52,21 +49,32 @@ protected void enable() { } this.logger().info("Enabled the Addon"); this.labyAPI().tagRegistry().register( - "pronouns", - PositionType.BELOW_NAME, - new PronounNameTag( - Laby.references().renderPipeline(), - Laby.references().renderPipeline().rectangleRenderer() - ) + "pronouns", + PositionType.BELOW_NAME, + new PronounNameTag( + Laby.references().renderPipeline(), + Laby.references().renderPipeline().rectangleRenderer() + ) ); this.labyAPI().tagRegistry().register( - "pronouns_flags", - PositionType.ABOVE_NAME, - new FlagNameTag( - Laby.references().renderPipeline(), - Laby.references().renderPipeline().rectangleRenderer() - ) + "pronouns_flags", + PositionType.ABOVE_NAME, + new FlagNameTag( + Laby.references().renderPipeline(), + Laby.references().renderPipeline().rectangleRenderer() + ) + ); + this.configuration().name().addChangeListener(this::publishNameUpdate); + } + + public void publishNameUpdate() { + String newName = configuration().name().get(); + logger().info("Publishing name change to " + newName); + JsonObject data = new JsonObject(); + data.addProperty("name", newName ); + labyAPI().labyConnect().getSession().sendBroadcastPayload("pronouns", data); + ProfileRepository.clearExpired(); } @Override diff --git a/core/src/main/java/com/funkeln/pronouns/PronounConfiguration.java b/core/src/main/java/com/funkeln/pronouns/PronounConfiguration.java index cbfd7f8..c461a34 100644 --- a/core/src/main/java/com/funkeln/pronouns/PronounConfiguration.java +++ b/core/src/main/java/com/funkeln/pronouns/PronounConfiguration.java @@ -35,7 +35,6 @@ public ConfigProperty renderFlags() { @TextFieldSetting private final ConfigProperty name = new ConfigProperty<>(""); - public ConfigProperty name() { return this.name; } diff --git a/core/src/main/java/com/funkeln/pronouns/event/ServerJoinListener.java b/core/src/main/java/com/funkeln/pronouns/event/ServerJoinListener.java new file mode 100644 index 0000000..0237e51 --- /dev/null +++ b/core/src/main/java/com/funkeln/pronouns/event/ServerJoinListener.java @@ -0,0 +1,23 @@ +package com.funkeln.pronouns.event; + +import com.funkeln.pronouns.PronounAddon; +import net.labymod.api.event.Subscribe; +import net.labymod.api.event.client.network.server.ServerJoinEvent; + +/** + * @author https://github.com/PrincessAkira (Sarah) Today is the 9/9/2024 @11:18 PM This project is + * named labymod4-addon-template + * @description Another day of Insanity + */ +public class ServerJoinListener { + private final PronounAddon addon; + + public ServerJoinListener(PronounAddon addon) { + this.addon = addon; + } + + @Subscribe + public void on(ServerJoinEvent event) { + addon.publishNameUpdate(); + } +} diff --git a/core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java b/core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java index e9f1f90..c42cb19 100644 --- a/core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java +++ b/core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java @@ -1,7 +1,9 @@ package com.funkeln.pronouns.nametag; import com.funkeln.pronouns.PronounAddon; -import com.funkeln.pronouns.utils.Profile; +import com.funkeln.pronouns.profile.Profile; +import com.funkeln.pronouns.profile.ProfileRepository; +import java.util.Optional; import net.labymod.api.Laby; import net.labymod.api.client.component.Component; import net.labymod.api.client.entity.player.Player; @@ -14,7 +16,6 @@ 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 @@ -65,27 +66,27 @@ public FlagNameTag(RectangleRenderer rectangleRenderer) { @Override protected void renderText( - Stack stack, - RenderableComponent component, - boolean discrete, - int textColor, - int backgroundColor, - float x, - float y + 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 + Icon[] flags = myFlags(); + if (flags != null) { + int flagcount = 0; + float padding = 0.5f; // Space between myFlags int widtz = 15; // Define the width for each flag 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 x += widtz + padding; // Increment x by the width of the flag and padding } @@ -100,10 +101,10 @@ public float getScale() { @Override public float getWidth() { - if (Profile.getFlags() == null) { + Icon[] flags = myFlags(); + if (flags == null) { return super.getWidth(); } - return super.getWidth() + 15 * flags.length; } @@ -111,4 +112,15 @@ public float getWidth() { public float getHeight() { return super.getHeight() + 1; } + + private Icon[] myFlags() { + Optional profileFetch = ProfileRepository.find(this.entity.getUniqueId()); + if (profileFetch.isPresent()) { + Profile profile = profileFetch.get(); + if (profile.flagsAvailable()) { + return profile.flags(); + } + } + return null; + } } diff --git a/core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java b/core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java index 87a1ae9..8fd81e2 100644 --- a/core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java +++ b/core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java @@ -1,6 +1,7 @@ package com.funkeln.pronouns.nametag; import com.funkeln.pronouns.PronounAddon; +import com.funkeln.pronouns.profile.ProfileRepository; import net.labymod.api.Laby; import net.labymod.api.client.component.Component; import net.labymod.api.client.entity.player.Player; @@ -12,6 +13,7 @@ import net.labymod.api.client.render.matrix.Stack; import com.funkeln.pronouns.profile.Profile; import org.jetbrains.annotations.Nullable; +import java.util.Optional; /** * @author https://github.com/PrincessAkira (Sarah) Today is the 8/16/2024 @7:26 PM This project is @@ -19,7 +21,6 @@ * @description Another day of Insanity */ public class PronounNameTag extends NameTag { - private final RectangleRenderer rectangleRenderer; public PronounNameTag(RenderPipeline renderPipeline, RectangleRenderer rectangleRenderer) { @@ -29,29 +30,31 @@ public PronounNameTag(RenderPipeline renderPipeline, RectangleRenderer rectangle @Override protected @Nullable RenderableComponent getRenderableComponent() { - if (!(this.entity instanceof Player) || this.entity.isCrouching() || !Laby.labyAPI().minecraft().getClientPlayer().getName().equals(((Player) this.entity).getName())) { - return null; - } - - HorizontalAlignment alignment; - alignment = HorizontalAlignment.CENTER; - - - PronounAddon addon = PronounAddon.getInstance(); - if (!addon.configuration().enabled().get()) { - return null; - } - - if (!addon.configuration().renderTag().get()) { + if (!(this.entity instanceof Player) || this.entity.isCrouching()) { return null; } - Component component = Component.text(PronounAddon.getInstance().pronoun); - if (component == null) { + String pronoun = myPronoun(); + if (pronoun != null) { + HorizontalAlignment alignment = HorizontalAlignment.CENTER; + PronounAddon addon = PronounAddon.getInstance(); + if (!addon.configuration().enabled().get()) { + return null; + } + + if (!addon.configuration().renderTag().get()) { + return null; + } + + Component component = Component.text(pronoun); + if (component == null) { + return null; + } + + return RenderableComponent.of(component, alignment); + } else { return null; } - - return RenderableComponent.of(component, alignment); } @Override @@ -85,16 +88,26 @@ public float getScale() { @Override public float getWidth() { - if (Profile.getFlags() == null) { + if (myPronoun() == null) { return super.getWidth(); } - return super.getWidth(); } - @Override public float getHeight() { return super.getHeight() + 1; } + + + private String myPronoun() { + Optional profileFetch = ProfileRepository.find(this.entity.getUniqueId()); + if (profileFetch.isPresent()) { + Profile profile = profileFetch.get(); + if (profile.pronounsAvailable()) { + return profile.pronoun(); + } + } + return null; + } } \ No newline at end of file diff --git a/core/src/main/java/com/funkeln/pronouns/profile/ProfileRepository.java b/core/src/main/java/com/funkeln/pronouns/profile/ProfileRepository.java new file mode 100644 index 0000000..7a3e244 --- /dev/null +++ b/core/src/main/java/com/funkeln/pronouns/profile/ProfileRepository.java @@ -0,0 +1,45 @@ +package com.funkeln.pronouns.profile; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author https://github.com/PrincessAkira (Sarah) Today is the 9/9/2024 @10:21 PM This project is + * named labymod4-addon-template + * @description Another day of Insanity + */ +public class ProfileRepository { + private final static Map profiles = new ConcurrentHashMap<>(); + + public static void enterName(UUID id, String name) { + profiles.computeIfAbsent(id, Profile::new).updateName(name); + } + + public static Optional find(UUID id) { + return Optional.ofNullable(profiles.get(id)); + } + + public static Set knownPlayers() { + return profiles.keySet(); + } + + public static void updateProfiles(boolean onlyUpdateFirstTimes) { + for (Profile profile : profiles.values()) { + if (profile.updateRequired()) { + if (onlyUpdateFirstTimes && !profile.requiresUpdateNow()) { + return; + } + profile.update(); + } + } + } + + public static void clearExpired() { + profiles.entrySet().removeIf( + entry -> entry.getValue().expired() + ); + } +}