diff --git a/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/pom.xml b/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/pom.xml index 879b9a7c8..2c02322e7 100644 --- a/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/pom.xml +++ b/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/pom.xml @@ -19,16 +19,16 @@ 1.8.8-1.9-SNAPSHOT compile - - com.mineplex - mineplex-core - dev-SNAPSHOT - org.zeroturnaround zt-zip 1.9 + + org.apache.httpcomponents + httpclient + 4.5.2 + org.slf4j slf4j-simple diff --git a/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/src/nautilus/game/arcade/uhc/WorldGen.java b/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/src/nautilus/game/arcade/uhc/WorldGen.java index debfeea29..c2e322e7f 100644 --- a/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/src/nautilus/game/arcade/uhc/WorldGen.java +++ b/Plugins/Nautilus.Game.Arcade.UHC.WorldGen/src/nautilus/game/arcade/uhc/WorldGen.java @@ -7,7 +7,9 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -28,20 +30,16 @@ import org.bukkit.Bukkit; import org.bukkit.Difficulty; import org.bukkit.World; import org.bukkit.WorldCreator; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.AsyncPlayerPreLoginEvent; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.spigotmc.WatchdogThread; import org.zeroturnaround.zip.ZipEntrySource; import org.zeroturnaround.zip.ZipUtil; +import com.google.gson.Gson; import com.google.gson.JsonObject; -import mineplex.core.common.Constants; -import mineplex.core.common.api.ApiHost; - -public class WorldGen extends JavaPlugin implements Runnable, Listener +public class WorldGen extends JavaPlugin { private static final int TIMEOUT = (int) TimeUnit.SECONDS.toMillis(10); @@ -51,6 +49,35 @@ public class WorldGen extends JavaPlugin implements Runnable, Listener private static final int MAX_Z = 1000; private static final int VIEW_DISTANCE = 5; + private static final String API_HOST_FILE = "api-config.dat"; + private static final Map API_HOST_MAP = new HashMap<>(); + + static + { + try + { + File configFile = new File(API_HOST_FILE); + YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); + + for (String key : configuration.getKeys(false)) + { + String ip = configuration.getConfigurationSection(key).getString("ip"); + // Use parseInt to catch non-ints instead of a 0 + int port = Integer.parseInt(configuration.getConfigurationSection(key).getString("port")); + if (ip == null) + { + throw new NullPointerException(); + } + + API_HOST_MAP.put(key, ip + ":" + port); + } + } + catch (Throwable t) + { + t.printStackTrace(); + } + } + @Override public void onEnable() { @@ -61,161 +88,127 @@ public class WorldGen extends JavaPlugin implements Runnable, Listener WatchdogThread.doStop(); - getServer().getScheduler().runTaskTimer(this, this, 20L, 20L * 5L); - getServer().getPluginManager().registerEvents(this, this); - } - - @EventHandler - public void onJoin(AsyncPlayerPreLoginEvent event) - { - event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); - event.setKickMessage("Shoo, go away"); - } - - @Override - public void run() - { - File root = new File("."); - - if (!root.exists()) - { - getLogger().severe("Root folder does not exist. Aborting"); - getServer().shutdown(); - return; - } - - File outputDirectory = new File(root, "output"); - if (!outputDirectory.exists()) - { - if (!outputDirectory.mkdir()) - { - getLogger().severe("Could not create output folder. Aborting"); - getServer().shutdown(); - return; - } - } - - long seed = ThreadLocalRandom.current().nextLong(); - - File outputFile = new File(outputDirectory, "UHC_Map" + seed + ".zip"); - - if (outputFile.exists()) - { - getLogger().info("Seed " + seed + " has already been generated. Skipping"); - return; - } try { - if (!outputFile.createNewFile()) + File root = new File("."); + + if (!root.exists()) { - getLogger().severe("Could not create new output file. Aborting"); - getServer().shutdown(); + getLogger().severe("Root folder does not exist. Aborting"); + System.exit(0); return; } - } - catch (IOException e) - { - getLogger().log(Level.SEVERE, "Could not create new output file. Aborting", e); - getServer().shutdown(); - return; - } - getLogger().info("Generating world seed " + seed); - - World world = new WorldCreator("generating") - .environment(World.Environment.NORMAL) - .seed(seed) - .createWorld(); - world.setDifficulty(Difficulty.HARD); - world.setKeepSpawnInMemory(false); - - int minChunkX = (MIN_X >> 4) - VIEW_DISTANCE; - int minChunkZ = (MIN_Z >> 4) - VIEW_DISTANCE; - int maxChunkX = (MAX_X >> 4) + VIEW_DISTANCE; - int maxChunkZ = (MAX_Z >> 4) + VIEW_DISTANCE; - - for (int x = minChunkX; x <= maxChunkX; x++) - { - getLogger().info("Generating x coord " + x); - for (int z = minChunkZ; z <= maxChunkZ; z++) + File outputDirectory = new File(root, "output"); + if (!outputDirectory.exists()) { - world.getChunkAt(x, z).load(true); - } - } - - for (int x = minChunkX; x <= maxChunkX; x++) - { - getLogger().info("Unloading x coord " + x); - for (int z = minChunkZ; z <= maxChunkZ; z++) - { - world.getChunkAt(x, z).unload(true, false); - } - } - - getLogger().info("Unloading and saving world"); - - Bukkit.unloadWorld(world, true); - - getLogger().info("Finished unloading and saving world"); - - StringBuilder worldconfig = new StringBuilder(); - worldconfig.append("MAP_NAME:UHC World").append(System.lineSeparator()); - worldconfig.append("MAP_AUTHOR:Mineplex").append(System.lineSeparator()); - worldconfig.append("MIN_X:").append(MIN_X).append(System.lineSeparator()); - worldconfig.append("MIN_Z:").append(MIN_Z).append(System.lineSeparator()); - worldconfig.append("MAX_X:").append(MAX_X).append(System.lineSeparator()); - worldconfig.append("MAX_Z:").append(MAX_Z).append(System.lineSeparator()); - for (int i = 1; i <= 60; i++) - { - worldconfig.append("TEAM_NAME:").append(i).append(System.lineSeparator()); - worldconfig.append("TEAM_SPAWNS:0,0,0").append(System.lineSeparator()); - } - - File worldFolder = new File(root, "generating"); - - File regionFolder = new File(worldFolder, "region"); - - File[] regionFiles = regionFolder.listFiles(); - - if (regionFiles == null) - { - getLogger().severe("Unexpected null region files. Aborting"); - getServer().shutdown(); - return; - } - - List zipEntrySourceList = new ArrayList<>(); - zipEntrySourceList.add(new ZipEntrySource() - { - @Override - public String getPath() - { - return "WorldConfig.dat"; + if (!outputDirectory.mkdir()) + { + getLogger().severe("Could not create output folder. Aborting"); + System.exit(0); + return; + } } - @Override - public ZipEntry getEntry() + long seed = ThreadLocalRandom.current().nextLong(); + + File outputFile = new File(outputDirectory, "UHC_Map" + seed + ".zip"); + + if (outputFile.exists()) { - return new ZipEntry(getPath()); + getLogger().info("Seed " + seed + " has already been generated. Skipping"); + System.exit(0); + return; } - @Override - public InputStream getInputStream() throws IOException + try { - return new ByteArrayInputStream(worldconfig.toString().getBytes(StandardCharsets.UTF_8)); + if (!outputFile.createNewFile()) + { + getLogger().severe("Could not create new output file. Aborting"); + System.exit(0); + return; + } + } + catch (IOException e) + { + getLogger().log(Level.SEVERE, "Could not create new output file. Aborting", e); + System.exit(0); + return; } - }); + getLogger().info("Generating world seed " + seed); - for (File file : regionFiles) - { + World world = new WorldCreator("generating") + .environment(World.Environment.NORMAL) + .seed(seed) + .createWorld(); + world.setDifficulty(Difficulty.HARD); + world.setKeepSpawnInMemory(false); + + int minChunkX = (MIN_X >> 4) - VIEW_DISTANCE; + int minChunkZ = (MIN_Z >> 4) - VIEW_DISTANCE; + int maxChunkX = (MAX_X >> 4) + VIEW_DISTANCE; + int maxChunkZ = (MAX_Z >> 4) + VIEW_DISTANCE; + + for (int x = minChunkX; x <= maxChunkX; x++) + { + getLogger().info("Generating x coord " + x); + for (int z = minChunkZ; z <= maxChunkZ; z++) + { + world.getChunkAt(x, z).load(true); + } + } + + for (int x = minChunkX; x <= maxChunkX; x++) + { + getLogger().info("Unloading x coord " + x); + for (int z = minChunkZ; z <= maxChunkZ; z++) + { + world.getChunkAt(x, z).unload(true, false); + } + } + + getLogger().info("Unloading and saving world"); + + Bukkit.unloadWorld(world, true); + + getLogger().info("Finished unloading and saving world"); + + StringBuilder worldconfig = new StringBuilder(); + worldconfig.append("MAP_NAME:UHC World").append(System.lineSeparator()); + worldconfig.append("MAP_AUTHOR:Mineplex").append(System.lineSeparator()); + worldconfig.append("MIN_X:").append(MIN_X).append(System.lineSeparator()); + worldconfig.append("MIN_Z:").append(MIN_Z).append(System.lineSeparator()); + worldconfig.append("MAX_X:").append(MAX_X).append(System.lineSeparator()); + worldconfig.append("MAX_Z:").append(MAX_Z).append(System.lineSeparator()); + for (int i = 1; i <= 60; i++) + { + worldconfig.append("TEAM_NAME:").append(i).append(System.lineSeparator()); + worldconfig.append("TEAM_SPAWNS:0,0,0").append(System.lineSeparator()); + } + + File worldFolder = new File(root, "generating"); + + File regionFolder = new File(worldFolder, "region"); + + File[] regionFiles = regionFolder.listFiles(); + + if (regionFiles == null) + { + getLogger().severe("Unexpected null region files. Aborting"); + System.exit(0); + return; + } + + List zipEntrySourceList = new ArrayList<>(); zipEntrySourceList.add(new ZipEntrySource() { @Override public String getPath() { - return "region/" + file.getName(); + return "WorldConfig.dat"; } @Override @@ -227,92 +220,122 @@ public class WorldGen extends JavaPlugin implements Runnable, Listener @Override public InputStream getInputStream() throws IOException { - return new FileInputStream(file); + return new ByteArrayInputStream(worldconfig.toString().getBytes(StandardCharsets.UTF_8)); } }); - } - zipEntrySourceList.add(new ZipEntrySource() - { - @Override - public String getPath() + + for (File file : regionFiles) { - return "level.dat"; - } - - @Override - public ZipEntry getEntry() - { - return new ZipEntry(getPath()); - } - - @Override - public InputStream getInputStream() throws IOException - { - return new FileInputStream(new File(worldFolder, "level.dat")); - } - }); - - ZipUtil.pack(zipEntrySourceList.toArray(new ZipEntrySource[zipEntrySourceList.size()]), outputFile); - - FileUtils.deleteQuietly(worldFolder); - - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(TIMEOUT) - .setSocketTimeout(TIMEOUT) - .build(); - - CloseableHttpClient httpClient = HttpClientBuilder.create() - .setDefaultRequestConfig(config) - .build(); - - HttpPost request = new HttpPost("http://" + ApiHost.getEnderchestService().getHost() + ":" + ApiHost.getEnderchestService().getPort() + "/map/uhc/upload"); - request.addHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); - - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("name", outputFile.getName()); - jsonObject.addProperty("location", outputFile.toURI().toString()); - - request.setEntity(new StringEntity(Constants.GSON.toJson(jsonObject), StandardCharsets.UTF_8)); - - try - { - getLogger().info("Uploading " + seed + "!"); - HttpResponse response = httpClient.execute(request); - - if (response.getStatusLine().getStatusCode() != 200) - { - if (response.getStatusLine().getStatusCode() == 409) + zipEntrySourceList.add(new ZipEntrySource() { - getLogger().warning("Oops - Server rejected " + seed + " because it was already generated"); + @Override + public String getPath() + { + return "region/" + file.getName(); + } + + @Override + public ZipEntry getEntry() + { + return new ZipEntry(getPath()); + } + + @Override + public InputStream getInputStream() throws IOException + { + return new FileInputStream(file); + } + }); + } + + zipEntrySourceList.add(new ZipEntrySource() + { + @Override + public String getPath() + { + return "level.dat"; + } + + @Override + public ZipEntry getEntry() + { + return new ZipEntry(getPath()); + } + + @Override + public InputStream getInputStream() throws IOException + { + return new FileInputStream(new File(worldFolder, "level.dat")); + } + }); + + ZipUtil.pack(zipEntrySourceList.toArray(new ZipEntrySource[zipEntrySourceList.size()]), outputFile); + + FileUtils.deleteQuietly(worldFolder); + + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(TIMEOUT) + .setSocketTimeout(TIMEOUT) + .build(); + + CloseableHttpClient httpClient = HttpClientBuilder.create() + .setDefaultRequestConfig(config) + .build(); + + HttpPost request = new HttpPost("http://" + API_HOST_MAP.get("ENDERCHEST") + "/map/uhc/upload"); + request.addHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); + + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("name", outputFile.getName()); + jsonObject.addProperty("location", outputFile.toURI().toString()); + + request.setEntity(new StringEntity(new Gson().toJson(jsonObject), StandardCharsets.UTF_8)); + + try + { + getLogger().info("Uploading " + seed + "!"); + HttpResponse response = httpClient.execute(request); + + if (response.getStatusLine().getStatusCode() != 200) + { + if (response.getStatusLine().getStatusCode() == 409) + { + getLogger().warning("Oops - Server rejected " + seed + " because it was already generated"); + + if (!outputFile.delete()) + { + getLogger().warning("Could not clean up " + seed); + } + } + else + { + getLogger().severe("Failed to upload " + seed + ": " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); + } + } + else + { + getLogger().info("Uploaded " + seed + "!"); if (!outputFile.delete()) { getLogger().warning("Could not clean up " + seed); } } - else - { - getLogger().severe("Failed to upload " + seed + ": " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); - } } - else + catch (IOException e) { - getLogger().info("Uploaded " + seed + "!"); - - if (!outputFile.delete()) - { - getLogger().warning("Could not clean up " + seed); - } + e.printStackTrace(); + } + finally + { + getLogger().info("Finished generating world seed " + seed); } } - catch (IOException e) + catch (Throwable t) { - e.printStackTrace(); - } - finally - { - getLogger().info("Finished generating world seed " + seed); + t.printStackTrace(); } + System.exit(0); } }