Search in sources :

Example 36 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class TeleportAskHereCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    Player target = args.<Player>getOne(PLAYER_KEY).get();
    if (src.equals(target)) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.teleport.self"));
        return CommandResult.empty();
    }
    // Before we do all this, check the event.
    RequestEvent.PlayerToCause event = new RequestEvent.PlayerToCause(CauseStackHelper.createCause(src), target);
    if (Sponge.getEventManager().post(event)) {
        throw new ReturnMessageException(event.getCancelMessage().orElseGet(() -> plugin.getMessageProvider().getTextMessageWithFormat("command.tpa.eventfailed")));
    }
    TeleportHandler.TeleportBuilder tb = tpHandler.getBuilder().setFrom(target).setTo(src).setSafe(!args.<Boolean>getOne("f").orElse(false));
    int warmup = getWarmup(target);
    if (warmup > 0) {
        tb.setWarmupTime(warmup);
    }
    double cost = getCost(src, args);
    if (cost > 0.) {
        tb.setCharge(src).setCost(cost);
    }
    // The question needs to be asked of the target
    tpHandler.addAskQuestion(target.getUniqueId(), new TeleportHandler.TeleportPrep(Instant.now().plus(30, ChronoUnit.SECONDS), src, cost, tb));
    target.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.tpahere.question", src.getName()));
    target.sendMessage(tpHandler.getAcceptDenyMessage());
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.tpask.sent", target.getName()));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) RequestEvent(io.github.nucleuspowered.nucleus.modules.teleport.events.RequestEvent) TeleportHandler(io.github.nucleuspowered.nucleus.modules.teleport.handlers.TeleportHandler) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)

Example 37 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SellAllCommand method executeCommand.

@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
    boolean accepted = args.hasAny("a");
    CatalogType ct = getCatalogTypeFromHandOrArgs(src, itemKey, args);
    String id = ct.getId();
    ItemStack query;
    if (ct instanceof BlockState) {
        query = ItemStack.builder().fromBlockState((BlockState) ct).quantity(1).build();
        // Yeah...
        query.setQuantity(-1);
    } else {
        // Having a quantity of -1 causes an IllegalArgumentException here...
        query = ItemStack.of((ItemType) ct, 1);
        // and doesn't care here...
        query.setQuantity(-1);
    }
    ItemDataNode node = itemDataService.getDataForItem(id);
    final double sellPrice = node.getServerSellPrice();
    if (sellPrice < 0) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.itemsell.notforselling"));
    }
    Iterable<Slot> slots = Util.getStandardInventory(src).query(query).slots();
    List<ItemStack> itemsToSell = StreamSupport.stream(Util.getStandardInventory(src).query(query).slots().spliterator(), false).map(Inventory::peek).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // Get the cost.
    final int amt = itemsToSell.stream().mapToInt(ItemStack::getQuantity).sum();
    if (amt <= 0) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.none", Text.of(query)));
    }
    final double overallCost = sellPrice * amt;
    if (accepted) {
        if (econHelper.depositInPlayer(src, overallCost, false)) {
            slots.forEach(Inventory::clear);
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost))));
            return CommandResult.success();
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.error", Text.of(query)));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost)), Text.of(id)).toBuilder().onClick(TextActions.runCommand("/nucleus:itemsellall -a " + id)).build());
    return CommandResult.success();
}
Also used : Optional(java.util.Optional) ItemType(org.spongepowered.api.item.ItemType) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 38 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SpawnCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    boolean force = args.hasAny("f");
    GlobalSpawnConfig gsc = sc.getGlobalSpawn();
    WorldProperties wp = args.<WorldProperties>getOne(key).orElseGet(() -> gsc.isOnSpawnCommand() ? gsc.getWorld().orElse(src.getWorld().getProperties()) : src.getWorld().getProperties());
    Optional<World> ow = Sponge.getServer().loadWorld(wp.getUniqueId());
    if (!ow.isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.noworld"));
    } else if (sc.isPerWorldPerms() && !permissions.testSuffix(src, "worlds." + ow.get().getName().toLowerCase())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.nopermsworld", ow.get().getName()));
    }
    Transform<World> worldTransform = SpawnHelper.getSpawn(wp, plugin, src);
    SendToSpawnEvent event = new SendToSpawnEvent(worldTransform, src, CauseStackHelper.createCause(src));
    if (Sponge.getEventManager().post(event)) {
        if (event.getCancelReason().isPresent()) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.reason", event.getCancelReason().get()));
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.noreason"));
    }
    // If we don't have a rotation, then use the current rotation
    NucleusTeleportHandler.TeleportResult result = this.plugin.getTeleportHandler().teleportPlayer(src, SpawnHelper.getSpawn(ow.get().getProperties(), this.plugin, src), !force && this.sc.isSafeTeleport());
    if (result.isSuccess()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.success", wp.getWorldName()));
        return CommandResult.success();
    }
    if (result == NucleusTeleportHandler.TeleportResult.FAILED_NO_LOCATION) {
        throw ReturnMessageException.fromKey("command.spawn.fail", wp.getWorldName());
    }
    throw ReturnMessageException.fromKey("command.spawn.cancelled", wp.getWorldName());
}
Also used : GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) SendToSpawnEvent(io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 39 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SpawnOtherCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User target = args.<User>getOne(otherKey).get();
    WorldProperties world = this.getWorldProperties(src, worldKey, args).orElseGet(() -> gsc.isOnSpawnCommand() ? gsc.getWorld().get() : Sponge.getServer().getDefaultWorld().get());
    Transform<World> worldTransform = SpawnHelper.getSpawn(world, plugin, target.getPlayer().orElse(null));
    SendToSpawnEvent event = new SendToSpawnEvent(worldTransform, target, CauseStackHelper.createCause(src));
    if (Sponge.getEventManager().post(event)) {
        if (event.getCancelReason().isPresent()) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.other.failed.reason", target.getName(), event.getCancelReason().get()));
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.other.failed.noreason", target.getName()));
    }
    if (!target.isOnline()) {
        return isOffline(src, target, worldTransform);
    }
    // If we don't have a rotation, then use the current rotation
    Player player = target.getPlayer().get();
    NucleusTeleportHandler.TeleportResult result = plugin.getTeleportHandler().teleportPlayer(player, worldTransform, this.safeTeleport);
    if (result.isSuccess()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.success.source", target.getName(), world.getWorldName()));
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.success.target", world.getWorldName()));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.spawnother.fail", target.getName(), world.getWorldName());
}
Also used : NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) SendToSpawnEvent(io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 40 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class DeleteWarpCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Warp warp = args.<Warp>getOne(WarpCommand.warpNameArg).get();
    NucleusWarpService qs = Sponge.getServiceManager().provideUnchecked(NucleusWarpService.class);
    DeleteWarpEvent event = new DeleteWarpEvent(CauseStackHelper.createCause(src), warp);
    if (Sponge.getEventManager().post(event)) {
        throw new ReturnMessageException(event.getCancelMessage().orElseGet(() -> plugin.getMessageProvider().getTextMessageWithFormat("nucleus.eventcancelled")));
    }
    if (qs.removeWarp(warp.getName())) {
        // Worked. Tell them.
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.del", warp.getName()));
        return CommandResult.success();
    }
    // Didn't work. Tell them.
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.delerror"));
    return CommandResult.empty();
}
Also used : Warp(io.github.nucleuspowered.nucleus.api.nucleusdata.Warp) DeleteWarpEvent(io.github.nucleuspowered.nucleus.modules.warp.event.DeleteWarpEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) NucleusWarpService(io.github.nucleuspowered.nucleus.api.service.NucleusWarpService)

Aggregations

ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)45 Player (org.spongepowered.api.entity.living.player.Player)14 World (org.spongepowered.api.world.World)14 Text (org.spongepowered.api.text.Text)11 User (org.spongepowered.api.entity.living.player.User)9 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)9 CommandResult (org.spongepowered.api.command.CommandResult)7 Inventory (org.spongepowered.api.item.inventory.Inventory)6 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)5 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)5 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)5 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)5 Optional (java.util.Optional)5 CommandContext (org.spongepowered.api.command.args.CommandContext)5 CommandElement (org.spongepowered.api.command.args.CommandElement)5 GenericArguments (org.spongepowered.api.command.args.GenericArguments)5 Home (io.github.nucleuspowered.nucleus.api.nucleusdata.Home)4 Warp (io.github.nucleuspowered.nucleus.api.nucleusdata.Warp)4 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)4 NucleusTeleportHandler (io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler)4