Skip to content

Commit 4dbbb44

Browse files
committed
packetwrapper (dmulloy2)
1 parent 9b30568 commit 4dbbb44

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packetwrapper;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.ProtocolLibrary;
23+
import com.comphenix.protocol.events.PacketContainer;
24+
import com.google.common.base.Objects;
25+
import org.bukkit.entity.Player;
26+
27+
import java.lang.reflect.InvocationTargetException;
28+
29+
public abstract class AbstractPacket {
30+
31+
// The packet we will be modifying
32+
protected PacketContainer handle;
33+
34+
/**
35+
* Constructs a new strongly typed wrapper for the given packet.
36+
*
37+
* @param handle - handle to the raw packet data.
38+
* @param type - the packet type.
39+
*/
40+
protected AbstractPacket(PacketContainer handle, PacketType type) {
41+
// Make sure we're given a valid packet
42+
if (handle == null)
43+
throw new IllegalArgumentException("Packet handle cannot be NULL.");
44+
if (!Objects.equal(handle.getType(), type))
45+
throw new IllegalArgumentException(handle.getHandle()
46+
+ " is not a packet of type " + type);
47+
48+
this.handle = handle;
49+
}
50+
51+
/**
52+
* Retrieve a handle to the raw packet data.
53+
*
54+
* @return Raw packet data.
55+
*/
56+
public PacketContainer getHandle() {
57+
return handle;
58+
}
59+
60+
/**
61+
* Send the current packet to the given receiver.
62+
*
63+
* @param receiver - the receiver.
64+
* @throws RuntimeException If the packet cannot be sent.
65+
*/
66+
public void sendPacket(Player receiver) {
67+
try {
68+
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver,
69+
getHandle());
70+
} catch (InvocationTargetException e) {
71+
throw new RuntimeException("Cannot send packet.", e);
72+
}
73+
}
74+
75+
/**
76+
* Send the current packet to all online players.
77+
*/
78+
public void broadcastPacket() {
79+
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle());
80+
}
81+
82+
/**
83+
* Simulate receiving the current packet from the given sender.
84+
*
85+
* @param sender - the sender.
86+
* @throws RuntimeException If the packet cannot be received.
87+
* @see #receivePacket(Player)
88+
* @deprecated Misspelled. recieve to receive
89+
*/
90+
@Deprecated
91+
public void recievePacket(Player sender) {
92+
try {
93+
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
94+
getHandle());
95+
} catch (Exception e) {
96+
throw new RuntimeException("Cannot recieve packet.", e);
97+
}
98+
}
99+
100+
/**
101+
* Simulate receiving the current packet from the given sender.
102+
*
103+
* @param sender - the sender.
104+
* @throws RuntimeException if the packet cannot be received.
105+
*/
106+
public void receivePacket(Player sender) {
107+
try {
108+
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
109+
getHandle());
110+
} catch (Exception e) {
111+
throw new RuntimeException("Cannot receive packet.", e);
112+
}
113+
}
114+
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packetwrapper;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.events.PacketContainer;
23+
24+
public class PacketGameState extends AbstractPacket {
25+
26+
public static final PacketType TYPE = PacketType.Play.Server.GAME_STATE_CHANGE;
27+
28+
public PacketGameState() {
29+
super(new PacketContainer(TYPE), TYPE);
30+
handle.getModifier().writeDefaults();
31+
}
32+
33+
public PacketGameState(PacketContainer packet) {
34+
super(packet, TYPE);
35+
}
36+
37+
public static PacketGameState creditScreenPacket() {
38+
final PacketGameState packet = new PacketGameState();
39+
packet.setReason(4);
40+
packet.setValue(1);
41+
return packet;
42+
}
43+
44+
/**
45+
* Retrieve Reason.
46+
*
47+
* @return The current Reason
48+
*/
49+
public int getReason() {
50+
return handle.getGameStateIDs().read(0);
51+
}
52+
53+
/**
54+
* Set Reason.
55+
*
56+
* @param value - new value.
57+
*/
58+
public void setReason(int value) {
59+
handle.getGameStateIDs().write(0, value);
60+
}
61+
62+
/**
63+
* Retrieve Value.
64+
* <p>
65+
* Notes: depends on reason
66+
*
67+
* @return The current Value
68+
*/
69+
public float getValue() {
70+
return handle.getFloat().read(0);
71+
}
72+
73+
/**
74+
* Set Value.
75+
*
76+
* @param value - new value.
77+
*/
78+
public void setValue(float value) {
79+
handle.getFloat().write(0, value);
80+
}
81+
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packetwrapper;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.events.PacketContainer;
23+
import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
24+
import com.comphenix.protocol.wrappers.PlayerInfoData;
25+
26+
import java.util.List;
27+
28+
public class PacketPlayerInfo extends AbstractPacket {
29+
30+
public static final PacketType TYPE = PacketType.Play.Server.PLAYER_INFO;
31+
32+
public PacketPlayerInfo() {
33+
super(new PacketContainer(TYPE), TYPE);
34+
handle.getModifier().writeDefaults();
35+
}
36+
37+
public PacketPlayerInfo(PacketContainer packet) {
38+
super(packet, TYPE);
39+
}
40+
41+
public PlayerInfoAction getAction() {
42+
return handle.getPlayerInfoAction().read(0);
43+
}
44+
45+
public void setAction(PlayerInfoAction value) {
46+
handle.getPlayerInfoAction().write(0, value);
47+
}
48+
49+
public List<PlayerInfoData> getData() {
50+
return handle.getPlayerInfoDataLists().read(0);
51+
}
52+
53+
public void setData(List<PlayerInfoData> value) {
54+
handle.getPlayerInfoDataLists().write(0, value);
55+
}
56+
}
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packetwrapper;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.events.PacketContainer;
23+
import com.comphenix.protocol.wrappers.WrappedChatComponent;
24+
25+
public class PacketPlayerList extends AbstractPacket {
26+
27+
public static final PacketType TYPE = PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER;
28+
29+
public PacketPlayerList() {
30+
super(new PacketContainer(TYPE), TYPE);
31+
handle.getModifier().writeDefaults();
32+
}
33+
34+
public PacketPlayerList(PacketContainer packet) {
35+
super(packet, TYPE);
36+
}
37+
38+
/**
39+
* Retrieve Header.
40+
*
41+
* @return The current Header
42+
*/
43+
public WrappedChatComponent getHeader() {
44+
return handle.getChatComponents().read(0);
45+
}
46+
47+
/**
48+
* Set Header.
49+
*
50+
* @param value - new value.
51+
*/
52+
public void setHeader(WrappedChatComponent value) {
53+
handle.getChatComponents().write(0, value);
54+
}
55+
56+
/**
57+
* Retrieve Footer.
58+
*
59+
* @return The current Footer
60+
*/
61+
public WrappedChatComponent getFooter() {
62+
return handle.getChatComponents().read(1);
63+
}
64+
65+
/**
66+
* Set Footer.
67+
*
68+
* @param value - new value.
69+
*/
70+
public void setFooter(WrappedChatComponent value) {
71+
handle.getChatComponents().write(1, value);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)