Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. public class Reflection {
  2.  
  3. private static String serverVersion;
  4.  
  5. private static HashMap<String, Sound> soundEffectTypes = new HashMap<>();
  6.  
  7. static {
  8. soundEffectTypes.put("block.wood.break", Sound.BLOCK_WOOD_BREAK);
  9. soundEffectTypes.put("block.gravel.break", Sound.BLOCK_GRAVEL_BREAK);
  10. soundEffectTypes.put("block.grass.break", Sound.BLOCK_GRASS_BREAK);
  11. soundEffectTypes.put("block.stone.break", Sound.BLOCK_STONE_BREAK);
  12. soundEffectTypes.put("block.metal.break", Sound.BLOCK_METAL_BREAK);
  13. soundEffectTypes.put("block.glass.break", Sound.BLOCK_GLASS_BREAK);
  14. soundEffectTypes.put("block.wool.break", Sound.BLOCK_WOOL_BREAK);
  15. soundEffectTypes.put("block.sand.break", Sound.BLOCK_SAND_BREAK);
  16. soundEffectTypes.put("block.snow.break", Sound.BLOCK_SNOW_BREAK);
  17. soundEffectTypes.put("block.ladder.break", Sound.BLOCK_LADDER_BREAK);
  18. soundEffectTypes.put("block.anvil.break", Sound.BLOCK_ANVIL_BREAK);
  19. soundEffectTypes.put("block.slime.block.break", Sound.BLOCK_SLIME_BLOCK_BREAK);
  20. soundEffectTypes.put("block.wet.grass.break", Sound.BLOCK_WET_GRASS_BREAK);
  21. soundEffectTypes.put("block.coral.block.break", Sound.BLOCK_CORAL_BLOCK_BREAK);
  22. }
  23.  
  24. /**
  25. * Send appropriate block break sound
  26. * @param block The block from which to choose the sound
  27. */
  28. public static void playBlockSound(Block block) {
  29. try{
  30. Object iBlockData = block.getClass().getMethod("getNMS").invoke(block);
  31. Object nmsBlock = iBlockData.getClass().getMethod("getBlock").invoke(iBlockData);
  32. Object soundEffectType = getNMSClass("Block").getMethod("getStepSound").invoke(nmsBlock);
  33.  
  34. Field q = soundEffectType.getClass().getDeclaredField("q");
  35. q.setAccessible(true);
  36.  
  37. Object soundEffect = q.get(soundEffectType);
  38. q.setAccessible(false);
  39.  
  40. Field a = soundEffect.getClass().getDeclaredField("a");
  41.  
  42. a.setAccessible(true);
  43. Object minecraftKey = a.get(soundEffect);
  44. a.setAccessible(false);
  45.  
  46. String key = (String) minecraftKey.getClass().getMethod("getKey") .invoke(minecraftKey);
  47.  
  48.  
  49. for(String sound : soundEffectTypes.keySet()){
  50.  
  51. if(key.equals(sound)){
  52. block.getWorld().playSound(block.getLocation(), soundEffectTypes.get(sound), 1, 1);
  53. }
  54. }
  55. } catch (Exception e){
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. /**
  61. * Send Crack Animation Packet
  62. * @param animation The stage of animation
  63. * @param block The target block
  64. */
  65. public static void sendBreakPacket(int animation, Block block){
  66. try {
  67. Object handle = Bukkit.getServer().getClass().getMethod("getHandle").invoke(Bukkit.getServer());
  68.  
  69. Object worldHandle = getCBClass("CraftWorld").getMethod("getHandle").invoke(block.getLocation().getWorld());
  70. Object dimension = worldHandle.getClass().getField("dimension").get(worldHandle);
  71.  
  72. Constructor<?> packetPlayOutBlockBreakAnimationConstructor = getNMSClass("PacketPlayOutBlockBreakAnimation").getConstructor(int.class,
  73. getNMSClass("BlockPosition"), int.class);
  74. Object packetPlayOutBlockBreakAnimation = packetPlayOutBlockBreakAnimationConstructor.newInstance(Utls.getBlockEntityId(block), getBlockPosition(block), animation);
  75.  
  76. handle.getClass().getMethod("sendPacketNearby", getNMSClass("EntityHuman"), double.class, double.class, double.class, double.class,
  77. getNMSClass("DimensionManager"), getNMSClass("Packet")).invoke(handle, null, (double) block.getX(), (double) block.getY(),
  78. (double) block.getZ(), 120D, dimension, packetPlayOutBlockBreakAnimation);
  79.  
  80. } catch (Exception e){
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. /**
  86. * Send Break Block Packet
  87. * @param player Who destroyed the block
  88. * @param block The destroyed block
  89. */
  90. public static void sendBreakBlock(Player player, Block block){
  91. try {
  92. Object handle = player.getClass().getMethod("getHandle").invoke(player);
  93. Object playerInteractManager = handle.getClass().getField("playerInteractManager").get(handle);
  94.  
  95.  
  96. playerInteractManager.getClass().getMethod("breakBlock", getNMSClass("BlockPosition")).invoke(playerInteractManager, getBlockPosition(block));
  97. } catch (Exception e){
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102. /**
  103. * Get BlockPosition instance from block
  104. * @param block The block to be transformed into BlockPosition
  105. * @return BlockPosition
  106. */
  107. public static Object getBlockPosition(Block block){
  108. try {
  109. return getNMSClass("BlockPosition").getConstructor(int.class, int.class, int.class).newInstance(block.getX(), block.getY(), block.getZ());
  110. } catch (Exception e){
  111. e.printStackTrace();
  112. return null;
  113. }
  114. }
  115.  
  116. /**
  117. * Get Version of Server
  118. * @return Version of Server
  119. */
  120. private static String getServerVersion(){
  121. if(serverVersion == null){
  122. serverVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  123. //serverVersion = "v1_13_R2";
  124. }
  125. return serverVersion;
  126. }
  127.  
  128. /**
  129. * Get NetMinecraftServer class using reflection
  130. * @param name Name of the class
  131. * @return Class
  132. */
  133. public static Class<?> getNMSClass(String name){
  134. try {
  135. return Class.forName("net.minecraft.server." + getServerVersion() + "." + name);
  136. } catch(ClassNotFoundException e) {
  137. e.printStackTrace();
  138. }
  139. return null;
  140. }
  141.  
  142. /**
  143. * Get NetMinecraft class using reflection
  144. * @param name Name of the class
  145. * @return Class
  146. */
  147. public static Class<?> getNMClass(String name) {
  148. try {
  149. return Class.forName("net.minecraft." + getServerVersion() + "." + name);
  150. } catch (ClassNotFoundException e) {
  151. e.printStackTrace();
  152. }
  153. return null;
  154. }
  155.  
  156. /**
  157. * Get CraftBukkit class using reflection
  158. * @param name Name of the class
  159. * @return Class
  160. */
  161. public static Class<?> getCBClass(String name) {
  162. try {
  163. return Class.forName("org.bukkit.craftbukkit." + getServerVersion() + "." + name);
  164. } catch (ClassNotFoundException e) {
  165. e.printStackTrace();
  166. }
  167. return null;
  168. }
  169.  
  170.  
  171. /**
  172. * Spawn an NPC using reflection
  173. * @param player Player who send the packet
  174. * @param packet The Packet you want to send
  175. * @return Send Packet
  176. */
  177. public static void sendPacket(Player player, Object packet){
  178. try {
  179. Object handle = player.getClass().getMethod("getHandle").invoke(player);
  180. Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
  181. playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
  182. } catch(Exception e) {
  183. e.printStackTrace();
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement