ProtocolLib/Readme.md

168 lines
5.3 KiB
Markdown
Raw Normal View History

# ProtocolLib
2018-09-22 23:48:05 +02:00
2019-10-24 15:57:58 +02:00
Certain tasks are impossible to perform with the standard Bukkit API, and may require
working with and even modifying Minecraft directly. A common technique is to modify incoming
and outgoing [packets](https://www.wiki.vg/Protocol), or inject custom packets into the
2019-10-24 15:57:58 +02:00
stream. This is quite cumbersome to do, however, and most implementations will break
2018-09-22 23:48:05 +02:00
as soon as a new version of Minecraft has been released, mostly due to obfuscation.
2019-10-24 15:57:58 +02:00
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
2018-09-22 23:48:05 +02:00
lead to more subtle bugs.
Currently maintained by dmulloy2 on behalf of [Spigot](https://www.spigotmc.org/).
2018-09-22 23:48:05 +02:00
### Resources
* [Resource Page](https://www.spigotmc.org/resources/protocollib.1997/)
* [Dev Builds](https://ci.dmulloy2.net/job/ProtocolLib)
* [JavaDoc](https://ci.dmulloy2.net/job/ProtocolLib/javadoc/index.html)
2018-09-22 23:48:05 +02:00
### Compilation
ProtocolLib is built with [Maven](https://maven.apache.org/). If you have it installed, just run
`mvn package` in the root project folder.
2018-09-22 23:48:05 +02:00
### A new API
__ProtocolLib__ attempts to solve this problem by providing an event API, much like Bukkit,
2019-10-24 15:57:58 +02:00
that allows plugins to monitor, modify, or cancel packets sent and received. But, more importantly,
the API also hides all the gritty, obfuscated classes with a simple index based read/write system.
2018-09-22 23:48:05 +02:00
You no longer have to reference CraftBukkit!
### Using ProtocolLib
To use this library, first add ProtocolLib.jar to your Java build path. Then, add ProtocolLib
as a dependency or soft dependency to your plugin.yml file like any other plugin:
````yml
depend: [ ProtocolLib ]
2018-09-22 23:48:05 +02:00
````
You can also add ProtocolLib as a Maven dependency:
````xml
<repositories>
<repository>
<id>dmulloy2-repo</id>
2021-02-15 19:59:14 +01:00
<url>https://repo.dmulloy2.net/repository/public/</url>
2018-09-22 23:48:05 +02:00
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.comphenix.protocol</groupId>
2019-10-24 15:57:58 +02:00
<artifactId>ProtocolLib</artifactId>
2021-07-09 23:16:07 +02:00
<version>4.7.0</version>
<scope>provided</scope>
2018-09-22 23:48:05 +02:00
</dependency>
</dependencies>
````
Or use the maven dependency with gradle:
```gradle
repositories {
2021-02-15 19:59:14 +01:00
maven { url "https://repo.dmulloy2.net/repository/public/" }
2018-09-22 23:48:05 +02:00
}
dependencies {
2021-07-09 23:16:07 +02:00
compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: "4.7.0";
2018-09-22 23:48:05 +02:00
}
```
Then get a reference to ProtocolManager in onLoad() or onEnable() and you're good to go.
````java
private ProtocolManager protocolManager;
public void onLoad() {
protocolManager = ProtocolLibrary.getProtocolManager();
}
````
To listen for packets sent by the server to a client, add a server-side listener:
````java
// Disable all sound effects
protocolManager.addPacketListener(new PacketAdapter(
this,
ListenerPriority.NORMAL,
PacketType.Play.Server.NAMED_SOUND_EFFECT
) {
2018-09-22 23:48:05 +02:00
@Override
public void onPacketSending(PacketEvent event) {
event.setCancelled(true);
2018-09-22 23:48:05 +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:
````java
// Censor
protocolManager.addPacketListener(new PacketAdapter(
this,
ListenerPriority.NORMAL,
PacketType.Play.Client.CHAT
) {
2018-09-22 23:48:05 +02:00
@Override
public void onPacketReceiving(PacketEvent event) {
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!");
2018-09-22 23:48:05 +02:00
}
}
});
````
### Sending packets
Normally, you might have to do something ugly like the following:
````java
PacketPlayOutExplosion fakeExplosion = new PacketPlayOutExplosion(
player.getLocation().getX(),
player.getLocation().getY(),
player.getLocation().getZ(),
3.0F,
new ArrayList<>(),
new Vec3D(
player.getVelocity().getX() + 1,
player.getVelocity().getY() + 1,
player.getVelocity().getZ() + 1
)
);
((CraftPlayer) player).getHandle().b.a(fakeExplosion);
2018-09-22 23:48:05 +02:00
````
But with ProtocolLib, you can turn that into something more manageable:
2018-09-22 23:48:05 +02:00
````java
PacketContainer fakeExplosion = new PacketContainer(PacketType.Play.Server.EXPLOSION);
fakeExplosion.getDoubles()
.write(0, player.getLocation().getX())
.write(1, player.getLocation().getY())
.write(2, player.getLocation().getZ());
2018-09-22 23:48:05 +02:00
fakeExplosion.getFloat().write(0, 3.0F);
fakeExplosion.getBlockPositionCollectionModifier().write(0, new ArrayList<>());
fakeExplosion.getVectors().write(0, player.getVelocity().add(new Vector(1, 1, 1)));
2018-09-22 23:48:05 +02:00
protocolManager.sendServerPacket(player, fakeExplosion);
2018-09-22 23:48:05 +02:00
````
### Compatibility
One of the main goals of this project was to achieve maximum compatibility with CraftBukkit. And the end
result is quite flexible. It's likely that I won't have to update ProtocolLib for anything but bug fixes
and new features.
2018-09-22 23:48:05 +02:00
2019-10-24 15:57:58 +02:00
How is this possible? It all comes down to reflection in the end. Essentially, no name is hard coded -
2018-09-22 23:48:05 +02:00
every field, method and class is deduced by looking at field types, package names or parameter
types. It's remarkably consistent across different versions.