Search in sources :

Example 6 with MissileWarsPlugin

use of com.leomelonseeds.missilewars.MissileWarsPlugin in project MissileWars by Leomelonseeds.

the class SchematicManager method spawnFAWESchematic.

/**
 * Spawn a WorldEdit schematic in a given world. The "maps.yml" file should have data on the spawn location and
 * schematic file.
 *
 * @param schematicName the name of the schematic in the maps.yml file
 * @param world the world to spawn the schematic in
 * @param async to run async
 * @return true if the schematic was generated successfully, otherwise false
 */
public static boolean spawnFAWESchematic(String schematicName, World world, String mapType, DBCallback callback) {
    // Find schematic data from file
    MissileWarsPlugin plugin = MissileWarsPlugin.getPlugin();
    FileConfiguration schematicConfig = ConfigUtils.getConfigFile(MissileWarsPlugin.getPlugin().getDataFolder().toString(), "maps.yml");
    // Acquire WE clipboard
    String possibleMapType = mapType == null ? "" : mapType + ".";
    if (!schematicConfig.contains(possibleMapType + schematicName + ".file")) {
        System.out.println("No schem file found!");
        return false;
    }
    File schematicFile = new File(plugin.getDataFolder() + File.separator + "maps", schematicConfig.getString(possibleMapType + schematicName + ".file"));
    Clipboard clipboard;
    ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
    try {
        ClipboardReader reader = format.getReader(new FileInputStream(schematicFile));
        clipboard = reader.read();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    // Paste WE clipboard
    Vector spawnPos;
    if (mapType == null) {
        spawnPos = getVector(schematicConfig, schematicName + ".pos", null, null);
    } else {
        spawnPos = getVector(schematicConfig, "pos", mapType, schematicName);
    }
    new BukkitRunnable() {

        @Override
        public void run() {
            clipboard.paste(BukkitAdapter.adapt(world), Vector3.toBlockPoint(spawnPos.getX(), spawnPos.getY(), spawnPos.getZ()));
            if (callback != null) {
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        callback.onQueryDone(null);
                    }
                }.runTask(MissileWarsPlugin.getPlugin());
            }
        }
    }.runTaskAsynchronously(MissileWarsPlugin.getPlugin());
    return true;
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) IOException(java.io.IOException) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) File(java.io.File) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) Vector(org.bukkit.util.Vector) MissileWarsPlugin(com.leomelonseeds.missilewars.MissileWarsPlugin) FileInputStream(java.io.FileInputStream)

Example 7 with MissileWarsPlugin

use of com.leomelonseeds.missilewars.MissileWarsPlugin in project MissileWars by Leomelonseeds.

the class SchematicManager method spawnNBTStructure.

/**
 * Spawn a structure at a given location with a given rotation.
 *
 * @param structureName the name of the structure
 * @param loc the location to spawn the structure (pre-offset)
 * @param redMissile if the NBT structure is a red missile
 * @param mapName The name of the Arena map the NBT structure is being spawned in
 * @return true if the NBT structure was found and spawned, otherwise false
 */
public static boolean spawnNBTStructure(String structureName, Location loc, boolean redMissile, String mapName) {
    // Don't kill the lobby
    if (loc.getWorld().getName().equals("world")) {
        return false;
    }
    // Don't be too high
    if (loc.getBlockY() > MissileWarsPlugin.getPlugin().getConfig().getInt("max-height")) {
        return false;
    }
    // Attempt to get structure file
    MissileWarsPlugin plugin = MissileWarsPlugin.getPlugin();
    FileConfiguration structureConfig = ConfigUtils.getConfigFile(MissileWarsPlugin.getPlugin().getDataFolder().toString(), "items.yml");
    // Attempt to get structure file
    if (!structureConfig.contains(structureName + ".file")) {
        return false;
    }
    String fileName = structureConfig.getString(structureName + ".file");
    if (fileName == null) {
        return false;
    }
    if (redMissile) {
        fileName = fileName.replaceAll(".nbt", "_red.nbt");
    }
    File structureFile = new File(plugin.getDataFolder() + File.separator + "structures", fileName);
    // Load structure data
    StructureManager manager = Bukkit.getStructureManager();
    Structure structure;
    try {
        structure = manager.loadStructure(structureFile);
    } catch (IOException e) {
        return false;
    }
    int bluespawnz = (int) Math.floor(ConfigUtils.getMapNumber("classic", mapName, "blue-spawn.z"));
    int redspawnz = (int) Math.floor(ConfigUtils.getMapNumber("classic", mapName, "red-spawn.z"));
    // Apply offset
    Location spawnLoc = loc.clone();
    // Cancel if attempt to grief team spawnpoint
    if (spawnLoc.getBlockZ() == bluespawnz || spawnLoc.getBlockZ() == redspawnz) {
        return false;
    }
    Vector offset = getVector(structureConfig, structureName + ".offset", null, null);
    // Flip z if on red team
    StructureRotation rotation = StructureRotation.NONE;
    // Normal red missile offset adjustment
    if (redMissile) {
        offset.setZ(offset.getZ() * -1);
        offset.setX(offset.getX() * -1);
        rotation = StructureRotation.CLOCKWISE_180;
    }
    spawnLoc = spawnLoc.add(offset);
    // Time to perform no place checks
    int spawnx = spawnLoc.getBlockX();
    int spawny = spawnLoc.getBlockY();
    int spawnz = spawnLoc.getBlockZ();
    int sizex = structure.getSize().getBlockX();
    int sizey = structure.getSize().getBlockY();
    int sizez = structure.getSize().getBlockZ();
    int barrierx = plugin.getConfig().getInt("barrier.center.x");
    int portalx1 = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.x1") - 1;
    int portalx2 = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.x4") + 1;
    int portaly1 = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.y1") - 1;
    int portaly2 = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.y4") + 1;
    int portalredz = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.red-z");
    int portalbluez = (int) ConfigUtils.getMapNumber("classic", mapName, "portal.blue-z");
    // Do not place if hitbox would intersect with barrier
    if (!redMissile && spawnx + sizex > barrierx) {
        return false;
    } else if (redMissile && spawnx >= barrierx) {
        return false;
    }
    // Do not place if hitbox would intersect with a portal
    if (!redMissile && ((spawnz <= portalredz && spawnz + sizez > portalredz) || (spawnz <= portalbluez && spawnz + sizez > portalbluez)) && spawnx <= portalx2 && spawnx + sizex > portalx1 && spawny <= portaly2 && spawny + sizey > portaly1) {
        return false;
    } else if (redMissile && ((spawnz >= portalbluez && spawnz - sizez < portalbluez) || (spawnz >= portalredz && spawnz - sizez < portalredz)) && spawnx >= portalx1 && spawnx - sizex < portalx2 && spawny <= portaly2 && spawny + sizey > portaly1) {
        return false;
    }
    // Place structure
    structure.place(spawnLoc, true, rotation, Mirror.NONE, 0, 1, new Random());
    // Temp hotfix for structure rail rotation bug
    if (redMissile && structureName.contains("thunderbolt")) {
        Location railLoc = spawnLoc.add(0, 1, -8);
        Block block = railLoc.getBlock();
        block.setType(Material.POWERED_RAIL);
        RedstoneRail rail = (RedstoneRail) block.getBlockData();
        rail.setShape(Shape.NORTH_SOUTH);
        block.setBlockData(rail);
        block.getState().update(true);
    }
    return true;
}
Also used : StructureRotation(org.bukkit.block.structure.StructureRotation) StructureManager(org.bukkit.structure.StructureManager) IOException(java.io.IOException) MissileWarsPlugin(com.leomelonseeds.missilewars.MissileWarsPlugin) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) RedstoneRail(org.bukkit.block.data.type.RedstoneRail) Random(java.util.Random) Block(org.bukkit.block.Block) Structure(org.bukkit.structure.Structure) File(java.io.File) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

MissileWarsPlugin (com.leomelonseeds.missilewars.MissileWarsPlugin)7 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)4 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)4 MissileWarsPlayer (com.leomelonseeds.missilewars.teams.MissileWarsPlayer)3 Vector (org.bukkit.util.Vector)3 Arena (com.leomelonseeds.missilewars.arenas.Arena)2 File (java.io.File)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Random (java.util.Random)2 Location (org.bukkit.Location)2 Block (org.bukkit.block.Block)2 Player (org.bukkit.entity.Player)2 ArenaManager (com.leomelonseeds.missilewars.arenas.ArenaManager)1 MissileWarsTeam (com.leomelonseeds.missilewars.teams.MissileWarsTeam)1 SQLManager (com.leomelonseeds.missilewars.utilities.SQLManager)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)1 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)1 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)1