Search in sources :

Example 1 with UserPromoteEvent

use of com.iridium.iridiumskyblock.api.UserPromoteEvent in project IridiumSkyblock by Iridium-Development.

the class PromoteCommand method execute.

/**
 * Executes the command for the specified {@link CommandSender} with the provided arguments.
 * Not called when the command execution was invalid (no permission, no player or command disabled).
 * Promotes a user in the Island rank system.
 *
 * @param sender The CommandSender which executes this command
 * @param args   The arguments used with this command. They contain the sub-command
 */
@Override
public boolean execute(CommandSender sender, String[] args) {
    if (args.length != 2) {
        sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    Player player = (Player) sender;
    User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
    Optional<Island> island = user.getIsland();
    if (!island.isPresent()) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    OfflinePlayer targetPlayer = Bukkit.getServer().getOfflinePlayer(args[1]);
    User targetUser = IridiumSkyblock.getInstance().getUserManager().getUser(targetPlayer);
    if (!island.get().equals(targetUser.getIsland().orElse(null))) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userNotInYourIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    IslandRank nextRank = IslandRank.getByLevel(targetUser.getIslandRank().getLevel() + 1);
    if (nextRank == null || nextRank.getLevel() >= user.getIslandRank().getLevel() || !IridiumSkyblock.getInstance().getIslandManager().getIslandPermission(island.get(), IridiumSkyblock.getInstance().getUserManager().getUser(player), PermissionType.PROMOTE)) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotPromoteUser.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    UserPromoteEvent userPromoteEvent = new UserPromoteEvent(island.get(), user, nextRank);
    Bukkit.getPluginManager().callEvent(userPromoteEvent);
    if (userPromoteEvent.isCancelled())
        return false;
    targetUser.setIslandRank(nextRank);
    for (User member : island.get().getMembers()) {
        Player islandMember = Bukkit.getPlayer(member.getUuid());
        if (islandMember != null) {
            if (islandMember.equals(player)) {
                islandMember.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().promotedPlayer.replace("%player%", targetUser.getName()).replace("%rank%", nextRank.getDisplayName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
            } else {
                islandMember.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userPromotedPlayer.replace("%promoter%", player.getName()).replace("%player%", targetUser.getName()).replace("%rank%", nextRank.getDisplayName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
            }
        }
    }
    IslandLog islandLog = new IslandLog(island.get(), LogAction.USER_PROMOTED, user, targetUser, 0, nextRank.getDisplayName());
    IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().addEntry(islandLog);
    return true;
}
Also used : IslandLog(com.iridium.iridiumskyblock.database.IslandLog) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) User(com.iridium.iridiumskyblock.database.User) UserPromoteEvent(com.iridium.iridiumskyblock.api.UserPromoteEvent) IslandRank(com.iridium.iridiumskyblock.IslandRank) OfflinePlayer(org.bukkit.OfflinePlayer) Island(com.iridium.iridiumskyblock.database.Island)

Aggregations

IslandRank (com.iridium.iridiumskyblock.IslandRank)1 UserPromoteEvent (com.iridium.iridiumskyblock.api.UserPromoteEvent)1 Island (com.iridium.iridiumskyblock.database.Island)1 IslandLog (com.iridium.iridiumskyblock.database.IslandLog)1 User (com.iridium.iridiumskyblock.database.User)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 Player (org.bukkit.entity.Player)1