Skip to content

Commit

Permalink
Cleaned up code and fixed error handling on refresh method
Browse files Browse the repository at this point in the history
Signed-off-by: DevDrizzy <[email protected]>
  • Loading branch information
DevDrizzy committed Aug 11, 2024
1 parent 974077a commit ace1f7a
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 227 deletions.
5 changes: 1 addition & 4 deletions src/main/java/xyz/refinedev/api/tablist/TablistHandler.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package xyz.refinedev.api.tablist;

import com.github.retrooper.packetevents.PacketEventsAPI;

import lombok.Getter;
import lombok.Setter;
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;
import xyz.refinedev.api.tablist.listener.SkinCacheListener;
Expand Down Expand Up @@ -126,7 +123,7 @@ public void unload() {
}

// Destroy main tablist team
Team team = player.getScoreboard().getTeam("ztab");
Team team = player.getScoreboard().getTeam("rtab");
if (team != null) {
team.unregister();
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/xyz/refinedev/api/tablist/TablistPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.PacketEventsAPI;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;

import lombok.Getter;

import org.bukkit.plugin.java.JavaPlugin;

import xyz.refinedev.api.tablist.adapter.impl.ExampleAdapter;
import xyz.refinedev.api.tablist.listener.TeamsPacketListener;

Expand All @@ -22,7 +19,7 @@
@Getter
public class TablistPlugin extends JavaPlugin {

private TablistHandler tablistHandler;
private xyz.refinedev.api.tablist.TablistHandler tablistHandler;
private PacketEventsAPI<?> packetEventsAPI;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package xyz.refinedev.api.tablist.adapter.impl;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import xyz.refinedev.api.tablist.adapter.TabAdapter;
import xyz.refinedev.api.tablist.setup.TabEntry;
import xyz.refinedev.api.tablist.util.Skin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import xyz.refinedev.api.tablist.TablistHandler;
import xyz.refinedev.api.tablist.setup.TabLayout;
import xyz.refinedev.api.tablist.skin.SkinCache;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package xyz.refinedev.api.tablist.listener;

import lombok.RequiredArgsConstructor;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Team;

import xyz.refinedev.api.tablist.TablistHandler;
import xyz.refinedev.api.tablist.setup.TabLayout;
import xyz.refinedev.api.tablist.util.PacketUtils;
Expand Down Expand Up @@ -37,9 +37,11 @@ public void onJoin(PlayerJoinEvent event) {
public void onQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();

Team team = player.getScoreboard().getTeam("ztab");
if (team != null) {
team.removeEntry(player.getName());
Team team = player.getScoreboard().getTeam("rtab");
if (team != null && player.getScoreboard() != Bukkit.getScoreboardManager().getMainScoreboard()) {
if (team.hasEntry(player.getName())) {
team.removeEntry(player.getName());
}
team.unregister();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;

import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerTeams;
import lombok.RequiredArgsConstructor;

import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

import xyz.refinedev.api.tablist.TablistHandler;
import xyz.refinedev.api.tablist.util.GlitchFixEvent;
import xyz.refinedev.api.tablist.util.PacketUtils;

import java.util.EnumSet;
import java.util.Optional;

/**
* <p>
Expand Down Expand Up @@ -66,14 +60,14 @@ public void onPacketSend(PacketSendEvent event) {
WrapperPlayServerTeams teams = new WrapperPlayServerTeams(event);
if (teams.getTeamMode() != WrapperPlayServerTeams.TeamMode.REMOVE_ENTITIES) return;
if (!teams.getTeamName().equals("ztab")) {
if (!teams.getTeamName().equals("rtab")) {
teams.setTeamMode(WrapperPlayServerTeams.TeamMode.ADD_ENTITIES);
teams.setTeamName("tab");
Optional<WrapperPlayServerTeams.ScoreBoardTeamInfo> teamInfo = teams.getTeamInfo();
if (teamInfo.isPresent()) {
WrapperPlayServerTeams.ScoreBoardTeamInfo info = teamInfo.get();
info.setDisplayName(Component.text("ztab"));
info.setDisplayName(Component.text("rtab"));
}
}
} else */if (isClientNew && event.getPacketType() == PacketType.Play.Server.PLAYER_INFO_UPDATE) {
Expand Down Expand Up @@ -118,10 +112,10 @@ private void preventGlitch(Player player, UserProfile userProfile) {
}

Scoreboard scoreboard = player.getScoreboard();
Team team = scoreboard.getTeam("ztab");
Team team = scoreboard.getTeam("rtab");

if (team == null) {
team = scoreboard.registerNewTeam("ztab");
team = scoreboard.registerNewTeam("rtab");
}

if (!team.hasEntry(online.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Setter;
import lombok.experimental.Accessors;
import xyz.refinedev.api.tablist.util.Skin;
import xyz.refinedev.api.tablist.util.TabLatency;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerTeams;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import xyz.refinedev.api.tablist.util.Skin;

/**
Expand Down
77 changes: 40 additions & 37 deletions src/main/java/xyz/refinedev/api/tablist/setup/TabLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.*;

import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerListHeaderAndFooter;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;

import lombok.Getter;
import lombok.extern.log4j.Log4j2;

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;
import xyz.refinedev.api.tablist.adapter.TabAdapter;
import xyz.refinedev.api.tablist.util.PacketUtils;
Expand All @@ -39,7 +38,7 @@ public class TabLayout {

/**
* {@link Integer Mod} is the modification integer
* used to determine rows/columns from index of the {@link TabEntry}.
* used to determine rows/columns from index of the {@link xyz.refinedev.api.tablist.setup.TabEntry}.
*/
@Getter private final int mod;
/**
Expand Down Expand Up @@ -145,9 +144,9 @@ public void create() {

// Add everyone to the "Tab" team
// These aren't really used for 1.17+ except for hiding our own name
Team bukkitTeam = scoreboard.getTeam("ztab");
Team bukkitTeam = scoreboard.getTeam("rtab");
if (bukkitTeam == null) {
bukkitTeam = scoreboard.registerNewTeam("ztab");
bukkitTeam = scoreboard.registerNewTeam("rtab");
}

for (Player target : Bukkit.getOnlinePlayers()) {
Expand Down Expand Up @@ -177,7 +176,7 @@ public void create() {


for ( Player target : Bukkit.getOnlinePlayers() ) {
Team team = target.getScoreboard().getTeam("ztab");
Team team = target.getScoreboard().getTeam("rtab");
if (team == null) continue;
if (team.hasEntry(player.getName())) continue;

Expand All @@ -187,42 +186,46 @@ public void create() {

public void refresh() {
TablistHandler tablistHandler = TablistHandler.getInstance();
List<TabEntry> entries = tablistHandler.getAdapter().getLines(player);
if (entries.isEmpty()) {
for ( int i = 0; i < 80; i++ ) {
this.update(i, "", 0, Skin.DEFAULT_SKIN);
}
return;
}

for ( int i = 0; i < 80; i++ ) {
TabEntry entry = i < entries.size() ? entries.get(i) : null;
if (entry == null) {
this.update(i, "", 0, Skin.DEFAULT_SKIN);
continue;
try {
List<xyz.refinedev.api.tablist.setup.TabEntry> entries = tablistHandler.getAdapter().getLines(player);
if (entries.isEmpty()) {
for ( int i = 0; i < 80; i++ ) {
this.update(i, "", 0, Skin.DEFAULT_SKIN);
}
return;
}

final int x = entry.getX();
final int y = entry.getY();
for ( int i = 0; i < 80; i++ ) {
xyz.refinedev.api.tablist.setup.TabEntry entry = i < entries.size() ? entries.get(i) : null;
if (entry == null) {
this.update(i, "", 0, Skin.DEFAULT_SKIN);
continue;
}

if (x >= 3 && PacketUtils.isLegacyClient(player)) continue;
final int x = entry.getX();
final int y = entry.getY();

final int index = y * mod + x;
if (x >= 3 && PacketUtils.isLegacyClient(player)) continue;

try {
this.update(index, entry.getText(), entry.getPing(), entry.getSkin());
} catch (NullPointerException e) {
if (tablistHandler.getPlugin().getName().equals("Bolt") && !tablistHandler.isDebug()) {
continue;
final int index = y * mod + x;
try {
this.update(index, entry.getText(), entry.getPing(), entry.getSkin());
} catch (Exception e) {
log.fatal("[{}] There was an error updating tablist for {}", tablistHandler.getPlugin().getName(), player.getName());
log.error(e);
e.printStackTrace();
}
log.fatal("[{}] There was an error updating tablist for {}", tablistHandler.getPlugin().getName(), player.getName());
log.error(e);
e.printStackTrace();
} catch (Exception e) {
}
} catch (NullPointerException e) {
if (!tablistHandler.getPlugin().getName().equals("Bolt") || tablistHandler.isDebug()) {
log.fatal("[{}] There was an error updating tablist for {}", tablistHandler.getPlugin().getName(), player.getName());
log.error(e);
e.printStackTrace();
}
} catch (Exception e) {
log.fatal("[{}] There was an error updating tablist for {}", tablistHandler.getPlugin().getName(), player.getName());
log.error(e);
e.printStackTrace();
}

this.setHeaderAndFooter();
Expand Down Expand Up @@ -363,7 +366,7 @@ public void update(int index, String text, int ping, Skin skin) {
}

/**
* Update the {@link TabEntry}'s ping
* Update the {@link xyz.refinedev.api.tablist.setup.TabEntry}'s ping
*
* @param info {@link TabEntryInfo info}
* @param ping {@link Integer ping}
Expand Down Expand Up @@ -563,4 +566,4 @@ public String getTeamAt(int index) {
}

}
}
}
2 changes: 0 additions & 2 deletions src/main/java/xyz/refinedev/api/tablist/skin/CachedSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.UUID;

/**
* This Project is property of Refine Development © 2021 - 2023
* Redistribution of this Project is not allowed
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/xyz/refinedev/api/tablist/skin/SkinCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import lombok.extern.log4j.Log4j2;

import org.bukkit.entity.Player;
import xyz.refinedev.api.tablist.util.Skin;

Expand All @@ -17,7 +15,8 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import xyz.refinedev.api.tablist.TablistHandler;
import xyz.refinedev.api.tablist.setup.TabEntry;
import xyz.refinedev.api.tablist.setup.TabLayout;

/**
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/xyz/refinedev/api/tablist/util/BaseEvent.java

This file was deleted.

Loading

0 comments on commit ace1f7a

Please sign in to comment.