Search in sources :

Example 1 with UserDemoteEvent

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

the class DemoteCommand 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).
 * Demotes 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 offlinePlayer = Bukkit.getServer().getOfflinePlayer(args[1]);
    User offlinePlayerUser = IridiumSkyblock.getInstance().getUserManager().getUser(offlinePlayer);
    if (!island.get().equals(offlinePlayerUser.getIsland().orElse(null))) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userNotInYourIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    IslandRank nextRank = IslandRank.getByLevel(offlinePlayerUser.getIslandRank().getLevel() - 1);
    if (nextRank == null || offlinePlayerUser.getIslandRank().getLevel() >= user.getIslandRank().getLevel() || !IridiumSkyblock.getInstance().getIslandManager().getIslandPermission(island.get(), IridiumSkyblock.getInstance().getUserManager().getUser(player), PermissionType.DEMOTE)) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotDemoteUser.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    if (nextRank.equals(IslandRank.VISITOR)) {
        IridiumSkyblock.getInstance().getCommands().kickCommand.execute(sender, args);
        return true;
    }
    UserDemoteEvent userDemoteEvent = new UserDemoteEvent(island.get(), user, nextRank);
    Bukkit.getPluginManager().callEvent(userDemoteEvent);
    if (userDemoteEvent.isCancelled())
        return false;
    offlinePlayerUser.setIslandRank(nextRank);
    for (User member : island.get().getMembers()) {
        Player islandMember = Bukkit.getPlayer(member.getUuid());
        if (islandMember == null)
            continue;
        if (islandMember.equals(player)) {
            islandMember.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().demotedPlayer.replace("%player%", offlinePlayerUser.getName()).replace("%rank%", nextRank.getDisplayName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        } else {
            islandMember.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userDemotedPlayer.replace("%promoter%", player.getName()).replace("%player%", offlinePlayerUser.getName()).replace("%rank%", nextRank.getDisplayName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        }
    }
    IslandLog islandLog = new IslandLog(island.get(), LogAction.USER_DEMOTED, user, offlinePlayerUser, 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) IslandRank(com.iridium.iridiumskyblock.IslandRank) OfflinePlayer(org.bukkit.OfflinePlayer) UserDemoteEvent(com.iridium.iridiumskyblock.api.UserDemoteEvent) Island(com.iridium.iridiumskyblock.database.Island)

Aggregations

IslandRank (com.iridium.iridiumskyblock.IslandRank)1 UserDemoteEvent (com.iridium.iridiumskyblock.api.UserDemoteEvent)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