This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
/
NetPackets.java
171 lines (160 loc) · 5.96 KB
/
NetPackets.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
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc.Utilities;
import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.injector.PacketConstructor;
import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.common.base.Charsets;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.entity.Player;
import org.pircbotx.Channel;
import org.pircbotx.User;
/**
*
* @author cnaude
*/
public class NetPackets {
PurpleIRC plugin;
private final ProtocolManager protocolManager;
private PacketConstructor playerListConstructor;
/**
*
* @param plugin
*/
public NetPackets(PurpleIRC plugin) {
this.plugin = plugin;
protocolManager = ProtocolLibrary.getProtocolManager();
}
/**
*
* @param name
* @param ircBot
* @param channel
*/
public void addToTabList(String name, PurpleBot ircBot, Channel channel) {
if (!plugin.customTabList) {
return;
}
String channelName = channel.getName();
if (ircBot.tabIgnoreNicks.containsKey(channelName)) {
if (ircBot.tabIgnoreNicks.get(channelName).contains(name)) {
plugin.logDebug("Not adding " + name + " to tab list.");
return;
}
}
try {
PacketContainer packet = tabPacket(name, true);
for (Player reciever : plugin.getServer().getOnlinePlayers()) {
if (reciever.hasPermission("irc.tablist")) {
protocolManager.sendServerPacket(reciever, packet);
}
}
} catch (FieldAccessException | InvocationTargetException e) {
plugin.logError(e.getMessage());
}
}
/**
*
* @param name
*/
public void remFromTabList(String name) {
if (!plugin.customTabList) {
return;
}
try {
PacketContainer packet = tabPacket(name, false);
for (Player reciever : plugin.getServer().getOnlinePlayers()) {
if (reciever.hasPermission("irc.tablist")) {
protocolManager.sendServerPacket(reciever, packet);
}
}
} catch (FieldAccessException | InvocationTargetException e) {
plugin.logError(e.getMessage());
}
}
private PacketContainer tabPacket(String name, boolean add) {
String displayName = truncateName(plugin.customTabPrefix + name);
PacketContainer packet = null;
String version = plugin.getServer().getVersion();
plugin.logDebug("tabPacket: " + version);
if (version.contains("MC: 1.7.10") || version.contains("MC: 1.8")) {
try {
packet = protocolManager.createPacket(PacketType.Play.Server.PLAYER_INFO);
packet.getIntegers().write(0, (add ? 0 : 4));
packet.getGameProfiles().write(0, new WrappedGameProfile(java.util.UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)), displayName));
packet.getIntegers().write(1, 0);
packet.getIntegers().write(2, 0);
packet.getStrings().write(0, displayName);
} catch (Exception ex) {
plugin.logError("tabPacket: " + ex.getMessage());
}
} else {
plugin.logDebug("tabPacket: deprecated ");
playerListConstructor = protocolManager.createPacketConstructor(Packets.Server.PLAYER_INFO, "", false, (int) 0);
packet = playerListConstructor.createPacket(displayName, add, 0);
}
return packet;
}
/**
*
* @param player
* @param ircBot
* @param channel
*/
public void updateTabList(Player player, final PurpleBot ircBot, final Channel channel) {
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
for (User user : channel.getUsers()) {
String nick = user.getNick();
addToTabList(nick, ircBot, channel);
}
}
}, 5);
}
/**
*
* @param player
*/
public void updateTabList(Player player) {
if (player.hasPermission("irc.tablist")) {
for (PurpleBot ircBot : plugin.ircBots.values()) {
if (ircBot.isConnected()) {
for (Channel channel : ircBot.getChannels()) {
if (ircBot.isValidChannel(channel.getName())) {
updateTabList(player, ircBot, channel);
}
}
}
}
}
}
private String truncateName(String name) {
if (name.length() > 16) {
return name.substring(0, 15);
} else {
return name;
}
}
}