public class PlayerListener
implements Listener {
private final TabPlugin plugin;
private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
this.onPlayerEntrance();
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
this.onPlayerEntrance();
}
public void onPlayerEntrance() {
EXECUTOR_SERVICE.schedule(() -> {
ByteBuf out = Unpooled.buffer();
out.writeByte(1);
Collection players = this.plugin.getServer().getOnlinePlayers();
out.writeInt(players.size());
players.forEach(onlinePlayer -> {
User user = this.plugin.getLuckPerms().getUserManager().getUser(onlinePlayer.getUniqueId());
if (user == null) {
this.plugin.getLogger().severe(String.format("Player %s not found in LuckPerms!", onlinePlayer.getDisplayName()));
}
String primaryGroupName = user.getPrimaryGroup();
ByteBufUtils.writeUTF8String(out, this.getGroup(primaryGroupName).getPrefix());
ByteBufUtils.writeUTF8String(out, onlinePlayer.getName());
ByteBufUtils.writeUTF8String(out, onlinePlayer.getDisplayName());
ByteBufUtils.writeUTF8String(out, this.getGroup(primaryGroupName).getColor());
out.writeInt(this.getGroup(primaryGroupName).getPriority());
out.writeInt(ReflectionUtils.getPing(this.plugin, onlinePlayer));
ReflectionUtils.serializeGameProfile(this.plugin, out, onlinePlayer);
});
byte[] bytes = new byte[out.readableBytes()];
out.readBytes(bytes);
players.forEach(player -> player.sendPluginMessage((Plugin)this.plugin, "tabinfo", bytes));
}, 5L, TimeUnit.SECONDS);
}
private GroupInfoModel getGroup(String primaryGroupName) {
Map<String, GroupInfoModel> groupMap = this.plugin.getConfiguration().getGroupInfoByNameMap();
return Optional.ofNullable(groupMap.get(primaryGroupName)).orElse(groupMap.get("default"));
}
public PlayerListener(TabPlugin plugin) {
this.plugin = plugin;
}
}