Merge pull request #49 from glen3b/patch-2

Update sample code in readme
This commit is contained in:
Kristian S. Stangeland 2014-05-17 17:13:38 +02:00
commit bbaea894d4

View File

@ -58,14 +58,14 @@ To listen for packets sent by the server to a client, add a server-side listener
````java ````java
// Disable all sound effects // Disable all sound effects
protocolManager.addPacketListener( protocolManager.addPacketListener(
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, 0x3E) { new PacketAdapter(this, ListenerPriority.NORMAL,
PacketType.Play.Server.NAMED_SOUND_EFFECT) {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
// Item packets // Item packets (id: 0x29)
switch (event.getPacketID()) { if (event.getPacketType() ==
case 0x3E: // Sound effect PacketType.Play.Server.NAMED_SOUND_EFFECT) {
event.setCancelled(true); event.setCancelled(true);
break;
} }
} }
}); });
@ -76,22 +76,19 @@ censor by listening for Packet3Chat events:
````java ````java
// Censor // Censor
protocolManager.addPacketListener( protocolManager.addPacketListener(new PacketAdapter(this,
new PacketAdapter(this, ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL, 0x03) { ListenerPriority.NORMAL,
PacketType.Play.Client.CHAT) {
@Override @Override
public void onPacketReceiving(PacketEvent event) { public void onPacketReceiving(PacketEvent event) {
if (event.getPacketID() == 0x03) { if (event.getPacketType() == PacketType.Play.Client.CHAT) {
try { PacketContainer packet = event.getPacket();
PacketContainer packet = event.getPacket(); String message = packet.getStrings().read(0);
String message = packet.getSpecificModifier(String.class).read(0);
if (message.contains("shit")
if (message.contains("shit") || message.contains("damn")) { || message.contains("damn")) {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage("Bad manners!"); event.getPlayer().sendMessage("Bad manners!");
}
} catch (FieldAccessException e) {
getLogger().log(Level.SEVERE, "Couldn't access field.", e);
} }
} }
} }
@ -115,19 +112,22 @@ fakeExplosion.e = new ArrayList<Object>();
```` ````
But with ProtocolLib, you can turn that into something more manageable. Notice that But with ProtocolLib, you can turn that into something more manageable. Notice that
you don't have to create an ArrayList this version: you don't have to create an ArrayList with this version:
````java ````java
PacketContainer fakeExplosion = protocolManager.createPacket(60);
fakeExplosion.getSpecificModifier(double.class). fakeExplosion.getDoubles().
write(0, player.getLocation().getX()). write(0, player.getLocation().getX()).
write(1, player.getLocation().getY()). write(1, player.getLocation().getY()).
write(2, player.getLocation().getZ()); write(2, player.getLocation().getZ());
fakeExplosion.getSpecificModifier(float.class). fakeExplosion.getFloat().write(0, 3.0F);
write(0, 3.0F);
protocolManager.sendServerPacket(player, fakeExplosion); try {
protocolManager.sendServerPacket(player, fakeExplosion);
} catch (InvocationTargetException e) {
throw new RuntimeException(
"Cannot send packet " + fakeExplosion, e);
}
```` ````
Compatiblity Compatiblity