Last active
June 28, 2017 22:31
-
-
Save Scarsz/ed934e31e61406d3bfad56a7a22e1ca3 to your computer and use it in GitHub Desktop.
Change lots of blocks lots of quick for lots of MC versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static Class<?> blockPositionClass = null; | |
| private static Class<?> blockDataClass = null; | |
| private static Class<?> craftWorldClass = null; | |
| /** | |
| * Change a given block extremely quickly using NMS & Reflection | |
| * @param block Block to change | |
| * @param material Material to set the block to | |
| * @param data Any associated data for the block's material | |
| */ | |
| public static void setBlockSuperFast(Block block, Material material, int data) { | |
| int blockId = material.getId(); | |
| try { | |
| if (blockPositionClass == null) blockPositionClass = Class.forName(nmsClass + ".BlockPosition"); | |
| if (blockDataClass == null) blockDataClass = Class.forName(nmsClass + ".IBlockData"); | |
| if (craftWorldClass == null) craftWorldClass = Class.forName(craftbukkitClass + ".CraftWorld"); | |
| Object world = craftWorldClass.getMethod("getHandle").invoke(craftWorldClass.cast(block.getWorld())); | |
| Object chunk = world.getClass().getMethod("getChunkAt", int.class, int.class).invoke(world, block.getX() >> 4, block.getZ() >> 4); | |
| Object blockPosition = blockPositionClass.getConstructor(int.class, int.class, int.class).newInstance(block.getX(), block.getY(), block.getZ()); | |
| int combined = blockId + ((byte) data << 12); | |
| Object blockData = Class.forName(nmsClass + ".Block").getMethod("getByCombinedId", int.class).invoke(null, combined); | |
| world.getClass().getMethod("setTypeAndData", blockPositionClass, blockDataClass, int.class).invoke(world, blockPosition, blockData, 2); | |
| chunk.getClass().getMethod("a", blockPositionClass, blockDataClass).invoke(chunk, blockPosition, blockData); | |
| } catch (Exception e) { | |
| System.out.println("Reflection error: " + e.getMessage()); | |
| e.printStackTrace(); | |
| block.setTypeIdAndData(blockId, (byte) data, false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment