|
| 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.packets; |
| 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 | + // The packet we will be modifying |
| 31 | + protected PacketContainer handle; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructs a new strongly typed wrapper for the given packet. |
| 35 | + * |
| 36 | + * @param handle - handle to the raw packet data. |
| 37 | + * @param type - the packet type. |
| 38 | + */ |
| 39 | + protected AbstractPacket(PacketContainer handle, PacketType type) { |
| 40 | + // Make sure we're given a valid packet |
| 41 | + if (handle == null) |
| 42 | + throw new IllegalArgumentException("Packet handle cannot be NULL."); |
| 43 | + if (!Objects.equal(handle.getType(), type)) |
| 44 | + throw new IllegalArgumentException(handle.getHandle() |
| 45 | + + " is not a packet of type " + type); |
| 46 | + |
| 47 | + this.handle = handle; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Retrieve a handle to the raw packet data. |
| 52 | + * |
| 53 | + * @return Raw packet data. |
| 54 | + */ |
| 55 | + public PacketContainer getHandle() { |
| 56 | + return handle; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Send the current packet to the given receiver. |
| 61 | + * |
| 62 | + * @param receiver - the receiver. |
| 63 | + * @throws RuntimeException If the packet cannot be sent. |
| 64 | + */ |
| 65 | + public void sendPacket(Player receiver) { |
| 66 | + try { |
| 67 | + ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, |
| 68 | + getHandle()); |
| 69 | + } catch (InvocationTargetException e) { |
| 70 | + throw new RuntimeException("Cannot send packet.", e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Send the current packet to all online players. |
| 76 | + */ |
| 77 | + public void broadcastPacket() { |
| 78 | + ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Simulate receiving the current packet from the given sender. |
| 83 | + * |
| 84 | + * @param sender - the sender. |
| 85 | + * @throws RuntimeException If the packet cannot be received. |
| 86 | + * @deprecated Misspelled. recieve to receive |
| 87 | + * @see #receivePacket(Player) |
| 88 | + */ |
| 89 | + @Deprecated |
| 90 | + public void recievePacket(Player sender) { |
| 91 | + try { |
| 92 | + ProtocolLibrary.getProtocolManager().recieveClientPacket(sender, |
| 93 | + getHandle()); |
| 94 | + } catch (Exception e) { |
| 95 | + throw new RuntimeException("Cannot recieve packet.", e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Simulate receiving the current packet from the given sender. |
| 101 | + * |
| 102 | + * @param sender - the sender. |
| 103 | + * @throws RuntimeException if the packet cannot be received. |
| 104 | + */ |
| 105 | + public void receivePacket(Player sender) { |
| 106 | + try { |
| 107 | + ProtocolLibrary.getProtocolManager().recieveClientPacket(sender, |
| 108 | + getHandle()); |
| 109 | + } catch (Exception e) { |
| 110 | + throw new RuntimeException("Cannot receive packet.", e); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments