Search in sources :

Example 1 with HeartConfiguration

use of net.dzikoysk.funnyguilds.config.sections.HeartConfiguration in project FunnyGuilds by FunnyGuilds.

the class CreateCommand method execute.

@FunnyCommand(name = "${user.create.name}", description = "${user.create.description}", aliases = "${user.create.aliases}", permission = "funnyguilds.create", acceptsExceeded = true, playerOnly = true)
public void execute(Player player, User user, String[] args) {
    when(!config.guildsEnabled, messages.adminGuildsDisabled);
    when(LocationUtils.checkWorld(player), messages.blockedWorld);
    when(user.hasGuild(), messages.generalHasGuild);
    if (args.length != 2) {
        when(args.length == 0, messages.generalNoTagGiven);
        when(args.length == 1, messages.generalNoNameGiven);
        throw new ValidationException(messages.createMore);
    }
    String tag = args[0];
    if (!config.guildTagKeepCase) {
        tag = config.guildTagUppercase ? tag.toUpperCase() : tag.toLowerCase();
    }
    String name = args[1];
    Location guildLocation = player.getLocation().getBlock().getLocation();
    World world = player.getWorld();
    when(tag.length() > config.createTagLength, messages.createTagLength.replace("{LENGTH}", Integer.toString(config.createTagLength)));
    when(tag.length() < config.createTagMinLength, messages.createTagMinLength.replace("{LENGTH}", Integer.toString(config.createTagMinLength)));
    when(name.length() > config.createNameLength, messages.createNameLength.replace("{LENGTH}", Integer.toString(config.createNameLength)));
    when(name.length() < config.createNameMinLength, messages.createNameMinLength.replace("{LENGTH}", Integer.toString(config.createNameMinLength)));
    when(!tag.matches(config.tagRegex.getPattern()), messages.createOLTag);
    when(!name.matches(config.nameRegex.getPattern()), messages.createOLName);
    when(guildManager.nameExists(name), messages.createNameExists);
    when(guildManager.tagExists(tag), messages.createTagExists);
    when(config.regionsEnabled && this.regionManager.isInRegion(guildLocation), messages.createIsNear);
    when(config.regionsEnabled && this.regionManager.isNearRegion(guildLocation), messages.createIsNear);
    if (config.checkForRestrictedGuildNames) {
        when(!GuildUtils.validateName(config, name), messages.restrictedGuildName);
        when(!GuildUtils.validateTag(config, tag), messages.restrictedGuildTag);
    }
    HeartConfiguration heart = config.heart;
    if (config.regionsEnabled) {
        if (!heart.usePlayerPositionForCenterY) {
            guildLocation.setY(heart.createCenterY);
        }
        if (heart.createEntityType != null && guildLocation.getBlockY() < (world.getMaxHeight() - 2)) {
            guildLocation.setY(guildLocation.getBlockY() + 2);
        }
        int distance = config.regionSize + config.createDistance;
        if (config.enlargeItems != null) {
            distance += config.enlargeItems.size() * config.enlargeSize;
        }
        when(distance > LocationUtils.flatDistance(player.getWorld().getSpawnLocation(), guildLocation), messages.createSpawn.replace("{DISTANCE}", Integer.toString(distance)));
    }
    if (config.rankCreateEnable) {
        int requiredRank = player.hasPermission("funnyguilds.vip.rank") ? config.rankCreateVip : config.rankCreate;
        int points = user.getRank().getPoints();
        if (points < requiredRank) {
            String msg = messages.createRank;
            msg = StringUtils.replace(msg, "{REQUIRED-FORMAT}", NumberRange.inRangeToString(requiredRank, config.pointsFormat).replace("{POINTS}", "{REQUIRED}"));
            msg = StringUtils.replace(msg, "{REQUIRED}", String.valueOf(requiredRank));
            msg = StringUtils.replace(msg, "{POINTS-FORMAT}", NumberRange.inRangeToString(points, config.pointsFormat));
            msg = StringUtils.replace(msg, "{POINTS}", String.valueOf(points));
            user.sendMessage(msg);
            return;
        }
    }
    List<ItemStack> requiredItems = player.hasPermission("funnyguilds.vip.items") ? config.createItemsVip : config.createItems;
    int requiredExperience = player.hasPermission("funnyguilds.vip.items") ? config.requiredExperienceVip : config.requiredExperience;
    double requiredMoney = player.hasPermission("funnyguilds.vip.items") ? config.requiredMoneyVip : config.requiredMoney;
    if (player.getTotalExperience() < requiredExperience) {
        String msg = messages.createExperience;
        msg = StringUtils.replace(msg, "{EXP}", String.valueOf(requiredExperience));
        user.sendMessage(msg);
        return;
    }
    if (VaultHook.isEconomyHooked() && !VaultHook.canAfford(player, requiredMoney)) {
        String notEnoughMoneyMessage = messages.createMoney;
        notEnoughMoneyMessage = StringUtils.replace(notEnoughMoneyMessage, "{MONEY}", Double.toString(requiredMoney));
        user.sendMessage(notEnoughMoneyMessage);
        return;
    }
    if (!ItemUtils.playerHasEnoughItems(player, requiredItems)) {
        return;
    }
    Guild guild = new Guild(name, tag);
    guild.setOwner(user);
    guild.setLives(config.warLives);
    guild.setBorn(System.currentTimeMillis());
    guild.setValidity(Instant.now().plus(config.validityStart).toEpochMilli());
    guild.setProtection(Instant.now().plus(config.warProtection).toEpochMilli());
    guild.setPvP(config.damageGuild);
    guild.setHome(guildLocation);
    if (config.regionsEnabled) {
        Region region = new Region(guild, guildLocation, config.regionSize);
        guild.setRegion(region);
        WorldBorder border = world.getWorldBorder();
        double radius = border.getSize() / 2;
        FunnyBox bbox = FunnyBox.of(border.getCenter().toVector(), radius - config.createMinDistanceFromBorder, world.getMaxHeight(), radius - config.createMinDistanceFromBorder);
        FunnyBox gbox = FunnyBox.of(region.getFirstCorner(), region.getSecondCorner());
        // border box does not contain guild box
        if (!bbox.contains(gbox)) {
            String notEnoughDistanceMessage = messages.createNotEnoughDistanceFromBorder;
            notEnoughDistanceMessage = StringUtils.replace(notEnoughDistanceMessage, "{BORDER-MIN-DISTANCE}", Double.toString(config.createMinDistanceFromBorder));
            user.sendMessage(notEnoughDistanceMessage);
            return;
        }
        HookManager.HOLOGRAPHIC_DISPLAYS.peek(hologramManager -> hologramManager.getCorrectedLocation(guild).peek(location -> hologramManager.getOrCreateHologram(guild).peek(hologram -> hologram.setLocation(location))));
    }
    if (!SimpleEventHandler.handle(new GuildPreCreateEvent(EventCause.USER, user, guild))) {
        return;
    }
    player.getInventory().removeItem(ItemUtils.toArray(requiredItems));
    player.setTotalExperience(player.getTotalExperience() - requiredExperience);
    if (VaultHook.isEconomyHooked()) {
        VaultHook.withdrawFromPlayerBank(player, requiredMoney);
    }
    if (config.regionsEnabled) {
        if (heart.pasteSchematicOnCreation) {
            HookManager.WORLD_EDIT.peek(worldEdit -> {
                if (worldEdit.pasteSchematic(heart.guildSchematicFile, guildLocation, heart.pasteSchematicWithAir)) {
                    user.sendMessage(messages.createGuildCouldNotPasteSchematic);
                }
            });
        } else if (heart.createCenterSphere) {
            for (Location locationInSphere : SpaceUtils.sphere(guildLocation, 4, 4, false, true, 0)) {
                if (locationInSphere.getBlock().getType() != Material.BEDROCK) {
                    locationInSphere.getBlock().setType(Material.AIR);
                }
            }
            for (Location locationInSphere : SpaceUtils.sphere(guildLocation, 4, 4, true, true, 0)) {
                if (locationInSphere.getBlock().getType() != Material.BEDROCK) {
                    locationInSphere.getBlock().setType(Material.OBSIDIAN);
                }
            }
            if (config.eventPhysics) {
                guildLocation.clone().subtract(0.0D, 2.0D, 0.0D).getBlock().setType(Material.OBSIDIAN);
            }
        }
        this.guildManager.spawnHeart(guild);
        player.teleport(guildLocation);
    }
    this.guildManager.addGuild(guild);
    user.setGuild(guild);
    guild.getRegion().peek(region -> this.regionManager.addRegion(region));
    this.concurrencyManager.postRequests(new PrefixGlobalAddGuildRequest(guild), new PrefixGlobalAddPlayerRequest(user.getName()), new DatabaseUpdateGuildRequest(this.config, this.plugin.getDataModel(), guild));
    SimpleEventHandler.handle(new GuildCreateEvent(EventCause.USER, user, guild));
    Formatter formatter = new Formatter().register("{GUILD}", name).register("{TAG}", tag).register("{PLAYER}", player.getName());
    user.sendMessage(formatter.format(messages.createGuild));
    broadcastMessage(formatter.format(messages.broadcastCreate));
    if (!config.giveRewardsForFirstGuild) {
        return;
    }
    for (ItemStack item : config.firstGuildRewards) {
        if (player.getInventory().firstEmpty() == -1) {
            player.getWorld().dropItemNaturally(player.getLocation(), item);
            continue;
        }
        player.getInventory().addItem(item);
    }
}
Also used : VaultHook(net.dzikoysk.funnyguilds.feature.hooks.vault.VaultHook) FunnyComponent(net.dzikoysk.funnycommands.stereotypes.FunnyComponent) AbstractFunnyCommand(net.dzikoysk.funnyguilds.feature.command.AbstractFunnyCommand) Player(org.bukkit.entity.Player) LocationUtils(net.dzikoysk.funnyguilds.shared.bukkit.LocationUtils) StringUtils(org.apache.commons.lang3.StringUtils) WorldBorder(org.bukkit.WorldBorder) DefaultValidation.when(net.dzikoysk.funnyguilds.feature.command.DefaultValidation.when) PrefixGlobalAddGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddGuildRequest) Location(org.bukkit.Location) FunnyBox(net.dzikoysk.funnyguilds.shared.bukkit.FunnyBox) World(org.bukkit.World) ValidationException(net.dzikoysk.funnycommands.resources.ValidationException) Region(net.dzikoysk.funnyguilds.guild.Region) User(net.dzikoysk.funnyguilds.user.User) Formatter(panda.utilities.text.Formatter) FunnyCommand(net.dzikoysk.funnycommands.stereotypes.FunnyCommand) GuildUtils(net.dzikoysk.funnyguilds.guild.GuildUtils) Material(org.bukkit.Material) GuildCreateEvent(net.dzikoysk.funnyguilds.event.guild.GuildCreateEvent) PrefixGlobalAddPlayerRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddPlayerRequest) SimpleEventHandler(net.dzikoysk.funnyguilds.event.SimpleEventHandler) Instant(java.time.Instant) ItemStack(org.bukkit.inventory.ItemStack) DatabaseUpdateGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateGuildRequest) List(java.util.List) EventCause(net.dzikoysk.funnyguilds.event.FunnyEvent.EventCause) NumberRange(net.dzikoysk.funnyguilds.config.NumberRange) HeartConfiguration(net.dzikoysk.funnyguilds.config.sections.HeartConfiguration) Guild(net.dzikoysk.funnyguilds.guild.Guild) ItemUtils(net.dzikoysk.funnyguilds.shared.bukkit.ItemUtils) GuildPreCreateEvent(net.dzikoysk.funnyguilds.event.guild.GuildPreCreateEvent) HookManager(net.dzikoysk.funnyguilds.feature.hooks.HookManager) SpaceUtils(net.dzikoysk.funnyguilds.shared.bukkit.SpaceUtils) ValidationException(net.dzikoysk.funnycommands.resources.ValidationException) GuildCreateEvent(net.dzikoysk.funnyguilds.event.guild.GuildCreateEvent) Formatter(panda.utilities.text.Formatter) FunnyBox(net.dzikoysk.funnyguilds.shared.bukkit.FunnyBox) HeartConfiguration(net.dzikoysk.funnyguilds.config.sections.HeartConfiguration) GuildPreCreateEvent(net.dzikoysk.funnyguilds.event.guild.GuildPreCreateEvent) World(org.bukkit.World) Guild(net.dzikoysk.funnyguilds.guild.Guild) DatabaseUpdateGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateGuildRequest) PrefixGlobalAddPlayerRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddPlayerRequest) WorldBorder(org.bukkit.WorldBorder) PrefixGlobalAddGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddGuildRequest) Region(net.dzikoysk.funnyguilds.guild.Region) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) AbstractFunnyCommand(net.dzikoysk.funnyguilds.feature.command.AbstractFunnyCommand) FunnyCommand(net.dzikoysk.funnycommands.stereotypes.FunnyCommand)

