Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import net.labymod.labygradle.common.extension.model.labymod.ReleaseChannels

plugins {
id("net.labymod.labygradle")
id("net.labymod.labygradle.addon")
Expand All @@ -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"
Expand All @@ -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
}
}
}
}
}

Expand Down
143 changes: 70 additions & 73 deletions core/src/main/java/net/crazy/pingtag/core/PingTag.java
Original file line number Diff line number Diff line change
@@ -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<String> customFormat = addon.configuration().getCustomFormat();
customFormat.addChangeListener(ignored -> PingTag.updateCustomFormat(customFormat));
updateCustomFormat(customFormat);
@Override
protected @NotNull List<Component> 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<String> 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;
}
}
23 changes: 5 additions & 18 deletions core/src/main/java/net/crazy/pingtag/core/PingTagAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@
@AddonMain
public class PingTagAddon extends LabyAddon<PingTagConfiguration> {

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<PingTagConfiguration> configurationClass() {
return PingTagConfiguration.class;
Expand Down
36 changes: 22 additions & 14 deletions core/src/main/java/net/crazy/pingtag/core/PingTagConfiguration.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,15 +16,13 @@ public class PingTagConfiguration extends AddonConfig {
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);

@SwitchSetting
private final ConfigProperty<Boolean> coloured = new ConfigProperty<>(false);
private final ConfigProperty<Boolean> coloured = new ConfigProperty<>(true);

@TextFieldWidget.TextFieldSetting
@SettingRequires(value = "coloured", invert = true)
private final ConfigProperty<String> customFormat = new ConfigProperty<>("%ping%ms");
@TextFieldSetting
private final ConfigProperty<String> customFormat = new ConfigProperty<>("&a%ping%ms");

@DropdownSetting
@DropdownEntryTranslationPrefix("pingtag.settings.position.type")
private final ConfigProperty<Position> position = new ConfigProperty<>(Position.ABOVE);
private final ConfigProperty<Position> position = new ConfigProperty<>(Position.BELOW);

@SliderSetting(min = 0.4F, max = 1.5F, steps = 0.1F)
private final ConfigProperty<Float> scale = new ConfigProperty<>(1.0F);
Expand All @@ -36,23 +33,34 @@ public ConfigProperty<Boolean> enabled() {
}

public ConfigProperty<Boolean> getColoured() {
return coloured;
return this.coloured;
}

public ConfigProperty<String> getCustomFormat() {
return customFormat;
return this.customFormat;
}

public ConfigProperty<Position> getPosition() {
return position;
return this.position;
}

public ConfigProperty<Float> 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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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<PingUserSnapshot> PING_USER = ExtraKey.of(
"ping_user",
PingUserSnapshot.class
);

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Player, PingUserSnapshot> {

public PingUserSnapshotFactory() {
super(PingTagExtraKeys.PING_USER);
}

@Override
protected PingUserSnapshot create(Player player, Extras extras) {
return new PingUserSnapshot(player, extras);
}
}
Loading
Loading