bentobox/src/main/java/world/bentobox/bentobox/api/commands/island/IslandSethomeCommand.java

117 lines
4.7 KiB
Java
Raw Normal View History

package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.Objects;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
2021-09-20 03:36:37 +02:00
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
public class IslandSethomeCommand extends ConfirmableCommand {
private @Nullable Island island;
public IslandSethomeCommand(CompositeCommand islandCommand) {
super(islandCommand, "sethome");
}
@Override
public void setup() {
setPermission("island.sethome");
setOnlyPlayer(true);
setDescription("commands.island.sethome.description");
setConfigurableRankCommand();
setDefaultCommandRank(RanksManager.MEMBER_RANK);
}
@Override
public boolean canExecute(User user, String label, List<String> args) {
island = getIslands().getIsland(getWorld(), user);
2019-02-12 07:44:38 +01:00
// Check island
if (island == null || island.getOwner() == null) {
user.sendMessage("general.errors.no-island");
return false;
}
if (!island.onIsland(user.getLocation())) {
user.sendMessage("commands.island.sethome.must-be-on-your-island");
return false;
}
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK,
user.getTranslation(RanksManager.getInstance().getRank(rank)));
return false;
}
// Check number of homes
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
int maxHomes = getIslands().getMaxHomes(island);
if (getIslands().getNumberOfHomesIfAdded(island, String.join(" ", args)) > maxHomes) {
user.sendMessage("commands.island.sethome.too-many-homes", TextVariables.NUMBER, String.valueOf(maxHomes));
user.sendMessage("commands.island.sethome.homes-are");
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
getIslands().getIslands(getWorld(), user).forEach(is ->
is.getHomes().keySet().stream().filter(s -> !s.isEmpty()).forEach(s -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, s)));
return false;
}
return true;
}
2019-02-12 07:44:38 +01:00
@Override
public boolean execute(User user, String label, List<String> args) {
String number = String.join(" ", args);
WorldSettings ws = getIWM().getWorldSettings(user.getWorld());
// Check if the player is in the Nether
if (getIWM().isNether(user.getWorld())) {
// Check if he is (not) allowed to set his home here
2021-09-20 03:36:37 +02:00
if (!ws.isAllowSetHomeInNether()) {
user.sendMessage("commands.island.sethome.nether.not-allowed");
return false;
}
// Check if a confirmation is required
2021-09-20 03:36:37 +02:00
if (ws.isRequireConfirmationToSetHomeInNether()) {
askConfirmation(user, user.getTranslation("commands.island.sethome.nether.confirmation"), () -> doSetHome(user, number));
} else {
doSetHome(user, number);
}
} else if (getIWM().isEnd(user.getWorld())) { // Check if the player is in the End
// Check if he is (not) allowed to set his home here
2021-09-20 03:36:37 +02:00
if (!ws.isAllowSetHomeInTheEnd()) {
user.sendMessage("commands.island.sethome.the-end.not-allowed");
return false;
}
// Check if a confirmation is required
2021-09-20 03:36:37 +02:00
if (ws.isRequireConfirmationToSetHomeInTheEnd()) {
askConfirmation(user, user.getTranslation("commands.island.sethome.the-end.confirmation"), () -> doSetHome(user, number));
} else {
doSetHome(user, number);
}
} else { // The player is in the Overworld, no need to run a check
doSetHome(user, number);
}
return true;
}
private void doSetHome(User user, String name) {
// Define a runnable as we will be using it often in the code below.
getIslands().setHomeLocation(user, user.getLocation(), name);
user.sendMessage("commands.island.sethome.home-set");
if (island.getHomes().size() > 1) {
user.sendMessage("commands.island.sethome.homes-are");
island
.getHomes()
.keySet()
.stream().filter(s -> !s.isEmpty()).forEach(s -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, s));
}
}
}