Advertisement
Guest User

Chunk test code for a "problem"

a guest
Jun 14th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package de.seadragon91.chunktesting;
  2.  
  3. import java.util.Random;
  4.  
  5. import org.bukkit.GameMode;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.World;
  9. import org.bukkit.WorldCreator;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.generator.ChunkGenerator;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15. import org.bukkit.scheduler.BukkitRunnable;
  16.  
  17. public class ChunkTesting extends JavaPlugin {
  18.  
  19. /*
  20. * Problem: Regenerating a chunk in a air world at players location, doesn't send the player an chunk update.
  21. * Using the method refreshChunk() from class world should send to all players the chunk, but it doesn't work.
  22. *
  23. * By running the command /chunktesting. This steps will be occur.
  24. * 1) Generates a air world.
  25. * 2) Teleports the player to the air world.
  26. * 3) Generates a cube of stone to see the the dark and have a place for placing blocks.
  27. * 4) Regenerate the chunk at players location, will remove stone the chunk will be then dark.
  28. *
  29. * Now try to fly to the wall and try to place blocks against the wall, they don't appear.
  30. * Disconnect and reconnect and you should see your "placed" blocks.
  31. *
  32. */
  33.  
  34. @Override
  35. public void onDisable() {
  36. this.getLogger().info("disabled");
  37. }
  38.  
  39. @Override
  40. public void onEnable() {
  41. this.getLogger().info("enabled.");
  42. }
  43.  
  44. // returns a chunk generator full with air
  45. class AirChunkGenerator extends ChunkGenerator {
  46.  
  47. @Override
  48. public byte[] generate(World world, Random random, int cx, int cz) {
  49. return new byte[65536];
  50. }
  51. }
  52.  
  53. @Override
  54. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  55.  
  56. if (!command.getName().equalsIgnoreCase("chunktesting"))
  57. return false;
  58.  
  59. if (!(sender instanceof Player)) {
  60. sender.sendMessage("[ChunkTesting] This command doesn't work from the console.");
  61. return true;
  62. }
  63.  
  64. final Player player = (Player) sender;
  65.  
  66. // set players gamemode to creative
  67. player.setGameMode(GameMode.CREATIVE);
  68.  
  69. // set player flying to true
  70. player.setFlying(true);
  71.  
  72. // create air world
  73. final World airWorld = this.getServer().createWorld(WorldCreator.name("chunktesting").generator(new AirChunkGenerator()));
  74.  
  75. // create a cube of stone at 0 0 0
  76. for (int x = 0; x < 30; x++) {
  77. for (int y = 0; y < 30; y++) {
  78. for (int z = 0; z < 30; z++) {
  79. airWorld.getBlockAt(x, y, z).setType(Material.STONE);
  80. }
  81. }
  82. }
  83.  
  84. // teleport player to the top of the cube
  85. player.teleport(new Location(airWorld, 5, 33, 5));
  86.  
  87. player.sendMessage("[ChunkTesting] Please wait 5s...");
  88.  
  89. // schedule snyc delayed task for 5s
  90. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable() {
  91.  
  92. @Override
  93. public void run() {
  94.  
  95. // regenerate the chunk at players location
  96. airWorld.regenerateChunk(player.getLocation().getChunk().getX(), player.getLocation().getChunk().getZ());
  97.  
  98. // refresh the chunk at players location
  99. airWorld.refreshChunk(player.getLocation().getChunk().getX(), player.getLocation().getChunk().getZ());
  100.  
  101. player.sendMessage("[ChunkTesting] Now try to place blocks against the wall.");
  102. }
  103. }, 100L);
  104.  
  105. return true;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement