Implements admin command to set an island's max homes #2517

This commit is contained in:
tastybento 2024-09-28 13:22:19 -07:00
parent 69de5ea564
commit fd1ab22cee
6 changed files with 198 additions and 8 deletions

View File

@ -88,7 +88,7 @@
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>2.5.4</build.version> <build.version>2.6.0</build.version>
<sonar.organization>bentobox-world</sonar.organization> <sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url> <sonar.host.url>https://sonarcloud.io</sonar.host.url>
<server.jars>${project.basedir}/lib</server.jars> <server.jars>${project.basedir}/lib</server.jars>

View File

@ -0,0 +1,169 @@
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.BentoBox;
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 &lt;player&gt; &lt;number&gt;</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 &lt;player&gt; &lt;number&gt; [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 &lt;number&gt;</b> - Sets the maximum number of homes for the island you are standing on (in-game only).</li>
* </ul>
*
* @author tastybento
*/
public class AdminMaxHomesCommand extends ConfirmableCommand {
private Integer maxHomes;
private 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) {
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(user).keySet()), lastArg));
}
return Optional.of(List.of("1"));
}
private 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;
}
}

View File

@ -102,6 +102,8 @@ public abstract class DefaultAdminCommand extends CompositeCommand {
new AdminDeleteHomesCommand(this); new AdminDeleteHomesCommand(this);
// Reset name // Reset name
new AdminResetNameCommand(this); new AdminResetNameCommand(this);
// Max homes
new AdminMaxHomesCommand(this);
} }
/** /**

View File

@ -95,13 +95,7 @@ public class JoinLeaveListener implements Listener {
// Set island max members and homes based on permissions if this player is the // Set island max members and homes based on permissions if this player is the
// owner of an island // owner of an island
plugin.getIWM().getOverWorlds().stream().map(w -> plugin.getIslands().getIsland(w, playerUUID)) updateIslandMaxTeamAndHomeSize(user);
.filter(Objects::nonNull).filter(i -> playerUUID.equals(i.getOwner())).forEach(i -> {
plugin.getIslands().getMaxMembers(i, RanksManager.MEMBER_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.COOP_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
plugin.getIslands().getMaxHomes(i);
});
// Add a player to the bStats cache. // Add a player to the bStats cache.
plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID)); plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID));
@ -125,6 +119,18 @@ public class JoinLeaveListener implements Listener {
}); });
} }
private void updateIslandMaxTeamAndHomeSize(User user) {
plugin.getIWM().getOverWorlds().stream()
.flatMap(w -> plugin.getIslands().getIslands(w, user.getUniqueId()).stream()) // Flatten the List<Island> into a Stream<Island>
.filter(Objects::nonNull).filter(i -> user.getUniqueId().equals(i.getOwner())).forEach(i -> {
plugin.getIslands().getMaxMembers(i, RanksManager.MEMBER_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.COOP_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
plugin.getIslands().getMaxHomes(i);
});
}
private void firstTime(User user) { private void firstTime(User user) {
// Make sure the player is loaded into the cache or create the player if they // Make sure the player is loaded into the cache or create the player if they
// don't exist // don't exist
@ -206,6 +212,10 @@ public class JoinLeaveListener implements Listener {
} }
} }
/**
* Update island range using player perms
* @param user user
*/
private void updateIslandRange(User user) { private void updateIslandRange(User user) {
plugin.getIslands().getIslands(user.getUniqueId()).stream() plugin.getIslands().getIslands(user.getUniqueId()).stream()
.filter(island -> island.getOwner() != null && island.getOwner().equals(user.getUniqueId())) .filter(island -> island.getOwner() != null && island.getOwner().equals(user.getUniqueId()))

View File

@ -143,6 +143,7 @@ public class IslandInfo {
// Show team members // Show team members
showMembers(user); showMembers(user);
} }
user.sendMessage("commands.admin.info.max-homes", TextVariables.NUMBER, String.valueOf(island.getMaxHomes()));
Vector location = island.getProtectionCenter().toVector(); Vector location = island.getProtectionCenter().toVector();
user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(location)); user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(location));
user.sendMessage("commands.admin.info.protection-range", RANGE, String.valueOf(island.getProtectionRange())); user.sendMessage("commands.admin.info.protection-range", RANGE, String.valueOf(island.getProtectionRange()));

View File

@ -59,6 +59,13 @@ commands:
admin: admin:
help: help:
description: admin command description: admin command
maxhomes:
description: change the number of homes allowed on this island or player's island
parameters: <number / player> <number> <island name>
max-homes-set: '&a [name] - Set island max homes to [number]'
errors:
unknown-island: &c Unknown island! [name]
resets: resets:
description: edit player reset values description: edit player reset values
set: set:
@ -213,6 +220,7 @@ commands:
last-login-date-time-format: EEE MMM dd HH:mm:ss zzz yyyy last-login-date-time-format: EEE MMM dd HH:mm:ss zzz yyyy
deaths: 'Deaths: [number]' deaths: 'Deaths: [number]'
resets-left: 'Resets: [number] (Max: [total])' resets-left: 'Resets: [number] (Max: [total])'
max-homes: 'Max homes: [number]'
team-members-title: 'Team members:' team-members-title: 'Team members:'
team-owner-format: '&a [name] [rank]' team-owner-format: '&a [name] [rank]'
team-member-format: '&b [name] [rank]' team-member-format: '&b [name] [rank]'