Skip to content

Commit

Permalink
Made IslandSethomeCommand a ConfirmableCommand
Browse files Browse the repository at this point in the history
#237

It adds a bunch of WorldSettings and updates the en-US locale as well!
  • Loading branch information
Poslovitch committed Oct 25, 2018
1 parent 09ce512 commit ec2793e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package world.bentobox.bentobox.api.commands.island;

import java.util.List;
import java.util.UUID;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;

public class IslandSethomeCommand extends CompositeCommand {
public class IslandSethomeCommand extends ConfirmableCommand {

public IslandSethomeCommand(CompositeCommand islandCommand) {
super(islandCommand, "sethome");
Expand All @@ -23,7 +23,6 @@ public void setup() {

@Override
public boolean execute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Check island
if (getPlugin().getIslands().getIsland(getWorld(), user.getUniqueId()) == null) {
user.sendMessage("general.errors.no-island");
Expand All @@ -35,8 +34,8 @@ public boolean execute(User user, String label, List<String> args) {
}
if (args.isEmpty()) {
// island sethome
getPlugin().getPlayers().setHomeLocation(playerUUID, user.getLocation());
user.sendMessage("commands.island.sethome.home-set");
setHome(user, 1);
return true;
} else {
// Dynamic home sizes with permissions
int maxHomes = user.getPermissionValue(getPermissionPrefix() + "island.maxhomes", getIWM().getMaxHomes(getWorld()));
Expand All @@ -49,8 +48,8 @@ public boolean execute(User user, String label, List<String> args) {
user.sendMessage("commands.island.sethome.num-homes", TextVariables.NUMBER, String.valueOf(maxHomes));
return false;
} else {
getPlugin().getPlayers().setHomeLocation(user, user.getLocation(), number);
user.sendMessage("commands.island.sethome.home-set");
setHome(user, number);
return true;
}
} catch (Exception e) {
user.sendMessage("commands.island.sethome.num-homes", TextVariables.NUMBER, String.valueOf(maxHomes));
Expand All @@ -61,7 +60,45 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}
}
return true;
}

private void setHome(User user, int number) {
// Define a runnable as we will be using it often in the code below.
Runnable setHomeRunnable = () -> {
getPlugin().getPlayers().setHomeLocation(user, user.getLocation(), number);
user.sendMessage("commands.island.sethome.home-set");
};

// Check if the player is in the Nether
if (getIWM().isNether(user.getLocation().getWorld())) {
// Check if he is (not) allowed to set his home here
if (!getIWM().getWorldSettings(user.getLocation().getWorld()).isAllowSetHomeInNether()) {
user.sendMessage("commands.island.sethome.nether.not-allowed");
return;
}

// Check if a confirmation is required
if (getIWM().getWorldSettings(user.getLocation().getWorld()).isRequireConfirmationToSetHomeInNether()) {
askConfirmation(user, "commands.island.sethome.nether.confirmation", setHomeRunnable);
} else {
setHomeRunnable.run();
}
} else if (getIWM().isEnd(user.getLocation().getWorld())) { // Check if the player is in the End
// Check if he is (not) allowed to set his home here
if (!getIWM().getWorldSettings(user.getLocation().getWorld()).isAllowSetHomeInTheEnd()) {
user.sendMessage("commands.island.sethome.the-end.not-allowed");
return;
}

// Check if a confirmation is required
if (getIWM().getWorldSettings(user.getLocation().getWorld()).isRequireConfirmationToSetHomeInTheEnd()) {
askConfirmation(user, "commands.island.sethome.the-end.confirmation", setHomeRunnable);
} else {
setHomeRunnable.run();
}
} else { // The player is in the Overworld, no need to run a check
setHomeRunnable.run();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,24 @@ public interface WorldSettings {
*/
boolean isDeathsCounted();

/**
* @return whether a player can set their home in the Nether or not.
*/
boolean isAllowSetHomeInNether();

/**
* @return whether a player can set their home in the End or not.
*/
boolean isAllowSetHomeInTheEnd();

/**
* @return whether a confirmation is required when a player tries to set their home in the Nether.
*/
boolean isRequireConfirmationToSetHomeInNether();

/**
* @return whether a confirmation is required when a player tries to set their home in the End.
*/
boolean isRequireConfirmationToSetHomeInTheEnd();

}
6 changes: 6 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ commands:
must-be-on-your-island: "&cYou must be on your island to set home!"
num-homes: "&cHomes can be 1 to [number]."
home-set: "&6Your island home has been set to your current location."
nether:
not-allowed: "&cYou are not allowed to set your home in the Nether."
confirmation: "&cAre you sure you want to set your home in the Nether?"
the-end:
not-allowed: "&cYou are not allowed to set your home in the End."
confirmation: "&cAre you sure you want to set your home in the End?"
parameters: "[home number]"
setname:
description: "set a name for your island"
Expand Down

0 comments on commit ec2793e

Please sign in to comment.