-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
2,313 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/java/com/pro4d/quickmc/attributes/AbstractPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* PacketWrapper - ProtocolLib wrappers for Minecraft packets | ||
* Copyright (C) dmulloy2 <http://dmulloy2.net> | ||
* Copyright (C) Kristian S. Strangeland | ||
* <p> | ||
* 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. | ||
* <p> | ||
* 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. | ||
* <p> | ||
* 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.pro4d.quickmc.attributes; | ||
|
||
import com.comphenix.protocol.PacketType; | ||
import com.comphenix.protocol.ProtocolLibrary; | ||
import com.comphenix.protocol.events.PacketContainer; | ||
import com.google.common.base.Objects; | ||
import lombok.Getter; | ||
import org.bukkit.entity.Player; | ||
|
||
@Getter | ||
public abstract class AbstractPacket { | ||
/** | ||
* -- GETTER -- | ||
* Retrieve a handle to the raw packet data. | ||
* | ||
*/ | ||
// The packet we will be modifying | ||
protected PacketContainer handle; | ||
|
||
/** | ||
* Constructs a new strongly typed wrapper for the given packet. | ||
* | ||
* @param handle - handle to the raw packet data. | ||
* @param type - the packet type. | ||
*/ | ||
protected AbstractPacket(PacketContainer handle, PacketType type) { | ||
// Make sure we're given a valid packet | ||
if (handle == null) { | ||
throw new IllegalArgumentException("Packet handle cannot be NULL."); | ||
} | ||
if (!Objects.equal(handle.getType(), type)) { | ||
throw new IllegalArgumentException(handle.getHandle() | ||
+ " is not a packet of type " + type); | ||
} | ||
|
||
this.handle = handle; | ||
} | ||
|
||
/** | ||
* Constructs a new strongly typed wrapper for the given packet and initialize it with a default packet instance. | ||
* | ||
* @param type - the packet type. | ||
*/ | ||
public AbstractPacket(PacketType type) { | ||
this.handle = new PacketContainer(type); | ||
this.handle.getModifier().writeDefaults(); | ||
} | ||
|
||
/** | ||
* Send the current packet to the given receiver. | ||
* | ||
* @param receiver - the receiver. | ||
* @throws RuntimeException If the packet cannot be sent. | ||
*/ | ||
public void sendPacket(Player receiver) { | ||
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, getHandle()); | ||
} | ||
|
||
/** | ||
* Send the current packet to all online players. | ||
*/ | ||
public void broadcastPacket() { | ||
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle()); | ||
} | ||
|
||
/** | ||
* Simulate receiving the current packet from the given sender. | ||
* | ||
* @param sender - the sender. | ||
* @throws RuntimeException if the packet cannot be received. | ||
*/ | ||
public void receivePacket(Player sender) { | ||
ProtocolLibrary.getProtocolManager().receiveClientPacket(sender, getHandle()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
AbstractPacket that = (AbstractPacket) o; | ||
|
||
return java.util.Objects.equals(handle, that.handle); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return handle != null ? handle.hashCode() : 0; | ||
} | ||
} |
Oops, something went wrong.