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

🐛 Actually should fix #12 #15

Merged
merged 1 commit into from
Oct 1, 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.mattmx.nametags.config;

import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityHeadLook;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRotation;
import com.mattmx.nametags.NameTags;
import com.mattmx.nametags.entity.trait.RefreshTrait;
import com.mattmx.nametags.entity.trait.SneakTrait;
import com.mattmx.nametags.event.NameTagEntityCreateEvent;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -46,48 +43,55 @@ public void onCreate(@NotNull NameTagEntityCreateEvent event) {

long refreshMillis = plugin.getConfig().getLong("defaults.refresh-every", 50);

if (refreshMillis == 0L) {
return;
}

event.getNameTag()
.getTraits()
.getOrAddTrait(RefreshTrait.class, () ->
RefreshTrait.ofMillis(
plugin,
refreshMillis,
(entity) -> {
TextDisplayMetaConfiguration.applyMeta(defaultSection(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(defaultSection(), entity.getMeta(), player, player);

// TODO we should cache this stuff
plugin.getGroups()
.entrySet()
.stream()
.filter((e) -> player.hasPermission(e.getKey()))
.forEach((e) -> {
TextDisplayMetaConfiguration.applyMeta(e.getValue(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(e.getValue(), entity.getMeta(), player, player);
});


if (entity.getMeta().getBillboardConstraints() == AbstractDisplayMeta.BillboardConstraints.CENTER) {
// Look passenger down to remove debug getting in the way
entity.getPassenger().rotateHead(0f, 90f);
}
synchronized (entity) {

// TODO we need to change the text based off the player the packet is being sent to.
TextDisplayMetaConfiguration.applyMeta(defaultSection(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(defaultSection(), entity.getMeta(), player, player);

// TODO we should cache this stuff
plugin.getGroups()
.entrySet()
.stream()
.filter((e) -> player.hasPermission(e.getKey()))
.forEach((e) -> {
TextDisplayMetaConfiguration.applyMeta(e.getValue(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(e.getValue(), entity.getMeta(), player, player);
});

// Preserve background color for sneaking
// 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());
});
});
if (entity.getMeta().getBillboardConstraints() == AbstractDisplayMeta.BillboardConstraints.CENTER) {
// Look passenger down to remove debug getting in the way
entity.getPassenger().rotateHead(0f, 90f);
}

entity.updateVisibility();
// Preserve background color for sneaking
// Maybe we should introduce an `afterRefresh` callback?
entity.getTraits()
.getTrait(SneakTrait.class)
.ifPresent((sneak) -> {
if (!sneak.isSneaking()) return;

Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> entity.getPassenger().refresh(), 0L);
entity.modify((tag) -> {
Color currentColor = Color.fromARGB(tag.getBackgroundColor());
tag.setBackgroundColor(sneak.withCustomSneakOpacity(currentColor).asARGB());
});
});

entity.updateVisibility();
entity.getPassenger().refresh();
}
}
)
);
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/mattmx/nametags/entity/trait/SneakTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ public void updateSneak(boolean sneaking) {
previousOpacity = color.getAlpha();

meta.setBackgroundColor(withCustomSneakOpacity(color).asARGB());
meta.setTextOpacity((byte) getCustomOpacity());
} else {
meta.setBackgroundColor(color.setAlpha(previousOpacity).asARGB());
meta.setTextOpacity((byte) getCustomOpacity());
}
});
getTag().getPassenger().refresh();
}

public Color withCustomSneakOpacity(@NotNull Color previous) {
int sneakAmount = NameTags.getInstance()
return previous.setAlpha(getCustomOpacity());
}

public int getCustomOpacity() {
return NameTags.getInstance()
.getConfig()
.getInt("sneak.opacity", 70);

return previous.setAlpha(sneakAmount);
}

public boolean isSneaking() {
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
# Should the player see their own tag?
show-self: false

sneak:
# Should we modify the opacity of the tag's background when the player shifts?
enabled: true
opacity: 70

defaults:
# If not enabled then plugin simulates vanilla name tags
enabled: true
# How often should we refresh tags (in milliseconds)
refresh-every: 50
refresh-every: 1
# Lines of text to display.
text:
- "<white>%player_name%</white>"
Expand All @@ -32,6 +24,14 @@ defaults:
# Gap above the head, closest to vanilla is 0.2
gap: 0.2

# Should the player see their own tag?
show-self: false

sneak:
# Should we modify the opacity of the tag's background when the player shifts?
enabled: true
opacity: 70

# Permission based groups.
# Will only work if you have the "defaults" section enabled.
# Specify sub-groups that override the defaults.
Expand Down
Loading