-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2527 from BentoBoxWorld/develop
Release 2.6.0
- Loading branch information
Showing
84 changed files
with
2,151 additions
and
1,013 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/main/java/world/bentobox/bentobox/api/commands/admin/AdminMaxHomesCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package world.bentobox.bentobox.api.commands.admin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import com.google.common.primitives.Ints; | ||
|
||
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; | ||
import world.bentobox.bentobox.database.objects.Island; | ||
import world.bentobox.bentobox.util.Util; | ||
|
||
/** | ||
* Sets the maximum number of homes allowed on this island. | ||
* | ||
* Commands: | ||
* <ul> | ||
* <li><b>/bsb maxhomes <player> <number></b> - Sets the maximum number of homes for each island where the player is the owner. This could apply to multiple islands.</li> | ||
* <li><b>/bsb maxhomes <player> <number> [island name]</b> - Sets the maximum number of homes for a specific named island where the player is the owner.</li> | ||
* <li><b>/bsb maxhomes <number></b> - Sets the maximum number of homes for the island you are standing on (in-game only).</li> | ||
* </ul> | ||
* | ||
* @author tastybento | ||
* @since 2.6.0 | ||
*/ | ||
|
||
public class AdminMaxHomesCommand extends ConfirmableCommand { | ||
|
||
Integer maxHomes; | ||
Map<String, Island> islands = new HashMap<>(); | ||
|
||
public AdminMaxHomesCommand(CompositeCommand parent) { | ||
super(parent, "setmaxhomes"); | ||
} | ||
|
||
@Override | ||
public void setup() { | ||
setPermission("mod.maxhomes"); | ||
setOnlyPlayer(false); | ||
setParametersHelp("commands.admin.maxhomes.parameters"); | ||
setDescription("commands.admin.maxhomes.description"); | ||
} | ||
|
||
@Override | ||
public boolean canExecute(User user, String label, List<String> args) { | ||
islands.clear(); | ||
if (args.isEmpty()) { | ||
showHelp(this, user); | ||
return false; | ||
} | ||
// Check arguments | ||
if (args.size() == 1) { | ||
// Player must be in game | ||
if (!user.isPlayer()) { | ||
user.sendMessage("general.errors.use-in-game"); | ||
return false; | ||
} | ||
// Check world | ||
if (user.getWorld() != getWorld()) { | ||
user.sendMessage("general.errors.wrong-world"); | ||
return false; | ||
} | ||
// Arg must be an integer to return true, otherwise false | ||
maxHomes = Ints.tryParse(args.get(0)); | ||
if (maxHomes == null || maxHomes < 1) { | ||
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(0)); | ||
return false; | ||
} | ||
// Get the island the user is standing on | ||
boolean onIsland = getIslands().getIslandAt(user.getLocation()).map(is -> { | ||
islands.put("", is); | ||
return true; | ||
}).orElse(false); | ||
if (!onIsland) { | ||
user.sendMessage("general.errors.not-on-island"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
// More than one argument | ||
// First arg must be a valid player name | ||
UUID targetUUID = getPlayers().getUUID(args.get(0)); | ||
if (targetUUID == null) { | ||
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); | ||
return false; | ||
} | ||
// Second arg must be the max homes number | ||
maxHomes = Ints.tryParse(args.get(1)); | ||
if (maxHomes == null) { | ||
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1)); | ||
return false; | ||
} | ||
// Get islands | ||
islands = this.getNameIslandMap(User.getInstance(targetUUID)); | ||
if (islands.isEmpty()) { | ||
user.sendMessage("general.errors.player-has-no-island"); | ||
return false; | ||
} | ||
if (args.size() > 2) { | ||
// A specific island is mentioned. Parse which one it is and remove the others | ||
final String name = String.join(" ", args.subList(2, args.size())); // Join all the args from here with spaces | ||
|
||
islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n)); | ||
|
||
if (islands.isEmpty()) { | ||
// Failed name check - there are either | ||
user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean execute(User user, String label, List<String> args) { | ||
if (islands.isEmpty() || maxHomes < 1) { | ||
// Sanity check | ||
return false; | ||
} | ||
islands.forEach((name, island) -> { | ||
island.setMaxHomes(maxHomes); | ||
user.sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, name, TextVariables.NUMBER, | ||
String.valueOf(maxHomes)); | ||
}); | ||
return true; | ||
} | ||
|
||
@Override | ||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { | ||
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; | ||
if (args.size() == 2) { | ||
// Suggest player names | ||
return Optional.of(Util.getOnlinePlayerList(user)); | ||
} | ||
if (args.size() > 3) { | ||
// Work out who is in arg 2 | ||
UUID targetUUID = getPlayers().getUUID(args.get(1)); | ||
if (targetUUID != null) { | ||
User target = User.getInstance(targetUUID); | ||
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(target).keySet()), lastArg)); | ||
} | ||
} | ||
return Optional.of(List.of("1")); | ||
|
||
} | ||
|
||
Map<String, Island> getNameIslandMap(User user) { | ||
Map<String, Island> islandMap = new HashMap<>(); | ||
int index = 0; | ||
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { | ||
index++; | ||
if (island.getName() != null && !island.getName().isBlank()) { | ||
// Name has been set | ||
islandMap.put(island.getName(), island); | ||
} else { | ||
// Name has not been set | ||
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, | ||
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index; | ||
islandMap.put(text, island); | ||
} | ||
} | ||
|
||
return islandMap; | ||
|
||
} | ||
|
||
} |
158 changes: 158 additions & 0 deletions
158
src/main/java/world/bentobox/bentobox/api/commands/admin/AdminResetHomeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package world.bentobox.bentobox.api.commands.admin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import world.bentobox.bentobox.api.commands.CompositeCommand; | ||
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.util.Util; | ||
|
||
|
||
/** | ||
* This command resets players island name. | ||
* @author BONNe | ||
*/ | ||
public class AdminResetHomeCommand extends CompositeCommand | ||
{ | ||
Map<String, Island> islands = new HashMap<>(); | ||
|
||
/** | ||
* Default constructor. | ||
* @param command Parent command. | ||
*/ | ||
public AdminResetHomeCommand(CompositeCommand command) | ||
{ | ||
super(command, "resethome"); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void setup() | ||
{ | ||
this.setPermission("mod.resethome"); | ||
this.setDescription("commands.admin.resethome.description"); | ||
this.setParametersHelp("commands.admin.resethome.parameters"); | ||
} | ||
|
||
|
||
/** | ||
* @param user the {@link User} who is executing this command. | ||
* @param label the label which has been used to execute this command. | ||
* It can be {@link CompositeCommand#getLabel()} or an alias. | ||
* @param args the command arguments. | ||
* @return {@code true} if name can be reset, {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean canExecute(User user, String label, List<String> args) | ||
{ | ||
islands.clear(); | ||
if (args.isEmpty()) { | ||
this.showHelp(this, user); | ||
return false; | ||
} | ||
// First arg must be a valid player name | ||
UUID targetUUID = getPlayers().getUUID(args.get(0)); | ||
if (targetUUID == null) { | ||
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); | ||
return false; | ||
} | ||
// Get islands | ||
islands = this.getNameIslandMap(User.getInstance(targetUUID)); | ||
if (islands.isEmpty()) { | ||
user.sendMessage("general.errors.player-has-no-island"); | ||
return false; | ||
} | ||
|
||
// Second optional arg must be the name of the island | ||
if (args.size() == 1) { | ||
return true; | ||
} | ||
|
||
// A specific island is mentioned. Parse which one it is and remove the others | ||
final String name = String.join(" ", args.subList(1, args.size())); // Join all the args from here with spaces | ||
|
||
islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n)); | ||
|
||
if (islands.isEmpty()) { | ||
// Failed name check - there are either | ||
user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** | ||
* @param user the {@link User} who is executing this command. | ||
* @param label the label which has been used to execute this command. | ||
* It can be {@link CompositeCommand#getLabel()} or an alias. | ||
* @param args the command arguments. | ||
* @return {@code true} | ||
*/ | ||
@Override | ||
public boolean execute(User user, String label, List<String> args) | ||
{ | ||
if (islands.isEmpty()) { | ||
// Sanity check | ||
return false; | ||
} | ||
islands.forEach((name, island) -> { | ||
island.getHomes().keySet().removeIf(String::isEmpty); // Remove the default home | ||
user.sendMessage("commands.admin.resethome.cleared", TextVariables.NAME, name); | ||
}); | ||
|
||
user.sendMessage("general.success"); | ||
return true; | ||
} | ||
|
||
|
||
@Override | ||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { | ||
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : ""; | ||
if (args.size() == 2) { | ||
// Suggest player names | ||
return Optional.of(Util.getOnlinePlayerList(user)); | ||
} | ||
if (args.size() > 2) { | ||
// Work out who is in arg 2 | ||
UUID targetUUID = getPlayers().getUUID(args.get(0)); | ||
if (targetUUID != null) { | ||
User target = User.getInstance(targetUUID); | ||
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(target).keySet()), lastArg)); | ||
} | ||
} | ||
return Optional.empty(); | ||
|
||
} | ||
|
||
Map<String, Island> getNameIslandMap(User user) { | ||
Map<String, Island> islandMap = new HashMap<>(); | ||
int index = 0; | ||
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { | ||
index++; | ||
if (island.getName() != null && !island.getName().isBlank()) { | ||
// Name has been set | ||
islandMap.put(island.getName(), island); | ||
} else { | ||
// Name has not been set | ||
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, | ||
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index; | ||
islandMap.put(text, island); | ||
} | ||
} | ||
|
||
return islandMap; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.