001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.bukkit.managers; 020 021import com.google.inject.Singleton; 022import com.plotsquared.core.configuration.file.YamlConfiguration; 023import com.plotsquared.core.util.PlatformWorldManager; 024import org.bukkit.Bukkit; 025import org.bukkit.World; 026import org.bukkit.WorldCreator; 027import org.bukkit.WorldType; 028import org.checkerframework.checker.nullness.qual.NonNull; 029import org.checkerframework.checker.nullness.qual.Nullable; 030 031import java.io.File; 032import java.io.IOException; 033import java.util.ArrayList; 034import java.util.Collection; 035import java.util.List; 036 037/** 038 * Default Bukkit world manager. It will handle world creation by 039 * registering the generator in bukkit.yml 040 */ 041@Singleton 042public class BukkitWorldManager implements PlatformWorldManager<World> { 043 044 @Override 045 public void initialize() { 046 } 047 048 @Override 049 public @Nullable World handleWorldCreation(@NonNull String worldName, @Nullable String generator) { 050 this.setGenerator(worldName, generator); 051 final WorldCreator wc = new WorldCreator(worldName); 052 wc.environment(World.Environment.NORMAL); 053 if (generator != null) { 054 wc.generator(generator); 055 wc.type(WorldType.FLAT); 056 } 057 return Bukkit.createWorld(wc); 058 } 059 060 protected void setGenerator(final @Nullable String worldName, final @Nullable String generator) { 061 if (generator == null) { 062 return; 063 } 064 File file = new File("bukkit.yml").getAbsoluteFile(); 065 YamlConfiguration yml = YamlConfiguration.loadConfiguration(file); 066 yml.set(String.format("worlds.%s.generator", worldName), generator); 067 try { 068 yml.save(file); 069 } catch (IOException e) { 070 e.printStackTrace(); 071 } 072 } 073 074 @Override 075 public String getName() { 076 return "bukkit"; 077 } 078 079 @Override 080 public Collection<String> getWorlds() { 081 final List<World> worlds = Bukkit.getWorlds(); 082 final List<String> worldNames = new ArrayList<>(); 083 for (final World world : worlds) { 084 worldNames.add(world.getName()); 085 } 086 return worldNames; 087 } 088 089}