use of com.alessiodp.parties.common.tasks.HomeDelayTask in project Parties by AlessioDP.
the class CommandHome method onCommand.
@Override
public void onCommand(@NotNull CommandData commandData) {
User sender = commandData.getSender();
PartyPlayerImpl partyPlayer = ((PartiesCommandData) commandData).getPartyPlayer();
PartyImpl party = getPlugin().getPartyManager().getPartyOfPlayer(partyPlayer);
// Command handling
PartyHomeImpl partyHome = null;
if (commandData.getArgs().length == 1) {
if (party == null) {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
return;
}
if (party.getHomes().size() == 0) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
return;
}
}
if (ConfigParties.ADDITIONAL_HOME_MAX_HOMES > 1) {
// Multiple home
if (commandData.getArgs().length == 1) {
if (party.getHomes().size() > 1) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_MUST_SELECT_HOME, party);
printValidHomes(sender, partyPlayer, party);
return;
}
Optional<PartyHome> opt = party.getHomes().stream().findFirst();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
else {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
printValidHomes(sender, partyPlayer, party);
return;
}
} else if (commandData.getArgs().length == 2) {
// Not admin - Not in party
if (!commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS) && party == null) {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
return;
}
// Get the partyHome of the current party
if (party != null) {
Optional<PartyHome> opt = party.getHomes().stream().filter((ph) -> ph.getName() != null && ph.getName().equalsIgnoreCase(commandData.getArgs()[1])).findAny();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
}
// If no home but home.others permission
if (partyHome == null && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
if (party != null) {
if (party.getHomes().size() > 1) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_MUST_SELECT_HOME, party);
printValidHomes(sender, partyPlayer, party);
return;
}
Optional<PartyHome> opt = party.getHomes().stream().findFirst();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
}
}
// Not in party
if (party == null) {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
return;
}
// No party home found
if (partyHome == null) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_INVALID_HOME, party);
printValidHomes(sender, partyPlayer, party);
return;
}
} else if (commandData.getArgs().length == 3 && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
if (party != null) {
Optional<PartyHome> opt = party.getHomes().stream().filter((ph) -> ph.getName() != null && ph.getName().equalsIgnoreCase(commandData.getArgs()[1])).findAny();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
else {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
printValidHomes(sender, partyPlayer, party);
return;
}
} else {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PARTYNOTFOUND.replace("%party%", commandData.getArgs()[1]));
return;
}
} else {
sendMessage(sender, partyPlayer, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
return;
}
} else {
// Single home
if (commandData.getArgs().length == 1) {
Optional<PartyHome> opt = party.getHomes().stream().findFirst();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
else {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
return;
}
} else if (commandData.getArgs().length == 2 && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
if (party != null) {
Optional<PartyHome> opt = party.getHomes().stream().findFirst();
if (opt.isPresent())
partyHome = (PartyHomeImpl) opt.get();
else {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
return;
}
} else {
sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PARTYNOTFOUND.replace("%party%", commandData.getArgs()[1]));
return;
}
} else {
sendMessage(sender, partyPlayer, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
return;
}
}
if (!commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS) && !getPlugin().getRankManager().checkPlayerRankAlerter(partyPlayer, RankPermission.HOME))
return;
if (partyPlayer.getPendingHomeDelay() != null) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_TELEPORTWAITING, party);
return;
}
boolean mustStartCooldown = false;
if (ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME > 0 && !commandData.havePermission(PartiesPermission.ADMIN_COOLDOWN_HOME_BYPASS)) {
mustStartCooldown = true;
long remainingCooldown = getPlugin().getCooldownManager().canAction(CooldownManager.Action.HOME, sender.getUUID(), ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME);
if (remainingCooldown > 0) {
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_COOLDOWN.replace("%seconds%", String.valueOf(remainingCooldown)));
return;
}
}
if (getPlugin().getEconomyManager().payCommand(EconomyManager.PaidCommand.HOME, partyPlayer, commandData.getCommandLabel(), commandData.getArgs()))
return;
if (mustStartCooldown)
getPlugin().getCooldownManager().startAction(CooldownManager.Action.HOME, sender.getUUID(), ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME);
// Command starts
int delay = ConfigParties.ADDITIONAL_HOME_DELAY;
String homeDelayPermission = sender.getDynamicPermission(PartiesPermission.USER_HOME + ".");
if (homeDelayPermission != null) {
try {
delay = Integer.parseInt(homeDelayPermission);
} catch (Exception ignored) {
}
}
if (delay > 0) {
HomeDelayTask homeDelayTask = teleportPlayerWithDelay(partyPlayer, partyHome, delay);
CancellableTask task = plugin.getScheduler().scheduleAsyncRepeating(homeDelayTask, 0, 300, TimeUnit.MILLISECONDS);
partyPlayer.setPendingHomeDelay(task);
sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_TELEPORTIN.replace("%seconds%", Integer.toString(delay)));
} else {
teleportPlayer(sender, partyPlayer, partyHome);
}
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_HOME, partyPlayer.getName(), party.getName() != null ? party.getName() : "_", CommonUtils.getNoEmptyOr(partyHome.getName(), "default")), true);
}
Aggregations