Minecraft world importer for Gamecraft.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

77 řádky
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 < args.length; 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. Blocks blocks = new Blocks();
  44. blocks.setStart(new Location(null, x, y, z));
  45. blocks.setEnd(blocks.getStart());
  46. Block block = world.getBlockAt(x, y, z);
  47. blocks.setMaterial(block.getType().name());
  48. list.add(blocks);
  49. }
  50. }
  51. }
  52. Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new JsonSerializer<Location>() {
  53. @Override
  54. public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
  55. val jo = new JsonObject();
  56. jo.addProperty("x", src.getBlockX() - xyz[0]);
  57. jo.addProperty("y", src.getBlockY() - xyz[1]);
  58. jo.addProperty("z", src.getBlockZ() - xyz[2]);
  59. return jo;
  60. }
  61. }).create();
  62. try {
  63. Files.write(gson.toJson(list), new File("result.txt"), StandardCharsets.UTF_8);
  64. sender.sendMessage("§bSuccess!");
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. sender.sendMessage("§cAn error occurred.");
  68. }
  69. return true;
  70. }
  71. }