use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.
the class KitViewCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.view.title", kitInfo.getName()))).build(this.plugin);
List<ItemStack> lis = kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).map(ItemStackSnapshot::createStack).collect(Collectors.toList());
if (this.processTokens) {
KIT_HANDLER.processTokensInItemStacks(src, lis);
}
lis.forEach(inventory::offer);
return src.openInventory(inventory).map(x -> {
KIT_HANDLER.addViewer(x);
return CommandResult.success();
}).orElseThrow(() -> ReturnMessageException.fromKey("command.kit.view.cantopen", kitInfo.getName()));
}
use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.
the class StaffChatCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
Optional<String> toSend = args.getOne(message);
if (toSend.isPresent()) {
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContexts.SHOULD_FORMAT_CHANNEL, StaffChatMessageChannel.getInstance().formatMessages());
if (src instanceof Player) {
Player pl = (Player) src;
frame.pushCause(pl);
frame.addContext(EventContextKeys.PLAYER_SIMULATED, pl.getProfile());
MessageChannel mc = pl.getMessageChannel();
pl.setMessageChannel(StaffChatMessageChannel.getInstance());
pl.simulateChat(TextParsingUtils.addUrls(toSend.get()), Sponge.getCauseStackManager().getCurrentCause());
pl.setMessageChannel(mc);
} else {
StaffChatMessageChannel.getInstance().send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);
}
return CommandResult.success();
}
}
if (!(src instanceof Player)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
}
Player player = (Player) src;
StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class)).orElseGet(StaffChatTransientModule::new);
boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
if (result) {
s.setPreviousMessageChannel(player.getMessageChannel());
src.setMessageChannel(StaffChatMessageChannel.getInstance());
} else {
src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
return CommandResult.success();
}
use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.
the class TemporaryMessageCommand method executeCommand.
@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
// Get the temporary message item.
ServerListGeneralDataModule mod = plugin.getGeneralService().get(ServerListGeneralDataModule.class);
if (args.hasAny("r")) {
if (mod.getMessage().isPresent()) {
// Remove
mod.remove();
// Send message.
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.removed"));
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.serverlist.message.noremoved");
}
// Which line?
boolean linetwo = args.<Integer>getOne(line).map(x -> x == 2).orElse(false);
Optional<String> onMessage = args.getOne(this.message);
if (!onMessage.isPresent()) {
boolean isValid = mod.getExpiry().map(x -> x.isAfter(Instant.now())).orElse(false);
if (!isValid) {
throw ReturnMessageException.fromKey("command.serverlist.message.isempty");
}
if (linetwo) {
mod.setLineTwo(null);
} else {
mod.setLineOne(null);
}
Optional<Text> newMessage = mod.getMessage();
if (newMessage.isPresent()) {
// Send message
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
src.sendMessage(newMessage.get());
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.empty"));
}
return CommandResult.success();
}
String nMessage = onMessage.get();
// If the expiry is null or before now, and there is no timespan, then it's an hour.
Instant endTime = args.<Long>getOne(timespan).map(x -> Instant.now().plus(x, ChronoUnit.SECONDS)).orElseGet(() -> mod.getExpiry().map(x -> x.isBefore(Instant.now()) ? x.plusSeconds(3600) : x).orElseGet(() -> Instant.now().plusSeconds(3600)));
// Set the expiry.
mod.setExpiry(endTime);
if (linetwo) {
mod.setLineTwo(nMessage);
} else {
mod.setLineOne(nMessage);
}
Optional<Text> newMessage = mod.getMessage();
if (newMessage.isPresent()) {
// Send message
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
src.sendMessage(newMessage.get());
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.expiry", Util.getTimeToNow(endTime)));
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.serverlist.message.notset");
}
use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.
the class PowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.powertool.noitem"));
Optional<String> command = args.getOne(commandKey);
PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
return command.map(s -> setPowertool(src, inu, itemStack.getType(), s)).orElseGet(() -> viewPowertool(src, inu, itemStack));
}
use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.
the class WarpCommand method preProcessChecks.
@Override
protected ContinueMode preProcessChecks(final CommandSource source, CommandContext args) {
if (args.<Player>getOne(playerKey).map(x -> !(source instanceof Player) || x.getUniqueId().equals(((Player) source).getUniqueId())).orElse(false)) {
// Bypass cooldowns
args.putArg(NoModifiersArgument.NO_COOLDOWN_ARGUMENT, true);
return ContinueMode.CONTINUE;
}
if (!plugin.getEconHelper().economyServiceExists() || permissions.testCostExempt(source) || args.hasAny("y")) {
return ContinueMode.CONTINUE;
}
Warp wd = args.<Warp>getOne(warpNameArg).get();
Optional<Double> i = wd.getCost();
double cost = i.orElse(this.defaultCost);
if (cost <= 0) {
return ContinueMode.CONTINUE;
}
String costWithUnit = plugin.getEconHelper().getCurrencySymbol(cost);
if (plugin.getEconHelper().hasBalance((Player) source, cost)) {
String command = String.format("/warp -y %s", wd.getName());
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.details", wd.getName(), costWithUnit));
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.clickaccept").toBuilder().onClick(TextActions.runCommand(command)).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.clickhover", command))).append(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.alt")).build());
} else {
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.nomoney", wd.getName(), costWithUnit));
}
return ContinueMode.STOP;
}
Aggregations