Minecraft world importer for Gamecraft.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

78 рядки
2.3KB

  1. package io.github.norbipeti.gcmc;
  2. import com.google.common.io.Files;
  3. import com.google.gson.*;
  4. import lombok.val;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.World;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.lang.reflect.Type;
  16. import java.nio.charset.StandardCharsets;
  17. import java.util.ArrayList;
  18. public class PluginMain extends JavaPlugin {
  19. @Override
  20. public void onEnable() {
  21. }
  22. @Override
  23. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  24. if (args.length < 6) {
  25. sender.sendMessage("§cUsage: /export <x1> <y1> <z1> <x2> <y2> <z2>");
  26. return true;
  27. }
  28. final int[] xyz = new int[6];
  29. for (int i = 0; i < 6; i++)
  30. xyz[i] = Integer.parseInt(args[i]);
  31. for (int i = 0; i < 3; i++) {
  32. if (xyz[i] >= xyz[i + 3]) {
  33. int tmp = xyz[i];
  34. xyz[i] = xyz[i + 3];
  35. xyz[i + 3] = tmp;
  36. }
  37. }
  38. World world = sender instanceof Player ? ((Player) sender).getWorld() : Bukkit.getWorlds().get(0);
  39. val list = new ArrayList<Blocks>();
  40. for (int y = xyz[1]; y <= xyz[4]; y++) {
  41. for (int x = xyz[0]; x <= xyz[3]; x++) {
  42. for (int z = xyz[2]; z <= xyz[5]; z++) {
  43. Block block = world.getBlockAt(x, y, z);
  44. if(block.getType().name().equals("AIR")) continue;
  45. Blocks blocks = new Blocks();
  46. blocks.setStart(new Location(null, x, y, z));
  47. blocks.setEnd(blocks.getStart());
  48. blocks.setMaterial(block.getType().name());
  49. list.add(blocks);
  50. }
  51. }
  52. }
  53. Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new JsonSerializer<Location>() {
  54. @Override
  55. public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
  56. val jo = new JsonObject();
  57. jo.addProperty("x", src.getBlockX() - xyz[0]);
  58. jo.addProperty("y", src.getBlockY() - xyz[1]);
  59. jo.addProperty("z", src.getBlockZ() - xyz[2]);
  60. return jo;
  61. }
  62. }).create();
  63. try {
  64. Files.write(gson.toJson(list), new File("result.json"), StandardCharsets.UTF_8);
  65. sender.sendMessage("§bSuccess!");
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. sender.sendMessage("§cAn error occurred.");
  69. }
  70. return true;
  71. }
  72. }