diff --git a/build.gradle.kts b/build.gradle.kts index 2805ded..1835c55 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import net.labymod.labygradle.common.extension.model.labymod.ReleaseChannels + plugins { id("net.labymod.labygradle") id("net.labymod.labygradle.addon") @@ -8,21 +10,8 @@ val versions = providers.gradleProperty("net.labymod.minecraft-versions").get(). group = "org.example" version = providers.environmentVariable("VERSION").getOrElse("1.0.0") -java.toolchain.languageVersion.set(JavaLanguageVersion.of(17)) - labyMod { - defaultPackageName = "net.crazy.pingtag" //change this to your main package name (used by all modules) - - minecraft { - registerVersion(versions.toTypedArray()) { - runs { - getByName("client") { - // When the property is set to true, you can log in with a Minecraft account - // devLogin = true - } - } - } - } + defaultPackageName = "net.crazy.pingtag" addonInfo { namespace = "pingtag" @@ -31,6 +20,18 @@ labyMod { description = "Displays a player's ping above their head." minecraftVersion = "*" version = rootProject.version.toString() + + releaseChannel.set(ReleaseChannels.SNAPSHOT) + } + + minecraft { + registerVersion(versions.toTypedArray()) { + runs { + getByName("client") { + devLogin = true + } + } + } } } diff --git a/core/src/main/java/net/crazy/pingtag/core/PingTag.java b/core/src/main/java/net/crazy/pingtag/core/PingTag.java index bd2780c..3835d96 100644 --- a/core/src/main/java/net/crazy/pingtag/core/PingTag.java +++ b/core/src/main/java/net/crazy/pingtag/core/PingTag.java @@ -1,86 +1,83 @@ package net.crazy.pingtag.core; -import net.labymod.api.Laby; +import java.util.Collections; +import java.util.List; +import net.crazy.pingtag.core.snapshot.PingTagExtraKeys; +import net.crazy.pingtag.core.snapshot.PingUserSnapshot; 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.network.NetworkPlayerInfo; -import net.labymod.api.client.render.font.ComponentMapper; -import net.labymod.api.client.render.font.RenderableComponent; -import net.labymod.api.configuration.loader.property.ConfigProperty; -import org.jetbrains.annotations.Nullable; - -public class PingTag extends NameTag { - - private final PingTagAddon addon; - - private static String prePingFormat; - private static String postPingFormat; +import net.labymod.api.client.component.format.NamedTextColor; +import net.labymod.api.client.component.format.TextColor; +import net.labymod.api.client.component.serializer.legacy.LegacyComponentSerializer; +import net.labymod.api.client.entity.player.tag.PositionType; +import net.labymod.api.client.entity.player.tag.tags.ComponentNameTag; +import net.labymod.api.client.render.state.entity.AvatarSnapshot; +import net.labymod.api.client.render.state.entity.EntitySnapshot; +import org.jetbrains.annotations.NotNull; + +public class PingTag extends ComponentNameTag { + + private static final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand(); + + private final PingTagAddon addon; + private final PositionType registeredPosition; + private PositionType activePosition; + private boolean coloured; + private String customFormat; + + public PingTag(PingTagAddon addon, PositionType registeredPosition) { + this.addon = addon; + this.registeredPosition = registeredPosition; + this.activePosition = this.addon.configuration().getPosition().get().getTagPosition(); + this.addon.configuration().getPosition().addChangeListener(position -> + this.activePosition = position.getTagPosition() + ); + this.coloured = this.addon.configuration().getColoured().get(); + this.addon.configuration().getColoured().addChangeListener(coloured -> + this.coloured = coloured + ); + this.customFormat = this.addon.configuration().getCustomFormat().get(); + this.addon.configuration().getCustomFormat().addChangeListener(customFormat -> + this.customFormat = customFormat + ); + } - public PingTag(PingTagAddon addon) { - this.addon = addon; - ConfigProperty customFormat = addon.configuration().getCustomFormat(); - customFormat.addChangeListener(ignored -> PingTag.updateCustomFormat(customFormat)); - updateCustomFormat(customFormat); + @Override + protected @NotNull List buildComponents(EntitySnapshot snapshot) { + if (this.registeredPosition != this.activePosition) { + return super.buildComponents(snapshot); } + if (!(snapshot instanceof AvatarSnapshot player) || player.isDiscrete() + || player.isInvisible()) { + return super.buildComponents(snapshot); + } + if (!player.has(PingTagExtraKeys.PING_USER)) { + return super.buildComponents(snapshot); + } + PingUserSnapshot pingUser = player.get(PingTagExtraKeys.PING_USER); - @Override - protected @Nullable RenderableComponent getRenderableComponent() { - if (!(this.entity instanceof Player player)) { - return null; - } - - PingTagConfiguration configuration = this.addon.configuration(); - if (!configuration.enabled().get()) { - return null; - } - - NetworkPlayerInfo networkPlayerInfo = player.networkPlayerInfo(); - if (networkPlayerInfo == null) { - return null; - } - - int ping = networkPlayerInfo.getCurrentPing(); - if(ping == 0) { - return null; - } - - String format = prePingFormat; - if (configuration.getColoured().get()) { - String color; - if (ping < 150) { - color = "§a"; - } else if (ping < 300) { - color = "§c"; - } else { - color = "§4"; - } - - format += color; - } - - format += ping; - if (postPingFormat != null) { - format += postPingFormat; - } - - return RenderableComponent.of(Component.text(format)); + Integer ping = pingUser.getPing(); + if (ping == null) { + return super.buildComponents(snapshot); + } + Component formattedPing = serializer.deserialize( + this.customFormat.replace("%ping%", ping.toString())); + if (this.coloured) { + TextColor color; + if (ping < 150) { + color = NamedTextColor.GREEN; + } else if (ping < 300) { + color = NamedTextColor.RED; + } else { + color = NamedTextColor.DARK_RED; + } + + formattedPing.color(color); } + return Collections.singletonList(formattedPing); + } @Override public float getScale() { return this.addon.configuration().getScale().get(); } - - public static void updateCustomFormat(ConfigProperty formatProperty) { - String format = formatProperty.get(); - if (format == null || format.trim().isEmpty()) { - format = formatProperty.defaultValue(); - } - - ComponentMapper componentMapper = Laby.labyAPI().minecraft().componentMapper(); - String[] parts = componentMapper.translateColorCodes(format).split("%ping%", 2); - prePingFormat = parts[0]; - postPingFormat = parts.length == 2 ? parts[1] : null; - } } \ No newline at end of file diff --git a/core/src/main/java/net/crazy/pingtag/core/PingTagAddon.java b/core/src/main/java/net/crazy/pingtag/core/PingTagAddon.java index d87ddc1..c1647d6 100644 --- a/core/src/main/java/net/crazy/pingtag/core/PingTagAddon.java +++ b/core/src/main/java/net/crazy/pingtag/core/PingTagAddon.java @@ -9,32 +9,19 @@ @AddonMain public class PingTagAddon extends LabyAddon { - private PingTag pingTag; - @Override protected void enable() { this.registerSettingCategory(); - pingTag = new PingTag(this); - - registerTag(); - - configuration().getPosition().addChangeListener(tagPosition -> { - Laby.labyAPI().tagRegistry().unregister("pingtag"); - registerTag(); - }); + for (Position position : Position.values()) { + PositionType positionType = position.getTagPosition(); + Laby.labyAPI().tagRegistry() + .register("ping_display", positionType, new PingTag(this, positionType)); + } this.logger().info("PingTag | Addon enabled."); } - public void registerTag() { - if(this.configuration().getPosition().get() == Position.ABOVE) { - labyAPI().tagRegistry().registerAfter("badge", "pingtag", PositionType.ABOVE_NAME, this.pingTag); - } else { - labyAPI().tagRegistry().register("pingtag", PositionType.BELOW_NAME, this.pingTag); - } - } - @Override protected Class configurationClass() { return PingTagConfiguration.class; diff --git a/core/src/main/java/net/crazy/pingtag/core/PingTagConfiguration.java b/core/src/main/java/net/crazy/pingtag/core/PingTagConfiguration.java index 8eac032..b4fd3ef 100644 --- a/core/src/main/java/net/crazy/pingtag/core/PingTagConfiguration.java +++ b/core/src/main/java/net/crazy/pingtag/core/PingTagConfiguration.java @@ -1,14 +1,13 @@ package net.crazy.pingtag.core; import net.labymod.api.addon.AddonConfig; +import net.labymod.api.client.entity.player.tag.PositionType; import net.labymod.api.client.gui.screen.widget.widgets.input.SliderWidget.SliderSetting; import net.labymod.api.client.gui.screen.widget.widgets.input.SwitchWidget.SwitchSetting; -import net.labymod.api.client.gui.screen.widget.widgets.input.TextFieldWidget; -import net.labymod.api.client.gui.screen.widget.widgets.input.dropdown.DropdownWidget.DropdownEntryTranslationPrefix; +import net.labymod.api.client.gui.screen.widget.widgets.input.TextFieldWidget.TextFieldSetting; import net.labymod.api.client.gui.screen.widget.widgets.input.dropdown.DropdownWidget.DropdownSetting; import net.labymod.api.configuration.loader.annotation.ConfigName; import net.labymod.api.configuration.loader.property.ConfigProperty; -import net.labymod.api.configuration.settings.annotation.SettingRequires; @ConfigName("settings") public class PingTagConfiguration extends AddonConfig { @@ -17,15 +16,13 @@ public class PingTagConfiguration extends AddonConfig { private final ConfigProperty enabled = new ConfigProperty<>(true); @SwitchSetting - private final ConfigProperty coloured = new ConfigProperty<>(false); + private final ConfigProperty coloured = new ConfigProperty<>(true); - @TextFieldWidget.TextFieldSetting - @SettingRequires(value = "coloured", invert = true) - private final ConfigProperty customFormat = new ConfigProperty<>("%ping%ms"); + @TextFieldSetting + private final ConfigProperty customFormat = new ConfigProperty<>("&a%ping%ms"); @DropdownSetting - @DropdownEntryTranslationPrefix("pingtag.settings.position.type") - private final ConfigProperty position = new ConfigProperty<>(Position.ABOVE); + private final ConfigProperty position = new ConfigProperty<>(Position.BELOW); @SliderSetting(min = 0.4F, max = 1.5F, steps = 0.1F) private final ConfigProperty scale = new ConfigProperty<>(1.0F); @@ -36,23 +33,34 @@ public ConfigProperty enabled() { } public ConfigProperty getColoured() { - return coloured; + return this.coloured; } public ConfigProperty getCustomFormat() { - return customFormat; + return this.customFormat; } public ConfigProperty getPosition() { - return position; + return this.position; } public ConfigProperty getScale() { - return scale; + return this.scale; } public enum Position { - ABOVE, BELOW + ABOVE(PositionType.ABOVE_NAME), + BELOW(PositionType.BELOW_NAME); + + private final PositionType tagPosition; + + Position(PositionType tagPosition) { + this.tagPosition = tagPosition; + } + + public PositionType getTagPosition() { + return this.tagPosition; + } } } diff --git a/core/src/main/java/net/crazy/pingtag/core/snapshot/PingTagExtraKeys.java b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingTagExtraKeys.java new file mode 100644 index 0000000..7759764 --- /dev/null +++ b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingTagExtraKeys.java @@ -0,0 +1,12 @@ +package net.crazy.pingtag.core.snapshot; + +import net.labymod.api.laby3d.renderer.snapshot.ExtraKey; + +public class PingTagExtraKeys { + + public static final ExtraKey PING_USER = ExtraKey.of( + "ping_user", + PingUserSnapshot.class + ); + +} diff --git a/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshot.java b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshot.java new file mode 100644 index 0000000..d6c73fc --- /dev/null +++ b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshot.java @@ -0,0 +1,27 @@ +package net.crazy.pingtag.core.snapshot; + +import net.labymod.api.client.entity.player.Player; +import net.labymod.api.client.network.NetworkPlayerInfo; +import net.labymod.api.laby3d.renderer.snapshot.AbstractLabySnapshot; +import net.labymod.api.laby3d.renderer.snapshot.Extras; +import org.jetbrains.annotations.Nullable; + +public class PingUserSnapshot extends AbstractLabySnapshot { + + private final Integer ping; + + public PingUserSnapshot(Player player, Extras extras) { + super(extras); + NetworkPlayerInfo info = player.getNetworkPlayerInfo(); + if (info == null) { + this.ping = null; + return; + } + this.ping = info.getCurrentPing(); + } + + @Nullable + public Integer getPing() { + return this.ping; + } +} diff --git a/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshotFactory.java b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshotFactory.java new file mode 100644 index 0000000..0e2096a --- /dev/null +++ b/core/src/main/java/net/crazy/pingtag/core/snapshot/PingUserSnapshotFactory.java @@ -0,0 +1,19 @@ +package net.crazy.pingtag.core.snapshot; + +import net.labymod.api.client.entity.player.Player; +import net.labymod.api.laby3d.renderer.snapshot.Extras; +import net.labymod.api.laby3d.renderer.snapshot.LabySnapshotFactory; +import net.labymod.api.service.annotation.AutoService; + +@AutoService(LabySnapshotFactory.class) +public class PingUserSnapshotFactory extends LabySnapshotFactory { + + public PingUserSnapshotFactory() { + super(PingTagExtraKeys.PING_USER); + } + + @Override + protected PingUserSnapshot create(Player player, Extras extras) { + return new PingUserSnapshot(player, extras); + } +} diff --git a/core/src/main/java/net/crazy/pingtag/core/snapshot/PlayerSnapshotProcessor.java b/core/src/main/java/net/crazy/pingtag/core/snapshot/PlayerSnapshotProcessor.java new file mode 100644 index 0000000..efdd627 --- /dev/null +++ b/core/src/main/java/net/crazy/pingtag/core/snapshot/PlayerSnapshotProcessor.java @@ -0,0 +1,26 @@ +package net.crazy.pingtag.core.snapshot; + +import net.labymod.api.client.entity.Entity; +import net.labymod.api.client.entity.player.Player; +import net.labymod.api.client.render.state.entity.EntitySnapshotProcessor; +import net.labymod.api.client.render.state.entity.EntitySnapshotRegistry; +import net.labymod.api.laby3d.renderer.snapshot.ExtrasWriter; +import net.labymod.api.service.annotation.AutoService; + +@AutoService(EntitySnapshotProcessor.class) +public class PlayerSnapshotProcessor extends EntitySnapshotProcessor { + + public PlayerSnapshotProcessor(EntitySnapshotRegistry registry) { + super(registry); + } + + @Override + public boolean supports(Entity entity) { + return entity instanceof Player; + } + + @Override + public void process(Player player, float partialTicks, ExtrasWriter entityWriter) { + this.registry().captureSnapshot(entityWriter, PingTagExtraKeys.PING_USER, player); + } +} diff --git a/core/src/main/resources/assets/pingtag/i18n/de_de.json b/core/src/main/resources/assets/pingtag/i18n/de_de.json index a40fdbd..de6f66c 100644 --- a/core/src/main/resources/assets/pingtag/i18n/de_de.json +++ b/core/src/main/resources/assets/pingtag/i18n/de_de.json @@ -12,7 +12,7 @@ "position": { "name": "Position", "description": "Setzte die Position des Ping Tag.", - "type": { + "entries": { "above": "Über dem Name", "below": "Unter dem Name" } diff --git a/core/src/main/resources/assets/pingtag/i18n/en_us.json b/core/src/main/resources/assets/pingtag/i18n/en_us.json index 27da5f3..0f0877b 100644 --- a/core/src/main/resources/assets/pingtag/i18n/en_us.json +++ b/core/src/main/resources/assets/pingtag/i18n/en_us.json @@ -12,7 +12,7 @@ "position": { "name": "Position", "description": "Set the Position of the Ping Tag.", - "type": { + "entries": { "above": "Above Name", "below": "Below Name" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e1adfb4..d706aba 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle.kts b/settings.gradle.kts index c371677..c8dcfef 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,7 +1,7 @@ -rootProject.name = "PingTag" +rootProject.name = "pingtag" pluginManagement { - val labyGradlePluginVersion = "0.5.5" + val labyGradlePluginVersion = "0.6.0-SNAPSHOT" buildscript { repositories {