use of io.github.nucleuspowered.nucleus.api.exceptions.NucleusException in project Nucleus by NucleusPowered.
the class HomeHandler method modifyHomeInternal.
public void modifyHomeInternal(Cause cause, Home home, Location<World> location, Vector3d rotation) throws NucleusException {
ModifyHomeEvent event = new ModifyHomeEvent(cause, home, location);
postEvent(event);
// Just in case.
if (!Nucleus.getNucleus().getUserDataManager().getUnchecked(home.getUser()).get(HomeUserDataModule.class).setHome(home.getName(), location, rotation, true)) {
throw new NucleusException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.sethome.seterror", home.getName()), NucleusException.ExceptionType.UNKNOWN_ERROR);
}
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NucleusException 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();
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NucleusException in project Nucleus by NucleusPowered.
the class HomeHandler method createHomeInternal.
public void createHomeInternal(Cause cause, User user, String name, Location<World> location, Vector3d rotation) throws NucleusException {
if (!NucleusHomeService.HOME_NAME_PATTERN.matcher(name).matches()) {
throw new NucleusException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.sethome.name"), NucleusException.ExceptionType.DISALLOWED_NAME);
}
int max = getMaximumHomes(user);
if (getHomes(user.getUniqueId()).size() >= max) {
throw new NucleusException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.sethome.limit", String.valueOf(max)), NucleusException.ExceptionType.LIMIT_REACHED);
}
CreateHomeEvent event = new CreateHomeEvent(name, user, cause, location);
postEvent(event);
// Just in case.
if (!Nucleus.getNucleus().getUserDataManager().get(user).get().get(HomeUserDataModule.class).setHome(name, location, rotation, false)) {
throw new NucleusException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.sethome.seterror", name), NucleusException.ExceptionType.UNKNOWN_ERROR);
}
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NucleusException in project Nucleus by NucleusPowered.
the class HomeHandler method removeHomeInternal.
public void removeHomeInternal(Cause cause, Home home) throws NucleusException {
DeleteHomeEvent event = new DeleteHomeEvent(cause, home);
postEvent(event);
if (!Nucleus.getNucleus().getUserDataManager().get(home.getOwnersUniqueId()).get().get(HomeUserDataModule.class).deleteHome(home.getName())) {
throw new NucleusException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.home.delete.fail", home.getName()), NucleusException.ExceptionType.UNKNOWN_ERROR);
}
}
use of io.github.nucleuspowered.nucleus.api.exceptions.NucleusException in project Nucleus by NucleusPowered.
the class InvulnerabilityService method setInvulnerable.
@Override
public void setInvulnerable(User user, boolean invulnerable) throws NucleusException {
ModularUserService mus = Nucleus.getNucleus().getUserDataManager().get(user).orElseThrow(() -> new NucleusException(Text.of("User does not have a data file available"), NucleusException.ExceptionType.DOES_NOT_EXIST));
mus.get(InvulnerabilityUserDataModule.class).setInvulnerable(invulnerable);
user.getPlayer().ifPresent(x -> this.invulnerabilityCache.put(user.getUniqueId(), invulnerable));
if (!mus.save()) {
throw new NucleusException(Text.of("Could not save data"), NucleusException.ExceptionType.UNKNOWN_ERROR);
}
}
Aggregations