/
TabAPI.java
395 lines (322 loc) · 10.4 KB
/
TabAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package org.mcsg.double0negative.tabapi;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
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.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
/**
* TabAPI
*
* Provides a simple interface for adding custom text to
* display on the minecraft tab menu on a player/plugin
* basis
*
* @author Double0negative
*
*
*/
public class TabAPI extends JavaPlugin implements Listener, CommandExecutor{
private static HashMap<String, TabObject>playerTab = new HashMap<String, TabObject>();
private static HashMap<String, TabHolder>playerTabLast = new HashMap<String, TabHolder>();
private static HashMap<Player, ArrayList<PacketContainer>>cachedPackets = new HashMap<Player, ArrayList<PacketContainer>>();
private static HashMap<Player, Integer>updateSchedules = new HashMap<Player, Integer>();
private static int horzTabSize = 3;
private static int vertTabSize = 20;
private static String[] colors = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a","b", "c", "d", "f", "g", "h", "i", "j","k","l","m","n","o","p","r","s","t","u","v","w","x","y","z"};
private static int e = 0;
private static int r = 0;
private static long flickerPrevention = 5L;
private static ProtocolManager protocolManager;
private static boolean shuttingdown = false;
private static TabAPI plugin;
public void onEnable(){
TabAPI.plugin = this;
// read initial config if there's any
FileConfiguration config = getConfig();
config.options().copyDefaults(true);
// add defaults if there are new ones
MemoryConfiguration defaultConfig = new MemoryConfiguration();
defaultConfig.set("flickerPrevention", flickerPrevention);
config.setDefaults(defaultConfig);
saveConfig();
// load config settings
reloadConfiguration();
this.getCommand("tabapi").setExecutor(this);
try {
new Metrics(this).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
protocolManager = ProtocolLibrary.getProtocolManager();
Bukkit.getServer().getPluginManager().registerEvents(this, this);
for(Player p: Bukkit.getOnlinePlayers()){
Plugin plugin = Bukkit.getPluginManager().getPlugin("TabAPI");
setPriority(plugin, p, 2);
resetTabList(p);
setPriority(plugin, p, -2);
}
protocolManager.addPacketListener(new PacketAdapter(this,
ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.PLAYER_INFO) {
@Override
public void onPacketSending(PacketEvent event) {
switch (event.getPacketID()) {
case Packets.Server.PLAYER_INFO:
PacketContainer p = event.getPacket();
String s = p.getStrings().read(0);
if(s.startsWith("$")){ // this is a packet sent by TabAPI **Work around until I figure out how to make my own
p.getStrings().write(0,s.substring(1)); // packets bypass this block**
event.setPacket(p);
}
else{
event.setCancelled(true);
}
break;
}
}
});
}
public void reloadConfiguration() {
reloadConfig();
flickerPrevention = getConfig().getLong("flickerPrevention");
}
public void onDisable(){
shuttingdown = true;
for(Player p: Bukkit.getOnlinePlayers()){
clearTab(p);
}
flushPackets();
playerTab = null;
playerTabLast = null;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd1, String commandLabel, String[] args){
PluginDescriptionFile pdfFile = this.getDescription();
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
if(args.length == 1 && player.hasPermission("tabapi.reload")){
reloadConfiguration();
updateAll();
}
else{
player.sendMessage(ChatColor.DARK_RED +""+ ChatColor.BOLD +"TabAPI - Double0negative"+ChatColor.RESET+ ChatColor.RED +" Version: "+ pdfFile.getVersion() );
}
}
else{
sender.sendMessage(ChatColor.DARK_RED +""+ ChatColor.BOLD +"TabAPI - Double0negative"+ChatColor.RESET+ ChatColor.RED +" Version: "+ pdfFile.getVersion() );
return true;
}
return true;
}
private static void addPacket(Player p, String msg, boolean b, int ping){
PacketContainer message = protocolManager.createPacket(Packets.Server.PLAYER_INFO);
message.getStrings().write(0, ((!shuttingdown)?"$":"")+msg);
message.getBooleans().write(0, b);
message.getIntegers().write(0, ping);
ArrayList<PacketContainer> packetList = cachedPackets.get(p);
if (packetList == null) {
packetList = new ArrayList<PacketContainer>();
cachedPackets.put(p, packetList);
}
packetList.add(message);
}
private static void flushPackets() {
final Player[] packetPlayers = cachedPackets.keySet().toArray(new Player[0]);
for (Player p : packetPlayers) {
flushPackets(p, null);
}
}
private static void flushPackets(final Player p, final TabHolder tabCopy) {
final PacketContainer[] packets = (PacketContainer[]) cachedPackets.get(p).toArray(new PacketContainer[0]);
// cancel old task (prevents flickering)
Integer taskID = updateSchedules.get(p);
if (taskID != null) {
Bukkit.getScheduler().cancelTask(taskID);
}
taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(TabAPI.plugin, new Runnable() {
@Override
public void run() {
if (p.isOnline()) {
for (PacketContainer packet : packets) {
try {
protocolManager.sendServerPacket(p, packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
System.out.println("[TabAPI] Error sending packet to client");
}
}
}
if (tabCopy != null) playerTabLast.put(p.getName(), tabCopy); // we set this only if we really finally flush it (which is just now)
updateSchedules.remove(p); // we're done, no need to cancel this one on next run
}
}, flickerPrevention);
// let's keep a reference to be able to cancel this (see above)
updateSchedules.put(p, taskID);
cachedPackets.remove(p);
}
private static TabObject getTab(Player p){
TabObject tabo = playerTab.get(p.getName());
if(tabo == null){
tabo = new TabObject();
playerTab.put(p.getName(), tabo);
}
return tabo;
}
/**
* Priorities
*
* -2 = no longer active, remove
* -1 = background, only show if nothing else is there
* 0 = normal
* 1 = high priority
* 2 = always show, only use if MUST show
*
*/
public static void setPriority(Plugin plugin, Player player, int pri){
getTab(player).setPriority(plugin, pri);
}
/**
* Returns the tab list to the vanilla tab list for a player.
* If another plugin holds higher priority, this does notning
* @param p
*/
public static void disableTabForPlayer(Player p){
playerTab.put(p.getName(), null);
resetTabList(p);
}
/**
* Resets tab to normal tab.
* @param p
*/
public static void resetTabList(Player p){
int a = 0; int b = 0;
for(Player pl : Bukkit.getOnlinePlayers()){
setTabString(Bukkit.getPluginManager().getPlugin("TabAPI"), p, a, b, pl.getPlayerListName());
b++;
if(b > horzTabSize){
b = 0;
a++;
}
}
}
public static void setTabString(Plugin plugin, Player p, int x, int y, String msg){
setTabString(plugin, p, x, y, msg, 0);
}
/**
* Set the tab for a player.
*
* If the plugin the tab is being set from does not have a priority, It will automatically be give a base
* priority of 0
*
* @param plugin
* @param p
* @param x
* @param y
* @param msg
* @param ping
*/
public static void setTabString(Plugin plugin, Player p, int x, int y, String msg, int ping){
try{
TabObject tabo = getTab(p);
tabo.setTab(plugin, x, y,msg,ping);
playerTab.put(p.getName(), tabo);
}catch(Exception e){e.printStackTrace();}
}
/**
* Updates a players tab
*
* A tab will be updated with the tab from the highest priority plugin
*
* @param p
*/
public static void updatePlayer(Player p){
if(!p.isOnline()) return;
r = 0; e = 0;
TabObject tabo = playerTab.get(p.getName());
TabHolder tab = tabo.getTab();
if(tab == null) return;
/* need to clear the tab first */
clearTab(p);
for(int b = 0; b < tab.maxv; b++){
for(int a = 0; a < tab.maxh ; a++){
// fix empty tabs
if(tab.tabs[a][b] == null) tab.tabs[a][b] = nextNull();
String msg = tab.tabs[a][b];
int ping = tab.tabPings[a][b];
addPacket(p, (msg == null)? " ": msg.substring(0, Math.min(msg.length(), 16)), true, ping);
}
}
flushPackets(p, tab.getCopy());
}
/**
* Clear a players tab menu
* @param p
*/
public static void clearTab(Player p){
if(!p.isOnline())return;
TabHolder tabold = playerTabLast.get(p.getName());
if(tabold != null){
for(String [] s: tabold.tabs){
for(String msg:s){
if(msg != null){
addPacket(p, msg.substring(0, Math.min(msg.length(), 16)), false, 0);
}
}
}
}
}
public static void updateAll(){
for(Player p:Bukkit.getOnlinePlayers()){
updatePlayer(p);
}
}
/* return the next null filler */
public static String nextNull(){
String s = "";
for(int a = 0; a < r; a++){
s = " "+s;
}
s = s + "\u00A7" + colors[e];
e++;
if(e > 14){
e = 0;
r++;
}
return s;
}
@EventHandler(priority = EventPriority.HIGHEST)
public void PlayerLeave(PlayerQuitEvent e){
//cleanup
playerTab.remove(e.getPlayer().getName());
playerTabLast.remove(e.getPlayer().getName());
}
public static int getVertSize(){
return vertTabSize;
}
public static int getHorizSize(){
return horzTabSize;
}
}