use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class SetDescriptionCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
String warpName = args.<Warp>getOne(warpKey).get().getName();
if (args.hasAny("r")) {
// Remove the desc.
if (handler.setWarpDescription(warpName, null)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.description.removed", warpName));
return CommandResult.success();
}
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.description.noremove", warpName));
}
// Add the category.
Text message = TextSerializers.FORMATTING_CODE.deserialize(args.<String>getOne(descriptionKey).get());
if (handler.setWarpDescription(warpName, message)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.warp.description.added", message, Text.of(warpName)));
return CommandResult.success();
}
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.warp.description.couldnotadd", Text.of(warpName)));
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class WarpCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource source, CommandContext args) throws Exception {
Player player = this.getUserFromArgs(Player.class, source, playerKey, args);
boolean isOther = !(source instanceof Player) || !((Player) source).getUniqueId().equals(player.getUniqueId());
// Permission checks are done by the parser.
Warp wd = args.<Warp>getOne(warpNameArg).get();
// Load the world in question
if (!wd.getTransform().isPresent()) {
Sponge.getServer().loadWorld(wd.getWorldProperties().get().getUniqueId()).orElseThrow(() -> ReturnMessageException.fromKey("command.warp.worldnotloaded"));
}
UseWarpEvent event = CauseStackHelper.createFrameWithCausesWithReturn(c -> new UseWarpEvent(c, player, wd), source);
if (Sponge.getEventManager().post(event)) {
throw new ReturnMessageException(event.getCancelMessage().orElseGet(() -> plugin.getMessageProvider().getTextMessageWithFormat("nucleus.eventcancelled")));
}
Optional<Double> i = wd.getCost();
double cost = i.orElse(this.defaultCost);
boolean charge = false;
if (!isOther && plugin.getEconHelper().economyServiceExists() && !permissions.testCostExempt(source) && cost > 0) {
if (plugin.getEconHelper().withdrawFromPlayer(player, cost, false)) {
// only true for a warp by the current subject.
charge = true;
} else {
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.nomoney", wd.getName(), plugin.getEconHelper().getCurrencySymbol(cost)));
return CommandResult.empty();
}
}
// We have a warp data, warp them.
if (isOther) {
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.namedstart", plugin.getNameUtil().getSerialisedName(player), wd.getName()));
} else {
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.start", wd.getName()));
}
// Warp them.
boolean isSafe = !args.getOne("f").isPresent() && this.isSafeTeleport;
NucleusTeleportHandler.TeleportResult result = plugin.getTeleportHandler().teleportPlayer(player, wd.getLocation().get(), wd.getRotation(), isSafe);
if (!result.isSuccess()) {
if (charge) {
plugin.getEconHelper().depositInPlayer(player, cost, false);
}
// Don't add the cooldown if enabled.
throw ReturnMessageException.fromKey(result == NucleusTeleportHandler.TeleportResult.FAILED_NO_LOCATION ? "command.warps.nosafe" : "command.warps.cancelled");
}
if (isOther) {
player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.warped", wd.getName()));
} else if (charge) {
source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.charged", plugin.getEconHelper().getCurrencySymbol(cost)));
}
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class TeleportCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
boolean beQuiet = args.<Boolean>getOne(quietKey).orElse(this.isDefaultQuiet);
Optional<Player> oTo = args.getOne(playerToKey);
User to;
Player from;
if (oTo.isPresent()) {
// Two player argument.
from = args.<User>getOne(playerKey).map(x -> x.getPlayer().orElse(null)).orElseThrow(() -> ReturnMessageException.fromKey("command.playeronly"));
to = oTo.get();
if (to.equals(src)) {
throw ReturnMessageException.fromKey("command.teleport.player.noself");
}
} else if (src instanceof Player) {
from = (Player) src;
to = args.<User>getOne(playerKey).get();
} else {
throw ReturnMessageException.fromKey("command.playeronly");
}
if (to.getPlayer().isPresent()) {
if (handler.getBuilder().setSource(src).setFrom(from).setTo(to.getPlayer().get()).setSafe(!args.<Boolean>getOne("f").orElse(false)).setSilentTarget(beQuiet).startTeleport()) {
return CommandResult.success();
}
return CommandResult.empty();
}
// We have an offline player.
permissions.checkSuffix(src, "offline", () -> ReturnMessageException.fromKey("command.teleport.noofflineperms"));
// Can we get a location?
Supplier<ReturnMessageException> r = () -> ReturnMessageException.fromKey("command.teleport.nolastknown", to.getName());
Location<World> l = plugin.getUserDataManager().get(to.getUniqueId()).orElseThrow(r).get(CoreUserDataModule.class).getLogoutLocation().orElseThrow(r);
MessageProvider provider = plugin.getMessageProvider();
if (CauseStackHelper.createFrameWithCausesWithReturn(c -> plugin.getTeleportHandler().teleportPlayer(from, l, NucleusTeleportHandler.StandardTeleportMode.FLYING_THEN_SAFE, c).isSuccess(), src)) {
if (!(src instanceof Player && ((Player) src).getUniqueId().equals(from.getUniqueId()))) {
src.sendMessage(provider.getTextMessageWithFormat("command.teleport.offline.other", from.getName(), to.getName()));
}
from.sendMessage(provider.getTextMessageWithFormat("command.teleport.offline.self", to.getName()));
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.teleport.error");
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class HomeCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
int max = this.homeHandler.getMaximumHomes(src);
int current = this.homeHandler.getHomeCount(src);
if (this.isPreventOverhang && max < current) {
// If the player has too many homes, tell them
throw ReturnMessageException.fromKey("command.home.overhang", String.valueOf(max), String.valueOf(current));
}
// Get the home.
Optional<Home> owl = args.getOne(home);
Home wl;
if (owl.isPresent()) {
wl = owl.get();
} else {
wl = homeHandler.getHome(src, NucleusHomeService.DEFAULT_HOME_NAME).orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("args.home.nohome", "home")));
}
Sponge.getServer().loadWorld(wl.getWorldProperties().orElseThrow(() -> ReturnMessageException.fromKey("command.home.invalid", wl.getName())));
Location<World> targetLocation = wl.getLocation().orElseThrow(() -> ReturnMessageException.fromKey("command.home.invalid", wl.getName()));
UseHomeEvent event = CauseStackHelper.createFrameWithCausesWithReturn(c -> new UseHomeEvent(c, src, wl), src);
if (Sponge.getEventManager().post(event)) {
throw new ReturnMessageException(event.getCancelMessage().orElseGet(() -> plugin.getMessageProvider().getTextMessageWithFormat("nucleus.eventcancelled")));
}
// Warp to it safely.
if (plugin.getTeleportHandler().teleportPlayer(src, targetLocation, wl.getRotation(), this.isSafeTeleport).isSuccess()) {
if (!wl.getName().equalsIgnoreCase(NucleusHomeService.DEFAULT_HOME_NAME)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.home.success", wl.getName()));
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.home.successdefault"));
}
return CommandResult.success();
} else {
throw ReturnMessageException.fromKey("command.home.fail", wl.getName());
}
}
use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.
the class SetHomeCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
// Get the home key.
String home = args.<String>getOne(homeKey).orElse(NucleusHomeService.DEFAULT_HOME_NAME).toLowerCase();
if (!NucleusHomeService.HOME_NAME_PATTERN.matcher(home).matches()) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.sethome.name"));
}
Optional<Home> currentHome = homeHandler.getHome(src, home);
boolean overwrite = currentHome.isPresent() && args.hasAny("o");
if (currentHome.isPresent() && !overwrite) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.sethome.seterror", home));
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.sethome.tooverwrite", home).toBuilder().onClick(TextActions.runCommand("/sethome " + home + " -o")).build());
return CommandResult.empty();
}
Cause cause = CauseStackHelper.createCause(src);
try {
if (overwrite) {
int max = this.homeHandler.getMaximumHomes(src);
int c = this.homeHandler.getHomeCount(src);
if (this.preventOverhang && max < c) {
// If the player has too many homes, tell them
throw ReturnMessageException.fromKey("command.sethome.overhang", String.valueOf(max), String.valueOf(c));
}
Home current = currentHome.get();
homeHandler.modifyHomeInternal(cause, current, src.getLocation(), src.getRotation());
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.sethome.overwrite", home));
} else {
homeHandler.createHomeInternal(cause, src, home, src.getLocation(), src.getRotation());
}
} catch (NucleusException e) {
throw new ReturnMessageException(e.getText(), e);
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.sethome.set", home));
return CommandResult.success();
}
Aggregations