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());
}
}
}
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();
}
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"));
}
}
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()));
}
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();
}
Aggregations