Search in sources :

Example 31 with ReturnMessageException

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

the class KitCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws ReturnMessageException {
    Kit kit = args.<Kit>getOne(KIT_PARAMETER).get();
    EconHelper econHelper = Nucleus.getNucleus().getEconHelper();
    double cost = econHelper.economyServiceExists() ? kit.getCost() : 0;
    if (permissions.testCostExempt(player)) {
        // If exempt - no cost.
        cost = 0;
    }
    // If we have a cost for the kit, check we have funds.
    if (cost > 0 && !econHelper.hasBalance(player, cost)) {
        throw ReturnMessageException.fromKey("command.kit.notenough", kit.getName(), econHelper.getCurrencySymbol(cost));
    }
    try {
        NucleusKitService.RedeemResult redeemResult = KIT_HANDLER.redeemKit(kit, player, true, this.mustGetAll);
        if (!redeemResult.rejected().isEmpty()) {
            // If we drop them, tell the user
            if (this.isDrop) {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.itemsdropped"));
                redeemResult.rejected().forEach(x -> Util.dropItemOnFloorAtLocation(x, player.getLocation()));
            } else {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.fullinventory"));
            }
        }
        if (kit.isDisplayMessageOnRedeem()) {
            player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.spawned", kit.getName()));
        }
        // Charge, if necessary
        if (cost > 0 && econHelper.economyServiceExists()) {
            econHelper.withdrawFromPlayer(player, cost);
        }
        return CommandResult.success();
    } catch (KitRedeemException ex) {
        switch(ex.getReason()) {
            case ALREADY_REDEEMED:
                throw ReturnMessageException.fromKey("command.kit.onetime.alreadyredeemed", kit.getName());
            case COOLDOWN_NOT_EXPIRED:
                KitRedeemException.Cooldown kre = (KitRedeemException.Cooldown) ex;
                throw ReturnMessageException.fromKey("command.kit.cooldown", Util.getTimeStringFromSeconds(kre.getTimeLeft().getSeconds()), kit.getName());
            case PRE_EVENT_CANCELLED:
                KitRedeemException.PreCancelled krepe = (KitRedeemException.PreCancelled) ex;
                throw new ReturnMessageException(krepe.getCancelMessage().orElseGet(() -> (Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.kit.cancelledpre", kit.getName()))));
            case NO_SPACE:
                throw ReturnMessageException.fromKey("command.kit.fullinventorynosave", kit.getName());
            case UNKNOWN:
            default:
                throw ReturnMessageException.fromKey("command.kit.fail", kit.getName());
        }
    }
}
Also used : NucleusKitService(io.github.nucleuspowered.nucleus.api.service.NucleusKitService) NoCooldown(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) EconHelper(io.github.nucleuspowered.nucleus.internal.EconHelper) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)

Example 32 with ReturnMessageException

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

the class KitCreateCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource source, CommandContext args) throws ReturnMessageException {
    String kitName = args.<String>getOne(name).get();
    if (KIT_HANDLER.getKitNames().stream().anyMatch(kitName::equalsIgnoreCase)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.add.alreadyexists", kitName));
    }
    if (source instanceof Player) {
        final Player player = (Player) source;
        Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.create.title", kitName))).build(plugin);
        Container container = player.openInventory(inventory).orElseThrow(() -> ReturnMessageException.fromKey("command.kit.create.notcreated"));
        Sponge.getEventManager().registerListeners(plugin, new TemporaryEventListener(inventory, container, kitName));
    } else {
        try {
            KIT_HANDLER.saveKit(KIT_HANDLER.createKit(kitName));
            source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.addempty.success", kitName));
        } catch (IllegalArgumentException ex) {
            throw ReturnMessageException.fromKey("command.kit.create.failed", kitName);
        }
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Container(org.spongepowered.api.item.inventory.Container) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 33 with ReturnMessageException

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

the class TopCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Player playerToTeleport = this.getUserFromArgs(Player.class, src, playerKey, args);
    // Get the topmost block for the subject.
    Location<World> location = playerToTeleport.getLocation();
    double x = location.getX();
    double z = location.getZ();
    Location<World> start = new Location<>(location.getExtent(), x, location.getExtent().getBlockMax().getY(), z);
    BlockRayHit<World> end = BlockRay.from(start).stopFilter(BlockRay.onlyAirFilter()).to(playerToTeleport.getLocation().getPosition().sub(0, 1, 0)).end().orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.top.nothingfound")));
    if (playerToTeleport.getLocation().getBlockPosition().equals(end.getBlockPosition())) {
        if (!playerToTeleport.equals(src)) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.top.attop.other", plugin.getNameUtil().getSerialisedName(playerToTeleport)));
        } else {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.top.attop.self"));
        }
    }
    NucleusTeleportHandler.TeleportResult result = plugin.getTeleportHandler().teleportPlayer(playerToTeleport, end.getLocation(), !args.hasAny("f"));
    if (result.isSuccess()) {
        // OK
        if (!playerToTeleport.equals(src)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.top.success.other", plugin.getNameUtil().getSerialisedName(playerToTeleport)));
        }
        playerToTeleport.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.top.success.self"));
        return CommandResult.success();
    }
    if (result == NucleusTeleportHandler.TeleportResult.FAILED_NO_LOCATION) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.top.notsafe"));
    } else {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.top.cancelled"));
    }
}
Also used : NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) Player(org.spongepowered.api.entity.living.player.Player) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location)

Example 34 with ReturnMessageException

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

the class SpawnHelper method getSpawn.

public static Transform<World> getSpawn(@Nonnull WorldProperties wp, Nucleus plugin, @Nullable Player player) throws ReturnMessageException {
    Preconditions.checkNotNull(wp, "WorldProperties");
    Optional<World> ow = Sponge.getServer().getWorld(wp.getUniqueId());
    if (!ow.isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.noworld"));
    }
    return new Transform<>(ow.get(), wp.getSpawnPosition().toDouble().add(0.5, 0, 0.5), Nucleus.getNucleus().getWorldDataManager().getWorld(wp.getUniqueId()).get().get(SpawnWorldDataModule.class).getSpawnRotation().orElseGet(() -> player == null ? new Vector3d(0, 0, 0) : player.getRotation()));
}
Also used : SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) Vector3d(com.flowpowered.math.vector.Vector3d) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) Transform(org.spongepowered.api.entity.Transform)

Example 35 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException 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();
}
Also used : ChatTypes(org.spongepowered.api.text.chat.ChatTypes) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) EventContexts(io.github.nucleuspowered.nucleus.api.EventContexts) CommandSource(org.spongepowered.api.command.CommandSource) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) Player(org.spongepowered.api.entity.living.player.Player) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel)

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