Skip to content

Commit

Permalink
Merge pull request #22 from Matt-MX/dev
Browse files Browse the repository at this point in the history
🐛 Fix legacy hex codes #18
  • Loading branch information
Matt-MX authored Oct 10, 2024
2 parents c636fed + e34f48d commit 1b8b049
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kotlin.code.style=official
# Project configuration
group_name = com.mattmx
id = nametags
version = 1.1
version = 1.2

plugin_name = NameTags
plugin_main_class_name = NameTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mattmx.nametags.entity.trait.SneakTrait;
import com.mattmx.nametags.event.NameTagEntityCreateEvent;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -80,14 +79,7 @@ public void onCreate(@NotNull NameTagEntityCreateEvent event) {
// Maybe we should introduce an `afterRefresh` callback?
entity.getTraits()
.getTrait(SneakTrait.class)
.ifPresent((sneak) -> {
if (!sneak.isSneaking()) return;

entity.modify((tag) -> {
Color currentColor = Color.fromARGB(tag.getBackgroundColor());
tag.setBackgroundColor(sneak.withCustomSneakOpacity(currentColor).asARGB());
});
});
.ifPresent(SneakTrait::manuallyUpdateSneakingOpacity);

entity.updateVisibility();
entity.getPassenger().refresh();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/mattmx/nametags/config/TextFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ public enum TextFormatter {
),
LEGACY(
"legacy",
(line) -> LegacyComponentSerializer.legacyAmpersand().deserialize(line)
(line) -> getLegacySerializer().deserialize(line)
)
;

private static final LegacyComponentSerializer legacy = LegacyComponentSerializer.builder()
.character('&')
.hexCharacter('#')
.hexColors()
.build();

public static @NotNull LegacyComponentSerializer getLegacySerializer() {
return legacy;
}

private final @NotNull String identifier;
private final @NotNull Function<String, Component> formatter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ public class SneakTrait extends Trait {
private int previousTextOpacity = Byte.MAX_VALUE;
private boolean isSneaking = false;

public void manuallyUpdateSneakingOpacity() {
if (!isSneaking()) return;

getTag().modify((tag) -> {
Color currentColor = Color.fromARGB(tag.getBackgroundColor());
tag.setBackgroundColor(withCustomSneakOpacity(currentColor).asARGB());
});
}

public void updateSneak(boolean sneaking) {
this.isSneaking = sneaking;
getTag().modify((meta) -> {
Expand Down

0 comments on commit 1b8b049

Please sign in to comment.