|
16 | 16 | * Represents an example plugin utilizing TinyProtocol
|
17 | 17 | */
|
18 | 18 | public class ExamplePlugin extends JavaPlugin {
|
19 |
| - // Chat packets |
20 |
| - private FieldAccessor<String> CHAT_MESSAGE = Reflection.getField("{nms}.PacketPlayInChat", String.class, 0); |
21 |
| - |
22 |
| - // Explosion packet |
23 |
| - private Class<?> particleClass = Reflection.getClass("{nms}.PacketPlayOutWorldParticles"); |
24 |
| - private FieldAccessor<String> particleName = Reflection.getField(particleClass, String.class, 0); |
25 |
| - private FieldAccessor<Float> particleX = Reflection.getField(particleClass, float.class, 0); |
26 |
| - private FieldAccessor<Float> particleY = Reflection.getField(particleClass, float.class, 1); |
27 |
| - private FieldAccessor<Float> particleZ = Reflection.getField(particleClass, float.class, 2); |
28 |
| - private FieldAccessor<Integer> particleCount = Reflection.getField(particleClass, int.class, 0); |
29 |
| - |
30 |
| - // Server info packet |
31 |
| - private Class<?> serverInfoClass = Reflection.getClass("{nms}.PacketStatusOutServerInfo"); |
32 |
| - private Class<Object> serverPingClass = Reflection.getUntypedClass("{nms}.ServerPing"); |
33 |
| - private Class<Object> playerSampleClass = Reflection.getUntypedClass("{nms}.ServerPingPlayerSample"); |
34 |
| - private FieldAccessor<Object> serverPing = Reflection.getField(serverInfoClass, serverPingClass, 0); |
35 |
| - private FieldAccessor<Object> playerSample = Reflection.getField(serverPingClass, playerSampleClass, 0); |
36 |
| - private ConstructorInvoker playerSampleInvoker = Reflection.getConstructor(playerSampleClass, int.class, int.class); |
37 |
| - |
38 |
| - private TinyProtocol protocol; |
39 |
| - |
40 |
| - @Override |
41 |
| - public void onEnable() { |
42 |
| - protocol = new TinyProtocol(this) { |
43 |
| - |
44 |
| - @Override |
45 |
| - public Object onPacketInAsync(Player sender, Channel channel, Object packet) { |
46 |
| - // Cancel chat packets |
47 |
| - if (CHAT_MESSAGE.hasField(packet)) { |
48 |
| - if (CHAT_MESSAGE.get(packet).contains("dirty")) { |
49 |
| - sendExplosion(sender); |
50 |
| - return null; |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - if (particleName.hasField(packet)) { |
55 |
| - System.out.println("Sending particle field:" + packet); |
56 |
| - } |
57 |
| - |
58 |
| - return super.onPacketInAsync(sender, channel, packet); |
59 |
| - } |
60 |
| - |
61 |
| - @Override |
62 |
| - public Object onPacketOutAsync(Player reciever, Channel channel, Object packet) { |
63 |
| - if (serverInfoClass.isInstance(packet)) { |
64 |
| - Object ping = serverPing.get(packet); |
65 |
| - playerSample.set(ping, playerSampleInvoker.invoke(1000, 0)); |
66 |
| - |
67 |
| - // Which is equivalent to: |
68 |
| - // serverPing.get(packet).setPlayerSample(new ServerPingPlayerSample(1000, 0)); |
69 |
| - return packet; |
70 |
| - } |
71 |
| - |
72 |
| - return super.onPacketOutAsync(reciever, channel, packet); |
73 |
| - } |
74 |
| - |
75 |
| - }; |
76 |
| - } |
77 |
| - |
78 |
| - @Override |
79 |
| - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
80 |
| - if (sender instanceof Player) { |
81 |
| - Player player = (Player) sender; |
82 |
| - |
83 |
| - // Toggle injection |
84 |
| - if (protocol.hasInjected(player)) { |
85 |
| - protocol.uninjectPlayer(player); |
86 |
| - sender.sendMessage(ChatColor.YELLOW + "Player " + player + " has been uninjected."); |
87 |
| - } else { |
88 |
| - protocol.injectPlayer(player); |
89 |
| - sender.sendMessage(ChatColor.DARK_GREEN + "Player " + player + " has been injected."); |
90 |
| - } |
91 |
| - |
92 |
| - return true; |
93 |
| - } else { |
94 |
| - sender.sendMessage(ChatColor.RED + "Can only be invoked by a player."); |
95 |
| - } |
96 |
| - |
97 |
| - return false; |
98 |
| - } |
99 |
| - |
100 |
| - private void sendExplosion(Player player) { |
101 |
| - try { |
102 |
| - // Only visible for the client |
103 |
| - Object explosionPacket = particleClass.newInstance(); |
104 |
| - Location loc = player.getLocation(); |
105 |
| - particleName.set(explosionPacket, "hugeexplosion"); |
106 |
| - particleX.set(explosionPacket, (float) loc.getX()); |
107 |
| - particleY.set(explosionPacket, (float) loc.getY()); |
108 |
| - particleZ.set(explosionPacket, (float) loc.getZ()); |
109 |
| - particleCount.set(explosionPacket, 1); |
110 |
| - |
111 |
| - // Send the packet to the player |
112 |
| - protocol.sendPacket(player, explosionPacket); |
113 |
| - } catch (Exception e) { |
114 |
| - throw new RuntimeException("Cannot send packet.", e); |
115 |
| - } |
116 |
| - } |
| 19 | + // Chat packets |
| 20 | + private FieldAccessor<String> CHAT_MESSAGE = Reflection.getField("{nms}.PacketPlayInChat", String.class, 0); |
| 21 | + |
| 22 | + // Explosion packet |
| 23 | + private Class<?> particleClass = Reflection.getClass("{nms}.PacketPlayOutWorldParticles"); |
| 24 | + private FieldAccessor<String> particleName = Reflection.getField(particleClass, String.class, 0); |
| 25 | + private FieldAccessor<Float> particleX = Reflection.getField(particleClass, float.class, 0); |
| 26 | + private FieldAccessor<Float> particleY = Reflection.getField(particleClass, float.class, 1); |
| 27 | + private FieldAccessor<Float> particleZ = Reflection.getField(particleClass, float.class, 2); |
| 28 | + private FieldAccessor<Integer> particleCount = Reflection.getField(particleClass, int.class, 0); |
| 29 | + |
| 30 | + // Server info packet |
| 31 | + private Class<?> serverInfoClass = Reflection.getClass("{nms}.PacketStatusOutServerInfo"); |
| 32 | + private Class<Object> serverPingClass = Reflection.getUntypedClass("{nms}.ServerPing"); |
| 33 | + private Class<Object> playerSampleClass = Reflection.getUntypedClass("{nms}.ServerPingPlayerSample"); |
| 34 | + private FieldAccessor<Object> serverPing = Reflection.getField(serverInfoClass, serverPingClass, 0); |
| 35 | + private FieldAccessor<Object> playerSample = Reflection.getField(serverPingClass, playerSampleClass, 0); |
| 36 | + private ConstructorInvoker playerSampleInvoker = Reflection.getConstructor(playerSampleClass, int.class, int.class); |
| 37 | + |
| 38 | + private TinyProtocol protocol; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onEnable() { |
| 42 | + protocol = new TinyProtocol(this) { |
| 43 | + |
| 44 | + @Override |
| 45 | + public Object onPacketInAsync(Player sender, Channel channel, Object packet) { |
| 46 | + // Cancel chat packets |
| 47 | + if (CHAT_MESSAGE.hasField(packet)) { |
| 48 | + if (CHAT_MESSAGE.get(packet).contains("dirty")) { |
| 49 | + sendExplosion(sender); |
| 50 | + return null; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (particleName.hasField(packet)) { |
| 55 | + System.out.println("Sending particle field:" + packet); |
| 56 | + } |
| 57 | + |
| 58 | + return super.onPacketInAsync(sender, channel, packet); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object onPacketOutAsync(Player reciever, Channel channel, Object packet) { |
| 63 | + if (serverInfoClass.isInstance(packet)) { |
| 64 | + Object ping = serverPing.get(packet); |
| 65 | + playerSample.set(ping, playerSampleInvoker.invoke(1000, 0)); |
| 66 | + |
| 67 | + // Which is equivalent to: |
| 68 | + // serverPing.get(packet).setPlayerSample(new ServerPingPlayerSample(1000, 0)); |
| 69 | + return packet; |
| 70 | + } |
| 71 | + |
| 72 | + return super.onPacketOutAsync(reciever, channel, packet); |
| 73 | + } |
| 74 | + |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
| 80 | + if (sender instanceof Player) { |
| 81 | + Player player = (Player) sender; |
| 82 | + |
| 83 | + // Toggle injection |
| 84 | + if (protocol.hasInjected(player)) { |
| 85 | + protocol.uninjectPlayer(player); |
| 86 | + sender.sendMessage(ChatColor.YELLOW + "Player " + player + " has been uninjected."); |
| 87 | + } else { |
| 88 | + protocol.injectPlayer(player); |
| 89 | + sender.sendMessage(ChatColor.DARK_GREEN + "Player " + player + " has been injected."); |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } else { |
| 94 | + sender.sendMessage(ChatColor.RED + "Can only be invoked by a player."); |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + private void sendExplosion(Player player) { |
| 101 | + try { |
| 102 | + // Only visible for the client |
| 103 | + Object explosionPacket = particleClass.newInstance(); |
| 104 | + Location loc = player.getLocation(); |
| 105 | + particleName.set(explosionPacket, "hugeexplosion"); |
| 106 | + particleX.set(explosionPacket, (float) loc.getX()); |
| 107 | + particleY.set(explosionPacket, (float) loc.getY()); |
| 108 | + particleZ.set(explosionPacket, (float) loc.getZ()); |
| 109 | + particleCount.set(explosionPacket, 1); |
| 110 | + |
| 111 | + // Send the packet to the player |
| 112 | + protocol.sendPacket(player, explosionPacket); |
| 113 | + } catch (Exception e) { |
| 114 | + throw new RuntimeException("Cannot send packet.", e); |
| 115 | + } |
| 116 | + } |
117 | 117 | }
|
0 commit comments