Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IslandManager {
- private Map<UUID, Island> islands;
- private final IslandNamingListener islandNamingListener;
- public IslandManager(IslandNamingListener islandNamingListener) {
- this.islands = new HashMap<>();
- this.islandNamingListener = islandNamingListener;
- }
- public World createIsland(UUID playerId, File schematicFile) {
- World islandWorld = null;
- try {
- ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
- if (format != null) {
- try (ClipboardReader reader = format.getReader(Files.newInputStream(schematicFile.toPath()))) {
- Clipboard clipboard = reader.read();
- BlockVector3 chestLocationInSchematic = BlockVector3.at(13, 62, -5);
- List<ItemStack> defaultItems = new ArrayList<>();
- defaultItems.add(new ItemStack(Material.STONE_SWORD));
- defaultItems.add(new ItemStack(Material.STONE_AXE));
- defaultItems.add(new ItemStack(Material.STONE_PICKAXE));
- defaultItems.add(new ItemStack(Material.STONE_SHOVEL));
- defaultItems.add(new ItemStack(Material.STONE_HOE));
- defaultItems.add(new ItemStack(Material.WATER_BUCKET));
- defaultItems.add(new ItemStack(Material.LAVA_BUCKET));
- defaultItems.add(new ItemStack(Material.LEATHER_HELMET));
- defaultItems.add(new ItemStack(Material.LEATHER_CHESTPLATE));
- defaultItems.add(new ItemStack(Material.LEATHER_LEGGINGS));
- defaultItems.add(new ItemStack(Material.LEATHER_BOOTS));
- defaultItems.add(new ItemStack(Material.OAK_SAPLING));
- defaultItems.add(new ItemStack(Material.SAND, 16));
- defaultItems.add(new ItemStack(Material.CACTUS, 16));
- defaultItems.add(new ItemStack(Material.WHEAT_SEEDS, 16));
- defaultItems.add(new ItemStack(Material.BONE_MEAL, 16));
- World bukkitWorld = Bukkit.getWorlds().isEmpty() ? null : Bukkit.getWorlds().get(0);
- if (bukkitWorld != null) {
- EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(bukkitWorld));
- com.sk89q.worldedit.world.block.BlockState chestBlock = clipboard.getBlock(chestLocationInSchematic);
- // create the island world and paste the modified schematic
- WorldCreator worldCreator = new WorldCreator("IslandWorld_" + playerId.toString());
- islandWorld = Bukkit.createWorld(worldCreator);
- if (chestBlock.getBlockType() == BlockTypes.CHEST && islandWorld != null) {
- Location chestLocation = BukkitAdapter.adapt(islandWorld, chestLocationInSchematic).toVector().toLocation(islandWorld);
- Block block = islandWorld.getBlockAt(chestLocation);
- if (block.getState() instanceof Chest) {
- Chest chest = (Chest) block.getState();
- Inventory inventory = chest.getInventory();
- inventory.clear();
- for (ItemStack item : defaultItems) {
- inventory.addItem(item.clone());
- }
- }
- } else {
- // case when the block at the specified location is not a chest
- System.out.println("Chest not in correct location in schematic!");
- }
- try (EditSession pasteSession = WorldEdit.getInstance().newEditSession(new BukkitWorld(islandWorld))) {
- Operation operation = new ClipboardHolder(clipboard)
- .createPaste(pasteSession)
- .to(BlockVector3.at(0, 64, 0))
- .ignoreAirBlocks(false)
- .build();
- Operations.complete(operation);
- }
- } else {
- // case when there are no loaded worlds in Bukkit
- System.out.println("No world loaded to bukkit!");
- }
- }
- } else {
- // unsupported schematic format
- System.out.println("Unsupported schematic format!");
- }
- } catch (IOException | WorldEditException e) {
- e.printStackTrace();
- } finally {
- if (islandWorld != null) {
- islands.put(playerId, new Island(islandWorld));
- } else {
- // case when island creation fails
- System.out.println("Island creation failed!");
- }
- }
- return islandWorld;
- }
Advertisement
Add Comment
Please, Sign In to add comment