Example 2 with HeartConfiguration

use of net.dzikoysk.funnyguilds.config.sections.HeartConfiguration in project FunnyGuilds by FunnyGuilds.

the class MoveCommand method execute.

@FunnyCommand(name = "${admin.move.name}", permission = "funnyguilds.admin", acceptsExceeded = true, playerOnly = true)
public void execute(Player player, User admin, String[] args) {
    when(!config.regionsEnabled, messages.regionsDisabled);
    when(args.length < 1, messages.generalNoTagGiven);
    HeartConfiguration heartConfig = config.heart;
    Guild guild = GuildValidation.requireGuildByTag(args[0]);
    Location location = player.getLocation();
    if (!heartConfig.usePlayerPositionForCenterY) {
        location.setY(heartConfig.createCenterY);
    }
    int distance = config.regionSize + config.createDistance;
    if (config.enlargeItems != null) {
        distance = config.enlargeItems.size() * config.enlargeSize + distance;
    }
    when(distance > LocationUtils.flatDistance(player.getWorld().getSpawnLocation(), location), messages.createSpawn.replace("{DISTANCE}", Integer.toString(distance)));
    when(this.regionManager.isNearRegion(location), messages.createIsNear);
    if (!SimpleEventHandler.handle(new GuildMoveEvent(AdminUtils.getCause(admin), admin, guild, location))) {
        return;
    }
    Region region = guild.getRegion().peek(peekRegion -> {
        if (heartConfig.createEntityType != null) {
            GuildEntityHelper.despawnGuildHeart(guild);
        } else if (heartConfig.createMaterial != null && heartConfig.createMaterial.getLeft() != Material.AIR) {
            peekRegion.getHeartBlock().filter(heart -> heart.getLocation().getBlockY() > 1).peek(heart -> Bukkit.getScheduler().runTask(this.plugin, () -> heart.setType(Material.AIR)));
        }
        peekRegion.setCenter(location);
    }).orElseGet(() -> new Region(guild, location, config.regionSize));
    if (heartConfig.createCenterSphere) {
        List<Location> sphere = SpaceUtils.sphere(location, 3, 3, false, true, 0);
        for (Location locationInSphere : sphere) {
            if (locationInSphere.getBlock().getType() != Material.BEDROCK) {
                locationInSphere.getBlock().setType(Material.AIR);
            }
        }
    }
    this.guildManager.spawnHeart(guild);
    admin.sendMessage(messages.adminGuildRelocated.replace("{GUILD}", guild.getName()).replace("{REGION}", region.getName()));
}
Also used : AbstractFunnyCommand(net.dzikoysk.funnyguilds.feature.command.AbstractFunnyCommand) GuildEntityHelper(net.dzikoysk.funnyguilds.nms.GuildEntityHelper) SimpleEventHandler(net.dzikoysk.funnyguilds.event.SimpleEventHandler) Player(org.bukkit.entity.Player) LocationUtils(net.dzikoysk.funnyguilds.shared.bukkit.LocationUtils) DefaultValidation.when(net.dzikoysk.funnyguilds.feature.command.DefaultValidation.when) List(java.util.List) Location(org.bukkit.Location) HeartConfiguration(net.dzikoysk.funnyguilds.config.sections.HeartConfiguration) Guild(net.dzikoysk.funnyguilds.guild.Guild) GuildValidation(net.dzikoysk.funnyguilds.feature.command.GuildValidation) GuildMoveEvent(net.dzikoysk.funnyguilds.event.guild.GuildMoveEvent) Region(net.dzikoysk.funnyguilds.guild.Region) User(net.dzikoysk.funnyguilds.user.User) FunnyCommand(net.dzikoysk.funnycommands.stereotypes.FunnyCommand) SpaceUtils(net.dzikoysk.funnyguilds.shared.bukkit.SpaceUtils) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) GuildMoveEvent(net.dzikoysk.funnyguilds.event.guild.GuildMoveEvent) HeartConfiguration(net.dzikoysk.funnyguilds.config.sections.HeartConfiguration) Region(net.dzikoysk.funnyguilds.guild.Region) Guild(net.dzikoysk.funnyguilds.guild.Guild) Location(org.bukkit.Location) AbstractFunnyCommand(net.dzikoysk.funnyguilds.feature.command.AbstractFunnyCommand) FunnyCommand(net.dzikoysk.funnycommands.stereotypes.FunnyCommand)

