Skip to content

Commit

Permalink
🐛 Fix #12 and add refresh timer customization option in config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Sep 29, 2024
1 parent 677c127 commit c5b663f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
11 changes: 3 additions & 8 deletions src/main/java/com/mattmx/nametags/EventsListener.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.mattmx.nametags;

import com.mattmx.nametags.config.TextDisplayMetaConfiguration;
import com.mattmx.nametags.entity.NameTagEntity;
import com.mattmx.nametags.entity.trait.RefreshTrait;
import com.mattmx.nametags.entity.trait.SneakTrait;
import com.mattmx.nametags.event.NameTagEntityCreateEvent;
import org.bukkit.Color;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
Expand All @@ -18,11 +14,10 @@ public class EventsListener implements Listener {

@EventHandler
public void onPlayerJoin(@NotNull PlayerJoinEvent event) {
final NameTagEntity tag = NameTags.getInstance()
NameTags.getInstance()
.getEntityManager()
.getOrCreateNameTagEntity(event.getPlayer());

tag.updateVisibility();
.getOrCreateNameTagEntity(event.getPlayer())
.updateVisibility();
}

@EventHandler
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/mattmx/nametags/NameTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.HandlerList;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ private ConfigurationSection defaultSection() {
public void onCreate(@NotNull NameTagEntityCreateEvent event) {
if (!(event.getNameTag().getBukkitEntity() instanceof Player player)) return;

// By default, we shouldn't notify until we have finished processing.
event.getNameTag()
.getPassenger()
.getEntityMeta()
.setNotifyAboutChanges(false);

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

event.getNameTag()
.getTraits()
.getOrAddTrait(RefreshTrait.class, () ->
RefreshTrait.ofSeconds(
NameTags.getInstance(),
1L,
RefreshTrait.ofMillis(
plugin,
refreshMillis,
(entity) -> {
TextDisplayMetaConfiguration.applyMeta(defaultSection(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(defaultSection(), entity.getMeta(), player, player);
Expand All @@ -52,13 +60,13 @@ public void onCreate(@NotNull NameTagEntityCreateEvent event) {
TextDisplayMetaConfiguration.applyTextMeta(e.getValue(), entity.getMeta(), player, player);
});

entity.updateVisibility();

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

entity.updateVisibility();
entity.getPassenger().refresh();
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public void onDestroy() {
return new RefreshTrait(plugin, seconds, TimeUnit.SECONDS, update);
}

public static @NotNull RefreshTrait ofMillis(@NotNull JavaPlugin plugin, long millis, Consumer<NameTagEntity> update) {
return new RefreshTrait(plugin, millis, TimeUnit.MILLISECONDS, update);
}

public static @NotNull RefreshTrait ofTicks(@NotNull JavaPlugin plugin, long ticks, Consumer<NameTagEntity> update) {
return new RefreshTrait(plugin, ticks * 50, TimeUnit.MILLISECONDS, update);
return ofMillis(plugin, ticks * 50, update);
}

}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sneak:
defaults:
# If not enabled then plugin simulates vanilla name tags
enabled: true
# How often should we refresh tags (in milliseconds)
refresh-every: 50
text:
- "<white>%player_name%</white>"
- "<blue>%player_ping%ms</blue>"
Expand Down

0 comments on commit c5b663f

Please sign in to comment.