Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix legacy hex codes #18 #22

Merged
merged 1 commit into from
Oct 10, 2024
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
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
Loading