Skip to content

Commit

Permalink
Changes by uwu
Browse files Browse the repository at this point in the history
  • Loading branch information
HerXayah committed Sep 9, 2024
1 parent 8f16d4b commit 1fb52e0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 53 deletions.
38 changes: 23 additions & 15 deletions core/src/main/java/com/funkeln/pronouns/PronounAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

@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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public ConfigProperty<Boolean> renderFlags() {
@TextFieldSetting
private final ConfigProperty<String> name = new ConfigProperty<>("");


public ConfigProperty<String> name() {
return this.name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
42 changes: 27 additions & 15 deletions core/src/main/java/com/funkeln/pronouns/nametag/FlagNameTag.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -100,15 +101,26 @@ 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;
}

@Override
public float getHeight() {
return super.getHeight() + 1;
}

private Icon[] myFlags() {
Optional<Profile> profileFetch = ProfileRepository.find(this.entity.getUniqueId());
if (profileFetch.isPresent()) {
Profile profile = profileFetch.get();
if (profile.flagsAvailable()) {
return profile.flags();
}
}
return null;
}
}
57 changes: 35 additions & 22 deletions core/src/main/java/com/funkeln/pronouns/nametag/PronounNameTag.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,14 +13,14 @@
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
* named labymod4-addon-template
* @description Another day of Insanity
*/
public class PronounNameTag extends NameTag {

private final RectangleRenderer rectangleRenderer;

public PronounNameTag(RenderPipeline renderPipeline, RectangleRenderer rectangleRenderer) {
Expand All @@ -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
Expand Down Expand Up @@ -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<Profile> profileFetch = ProfileRepository.find(this.entity.getUniqueId());
if (profileFetch.isPresent()) {
Profile profile = profileFetch.get();
if (profile.pronounsAvailable()) {
return profile.pronoun();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID, Profile> profiles = new ConcurrentHashMap<>();

public static void enterName(UUID id, String name) {
profiles.computeIfAbsent(id, Profile::new).updateName(name);
}

public static Optional<Profile> find(UUID id) {
return Optional.ofNullable(profiles.get(id));
}

public static Set<UUID> 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()
);
}
}

0 comments on commit 1fb52e0

Please sign in to comment.