Guest User

Untitled

a guest
Dec 15th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. public class IslandManager {
  2. private Map<UUID, Island> islands;
  3. private final IslandNamingListener islandNamingListener;
  4. public IslandManager(IslandNamingListener islandNamingListener) {
  5. this.islands = new HashMap<>();
  6. this.islandNamingListener = islandNamingListener;
  7. }
  8. public World createIsland(UUID playerId, File schematicFile) {
  9. World islandWorld = null;
  10. try {
  11. ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
  12. if (format != null) {
  13. try (ClipboardReader reader = format.getReader(Files.newInputStream(schematicFile.toPath()))) {
  14. Clipboard clipboard = reader.read();
  15. BlockVector3 chestLocationInSchematic = BlockVector3.at(13, 62, -5);
  16. List<ItemStack> defaultItems = new ArrayList<>();
  17. defaultItems.add(new ItemStack(Material.STONE_SWORD));
  18. defaultItems.add(new ItemStack(Material.STONE_AXE));
  19. defaultItems.add(new ItemStack(Material.STONE_PICKAXE));
  20. defaultItems.add(new ItemStack(Material.STONE_SHOVEL));
  21. defaultItems.add(new ItemStack(Material.STONE_HOE));
  22. defaultItems.add(new ItemStack(Material.WATER_BUCKET));
  23. defaultItems.add(new ItemStack(Material.LAVA_BUCKET));
  24. defaultItems.add(new ItemStack(Material.LEATHER_HELMET));
  25. defaultItems.add(new ItemStack(Material.LEATHER_CHESTPLATE));
  26. defaultItems.add(new ItemStack(Material.LEATHER_LEGGINGS));
  27. defaultItems.add(new ItemStack(Material.LEATHER_BOOTS));
  28. defaultItems.add(new ItemStack(Material.OAK_SAPLING));
  29. defaultItems.add(new ItemStack(Material.SAND, 16));
  30. defaultItems.add(new ItemStack(Material.CACTUS, 16));
  31. defaultItems.add(new ItemStack(Material.WHEAT_SEEDS, 16));
  32. defaultItems.add(new ItemStack(Material.BONE_MEAL, 16));
  33.  
  34. World bukkitWorld = Bukkit.getWorlds().isEmpty() ? null : Bukkit.getWorlds().get(0);
  35. if (bukkitWorld != null) {
  36. EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(bukkitWorld));
  37. com.sk89q.worldedit.world.block.BlockState chestBlock = clipboard.getBlock(chestLocationInSchematic);
  38.  
  39. // create the island world and paste the modified schematic
  40. WorldCreator worldCreator = new WorldCreator("IslandWorld_" + playerId.toString());
  41. islandWorld = Bukkit.createWorld(worldCreator);
  42.  
  43. if (chestBlock.getBlockType() == BlockTypes.CHEST && islandWorld != null) {
  44. Location chestLocation = BukkitAdapter.adapt(islandWorld, chestLocationInSchematic).toVector().toLocation(islandWorld);
  45. Block block = islandWorld.getBlockAt(chestLocation);
  46. if (block.getState() instanceof Chest) {
  47. Chest chest = (Chest) block.getState();
  48. Inventory inventory = chest.getInventory();
  49.  
  50. inventory.clear();
  51.  
  52. for (ItemStack item : defaultItems) {
  53. inventory.addItem(item.clone());
  54. }
  55. }
  56. } else {
  57. // case when the block at the specified location is not a chest
  58. System.out.println("Chest not in correct location in schematic!");
  59. }
  60. try (EditSession pasteSession = WorldEdit.getInstance().newEditSession(new BukkitWorld(islandWorld))) {
  61. Operation operation = new ClipboardHolder(clipboard)
  62. .createPaste(pasteSession)
  63. .to(BlockVector3.at(0, 64, 0))
  64. .ignoreAirBlocks(false)
  65. .build();
  66. Operations.complete(operation);
  67. }
  68. } else {
  69. // case when there are no loaded worlds in Bukkit
  70. System.out.println("No world loaded to bukkit!");
  71. }
  72. }
  73. } else {
  74. // unsupported schematic format
  75. System.out.println("Unsupported schematic format!");
  76. }
  77. } catch (IOException | WorldEditException e) {
  78. e.printStackTrace();
  79. } finally {
  80. if (islandWorld != null) {
  81. islands.put(playerId, new Island(islandWorld));
  82. } else {
  83. // case when island creation fails
  84. System.out.println("Island creation failed!");
  85. }
  86. }
  87. return islandWorld;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment