Advertisement
sgtcazeyt

PacketProtocol 1.7.10

Aug 29th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. package example;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.NoSuchElementException;
  8. import java.util.concurrent.Callable;
  9.  
  10. import net.minecraft.server.v1_7_R4.MinecraftServer;
  11. import net.minecraft.server.v1_7_R4.NetworkManager;
  12. import net.minecraft.server.v1_7_R4.Packet;
  13. import net.minecraft.server.v1_7_R4.ServerConnection;
  14. import net.minecraft.util.io.netty.channel.Channel;
  15. import net.minecraft.util.io.netty.channel.ChannelDuplexHandler;
  16. import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
  17. import net.minecraft.util.io.netty.channel.ChannelPromise;
  18.  
  19. import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
  20. import org.bukkit.event.EventHandler;
  21. import org.bukkit.event.Listener;
  22. import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
  23. import org.bukkit.event.server.ServerListPingEvent;
  24. import org.bukkit.plugin.Plugin;
  25.  
  26. public class PacketProtocol {
  27.  
  28. /*
  29. * Class made by BigTeddy98.
  30. *
  31. * PacketProtocol is a simple class to intercept packets.
  32. *
  33. * 1. No warranty is given or implied. 2. All damage is your own
  34. * responsibility. 3. If you want to use this in your plugins, a credit
  35. * would we appreciated.
  36. */
  37.  
  38. private Plugin plugin;
  39. private List<PacketListener> packetListeners = Collections
  40. .synchronizedList(new ArrayList<PacketListener>());
  41.  
  42. // injected channels, stored so we can remove our handler on disable
  43. private List<NetworkManager> injectedNetworkManagers = Collections
  44. .synchronizedList(new ArrayList<NetworkManager>());
  45. private List<NetworkManager> list;
  46.  
  47. // some reflection
  48. private Field m;
  49. private Field getConsole;
  50. private Field getServerConnection;
  51. private Field getF;
  52.  
  53. public PacketProtocol(Plugin plugin) throws Exception {
  54.  
  55. this.plugin = plugin;
  56.  
  57. // set up our little bit of reflection
  58. this.m = NetworkManager.class.getDeclaredField("m");
  59. this.m.setAccessible(true);
  60. this.getConsole = CraftServer.class.getDeclaredField("console");
  61. this.getConsole.setAccessible(true);
  62. this.getServerConnection = MinecraftServer.class.getDeclaredField("p");
  63. this.getServerConnection.setAccessible(true);
  64. this.getF = ServerConnection.class.getDeclaredField("f");
  65. this.getF.setAccessible(true);
  66.  
  67. // call initialize
  68. this.initialize();
  69. }
  70.  
  71. // calling this method onDisable is very IMPORTANT, without calling this
  72. // method this class won't work correctly.
  73. public void disable() {
  74. // loop through all channels and remove our handler
  75. for (NetworkManager netManager : injectedNetworkManagers) {
  76. try {
  77. Channel channel = (Channel) this.m.get(netManager);
  78. removeHandler(channel, plugin.getName(), false);
  79. } catch (NoSuchElementException e) {
  80. // something changed, no problem
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86.  
  87. // used to thread safe remove our handler
  88. private void removeHandler(final Channel channel, final String name,
  89. final boolean b) throws NoSuchElementException {
  90. channel.eventLoop().submit(new Callable<Object>() {
  91.  
  92. @Override
  93. public Object call() throws Exception {
  94. channel.pipeline().remove(name);
  95. if (b) {
  96. channel.pipeline().addBefore("packet_handler",
  97. plugin.getName(), new DuplexHandler());
  98. }
  99. return null;
  100. }
  101. });
  102. }
  103.  
  104. // register out listeners
  105. @SuppressWarnings("unchecked")
  106. private void initialize() throws IllegalArgumentException,
  107. IllegalAccessException {
  108.  
  109. // get the NMS minecraftserver
  110. MinecraftServer server = (MinecraftServer) getConsole
  111. .get(PacketProtocol.this.plugin.getServer());
  112. // get the serverconnection
  113. ServerConnection connection = (ServerConnection) PacketProtocol.this.getServerConnection
  114. .get(server);
  115. // get a list of all waiting connections
  116. Object obj = getF.get(connection);
  117. this.list = Collections.synchronizedList((List<NetworkManager>) obj);
  118.  
  119. this.plugin.getServer().getPluginManager()
  120. .registerEvents(new Listener() {
  121.  
  122. @EventHandler
  123. private void onPing(ServerListPingEvent event)
  124. throws IllegalArgumentException,
  125. IllegalAccessException {
  126. injectConnections();
  127. }
  128.  
  129. @EventHandler
  130. private void onLogin(AsyncPlayerPreLoginEvent event)
  131. throws IllegalArgumentException,
  132. IllegalAccessException {
  133. injectConnections();
  134. }
  135. }, this.plugin);
  136. }
  137.  
  138. private synchronized void injectConnections()
  139. throws IllegalArgumentException, IllegalAccessException {
  140.  
  141. // inject them all, to intercept our packets
  142. for (NetworkManager manager : list) {
  143. // inject connection
  144. injectConnection(manager);
  145. }
  146. }
  147.  
  148. // method for injecting ping connections
  149. private void injectConnection(final NetworkManager manager)
  150. throws IllegalArgumentException, IllegalAccessException {
  151. // get channel using reflection
  152.  
  153. // check if it already has been injected
  154. if (this.injectedNetworkManagers.contains(manager)) {
  155. return;
  156. }
  157. // store it for later
  158. this.injectedNetworkManagers.add(manager);
  159.  
  160. final Channel channel = (Channel) this.m.get(manager);
  161.  
  162. if (channel.pipeline().context("packet_handler") == null) {
  163. return;
  164. }
  165.  
  166. try {
  167. // add the packet_handler
  168. channel.pipeline().addBefore("packet_handler", plugin.getName(),
  169. new DuplexHandler());
  170. } catch (IllegalArgumentException e) {
  171. // handler already registered
  172. System.out.println("reinjecting");
  173. removeHandler(channel, plugin.getName(), true);
  174. }
  175. }
  176.  
  177. // used to register your packet listeners
  178. public void registerListener(PacketListener listener) {
  179. this.packetListeners.add(listener);
  180. }
  181.  
  182. private class DuplexHandler extends ChannelDuplexHandler {
  183. // this method comes from the netty thread
  184. @Override
  185. public void write(ChannelHandlerContext ctx, Object msg,
  186. ChannelPromise promise) throws Exception {
  187.  
  188. // loop and call
  189. Packet packet = (Packet) msg;
  190. for (PacketListener listener : packetListeners) {
  191. try {
  192. Packet newPacket = listener.onOutgoingPacket(packet);
  193. if (newPacket == null) {
  194. return;
  195. } else {
  196. packet = newPacket;
  197. }
  198. } catch (Exception e) {
  199.  
  200. }
  201. }
  202. // write it if it hasn't been cancelled
  203. super.write(ctx, packet, promise);
  204. }
  205.  
  206. // this method comes from the netty thread
  207. @Override
  208. public void channelRead(ChannelHandlerContext ctx, Object msg)
  209. throws Exception {
  210. // loop and call
  211. Packet packet = (Packet) msg;
  212. for (PacketListener listener : packetListeners) {
  213. try {
  214. Packet newPacket = listener.onIncomingPacket(packet);
  215. if (newPacket == null) {
  216. return;
  217. } else {
  218. packet = newPacket;
  219. }
  220. } catch (Exception e) {
  221.  
  222. }
  223. }
  224. // write it if it hasn't been cancelled
  225. super.channelRead(ctx, packet);
  226. }
  227. }
  228.  
  229. public interface PacketListener {
  230.  
  231. // PAY ATTENTION, these methods are being called from another thread!
  232. public Packet onIncomingPacket(Packet packet);
  233.  
  234. // PAY ATTENTION, these methods are being called from another thread!
  235. public Packet onOutgoingPacket(Packet packet);
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement