use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class BasicCraftingCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
Inventory i = Inventory.builder().of(getArchetype()).build(plugin);
src.openInventory(i).orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.crafting.error")));
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class TempBanCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
User u = args.<User>getOne(user).get();
Long time = args.<Long>getOne(duration).get();
String reason = args.<String>getOne(reasonKey).orElse(plugin.getMessageProvider().getMessageWithFormat("ban.defaultreason"));
if (permissions.testSuffix(u, "exempt.target", src, false)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.exempt", u.getName()));
}
if (!u.isOnline() && !permissions.testSuffix(src, "offline")) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.offline.noperms"));
}
if (time > banConfig.getMaximumTempBanLength() && banConfig.getMaximumTempBanLength() != -1 && !permissions.testSuffix(src, "exempt.length")) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.length.toolong", Util.getTimeStringFromSeconds(this.banConfig.getMaximumTempBanLength())));
return CommandResult.success();
}
BanService service = Sponge.getServiceManager().provideUnchecked(BanService.class);
if (service.isBanned(u.getProfile())) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.alreadyset", u.getName()));
return CommandResult.empty();
}
// Expiration date
Instant date = Instant.now().plus(time, ChronoUnit.SECONDS);
// Create the ban.
Ban bp = Ban.builder().type(BanTypes.PROFILE).profile(u.getProfile()).source(src).expirationDate(date).reason(TextSerializers.FORMATTING_CODE.deserialize(reason)).build();
service.addBan(bp);
MutableMessageChannel send = new PermissionMessageChannel(BanCommand.notifyPermission).asMutable();
send.addMember(src);
send.send(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.applied", u.getName(), Util.getTimeStringFromSeconds(time), src.getName()));
send.send(plugin.getMessageProvider().getTextMessageWithFormat("standard.reasoncoloured", reason));
if (Sponge.getServer().getPlayer(u.getUniqueId()).isPresent()) {
Sponge.getServer().getPlayer(u.getUniqueId()).get().kick(TextSerializers.FORMATTING_CODE.deserialize(reason));
}
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class GamemodeBase method baseCommand.
CommandResult baseCommand(CommandSource src, Player user, GameMode gm) throws Exception {
if (!this.permissions.testSuffix(src, MODE_MAP.computeIfAbsent(gm.getId(), key -> {
String[] keySplit = key.split(":", 2);
String r = keySplit[keySplit.length - 1].toLowerCase();
MODE_MAP.put(key, "modes." + r);
return "modes." + r;
}))) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.permission", gm.getTranslation().get()));
}
DataTransactionResult dtr = user.offer(Keys.GAME_MODE, gm);
if (dtr.isSuccessful()) {
if (!src.equals(user)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.set.other", user.getName(), gm.getName()));
}
user.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.set.base", gm.getName()));
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.gamemode.error", user.getName());
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class MeCommand method executeCommand.
@Override
public CommandResult executeCommand(@Nonnull CommandSource src, CommandContext args) throws Exception {
String message = ChatListener.stripPermissionless(src, args.<String>getOne(messageKey).get());
Text header = config.getMePrefix().getForCommandSource(src);
TextParsingUtils.StyleTuple t = plugin.getTextParsingUtils().getLastColourAndStyle(header, null);
Text originalMessage = TextSerializers.FORMATTING_CODE.deserialize(message);
MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(Text.builder().color(t.colour).style(t.style).append(TextSerializers.FORMATTING_CODE.deserialize(message)).toText());
// Doing this here rather than in the constructor removes the < > notation.
formatter.setHeader(header);
// We create an event so that other plugins can provide transforms, such as Boop, and that we
// can catch it in ignore and mutes, and so can other plugins.
MessageChannelEvent.Chat event = CauseStackHelper.createFrameWithCausesAndContextWithReturn(c -> SpongeEventFactory.createMessageChannelEventChat(c, channel, Optional.of(channel), formatter, originalMessage, false), EventContext.builder().add(EventContexts.SHOULD_FORMAT_CHANNEL, false).build(), src);
if (Sponge.getEventManager().post(event)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.me.cancel"));
}
event.getChannel().orElse(channel).send(src, Util.applyChatTemplate(event.getFormatter()), ChatTypes.CHAT);
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class SkullCommand method executeCommand.
@Override
public CommandResult executeCommand(Player pl, CommandContext args) throws Exception {
User user = this.getUserFromArgs(User.class, pl, player, args);
int amount = args.<Integer>getOne(amountKey).orElse(1);
if (this.isUseMinecraftCommand) {
CommandResult result = Sponge.getCommandManager().process(Sponge.getServer().getConsole(), String.format("minecraft:give %s skull %d 3 {SkullOwner:%s}", pl.getName(), amount, user.getName()));
if (result.getSuccessCount().orElse(0) > 0) {
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(amount), user.getName()));
return result;
}
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
}
int fullStacks = amount / 64;
int partialStack = amount % 64;
// Create the Skull
ItemStack skullStack = ItemStack.builder().itemType(ItemTypes.SKULL).quantity(64).build();
// Set it to subject skull type and set the owner to the specified subject
if (skullStack.offer(Keys.SKULL_TYPE, SkullTypes.PLAYER).isSuccessful() && skullStack.offer(Keys.REPRESENTED_PLAYER, user.getProfile()).isSuccessful()) {
skullStack.toContainer().set(DataQuery.of("SkullOwner"), user.getName());
List<ItemStack> itemStackList = Lists.newArrayList();
// If there were stacks, create as many as needed.
if (fullStacks > 0) {
itemStackList.add(skullStack);
for (int i = 2; i <= fullStacks; i++) {
itemStackList.add(skullStack.copy());
}
}
// Same with the partial stacks.
if (partialStack > 0) {
ItemStack is = skullStack.copy();
is.setQuantity(partialStack);
itemStackList.add(is);
}
int accepted = 0;
int failed = 0;
Inventory inventoryToOfferTo = pl.getInventory().query(Hotbar.class, GridInventory.class);
for (ItemStack itemStack : itemStackList) {
int stackSize = itemStack.getQuantity();
InventoryTransactionResult itr = inventoryToOfferTo.offer(itemStack);
int currentFail = itr.getRejectedItems().stream().mapToInt(ItemStackSnapshot::getQuantity).sum();
failed += currentFail;
accepted += stackSize - currentFail;
}
// What was accepted?
if (accepted > 0) {
if (failed > 0) {
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.semifull", String.valueOf(failed)));
}
if (accepted == 1) {
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.single", user.getName()));
} else {
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(accepted), user.getName()));
}
return CommandResult.success();
}
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.full", user.getName()));
return CommandResult.empty();
} else {
pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
return CommandResult.empty();
}
}
Aggregations