More work on commands

This commit is contained in:
William committed 2023-03-12 12:49:46 +00:00
1 parent 0bd63a9f01
commit a72109667a
33 files changed
+445 -382

No files matched your search

@@ -1,6 +1,9 @@
package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.util.ValidationException;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Home;
@@ -17,31 +20,52 @@ public class HomesManager {
this.plugin = plugin;
}
public void createHome(@NotNull User owner, @NotNull String name, @NotNull Position position, boolean overwrite) throws ValidationException {
public void createHome(@NotNull User owner, @NotNull String name, @NotNull Position position,
boolean overwrite, boolean buyAdditionalSlots) throws ValidationException {
final Optional<Home> existingHome = plugin.getDatabase().getHome(owner, name);
if (existingHome.isPresent() && !overwrite) {
throw new ValidationException(ValidationException.Type.NAME_TAKEN);
}
if (!plugin.getValidator().isValidName(name)) {
throw new ValidationException(ValidationException.Type.NAME_INVALID);
}
// Validate against user max homes
final int homeCount = plugin.getDatabase().getHomes(owner).size();
int maxHomes = plugin.getSettings().getMaxHomes();
if (owner instanceof OnlineUser online) {
maxHomes = online.getMaxHomes(maxHomes, plugin.getSettings().doStackPermissionLimits());
}
if (homeCount >= maxHomes) {
throw new ValidationException(ValidationException.Type.REACHED_MAX_HOMES);
}
// Validate against user home slots
final SavedUser savedOwner = plugin.getDatabase().getUserData(owner.getUuid())
.orElseThrow(() -> new IllegalStateException("User data not found for " + owner.getUuid()));
if (plugin.getSettings().doEconomy() && homeCount >= savedOwner.getHomeSlots()) {
if (!buyAdditionalSlots || plugin.getEconomyHook().isEmpty() || !(owner instanceof OnlineUser online)) {
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_HOME_SLOTS);
}
plugin.performEconomyTransaction(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT);
savedOwner.setHomeSlots(savedOwner.getHomeSlots() + 1);
plugin.getDatabase().updateUserData(savedOwner);
}
final Home home = existingHome
.map(existing -> {
existing.setX(position.getX());
existing.setY(position.getY());
existing.setZ(position.getZ());
existing.setWorld(position.getWorld());
existing.setServer(position.getServer());
existing.setYaw(position.getYaw());
existing.setPitch(position.getPitch());
existing.update(position);
return existing;
})
.orElse(new Home(position, new PositionMeta(name, ""), owner));
plugin.getDatabase().saveHome(home);
}
public void createHome(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) throws ValidationException {
createHome(owner, name, position, plugin.getSettings().doOverwriteExistingHomesWarps(), true);
}
public void deleteHome(@NotNull User owner, @NotNull String name) throws ValidationException {
final Optional<Home> home = plugin.getDatabase().getHome(owner, name);
if (home.isEmpty()) {
@@ -55,7 +79,14 @@ public class HomesManager {
}
public void relocateHome(@NotNull User owner, @NotNull String name, @NotNull Position position) throws ValidationException {
createHome(owner, name, position, true);
final Optional<Home> optionalHome = plugin.getDatabase().getHome(owner, name);
if (optionalHome.isEmpty()) {
throw new ValidationException(ValidationException.Type.NOT_FOUND);
}
final Home home = optionalHome.get();
home.update(position);
plugin.getDatabase().saveHome(home);
}
public void renameHome(@NotNull User owner, @NotNull String name, @NotNull String newName) throws ValidationException {
@@ -95,6 +126,21 @@ public class HomesManager {
}
final Home home = optionalHome.get();
if (isPublic == home.isPublic()) {
return;
}
if (isPublic && owner instanceof OnlineUser online) {
final int publicHomes = plugin.getDatabase().getHomes(owner).stream()
.filter(Home::isPublic)
.toList().size();
final int publicSlots = online.getMaxPublicHomes(plugin.getSettings().getMaxPublicHomes(),
plugin.getSettings().doStackPermissionLimits());
if (publicHomes >= publicSlots) {
throw new ValidationException(ValidationException.Type.REACHED_MAX_PUBLIC_HOMES);
}
}
home.setPublic(isPublic);
plugin.getDatabase().saveHome(home);
}