Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Reflection {
- private static String serverVersion;
- private static HashMap<String, Sound> soundEffectTypes = new HashMap<>();
- static {
- soundEffectTypes.put("block.wood.break", Sound.BLOCK_WOOD_BREAK);
- soundEffectTypes.put("block.gravel.break", Sound.BLOCK_GRAVEL_BREAK);
- soundEffectTypes.put("block.grass.break", Sound.BLOCK_GRASS_BREAK);
- soundEffectTypes.put("block.stone.break", Sound.BLOCK_STONE_BREAK);
- soundEffectTypes.put("block.metal.break", Sound.BLOCK_METAL_BREAK);
- soundEffectTypes.put("block.glass.break", Sound.BLOCK_GLASS_BREAK);
- soundEffectTypes.put("block.wool.break", Sound.BLOCK_WOOL_BREAK);
- soundEffectTypes.put("block.sand.break", Sound.BLOCK_SAND_BREAK);
- soundEffectTypes.put("block.snow.break", Sound.BLOCK_SNOW_BREAK);
- soundEffectTypes.put("block.ladder.break", Sound.BLOCK_LADDER_BREAK);
- soundEffectTypes.put("block.anvil.break", Sound.BLOCK_ANVIL_BREAK);
- soundEffectTypes.put("block.slime.block.break", Sound.BLOCK_SLIME_BLOCK_BREAK);
- soundEffectTypes.put("block.wet.grass.break", Sound.BLOCK_WET_GRASS_BREAK);
- soundEffectTypes.put("block.coral.block.break", Sound.BLOCK_CORAL_BLOCK_BREAK);
- }
- /**
- * Send appropriate block break sound
- * @param block The block from which to choose the sound
- */
- public static void playBlockSound(Block block) {
- try{
- Object iBlockData = block.getClass().getMethod("getNMS").invoke(block);
- Object nmsBlock = iBlockData.getClass().getMethod("getBlock").invoke(iBlockData);
- Object soundEffectType = getNMSClass("Block").getMethod("getStepSound").invoke(nmsBlock);
- Field q = soundEffectType.getClass().getDeclaredField("q");
- q.setAccessible(true);
- Object soundEffect = q.get(soundEffectType);
- q.setAccessible(false);
- Field a = soundEffect.getClass().getDeclaredField("a");
- a.setAccessible(true);
- Object minecraftKey = a.get(soundEffect);
- a.setAccessible(false);
- String key = (String) minecraftKey.getClass().getMethod("getKey") .invoke(minecraftKey);
- for(String sound : soundEffectTypes.keySet()){
- if(key.equals(sound)){
- block.getWorld().playSound(block.getLocation(), soundEffectTypes.get(sound), 1, 1);
- }
- }
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- /**
- * Send Crack Animation Packet
- * @param animation The stage of animation
- * @param block The target block
- */
- public static void sendBreakPacket(int animation, Block block){
- try {
- Object handle = Bukkit.getServer().getClass().getMethod("getHandle").invoke(Bukkit.getServer());
- Object worldHandle = getCBClass("CraftWorld").getMethod("getHandle").invoke(block.getLocation().getWorld());
- Object dimension = worldHandle.getClass().getField("dimension").get(worldHandle);
- Constructor<?> packetPlayOutBlockBreakAnimationConstructor = getNMSClass("PacketPlayOutBlockBreakAnimation").getConstructor(int.class,
- getNMSClass("BlockPosition"), int.class);
- Object packetPlayOutBlockBreakAnimation = packetPlayOutBlockBreakAnimationConstructor.newInstance(Utls.getBlockEntityId(block), getBlockPosition(block), animation);
- handle.getClass().getMethod("sendPacketNearby", getNMSClass("EntityHuman"), double.class, double.class, double.class, double.class,
- getNMSClass("DimensionManager"), getNMSClass("Packet")).invoke(handle, null, (double) block.getX(), (double) block.getY(),
- (double) block.getZ(), 120D, dimension, packetPlayOutBlockBreakAnimation);
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- /**
- * Send Break Block Packet
- * @param player Who destroyed the block
- * @param block The destroyed block
- */
- public static void sendBreakBlock(Player player, Block block){
- try {
- Object handle = player.getClass().getMethod("getHandle").invoke(player);
- Object playerInteractManager = handle.getClass().getField("playerInteractManager").get(handle);
- playerInteractManager.getClass().getMethod("breakBlock", getNMSClass("BlockPosition")).invoke(playerInteractManager, getBlockPosition(block));
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- /**
- * Get BlockPosition instance from block
- * @param block The block to be transformed into BlockPosition
- * @return BlockPosition
- */
- public static Object getBlockPosition(Block block){
- try {
- return getNMSClass("BlockPosition").getConstructor(int.class, int.class, int.class).newInstance(block.getX(), block.getY(), block.getZ());
- } catch (Exception e){
- e.printStackTrace();
- return null;
- }
- }
- /**
- * Get Version of Server
- * @return Version of Server
- */
- private static String getServerVersion(){
- if(serverVersion == null){
- serverVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
- //serverVersion = "v1_13_R2";
- }
- return serverVersion;
- }
- /**
- * Get NetMinecraftServer class using reflection
- * @param name Name of the class
- * @return Class
- */
- public static Class<?> getNMSClass(String name){
- try {
- return Class.forName("net.minecraft.server." + getServerVersion() + "." + name);
- } catch(ClassNotFoundException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Get NetMinecraft class using reflection
- * @param name Name of the class
- * @return Class
- */
- public static Class<?> getNMClass(String name) {
- try {
- return Class.forName("net.minecraft." + getServerVersion() + "." + name);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Get CraftBukkit class using reflection
- * @param name Name of the class
- * @return Class
- */
- public static Class<?> getCBClass(String name) {
- try {
- return Class.forName("org.bukkit.craftbukkit." + getServerVersion() + "." + name);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Spawn an NPC using reflection
- * @param player Player who send the packet
- * @param packet The Packet you want to send
- * @return Send Packet
- */
- public static void sendPacket(Player player, Object packet){
- try {
- Object handle = player.getClass().getMethod("getHandle").invoke(player);
- Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
- playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement