Search in sources :

Example 41 with BuildWorld

use of com.eintosti.buildsystem.object.world.BuildWorld in project BuildSystem by Trichtern.

the class WorldManager method createTemplateWorld.

/**
 * Generate a {@link BuildWorld} with a template.
 *
 * @param player       The player who is creating the world
 * @param worldName    The name of the world
 * @param template     The name of the template world
 * @param privateWorld Is world going to be a private world?
 * @return {@code true} if the world was successfully created, {@code false otherwise}
 */
private boolean createTemplateWorld(Player player, String worldName, String template, boolean privateWorld) {
    boolean worldExists = getBuildWorld(worldName) != null;
    File worldFile = new File(Bukkit.getWorldContainer(), worldName);
    if (worldExists || worldFile.exists()) {
        player.sendMessage(plugin.getString("worlds_world_exists"));
        return false;
    }
    File templateFile = new File(plugin.getDataFolder() + File.separator + "templates" + File.separator + template);
    if (!templateFile.exists()) {
        player.sendMessage(plugin.getString("worlds_template_does_not_exist"));
        return false;
    }
    BuildWorld buildWorld = new BuildWorld(plugin, worldName, player.getName(), player.getUniqueId(), WorldType.TEMPLATE, System.currentTimeMillis(), privateWorld);
    buildWorlds.add(buildWorld);
    player.sendMessage(plugin.getString("worlds_template_creation_started").replace("%world%", buildWorld.getName()).replace("%template%", template));
    FileUtils.copy(templateFile, worldFile);
    Bukkit.createWorld(WorldCreator.name(worldName).type(org.bukkit.WorldType.FLAT).generateStructures(false));
    player.sendMessage(plugin.getString("worlds_creation_finished"));
    return true;
}
Also used : BuildWorld(com.eintosti.buildsystem.object.world.BuildWorld) File(java.io.File)

Example 42 with BuildWorld

use of com.eintosti.buildsystem.object.world.BuildWorld in project BuildSystem by Trichtern.

the class WorldManager method manageWorldType.

/**
 * Depending on the {@link BuildWorld}'s {@link WorldType}, the corresponding {@link World} will be generated in a different way.
 * Then, if the creation of the world was successful and the config is set accordingly, the player is teleported to the world.
 *
 * @param player       The player who is creating the world
 * @param worldName    The name of the world
 * @param worldType    The world type
 * @param template     The name of the template world. Only if the world is being created with a template, otherwise {@code null}
 * @param privateWorld Is world going to be a private world?
 */
private void manageWorldType(Player player, String worldName, WorldType worldType, @Nullable String template, boolean privateWorld) {
    switch(worldType) {
        default:
            if (!createWorld(player, worldName, worldType, privateWorld)) {
                return;
            }
            break;
        case CUSTOM:
            if (!createCustomWorld(player, worldName, privateWorld)) {
                return;
            }
            break;
        case TEMPLATE:
            if (!createTemplateWorld(player, worldName, ChatColor.stripColor(template), privateWorld)) {
                return;
            }
            break;
    }
    if (configValues.isTeleportAfterCreation()) {
        BuildWorld buildWorld = getBuildWorld(worldName);
        if (buildWorld == null) {
            return;
        }
        buildWorld.manageUnload();
        teleport(player, buildWorld);
    }
}
Also used : BuildWorld(com.eintosti.buildsystem.object.world.BuildWorld)

Example 43 with BuildWorld

use of com.eintosti.buildsystem.object.world.BuildWorld in project BuildSystem by Trichtern.

the class WorldManager method createWorld.

/**
 * Generate a {@link BuildWorld} with a predefined generator.
 *
 * @param player       The player who is creating the world
 * @param worldName    The name of the world
 * @param worldType    The world type
 * @param privateWorld Is world going to be a private world?
 * @return {@code true} if the world was successfully created, {@code false otherwise}
 */
public boolean createWorld(Player player, String worldName, WorldType worldType, boolean privateWorld) {
    if (worldExists(player, worldName)) {
        return false;
    }
    BuildWorld buildWorld = new BuildWorld(plugin, worldName, player.getName(), player.getUniqueId(), worldType, System.currentTimeMillis(), privateWorld);
    buildWorlds.add(buildWorld);
    player.sendMessage(plugin.getString("worlds_world_creation_started").replace("%world%", worldName).replace("%type%", buildWorld.getType().getName()));
    finishPreparationsAndGenerate(buildWorld);
    player.sendMessage(plugin.getString("worlds_creation_finished"));
    return true;
}
Also used : BuildWorld(com.eintosti.buildsystem.object.world.BuildWorld)

Example 44 with BuildWorld

use of com.eintosti.buildsystem.object.world.BuildWorld in project BuildSystem by Trichtern.

the class WorldManager method renameWorld.

/**
 * Change the name of a {@link BuildWorld} to a given name.
 *
 * @param player     The player who issued the world rename
 * @param buildWorld The build world object
 * @param newName    The name the world should be renamed to
 */
public void renameWorld(Player player, BuildWorld buildWorld, String newName) {
    String oldName = buildWorld.getName();
    if (oldName.equalsIgnoreCase(newName)) {
        player.sendMessage(plugin.getString("worlds_rename_same_name"));
        return;
    }
    for (String charString : newName.split("")) {
        if (charString.matches("[^A-Za-z0-9/_-]")) {
            player.sendMessage(plugin.getString("worlds_world_creation_invalid_characters"));
            break;
        }
    }
    player.closeInventory();
    String parsedNewName = newName.replaceAll("[^A-Za-z0-9/_-]", "").replace(" ", "_").trim();
    if (parsedNewName.isEmpty()) {
        player.sendMessage(plugin.getString("worlds_world_creation_name_bank"));
        return;
    }
    if (Bukkit.getWorld(oldName) == null && !buildWorld.isLoaded()) {
        buildWorld.load();
    }
    World oldWorld = Bukkit.getWorld(oldName);
    if (oldWorld == null) {
        player.sendMessage(plugin.getString("worlds_rename_unknown_world"));
        return;
    }
    List<Player> removedPlayers = removePlayersFromWorld(oldName, plugin.getString("worlds_rename_players_world"));
    Bukkit.getScheduler().runTaskLater(plugin, () -> {
        oldWorld.save();
        Bukkit.getServer().unloadWorld(oldWorld, true);
        Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
            worldConfig.getFile().set("worlds." + parsedNewName, worldConfig.getFile().getConfigurationSection("worlds." + buildWorld.getName()));
            worldConfig.getFile().set("worlds." + oldName, null);
        });
        File oldWorldFile = new File(Bukkit.getWorldContainer(), oldName);
        File newWorldFile = new File(Bukkit.getWorldContainer(), parsedNewName);
        FileUtils.copy(oldWorldFile, newWorldFile);
        FileUtils.deleteDirectory(oldWorldFile);
        buildWorld.setName(parsedNewName);
        World newWorld = generateBukkitWorld(buildWorld.getName(), buildWorld.getType(), buildWorld.getDifficulty(), buildWorld.getChunkGenerator());
        Location spawnLocation = oldWorld.getSpawnLocation();
        spawnLocation.setWorld(newWorld);
        removedPlayers.stream().filter(Objects::nonNull).forEach(pl -> pl.teleport(spawnLocation.add(0.5, 0, 0.5)));
        removedPlayers.clear();
        SpawnManager spawnManager = plugin.getSpawnManager();
        if (spawnManager.spawnExists() && Objects.equals(spawnManager.getSpawnWorld(), oldWorld)) {
            Location oldSpawn = spawnManager.getSpawn();
            Location newSpawn = new Location(spawnLocation.getWorld(), oldSpawn.getX(), oldSpawn.getY(), oldSpawn.getZ(), oldSpawn.getYaw(), oldSpawn.getPitch());
            spawnManager.set(newSpawn, newSpawn.getWorld().getName());
        }
        player.sendMessage(plugin.getString("worlds_rename_set").replace("%oldName%", oldName).replace("%newName%", parsedNewName));
    }, 20L);
}
Also used : Player(org.bukkit.entity.Player) World(org.bukkit.World) BuildWorld(com.eintosti.buildsystem.object.world.BuildWorld) File(java.io.File) Location(org.bukkit.Location)

Example 45 with BuildWorld

use of com.eintosti.buildsystem.object.world.BuildWorld in project BuildSystem by Trichtern.

the class EditInventory method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (!inventoryManager.checkIfValidClick(event, "worldeditor_title")) {
        return;
    }
    ItemStack itemStack = event.getCurrentItem();
    if (itemStack == null) {
        return;
    }
    Player player = (Player) event.getWhoClicked();
    BuildWorld buildWorld = plugin.getPlayerManager().getSelectedWorld().get(player.getUniqueId());
    if (buildWorld == null) {
        player.closeInventory();
        player.sendMessage(plugin.getString("worlds_edit_error"));
        return;
    }
    switch(event.getSlot()) {
        case 20:
            if (hasPermission(player, "buildsystem.edit.breaking")) {
                buildWorld.setBlockBreaking(!buildWorld.isBlockBreaking());
            }
            break;
        case 21:
            if (hasPermission(player, "buildsystem.edit.placement")) {
                buildWorld.setBlockPlacement(!buildWorld.isBlockPlacement());
            }
            break;
        case 22:
            if (hasPermission(player, "buildsystem.edit.physics")) {
                buildWorld.setPhysics(!buildWorld.isPhysics());
            }
            break;
        case 23:
            if (hasPermission(player, "buildsystem.edit.time")) {
                changeTime(player, buildWorld);
            }
            break;
        case 24:
            if (hasPermission(player, "buildsystem.edit.explosions")) {
                buildWorld.setExplosions(!buildWorld.isExplosions());
            }
            break;
        case 29:
            if (hasPermission(player, "buildsystem.edit.entities")) {
                removeEntities(player, buildWorld);
            }
            return;
        case 30:
            if (itemStack.getType() == XMaterial.BARRIER.parseMaterial()) {
                XSound.ENTITY_ITEM_BREAK.play(player);
                return;
            }
            if (!hasPermission(player, "buildsystem.edit.builders")) {
                return;
            }
            if (event.isRightClick()) {
                XSound.BLOCK_CHEST_OPEN.play(player);
                player.openInventory(plugin.getBuilderInventory().getInventory(buildWorld, player));
                return;
            }
            buildWorld.setBuilders(!buildWorld.isBuilders());
            break;
        case 31:
            if (hasPermission(player, "buildsystem.edit.mobai")) {
                buildWorld.setMobAI(!buildWorld.isMobAI());
            }
            break;
        case 32:
            if (itemStack.getType() == XMaterial.BARRIER.parseMaterial()) {
                XSound.ENTITY_ITEM_BREAK.play(player);
                return;
            }
            if (!hasPermission(player, "buildsystem.edit.visibility")) {
                return;
            }
            buildWorld.setPrivate(!buildWorld.isPrivate());
            break;
        case 33:
            if (hasPermission(player, "buildsystem.edit.interactions")) {
                buildWorld.setBlockInteractions(!buildWorld.isBlockInteractions());
            }
            break;
        case 38:
            if (hasPermission(player, "buildsystem.edit.gamerules")) {
                XSound.BLOCK_CHEST_OPEN.play(player);
                plugin.getGameRules().resetInvIndex(player.getUniqueId());
                plugin.getGameRuleInventory().openInventory(player, buildWorld);
            }
            return;
        case 39:
            if (hasPermission(player, "buildsystem.edit.difficulty")) {
                buildWorld.cycleDifficulty();
                buildWorld.getWorld().setDifficulty(buildWorld.getDifficulty());
            }
            break;
        case 40:
            if (hasPermission(player, "buildsystem.edit.status")) {
                XSound.ENTITY_CHICKEN_EGG.play(player);
                plugin.getStatusInventory().openInventory(player);
            }
            return;
        case 41:
            if (hasPermission(player, "buildsystem.edit.project")) {
                XSound.ENTITY_CHICKEN_EGG.play(player);
                plugin.getWorldsCommand().getProjectInput(player, false);
            }
            return;
        case 42:
            if (hasPermission(player, "buildsystem.edit.permission")) {
                XSound.ENTITY_CHICKEN_EGG.play(player);
                plugin.getWorldsCommand().getPermissionInput(player, false);
            }
            return;
        default:
            return;
    }
    XSound.ENTITY_CHICKEN_EGG.play(player);
    openInventory(player, buildWorld);
}
Also used : BuildWorld(com.eintosti.buildsystem.object.world.BuildWorld) Player(org.bukkit.entity.Player) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BuildWorld (com.eintosti.buildsystem.object.world.BuildWorld)143 EventHandler (org.bukkit.event.EventHandler)58 Player (org.bukkit.entity.Player)52 World (org.bukkit.World)40 Block (org.bukkit.block.Block)20 ItemStack (org.bukkit.inventory.ItemStack)18 XMaterial (com.cryptomorin.xseries.XMaterial)16 PlayerChatInput (com.eintosti.buildsystem.util.external.PlayerChatInput)16 UUID (java.util.UUID)14 Location (org.bukkit.Location)14 File (java.io.File)12 ArrayList (java.util.ArrayList)9 BuildSystem (com.eintosti.buildsystem.BuildSystem)6 Builder (com.eintosti.buildsystem.object.world.Builder)6 List (java.util.List)6 Bukkit (org.bukkit.Bukkit)6 Material (org.bukkit.Material)6 ChunkGenerator (org.bukkit.generator.ChunkGenerator)6 Inventory (org.bukkit.inventory.Inventory)6 ItemMeta (org.bukkit.inventory.meta.ItemMeta)6