Skip to content

Commit

Permalink
Fixed 1.7 tablist and added unload logic
Browse files Browse the repository at this point in the history
Signed-off-by: DevDrizzy <[email protected]>
  • Loading branch information
DevDrizzy committed Dec 1, 2023
1 parent 004561d commit 6130484
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
45 changes: 44 additions & 1 deletion src/main/java/xyz/refinedev/api/tablist/TablistHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import lombok.extern.log4j.Log4j2;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Team;

import xyz.refinedev.api.tablist.adapter.TabAdapter;
import xyz.refinedev.api.tablist.adapter.impl.ExampleAdapter;
Expand All @@ -16,6 +19,7 @@
import xyz.refinedev.api.tablist.setup.TabLayout;
import xyz.refinedev.api.tablist.skin.SkinCache;
import xyz.refinedev.api.tablist.thread.TablistThread;
import xyz.refinedev.api.tablist.util.PacketUtils;

import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -45,6 +49,10 @@ public class TablistHandler {
* Tablist Adapter of this instance
*/
private TabAdapter adapter;
/**
* Main tablist listener
*/
private TabListener listener;
/**
* This thread handles all the operations surrounding
* ticking and updating the NameTags
Expand All @@ -66,9 +74,10 @@ public TablistHandler(JavaPlugin plugin) {
public void init(PacketEventsAPI<?> packetEventsAPI, TeamsPacketListener listener) {
this.packetEvents = packetEventsAPI;
this.adapter = new ExampleAdapter();
this.listener = new TabListener(this);

this.packetEvents.getEventManager().registerListener(listener);
Bukkit.getPluginManager().registerEvents(new TabListener(this), plugin);
Bukkit.getPluginManager().registerEvents(this.listener, plugin);

this.setupSkinCache();
}
Expand All @@ -95,6 +104,40 @@ public void registerAdapter(TabAdapter tabAdapter, long ticks) {
}

public void unload() {
if (this.listener != null) {
HandlerList.unregisterAll(this.listener);
this.listener = null;
}

// Destroy player scoreboards.
for ( Map.Entry<UUID, TabLayout> entry : this.layoutMapping.entrySet()) {
UUID uuid = entry.getKey();

Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) {
continue;
}

// Destroy 1.7 teams
if (PacketUtils.isLegacyClient(player)) {
for ( int i = 0; i < 60; i++ ) {
Team team = player.getScoreboard().getTeam("$" + TabLayout.TAB_NAMES[i]);
if (team != null) {
team.unregister();
}
}
}

// Destroy main tablist team
Team team = player.getScoreboard().getTeam("tab");
if (team != null) {
team.unregister();
}

this.layoutMapping.remove(uuid);
}

this.thread.cancel();
this.thread = null;
}
}
12 changes: 6 additions & 6 deletions src/main/java/xyz/refinedev/api/tablist/setup/TabLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

import xyz.refinedev.api.tablist.TablistHandler;
Expand Down Expand Up @@ -182,7 +183,7 @@ public void setHeaderAndFooter() {
* @param ping {@link Integer Latency}
* @param skin {@link Skin Entry Skin}
*/
public void update(int index, int x, int y, String text, int ping, Skin skin) {
public void update(int index, String text, int ping, Skin skin) {
if (PacketUtils.isLegacyClient(player) && index >= 60) {
return;
}
Expand Down Expand Up @@ -212,20 +213,19 @@ public void update(int index, int x, int y, String text, int ping, Skin skin) {

// 1.7 and below support
if (PacketUtils.isLegacyClient(player)) {
Team bukkitTeam = player.getScoreboard().getTeam(team);
Scoreboard scoreboard = player.getScoreboard();
Team bukkitTeam = scoreboard.getTeam(team);
boolean teamExists = bukkitTeam != null;

// This is a new entry, make it's team
if (bukkitTeam == null) {
bukkitTeam = player.getScoreboard().registerNewTeam(team);
bukkitTeam = scoreboard.registerNewTeam(team);
bukkitTeam.addEntry(displayName);
}

if (changed || !teamExists) {
bukkitTeam.setPrefix(prefix);
if (!suffix.isEmpty()) {
bukkitTeam.setSuffix(suffix);
}
bukkitTeam.setSuffix(suffix);
}
this.updatePing(entry, ping);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void tick() {
final int i = y * layout.getMod() + x;

try {
layout.update(i, x, y, entry.getText(), entry.getPing(), entry.getSkin());
layout.update(i, entry.getText(), entry.getPing(), entry.getSkin());
} catch (NullPointerException e) {
if (handler.getPlugin().getName().equals("Bolt") && !handler.isDebug()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class StringUtils {


// Thnx scifi, love you <3

//TODO: Last colors in scoreboard team suffix not working properly
public String[] split(String text) {
if (text.length() <= 16) {
return new String[] { text, "" };
Expand All @@ -45,7 +47,7 @@ public String[] split(String text) {
prefix = prefix.substring(0, 14);
suffix = text.substring(14);
} else {
suffix = StringUtils.getLastColors(StringUtils.color(prefix)) + text.substring(16);
suffix = StringUtils.getLastColors(prefix) + text.substring(16);
}

return new String[] { prefix, suffix };
Expand Down

0 comments on commit 6130484

Please sign in to comment.