Search in sources :

Example 1 with UserJoinEvent

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

the class JoinCommand 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).
 * Enables users to join other Islands.
 *
 * @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);
    if (user.getIsland().isPresent()) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().alreadyHaveIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(args[1]);
    User offlinePlayerUser = IridiumSkyblock.getInstance().getUserManager().getUser(offlinePlayer);
    Optional<Island> island = offlinePlayerUser.getIsland();
    if (!island.isPresent()) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userNoIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    Optional<IslandInvite> islandInvite = IridiumSkyblock.getInstance().getIslandManager().getIslandInvite(island.get(), user);
    if (!islandInvite.isPresent() && !user.isBypassing()) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noInvite.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    IslandUpgrade islandUpgrade = IridiumSkyblock.getInstance().getIslandManager().getIslandUpgrade(island.get(), "member");
    int memberLimit = IridiumSkyblock.getInstance().getUpgrades().memberUpgrade.upgrades.get(islandUpgrade.getLevel()).amount;
    if (!user.isBypassing() && island.get().getMembers().size() >= memberLimit) {
        player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().islandMemberLimitReached.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        return false;
    }
    Optional<User> inviter = islandInvite.map(IslandInvite::getInviter);
    UserJoinEvent userJoinEvent = new UserJoinEvent(island.get(), user, inviter.orElse(null));
    Bukkit.getPluginManager().callEvent(userJoinEvent);
    if (userJoinEvent.isCancelled())
        return false;
    // Send a message to all other members
    for (User member : island.get().getMembers()) {
        Player islandMember = Bukkit.getPlayer(member.getName());
        if (islandMember != null) {
            islandMember.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().playerJoinedYourIsland.replace("%player%", player.getName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
        }
    }
    user.setIsland(island.get());
    user.setIslandRank(IslandRank.MEMBER);
    islandInvite.ifPresent(invite -> IridiumSkyblock.getInstance().getDatabaseManager().getIslandInviteTableManager().delete(invite));
    IridiumSkyblock.getInstance().getIslandManager().teleportHome(player, island.get(), 0);
    IslandLog islandLog = new IslandLog(island.get(), LogAction.USER_JOINED, user, null, 0, "");
    IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().addEntry(islandLog);
    return true;
}
Also used : IslandUpgrade(com.iridium.iridiumskyblock.database.IslandUpgrade) IslandLog(com.iridium.iridiumskyblock.database.IslandLog) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) User(com.iridium.iridiumskyblock.database.User) OfflinePlayer(org.bukkit.OfflinePlayer) IslandInvite(com.iridium.iridiumskyblock.database.IslandInvite) UserJoinEvent(com.iridium.iridiumskyblock.api.UserJoinEvent) Island(com.iridium.iridiumskyblock.database.Island)

Aggregations

UserJoinEvent (com.iridium.iridiumskyblock.api.UserJoinEvent)1 Island (com.iridium.iridiumskyblock.database.Island)1 IslandInvite (com.iridium.iridiumskyblock.database.IslandInvite)1 IslandLog (com.iridium.iridiumskyblock.database.IslandLog)1 IslandUpgrade (com.iridium.iridiumskyblock.database.IslandUpgrade)1 User (com.iridium.iridiumskyblock.database.User)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 Player (org.bukkit.entity.Player)1