use of io.github.nucleuspowered.nucleus.modules.warp.event.UseWarpEvent 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();
}
Aggregations