From 9d0cbd154e8698837e6fd9a471d4126e1a8330e9 Mon Sep 17 00:00:00 2001 From: MattMX Date: Wed, 11 Sep 2024 23:41:12 +0100 Subject: [PATCH] :fire: Remove debug, API is now working and probably finished. --- .../com/mattmx/nametags/EventsListener.java | 2 + .../java/com/mattmx/nametags/NameTags.java | 5 -- .../mattmx/nametags/entity/NameTagEntity.java | 5 ++ .../mattmx/nametags/entity/TestImplTrait.java | 44 ---------------- .../nametags/entity/trait/RefreshTrait.java | 52 +++++++++++++++++++ .../nametags/entity/trait/TraitHolder.java | 4 ++ 6 files changed, 63 insertions(+), 49 deletions(-) delete mode 100644 src/main/java/com/mattmx/nametags/entity/TestImplTrait.java create mode 100644 src/main/java/com/mattmx/nametags/entity/trait/RefreshTrait.java diff --git a/src/main/java/com/mattmx/nametags/EventsListener.java b/src/main/java/com/mattmx/nametags/EventsListener.java index b6e9f6e..2851b6b 100644 --- a/src/main/java/com/mattmx/nametags/EventsListener.java +++ b/src/main/java/com/mattmx/nametags/EventsListener.java @@ -1,6 +1,8 @@ package com.mattmx.nametags; import com.mattmx.nametags.entity.NameTagEntity; +import com.mattmx.nametags.entity.trait.RefreshTrait; +import com.mattmx.nametags.event.NameTagEntityCreateEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerChangedWorldEvent; diff --git a/src/main/java/com/mattmx/nametags/NameTags.java b/src/main/java/com/mattmx/nametags/NameTags.java index a93743a..3f2e6f2 100644 --- a/src/main/java/com/mattmx/nametags/NameTags.java +++ b/src/main/java/com/mattmx/nametags/NameTags.java @@ -3,13 +3,11 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.util.Vector3f; import com.mattmx.nametags.entity.NameTagEntityManager; -import com.mattmx.nametags.entity.TestImplTrait; import me.tofaa.entitylib.APIConfig; import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.meta.display.AbstractDisplayMeta; import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.plugin.java.JavaPlugin; @@ -54,9 +52,6 @@ public void onEnable() { .registerListener(packetListener); Bukkit.getPluginManager().registerEvents(eventsListener, this); - - // DEBUG - Bukkit.getPluginManager().registerEvents(new TestImplTrait(), this); } public @NotNull NameTagEntityManager getEntityManager() { diff --git a/src/main/java/com/mattmx/nametags/entity/NameTagEntity.java b/src/main/java/com/mattmx/nametags/entity/NameTagEntity.java index a479ee0..3d62b5f 100644 --- a/src/main/java/com/mattmx/nametags/entity/NameTagEntity.java +++ b/src/main/java/com/mattmx/nametags/entity/NameTagEntity.java @@ -12,6 +12,7 @@ import me.tofaa.entitylib.wrapper.WrapperEntity; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.TextDisplay; import org.jetbrains.annotations.NotNull; import java.util.function.Consumer; @@ -52,6 +53,10 @@ public void modify(Consumer consumer) { this.passenger.consumeEntityMeta(TextDisplayMeta.class, consumer); } + public @NotNull TextDisplayMeta getMeta() { + return this.passenger.getEntityMeta(TextDisplayMeta.class); + } + public void sendPassengerPacket(Player target) { PacketEvents.getAPI() .getPlayerManager() diff --git a/src/main/java/com/mattmx/nametags/entity/TestImplTrait.java b/src/main/java/com/mattmx/nametags/entity/TestImplTrait.java deleted file mode 100644 index daab3ed..0000000 --- a/src/main/java/com/mattmx/nametags/entity/TestImplTrait.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.mattmx.nametags.entity; - -import com.mattmx.nametags.NameTags; -import com.mattmx.nametags.entity.trait.Trait; -import com.mattmx.nametags.event.NameTagEntityCreateEvent; -import net.kyori.adventure.text.format.NamedTextColor; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.scheduler.BukkitTask; -import org.jetbrains.annotations.NotNull; - -public class TestImplTrait implements Listener { - - @EventHandler - public void onNameTagCreate(@NotNull NameTagEntityCreateEvent event) { - event.getNameTag() - .getTraits() - .getOrAddTrait(RefreshTrait.class, RefreshTrait::new); - } - - static class RefreshTrait extends Trait { - private int i = 0; - private boolean cancel = false; - private final @NotNull BukkitTask task = Bukkit.getScheduler() - .runTaskTimerAsynchronously(NameTags.getInstance(), () -> { - if (cancel) return; - - getTag().modify((meta) -> { - meta.setText(getTag().getBukkitEntity() - .name() - .color(i++ % 2 == 0 ? NamedTextColor.RED : NamedTextColor.GREEN)); - }); - - }, 0L, 20L); - - @Override - public void onDestroy() { - cancel = true; - task.cancel(); - } - } - -} diff --git a/src/main/java/com/mattmx/nametags/entity/trait/RefreshTrait.java b/src/main/java/com/mattmx/nametags/entity/trait/RefreshTrait.java new file mode 100644 index 0000000..469fb91 --- /dev/null +++ b/src/main/java/com/mattmx/nametags/entity/trait/RefreshTrait.java @@ -0,0 +1,52 @@ +package com.mattmx.nametags.entity.trait; + +import com.mattmx.nametags.entity.NameTagEntity; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class RefreshTrait extends Trait { + private final @NotNull ScheduledTask task; + private boolean paused = false; + + public RefreshTrait(@NotNull JavaPlugin plugin, long period, TimeUnit unit, Consumer update) { + this.task = Bukkit.getAsyncScheduler() + .runAtFixedRate(plugin, (task) -> { + + if (!this.isPaused()) { + update.accept(getTag()); + } + + }, 0L, period, unit); + } + + public void setPaused(boolean paused) { + this.paused = paused; + } + + public boolean isPaused() { + return this.paused; + } + + @Override + public void onDestroy() { + task.cancel(); + } + + public static @NotNull RefreshTrait ofMinutes(@NotNull JavaPlugin plugin, long minutes, Consumer update) { + return new RefreshTrait(plugin, minutes, TimeUnit.MINUTES, update); + } + + public static @NotNull RefreshTrait ofSeconds(@NotNull JavaPlugin plugin, long seconds, Consumer update) { + return new RefreshTrait(plugin, seconds, TimeUnit.SECONDS, update); + } + + public static @NotNull RefreshTrait ofTicks(@NotNull JavaPlugin plugin, long ticks, Consumer update) { + return new RefreshTrait(plugin, ticks * 50, TimeUnit.MILLISECONDS, update); + } + +} diff --git a/src/main/java/com/mattmx/nametags/entity/trait/TraitHolder.java b/src/main/java/com/mattmx/nametags/entity/trait/TraitHolder.java index 399fa45..b611f63 100644 --- a/src/main/java/com/mattmx/nametags/entity/trait/TraitHolder.java +++ b/src/main/java/com/mattmx/nametags/entity/trait/TraitHolder.java @@ -46,6 +46,10 @@ public TraitHolder(@NotNull NameTagEntity owner) { return null; } + public boolean hasTrait(@NotNull Class traitClazz) { + return map.containsKey(traitClazz); + } + public void destroy() { for (Trait trait : map.values()) { trait.onDestroy();