|
|
|
@ -5,10 +5,9 @@ import com.comphenix.protocol.ProtocolManager;
|
|
|
|
|
import com.comphenix.protocol.events.PacketContainer;
|
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedChatComponent;
|
|
|
|
|
import hu.ditservices.STPlugin;
|
|
|
|
|
import hu.ditservices.utils.*;
|
|
|
|
|
import hu.ditservices.utils.Math;
|
|
|
|
|
import hu.ditservices.utils.reflection.ClazzContainer;
|
|
|
|
|
import hu.ditservices.utils.reflection.Reflection;
|
|
|
|
|
import hu.ditservices.utils.Server;
|
|
|
|
|
import hu.ditservices.utils.TPS;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
@ -17,9 +16,6 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TabHandler {
|
|
|
|
|
public final List<String> headers = new ArrayList<>();
|
|
|
|
|
public final List<String> footers = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public final List<WrappedChatComponent> headerComponents = new ArrayList<>();
|
|
|
|
|
public final List<WrappedChatComponent> footerComponents = new ArrayList<>();
|
|
|
|
|
|
|
|
|
@ -37,31 +33,20 @@ public class TabHandler {
|
|
|
|
|
private int count2 = 0; //footers
|
|
|
|
|
private Object packet;
|
|
|
|
|
|
|
|
|
|
private boolean isNewerThan1_20_2;
|
|
|
|
|
|
|
|
|
|
public TabHandler() throws Exception {
|
|
|
|
|
public TabHandler() {
|
|
|
|
|
|
|
|
|
|
this.plugin = STPlugin.getInstance();
|
|
|
|
|
this.isNewerThan1_20_2 = Version.ServerVersion.isCurrentHigher(Version.ServerVersion.v1_20_R2);
|
|
|
|
|
|
|
|
|
|
if(this.init()){
|
|
|
|
|
if ((headers.isEmpty() && footers.isEmpty() && !isNewerThan1_20_2)
|
|
|
|
|
|| (headerComponents.isEmpty() && footerComponents.isEmpty() && isNewerThan1_20_2)){
|
|
|
|
|
plugin.getLogger().warning(plugin.getPrefix()+"TAB customization disabled because empty customization config!");
|
|
|
|
|
if (headerComponents.isEmpty() && footerComponents.isEmpty()){
|
|
|
|
|
plugin.getLogger().warning(plugin.getPrefix()+"TAB customization disabled because empty customization config or feature related errors!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.updateTab();
|
|
|
|
|
/*packet = plugin.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER);
|
|
|
|
|
boolean both = headers.size() > 0 && footers.size() > 0;
|
|
|
|
|
boolean header = !both && headers.size() > 0;
|
|
|
|
|
boolean footer = !both && footers.size() > 0;*/
|
|
|
|
|
|
|
|
|
|
//scheduleSyncRepeatingTask
|
|
|
|
|
//Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new TabRunnable(this),20, Math.convert(Math.Convert.SECONDS,Math.Convert.TICKS,this.refreshRate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean init() throws Exception {
|
|
|
|
|
private boolean init() {
|
|
|
|
|
if (plugin.getConfig().getBoolean("Tab.enabled")){
|
|
|
|
|
|
|
|
|
|
int availProcessors = Server.getCpuCores();
|
|
|
|
@ -124,20 +109,12 @@ public class TabHandler {
|
|
|
|
|
if (Bukkit.getOnlinePlayers().isEmpty()){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isNewerThan1_20_2) {
|
|
|
|
|
if (count1 >= headerComponents.size()) {
|
|
|
|
|
count1 = 0;
|
|
|
|
|
}
|
|
|
|
|
if (count2 >= footerComponents.size()) {
|
|
|
|
|
count2 = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (count1 >= headers.size()) {
|
|
|
|
|
count1 = 0;
|
|
|
|
|
}
|
|
|
|
|
if (count2 >= footers.size()) {
|
|
|
|
|
count2 = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count1 >= headerComponents.size()) {
|
|
|
|
|
count1 = 0;
|
|
|
|
|
}
|
|
|
|
|
if (count2 >= footerComponents.size()) {
|
|
|
|
|
count2 = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Re adding all lines where we replaced something like the RAM usage to every refresh
|
|
|
|
@ -153,36 +130,21 @@ public class TabHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Version.ServerVersion.isCurrentEqualOrLower(Version.ServerVersion.v1_12_R1)) {
|
|
|
|
|
packet = ClazzContainer.buildPacketPlayOutPlayerListHeaderFooter(headers.get(count1),footers.get(count2));
|
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()){
|
|
|
|
|
Reflection.sendPacket(player,packet);
|
|
|
|
|
}
|
|
|
|
|
} else if (Version.ServerVersion.isCurrentHigher(Version.ServerVersion.v1_20_R2)) {
|
|
|
|
|
// Use ProtocolLib for versions higher than 1.20.2
|
|
|
|
|
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
|
|
|
|
|
PacketContainer packet = protocolManager.createPacket(com.comphenix.protocol.PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER);
|
|
|
|
|
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
|
|
|
|
|
PacketContainer packet = protocolManager.createPacket(com.comphenix.protocol.PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER);
|
|
|
|
|
|
|
|
|
|
// Set header and footer
|
|
|
|
|
packet.getChatComponents().write(0, headerComponents.get(count1));
|
|
|
|
|
packet.getChatComponents().write(1, footerComponents.get(count2));
|
|
|
|
|
packet.getChatComponents().write(0, headerComponents.get(count1));
|
|
|
|
|
packet.getChatComponents().write(1, footerComponents.get(count2));
|
|
|
|
|
|
|
|
|
|
// Send the packet to all players
|
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
|
protocolManager.sendServerPacket(player, packet);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Use the built-in Spigot API for versions between 1.13 and 1.20.1
|
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
|
player.setPlayerListHeaderFooter(headers.get(count1), footers.get(count2));
|
|
|
|
|
}
|
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
|
protocolManager.sendServerPacket(player, packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (headers.size() > 1 || headerComponents.size() > 1) {
|
|
|
|
|
if (headerComponents.size() > 1) {
|
|
|
|
|
count1++;
|
|
|
|
|
}
|
|
|
|
|
if (footers.size() > 1 || footerComponents.size() > 1) {
|
|
|
|
|
if (footerComponents.size() > 1) {
|
|
|
|
|
count2++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -199,47 +161,28 @@ public class TabHandler {
|
|
|
|
|
* Manages the adding of the tab line and the final formatting of the text.
|
|
|
|
|
* @param header If true the text will be added to the header, otherwise to the footer.
|
|
|
|
|
* @param text The formatted tab line text.
|
|
|
|
|
* @param dynamic If the text contains a replace which need to run every tab refresh then it's true.
|
|
|
|
|
* @param dynamic If the text contains a replacement which need to run every tab refresh then it's true.
|
|
|
|
|
* @param index Index of the 'dynamic' line. This is an overloading so there we need the index[0] element.
|
|
|
|
|
*/
|
|
|
|
|
private void addHeaderFooter(boolean header,String text,boolean dynamic,int... index) {
|
|
|
|
|
try {
|
|
|
|
|
String Json = "{\"text\": \""+format(text)+"\"}";
|
|
|
|
|
boolean isNewerThan1202 = Version.ServerVersion.isCurrentHigher(Version.ServerVersion.v1_20_R2);
|
|
|
|
|
WrappedChatComponent tabText = WrappedChatComponent.fromJson(Json);
|
|
|
|
|
|
|
|
|
|
if (isNewerThan1202) {
|
|
|
|
|
WrappedChatComponent tabText = WrappedChatComponent.fromJson(Json);
|
|
|
|
|
|
|
|
|
|
if (header){
|
|
|
|
|
if (dynamic){
|
|
|
|
|
headerComponents.set(index[0], tabText);
|
|
|
|
|
}else {
|
|
|
|
|
headerComponents.add(tabText);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if (dynamic){
|
|
|
|
|
footerComponents.set(index[0], tabText);
|
|
|
|
|
}else {
|
|
|
|
|
footerComponents.add(tabText);
|
|
|
|
|
}
|
|
|
|
|
if (header){
|
|
|
|
|
if (dynamic){
|
|
|
|
|
headerComponents.set(index[0], tabText);
|
|
|
|
|
}else {
|
|
|
|
|
headerComponents.add(tabText);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Object tabText = Reflection.asChatSerializer(Json);
|
|
|
|
|
|
|
|
|
|
if (header){
|
|
|
|
|
if (dynamic){
|
|
|
|
|
headers.set(index[0], Reflection.getChatSerializerString(tabText));
|
|
|
|
|
}else {
|
|
|
|
|
headers.add(Reflection.getChatSerializerString(tabText));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if (dynamic){
|
|
|
|
|
footers.set(index[0], Reflection.getChatSerializerString(tabText));
|
|
|
|
|
}else {
|
|
|
|
|
footers.add(Reflection.getChatSerializerString(tabText));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if (dynamic){
|
|
|
|
|
footerComponents.set(index[0], tabText);
|
|
|
|
|
} else {
|
|
|
|
|
footerComponents.add(tabText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|