Search in sources :

Example 1 with Home

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Home 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 Home

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Home 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();
}
Also used : Cause(org.spongepowered.api.event.cause.Cause) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) NucleusException(io.github.nucleuspowered.nucleus.api.exceptions.NucleusException) Home(io.github.nucleuspowered.nucleus.api.nucleusdata.Home)

Example 3 with Home

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Home in project Nucleus by NucleusPowered.

the class DeleteHomeCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    Home wl = args.<Home>getOne(homeKey).get();
    CauseStackHelper.createFrameWithCausesWithConsumer(c -> homeHandler.removeHomeInternal(c, wl), src);
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.home.delete.success", wl.getName()));
    return CommandResult.success();
}
Also used : Home(io.github.nucleuspowered.nucleus.api.nucleusdata.Home)

Example 4 with Home

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Home in project Nucleus by NucleusPowered.

the class DeleteOtherHomeCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Home wl = args.<Home>getOne(homeKey).get();
    CauseStackHelper.createFrameWithCausesWithConsumer(c -> homeHandler.removeHomeInternal(c, wl), src);
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.home.delete.other.success", wl.getUser().getName(), wl.getName()));
    return CommandResult.success();
}
Also used : Home(io.github.nucleuspowered.nucleus.api.nucleusdata.Home)

Example 5 with Home

use of io.github.nucleuspowered.nucleus.api.nucleusdata.Home 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)6 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)4 World (org.spongepowered.api.world.World)3 UseHomeEvent (io.github.nucleuspowered.nucleus.modules.home.events.UseHomeEvent)2 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 Util (io.github.nucleuspowered.nucleus.Util)1 NucleusException (io.github.nucleuspowered.nucleus.api.exceptions.NucleusException)1 NamedLocation (io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation)1 NicknameArgument (io.github.nucleuspowered.nucleus.argumentparsers.NicknameArgument)1 SelectorWrapperArgument (io.github.nucleuspowered.nucleus.argumentparsers.SelectorWrapperArgument)1 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)1 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)1 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)1 HomeHandler (io.github.nucleuspowered.nucleus.modules.home.handlers.HomeHandler)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1