Aggregations

List (java.util.List)2 FunnyCommand (net.dzikoysk.funnycommands.stereotypes.FunnyCommand)2 HeartConfiguration (net.dzikoysk.funnyguilds.config.sections.HeartConfiguration)2 SimpleEventHandler (net.dzikoysk.funnyguilds.event.SimpleEventHandler)2 AbstractFunnyCommand (net.dzikoysk.funnyguilds.feature.command.AbstractFunnyCommand)2 DefaultValidation.when (net.dzikoysk.funnyguilds.feature.command.DefaultValidation.when)2 Guild (net.dzikoysk.funnyguilds.guild.Guild)2 Region (net.dzikoysk.funnyguilds.guild.Region)2 LocationUtils (net.dzikoysk.funnyguilds.shared.bukkit.LocationUtils)2 SpaceUtils (net.dzikoysk.funnyguilds.shared.bukkit.SpaceUtils)2 User (net.dzikoysk.funnyguilds.user.User)2 Location (org.bukkit.Location)2 Material (org.bukkit.Material)2 Player (org.bukkit.entity.Player)2 Instant (java.time.Instant)1 ValidationException (net.dzikoysk.funnycommands.resources.ValidationException)1 FunnyComponent (net.dzikoysk.funnycommands.stereotypes.FunnyComponent)1 DatabaseUpdateGuildRequest (net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateGuildRequest)1 PrefixGlobalAddGuildRequest (net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddGuildRequest)1 PrefixGlobalAddPlayerRequest (net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixGlobalAddPlayerRequest)1