2015-06-23 20:01:50 +02:00
|
|
|
# ProtocolLib [![Travis Status](https://travis-ci.org/dmulloy2/ProtocolLib.svg?branch=master)](https://travis-ci.org/dmulloy2/ProtocolLib)
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
Certain tasks are impossible to perform with the standard Bukkit API, and may require
|
|
|
|
working with and even modify Minecraft directly. A common technique is to modify incoming
|
|
|
|
and outgoing [packets](http://www.wiki.vg/Protocol), or inject custom packets into the
|
|
|
|
stream. This is quite cumbersome to do, however, and most implementations will break
|
|
|
|
as soon as a new version of Minecraft has been released, mostly due to obfuscation.
|
|
|
|
|
|
|
|
Critically, different plugins that use this approach may _hook_ into the same classes,
|
|
|
|
with unpredictable outcomes. More than often this causes plugins to crash, but it may also
|
|
|
|
lead to more subtle bugs.
|
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
Currently maintained by dmulloy2 on behalf of [Spigot](http://www.spigotmc.org/).
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
### Resources
|
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
* [Resource Page](http://www.spigotmc.org/resources/protocollib.1997/)
|
2016-02-28 04:48:54 +01:00
|
|
|
* [Dev Builds](http://ci.dmulloy2.net/job/ProtocolLib)
|
|
|
|
* [JavaDoc](http://ci.dmulloy2.net/job/ProtocolLib/javadoc)
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2015-06-23 20:01:50 +02:00
|
|
|
### Compilation
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
ProtocolLib is built with Maven and requires Spigot and SpigotAPI, which can be found [here](http://www.spigotmc.org/wiki/buildtools/).
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2015-06-23 20:01:50 +02:00
|
|
|
### A new API
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
__ProtocolLib__ attempts to solve this problem by providing a event API, much like Bukkit,
|
2014-12-09 23:41:37 +01:00
|
|
|
that allows plugins to monitor, modify, or cancel packets sent and received. But, more importantly,
|
2012-10-12 00:24:15 +02:00
|
|
|
the API also hides all the gritty, obfuscated classes with a simple index based read/write system.
|
|
|
|
You no longer have to reference CraftBukkit!
|
|
|
|
|
|
|
|
### Using ProtocolLib
|
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
To use this library, first add ProtocolLib.jar to your Java build path. Then, add ProtocolLib
|
2015-04-20 02:18:43 +02:00
|
|
|
as a dependency or soft dependency to your plugin.yml file like any other plugin:
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````yml
|
2015-04-20 02:18:43 +02:00
|
|
|
depend: [ProtocolLib]
|
2013-12-12 18:02:57 +01:00
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
You can also add ProtocolLib as a Maven dependency:
|
|
|
|
|
|
|
|
````xml
|
|
|
|
<repositories>
|
|
|
|
<repository>
|
2015-05-21 01:28:56 +02:00
|
|
|
<id>dmulloy2-repo</id>
|
2018-06-03 06:26:31 +02:00
|
|
|
<url>http://repo.dmulloy2.net/nexus/repository/public/</url>
|
2014-12-09 23:41:37 +01:00
|
|
|
</repository>
|
|
|
|
...
|
2016-09-11 22:56:05 +02:00
|
|
|
</repositories>
|
2014-12-09 23:41:37 +01:00
|
|
|
|
|
|
|
<dependencies>
|
|
|
|
<dependency>
|
|
|
|
<groupId>com.comphenix.protocol</groupId>
|
2018-06-12 20:45:36 +02:00
|
|
|
<artifactId>ProtocolLib-API</artifactId>
|
|
|
|
<version>4.4.0</version>
|
2014-12-09 23:41:37 +01:00
|
|
|
</dependency>
|
|
|
|
</dependencies>
|
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2017-09-28 01:19:42 +02:00
|
|
|
Or use the maven dependency with gradle:
|
|
|
|
|
|
|
|
```gradle
|
|
|
|
repositories {
|
2018-06-03 06:26:31 +02:00
|
|
|
maven { url "http://repo.dmulloy2.net/nexus/repository/public/" }
|
2017-09-28 01:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dependencies {
|
2018-06-12 20:45:36 +02:00
|
|
|
compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: "4.4.0";
|
2017-09-28 01:19:42 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-12-09 23:41:37 +01:00
|
|
|
Then get a reference to ProtocolManager in onLoad() or onEnable() and you're good to go.
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````java
|
|
|
|
private ProtocolManager protocolManager;
|
|
|
|
|
|
|
|
public void onLoad() {
|
|
|
|
protocolManager = ProtocolLibrary.getProtocolManager();
|
|
|
|
}
|
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
To listen for packets sent by the server to a client, add a server-side listener:
|
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````java
|
|
|
|
// Disable all sound effects
|
|
|
|
protocolManager.addPacketListener(
|
2014-05-15 06:30:40 +02:00
|
|
|
new PacketAdapter(this, ListenerPriority.NORMAL,
|
|
|
|
PacketType.Play.Server.NAMED_SOUND_EFFECT) {
|
2013-12-12 18:02:57 +01:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
2014-05-15 06:30:40 +02:00
|
|
|
// Item packets (id: 0x29)
|
|
|
|
if (event.getPacketType() ==
|
|
|
|
PacketType.Play.Server.NAMED_SOUND_EFFECT) {
|
2013-12-12 18:02:57 +01:00
|
|
|
event.setCancelled(true);
|
2012-10-12 00:24:15 +02:00
|
|
|
}
|
2013-12-12 18:02:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
It's also possible to read and modify the content of these packets. For instance, you can create a global
|
|
|
|
censor by listening for Packet3Chat events:
|
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````java
|
|
|
|
// Censor
|
2014-05-15 06:30:40 +02:00
|
|
|
protocolManager.addPacketListener(new PacketAdapter(this,
|
|
|
|
ListenerPriority.NORMAL,
|
|
|
|
PacketType.Play.Client.CHAT) {
|
2013-12-12 18:02:57 +01:00
|
|
|
@Override
|
|
|
|
public void onPacketReceiving(PacketEvent event) {
|
2014-05-15 06:30:40 +02:00
|
|
|
if (event.getPacketType() == PacketType.Play.Client.CHAT) {
|
|
|
|
PacketContainer packet = event.getPacket();
|
|
|
|
String message = packet.getStrings().read(0);
|
|
|
|
|
|
|
|
if (message.contains("shit")
|
|
|
|
|| message.contains("damn")) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
event.getPlayer().sendMessage("Bad manners!");
|
2012-10-12 00:24:15 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 18:02:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
### Sending packets
|
|
|
|
|
|
|
|
Normally, you might have to do something ugly like the following:
|
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````java
|
|
|
|
Packet60Explosion fakeExplosion = new Packet60Explosion();
|
|
|
|
|
|
|
|
fakeExplosion.a = player.getLocation().getX();
|
|
|
|
fakeExplosion.b = player.getLocation().getY();
|
|
|
|
fakeExplosion.c = player.getLocation().getZ();
|
|
|
|
fakeExplosion.d = 3.0F;
|
|
|
|
fakeExplosion.e = new ArrayList<Object>();
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(fakeExplosion);
|
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
But with ProtocolLib, you can turn that into something more manageable. Notice that
|
2014-05-15 06:30:40 +02:00
|
|
|
you don't have to create an ArrayList with this version:
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2013-12-12 18:02:57 +01:00
|
|
|
````java
|
|
|
|
|
2016-02-23 08:28:14 +01:00
|
|
|
PacketContainer fakeExplosion = new PacketContainer(PacketType.Play.Server.EXPLOSION);
|
2014-05-15 06:30:40 +02:00
|
|
|
fakeExplosion.getDoubles().
|
2013-12-12 18:02:57 +01:00
|
|
|
write(0, player.getLocation().getX()).
|
|
|
|
write(1, player.getLocation().getY()).
|
|
|
|
write(2, player.getLocation().getZ());
|
2014-05-15 06:30:40 +02:00
|
|
|
fakeExplosion.getFloat().write(0, 3.0F);
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2014-05-15 06:30:40 +02:00
|
|
|
try {
|
|
|
|
protocolManager.sendServerPacket(player, fakeExplosion);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Cannot send packet " + fakeExplosion, e);
|
|
|
|
}
|
2013-12-12 18:02:57 +01:00
|
|
|
````
|
2012-10-12 00:24:15 +02:00
|
|
|
|
2015-06-23 20:01:50 +02:00
|
|
|
### Compatibility
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
One of the main goals of this project was to achieve maximum compatibility with CraftBukkit. And the end
|
2014-12-02 22:20:03 +01:00
|
|
|
result is quite flexible. Aside from netty package changes, it should be resilient against future changes.
|
|
|
|
It's likely that I won't have to update ProtocolLib for anything but bug fixes and new features.
|
2012-10-12 00:24:15 +02:00
|
|
|
|
|
|
|
How is this possible? It all comes down to reflection in the end. Essentially, no name is hard coded -
|
|
|
|
every field, method and class is deduced by looking at field types, package names or parameter
|
|
|
|
types. It's remarkably consistent across different versions.
|