use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.
the class EnchantCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
// Check for item in hand
if (!src.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.noitem"));
return CommandResult.empty();
}
// Get the arguments
ItemStack itemInHand = src.getItemInHand(HandTypes.MAIN_HAND).get();
EnchantmentType enchantment = args.<EnchantmentType>getOne(enchantmentKey).get();
int level = args.<Integer>getOne(levelKey).get();
boolean allowUnsafe = args.hasAny("u");
boolean allowOverwrite = args.hasAny("o");
// Can we apply the enchantment?
if (!allowUnsafe) {
if (!enchantment.canBeAppliedToStack(itemInHand)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.enchant", itemInHand.getTranslation().get()));
return CommandResult.empty();
}
if (level > enchantment.getMaximumLevel()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.level", itemInHand.getTranslation().get()));
return CommandResult.empty();
}
}
// We know this should exist.
EnchantmentData ed = itemInHand.getOrCreate(EnchantmentData.class).get();
// Get all the enchantments.
List<Enchantment> currentEnchants = ed.getListValue().get();
List<Enchantment> enchantmentsToRemove = currentEnchants.stream().filter(x -> !x.getType().isCompatibleWith(enchantment) || x.getType().equals(enchantment)).collect(Collectors.toList());
if (!allowOverwrite && !enchantmentsToRemove.isEmpty()) {
// Build the list of the enchantment names, and send it.
final StringBuilder sb = new StringBuilder();
enchantmentsToRemove.forEach(x -> {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(Util.getTranslatableIfPresent(x.getType()));
});
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.overwrite", sb.toString()));
return CommandResult.empty();
}
// Remove all enchants that cannot co-exist.
currentEnchants.removeIf(enchantmentsToRemove::contains);
// Create the enchantment
currentEnchants.add(Enchantment.of(enchantment, level));
ed.setElements(currentEnchants);
// Offer it to the item.
DataTransactionResult dtr = itemInHand.offer(ed);
if (dtr.isSuccessful()) {
// If successful, we need to put the item in the player's hand for it to actually take effect.
src.setItemInHand(HandTypes.MAIN_HAND, itemInHand);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.success", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
return CommandResult.success();
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.error", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
return CommandResult.empty();
}
use of org.spongepowered.api.command.CommandResult 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();
}
}
use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.
the class KitEditCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
if (KIT_HANDLER.isOpen(kitInfo.getName())) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.current", kitInfo.getName()));
}
Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.title", kitInfo.getName()))).build(plugin);
kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).forEach(x -> inventory.offer(x.createStack()));
Optional<Container> openedInventory = src.openInventory(inventory);
if (openedInventory.isPresent()) {
KIT_HANDLER.addKitInventoryToListener(Tuple.of(kitInfo, inventory), openedInventory.get());
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.kit.edit.cantopen", kitInfo.getName());
}
use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.
the class CheckJailedCommand method executeCommand.
@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
// Using the cache, tell us who is jailed.
MessageProvider provider = plugin.getMessageProvider();
Optional<NamedLocation> jail = args.getOne(jailNameKey);
List<UUID> usersInJail = jail.map(x -> plugin.getUserCacheService().getJailedIn(x.getName())).orElseGet(() -> plugin.getUserCacheService().getJailed());
String jailName = jail.map(NamedLocation::getName).orElseGet(() -> provider.getMessageWithFormat("standard.alljails"));
if (usersInJail.isEmpty()) {
src.sendMessage(provider.getTextMessageWithFormat("command.checkjailed.none", jailName));
return CommandResult.success();
}
// Get the users in this jail, or all jails
Util.getPaginationBuilder(src).title(provider.getTextMessageWithFormat("command.checkjailed.header", jailName)).contents(usersInJail.stream().map(x -> {
Text name = plugin.getNameUtil().getName(x).orElseGet(() -> Text.of("unknown: ", x.toString()));
return name.toBuilder().onHover(TextActions.showText(provider.getTextMessageWithFormat("command.checkjailed.hover"))).onClick(TextActions.runCommand("/nucleus:checkjail " + x.toString())).build();
}).collect(Collectors.toList())).sendTo(src);
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.
the class KitEditCommandCommand method executeCommand.
@Override
protected CommandResult executeCommand(Player src, CommandContext args) throws Exception {
final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
List<String> commands = kitInfo.getCommands();
if (commands.size() > 9 * 6) {
throw ReturnMessageException.fromKey("command.kit.command.edit.toomany", kitInfo.getName());
}
// Create an inventory with signed books.
Random r = new Random();
List<ItemStack> books = commands.stream().map(x -> {
ItemStack stack = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
Text command = Text.of(x);
stack.offer(Keys.DISPLAY_NAME, command);
stack.offer(Keys.BOOK_PAGES, Lists.newArrayList(command));
// So books don't stack.
stack.offer(Keys.BOOK_AUTHOR, Text.of(kitInfo.getName(), "-", r.nextInt()));
return stack;
}).collect(Collectors.toList());
// Create Inventory GUI.
final InventoryTitle title = InventoryTitle.of(Text.of("Kit Commands: ", kitInfo.getName()));
final Inventory inventory = Inventory.builder().of(InventoryArchetypes.DOUBLE_CHEST).property(InventoryTitle.PROPERTY_NAME, title).build(plugin);
books.forEach(inventory::offer);
src.openInventory(inventory).ifPresent(x -> KIT_HANDLER.addKitCommandInventoryToListener(Tuple.of(kitInfo, inventory), x));
return CommandResult.success();
}
Aggregations