Search in sources :

Example 1 with UseHomeEvent

use of io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent 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());
    }
}
Also used : UseHomeEvent(io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) Home(io.github.nucleuspowered.nucleus.api.nucleusdata.Home)

Example 2 with UseHomeEvent

use of io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent in project Nucleus by NucleusPowered.

the class HomeOtherCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    // Get the home.
    Home wl = args.<Home>getOne(home).get();
    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()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.homeother.success", wl.getUser().getName(), wl.getName()));
        return CommandResult.success();
    } else {
        throw ReturnMessageException.fromKey("command.homeother.fail", wl.getUser().getName(), wl.getName());
    }
}
Also used : UseHomeEvent(io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) Home(io.github.nucleuspowered.nucleus.api.nucleusdata.Home)

Aggregations

Home (io.github.nucleuspowered.nucleus.api.nucleusdata.Home)2 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)2 UseHomeEvent (io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent)2 World (org.spongepowered.api.world.World)2