use of com.alessiodp.core.common.user.OfflineUser in project Parties by AlessioDP.
the class CommandKick method onCommand.
@Override
public void onCommand(@NotNull CommandData commandData) {
User sender = commandData.getSender();
PartyPlayerImpl partyPlayer = ((PartiesCommandData) commandData).getPartyPlayer();
// Command handling
String playerName = commandData.getArgs()[1];
UUID playerUUID = null;
Set<UUID> matchingPlayers = LLAPIHandler.getPlayerByName(playerName);
List<UUID> listPlayers = new LinkedList<>(matchingPlayers);
listPlayers.removeIf((uuid) -> !getPlugin().getPlayerManager().getPlayer(uuid).isInParty());
Collections.sort(listPlayers);
if (listPlayers.size() > 1) {
// Check if the player have selected someone
if (commandData.getArgs().length > 2) {
try {
int sel = Integer.parseInt(commandData.getArgs()[2]);
playerUUID = listPlayers.get(sel - 1);
} catch (Exception ignored) {
}
}
// The player didn't select a player
if (playerUUID == null) {
for (String str : Messages.MAINCMD_KICK_CONFLICT_CONTENT) {
if (str.contains("%list_players%")) {
int i = 1;
for (UUID uuid : listPlayers) {
PartyPlayerImpl pp = getPlugin().getPlayerManager().getPlayer(uuid);
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_CONFLICT_PLAYER.replace("%number%", Integer.toString(i)).replace("%username%", playerName), pp, getPlugin().getPartyManager().getPartyOfPlayer(pp));
i++;
}
} else {
sendMessage(sender, partyPlayer, str);
}
}
return;
}
} else if (listPlayers.size() == 1) {
// Player found
playerUUID = listPlayers.get(0);
} else {
// Not found
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PLAYER_NOT_FOUND.replace("%player%", playerName));
return;
}
OfflineUser kickedPlayer = plugin.getOfflinePlayer(playerUUID);
Objects.requireNonNull(kickedPlayer);
PartyPlayerImpl kickedPp = getPlugin().getPlayerManager().getPlayer(playerUUID);
PartyImpl party = partyPlayer != null ? getPlugin().getPartyManager().getParty(partyPlayer.getPartyId()) : null;
boolean otherParty = false;
if (party == null || !party.getMembers().contains(kickedPlayer.getUUID())) {
// Other party
otherParty = true;
if (commandData.havePermission(PartiesPermission.ADMIN_KICK_OTHERS)) {
party = getPlugin().getPartyManager().getParty(kickedPp.getPartyId());
if (party == null) {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PLAYER_NOT_IN_PARTY, kickedPp);
return;
}
} else {
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_PLAYERNOTINPARTY, kickedPp);
return;
}
} else {
// Same party
if (partyPlayer.getRank() < kickedPp.getRank() && !commandData.havePermission(PartiesPermission.ADMIN_KICK_OTHERS)) {
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_PLAYERHIGHERRANK, kickedPp);
return;
}
}
// Command starts
// Calling API event
IPlayerPreLeaveEvent partiesPreLeaveEvent = getPlugin().getEventManager().preparePlayerPreLeaveEvent(kickedPp, party, LeaveCause.KICK, partyPlayer);
getPlugin().getEventManager().callEvent(partiesPreLeaveEvent);
if (!partiesPreLeaveEvent.isCancelled()) {
if (party.getLeader() != null && party.getLeader().equals(kickedPlayer.getUUID())) {
// Leader
boolean mustDelete = true;
// Check if leader can be changed
if (ConfigParties.GENERAL_MEMBERS_ON_PARTY_LEAVE_CHANGE_LEADER && party.getMembers().size() > 1) {
PartyPlayerImpl newLeader = party.findNewLeader();
if (newLeader != null) {
// Found a new leader
mustDelete = false;
party.changeLeader(newLeader);
party.removeMember(kickedPp, LeaveCause.KICK, partyPlayer);
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_SENT, kickedPp);
party.broadcastMessage(Messages.MAINCMD_KICK_BROADCAST_LEADER_CHANGED, newLeader);
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_KICK_LEADER_CHANGE, sender.getName(), kickedPp.getName(), party.getName() != null ? party.getName() : "_", newLeader.getName()), true);
}
}
if (mustDelete) {
// Calling Pre API event
IPartyPreDeleteEvent partiesPreDeleteEvent = getPlugin().getEventManager().preparePartyPreDeleteEvent(party, DeleteCause.KICK, kickedPp, partyPlayer);
getPlugin().getEventManager().callEvent(partiesPreDeleteEvent);
if (!partiesPreDeleteEvent.isCancelled()) {
// Disbanding party
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_SENT, kickedPp);
party.broadcastMessage(Messages.MAINCMD_KICK_BROADCAST_DISBAND, kickedPp);
party.delete(DeleteCause.KICK, kickedPp, partyPlayer);
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_KICK, sender.getName(), kickedPp.getName(), party.getName() != null ? party.getName() : "_", otherParty, true), true);
} else
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_API_DELETEEVENT_DENY, party.getId(), sender.getName(), sender.getUUID().toString()), true);
}
} else {
// Normal
if (kickedPlayer.isOnline()) {
// Online
kickedPp.sendMessage(Messages.MAINCMD_KICK_PLAYERKICKED, partyPlayer);
}
party.removeMember(kickedPp, LeaveCause.KICK, partyPlayer);
sendMessage(sender, partyPlayer, Messages.MAINCMD_KICK_SENT, kickedPp);
party.broadcastMessage(Messages.MAINCMD_KICK_BROADCAST, kickedPp);
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_KICK, sender.getName(), kickedPp.getName(), party.getName() != null ? party.getName() : "_", otherParty, false), true);
}
} else
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_API_LEAVEEVENT_DENY, sender.getUUID().toString(), party.getId()), true);
}
use of com.alessiodp.core.common.user.OfflineUser in project Parties by AlessioDP.
the class CommandNickname method onCommand.
@Override
public void onCommand(@NotNull CommandData commandData) {
User sender = commandData.getSender();
PartyPlayerImpl senderPp = ((PartiesCommandData) commandData).getPartyPlayer();
// Command handling
String playerName = commandData.getArgs()[1];
UUID playerUUID;
Set<UUID> matchingPlayers;
if (LLAPIHandler.isEnabled()) {
// Use LastLoginAPI to get a list of players with the same name
matchingPlayers = LLAPIHandler.getPlayerByName(playerName);
} else {
// Get only the online player with the same name
matchingPlayers = new HashSet<>();
User user = plugin.getPlayerByName(playerName);
if (user != null)
matchingPlayers.add(user.getUUID());
}
List<UUID> listPlayers = new LinkedList<>(matchingPlayers);
if (listPlayers.size() == 0) {
sendMessage(sender, senderPp, Messages.PARTIES_COMMON_PLAYER_NOT_FOUND.replace("%player%", playerName));
return;
}
listPlayers.removeIf((uuid) -> !getPlugin().getPlayerManager().getPlayer(uuid).isInParty());
playerUUID = listPlayers.stream().findAny().orElse(null);
if (playerUUID == null) {
sendMessage(sender, senderPp, Messages.PARTIES_COMMON_PLAYER_NOT_IN_PARTY.replace("%player%", playerName));
return;
}
OfflineUser targetPlayer = plugin.getOfflinePlayer(playerUUID);
Objects.requireNonNull(targetPlayer);
PartyPlayerImpl targetPp = getPlugin().getPlayerManager().getPlayer(playerUUID);
PartyImpl party = getPlugin().getPartyManager().getParty(targetPp.getPartyId());
boolean isOwn = senderPp != null && senderPp.getPlayerUUID().equals(targetPp.getPlayerUUID());
if (party == null) {
sendMessage(sender, senderPp, Messages.PARTIES_COMMON_NOTINPARTY);
return;
}
if (senderPp != null) {
if (isOwn) {
if (!getPlugin().getRankManager().checkPlayerRank(senderPp, RankPermission.EDIT_NICKNAME_OWN)) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OWN_NO_PERMISSION);
return;
}
} else {
if (!party.getId().equals(senderPp.getPartyId()) && !commandData.havePermission(PartiesPermission.ADMIN_NICKNAME_OTHERS)) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OTHERS_NO_PERMISSION);
return;
}
if (!getPlugin().getRankManager().checkPlayerRank(senderPp, RankPermission.EDIT_NICKNAME_OTHERS)) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OTHERS_NO_PERMISSION);
return;
}
}
}
if (commandData.getArgs().length == 2) {
if (targetPp.getNickname() != null) {
sendMessage(sender, senderPp, isOwn ? Messages.ADDCMD_NICKNAME_SHOW_OWN : Messages.ADDCMD_NICKNAME_SHOW_OTHER, targetPp);
} else {
sendMessage(sender, senderPp, isOwn ? Messages.ADDCMD_NICKNAME_SHOW_OWN_NONE : Messages.ADDCMD_NICKNAME_SHOW_OTHER_NONE, targetPp);
}
} else if (commandData.getArgs().length >= 3) {
// Set/remove
boolean isRemove = false;
String nickname = "";
if (commandData.getArgs()[2].equalsIgnoreCase(ConfigMain.COMMANDS_MISC_REMOVE)) {
// Remove command
isRemove = true;
} else {
nickname = plugin.getCommandManager().getCommandUtils().handleCommandString(commandData.getArgs(), 2);
if (!CensorUtils.checkAllowedCharacters(ConfigParties.ADDITIONAL_NICKNAME_ALLOWEDCHARS, nickname, PartiesConstants.DEBUG_CMD_NICKNAME_REGEXERROR_ALLOWEDCHARS) || (nickname.length() > ConfigParties.ADDITIONAL_NICKNAME_MAXLENGTH) || (nickname.length() < ConfigParties.ADDITIONAL_NICKNAME_MINLENGTH)) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_INVALID);
return;
}
if (CensorUtils.checkCensor(ConfigParties.ADDITIONAL_NICKNAME_CENSORREGEX, nickname, PartiesConstants.DEBUG_CMD_NICKNAME_REGEXERROR_CENSORED)) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_CENSORED);
return;
}
if (getPlugin().getEconomyManager().payCommand(EconomyManager.PaidCommand.NICKNAME, senderPp, commandData.getCommandLabel(), commandData.getArgs()))
return;
}
// Command starts
if (isRemove) {
targetPp.setNickname(null);
if (isOwn) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OWN_REMOVED, targetPp);
} else {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OTHERS_REMOVED, targetPp);
if (senderPp != null && targetPlayer.isOnline())
targetPp.sendMessage(Messages.ADDCMD_NICKNAME_OTHERS_TARGET_REMOVED, senderPp);
}
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_NICKNAME, sender.getName(), targetPp.getName()), true);
} else {
targetPp.setNickname(nickname);
if (isOwn) {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OWN_CHANGED, targetPp);
} else {
sendMessage(sender, senderPp, Messages.ADDCMD_NICKNAME_OTHERS_CHANGED, targetPp);
if (senderPp != null && targetPlayer.isOnline())
targetPp.sendMessage(Messages.ADDCMD_NICKNAME_OTHERS_TARGET_CHANGED.replace("%nickname%", nickname), senderPp);
}
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_NICKNAME_REM, sender.getName(), targetPp.getName()), true);
}
}
}
Aggregations