Added WorldSettings to run commands on island creation/join and deletion/leave

Implements https://github.com/BentoBoxWorld/BentoBox/issues/704

Gamemode addons will need to be updated to use the new WorldSettings, which have been properly documented so it should be easy to write down comments about them.
This commit is contained in:
Florian CUNY 2019-09-28 13:48:24 +02:00
parent 6e3e3561c1
commit c53fece28f
7 changed files with 126 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -76,6 +77,17 @@ public class AdminDeleteCommand extends ConfirmableCommand {
// Remove them from this island (it still exists and will be deleted later) // Remove them from this island (it still exists and will be deleted later)
getIslands().removePlayer(getWorld(), targetUUID); getIslands().removePlayer(getWorld(), targetUUID);
if (target.isOnline()) { if (target.isOnline()) {
// Execute commands when leaving
getIWM().getOnLeaveCommands(getWorld()).forEach(command -> {
command = command.replace("[player]", target.getName());
if (command.startsWith("[SUDO]")) {
// Execute the command by the player
target.performCommand(command.substring(6));
} else {
// Otherwise execute as the server console
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
});
// Remove money inventory etc. // Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) { if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
target.getPlayer().getEnderChest().clear(); target.getPlayer().getEnderChest().clear();

View File

@ -167,6 +167,18 @@ public class IslandResetCommand extends ConfirmableCommand {
// Remove player // Remove player
getIslands().removePlayer(getWorld(), memberUUID); getIslands().removePlayer(getWorld(), memberUUID);
// Execute commands when leaving
getIWM().getOnLeaveCommands(island.getWorld()).forEach(command -> {
command = command.replace("[player]", member.getName());
if (command.startsWith("[SUDO]") && member.isOnline()) {
// Execute the command by the player
member.performCommand(command.substring(6));
} else {
// Otherwise execute as the server console
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
});
// Remove money inventory etc. // Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) { if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
if (member.isOnline()) { if (member.isOnline()) {

View File

@ -78,6 +78,17 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
target.sendMessage("commands.island.team.kick.owner-kicked", "[gamemode]", getAddon().getDescription().getName()); target.sendMessage("commands.island.team.kick.owner-kicked", "[gamemode]", getAddon().getDescription().getName());
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID); Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
getIslands().removePlayer(getWorld(), targetUUID); getIslands().removePlayer(getWorld(), targetUUID);
// Execute commands when leaving
getIWM().getOnLeaveCommands(oldIsland.getWorld()).forEach(command -> {
command = command.replace("[player]", target.getName());
if (command.startsWith("[SUDO]") && target.isOnline()) {
// Execute the command by the player
target.performCommand(command.substring(6));
} else {
// Otherwise execute as the server console
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
});
// Remove money inventory etc. // Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) { if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
if (target.isOnline()) { if (target.isOnline()) {

View File

@ -72,6 +72,17 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand {
User.getInstance(ownerUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME, user.getName()); User.getInstance(ownerUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME, user.getName());
} }
getIslands().setLeaveTeam(getWorld(), user.getUniqueId()); getIslands().setLeaveTeam(getWorld(), user.getUniqueId());
// Execute commands when leaving
getIWM().getOnLeaveCommands(island.getWorld()).forEach(command -> {
command = command.replace("[player]", user.getName());
if (command.startsWith("[SUDO]") && user.isOnline()) {
// Execute the command by the player
user.performCommand(command.substring(6));
} else {
// Otherwise execute as the server console
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
});
// Remove money inventory etc. // Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) { if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
user.getPlayer().getEnderChest().clear(); user.getPlayer().getEnderChest().clear();

View File

@ -8,6 +8,7 @@ import org.bukkit.Difficulty;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
/** /**
@ -186,6 +187,27 @@ public interface WorldSettings extends ConfigObject {
*/ */
boolean isOnJoinResetMoney(); boolean isOnJoinResetMoney();
/**
* Returns a list of commands that should be executed when the player joins an island or creates one.<br/>
* These commands are executed by the console, unless otherwise stated using the {@code [SUDO]} prefix, in which case they are executed by the player.<br/>
* <br/>
* Available placeholders for the commands are the following:
* <ul>
* <li>{@code [player]}: name of the player</li>
* </ul>
* <br/>
* Here are some examples of valid commands to execute:
* <ul>
* <li>{@code "[SUDO] bbox version"}</li>
* <li>{@code "bsbadmin deaths set [player] 0"}</li>
* </ul>
* @return a list of commands.
* @since 1.8.0
* @see #getOnLeaveCommands()
*/
@NonNull
List<String> getOnJoinCommands();
/** /**
* @return the onLeaveResetEnderChest * @return the onLeaveResetEnderChest
*/ */
@ -201,6 +223,29 @@ public interface WorldSettings extends ConfigObject {
*/ */
boolean isOnLeaveResetMoney(); boolean isOnLeaveResetMoney();
/**
* Returns a list of commands that should be executed when the player leaves an island or resets one.<br/>
* These commands are executed by the console, unless otherwise stated using the {@code [SUDO]} prefix, in which case they are executed by the player.<br/>
* <br/>
* Available placeholders for the commands are the following:
* <ul>
* <li>{@code [player]}: name of the player</li>
* </ul>
* <br/>
* Here are some examples of valid commands to execute:
* <ul>
* <li>{@code "[SUDO] bbox version"}</li>
* <li>{@code "bsbadmin deaths set [player] 0"}</li>
* </ul>
* <br/>
* Note that player-executed commands might not work, as these commands can be run with said player being offline.
* @return a list of commands.
* @since 1.8.0
* @see #getOnJoinCommands()
*/
@NonNull
List<String> getOnLeaveCommands();
/** /**
* @return true if the default world generator should not operate in this world * @return true if the default world generator should not operate in this world
*/ */

View File

@ -562,6 +562,18 @@ public class IslandWorldManager {
return gameModes.get(world).getWorldSettings().isOnJoinResetEnderChest(); return gameModes.get(world).getWorldSettings().isOnJoinResetEnderChest();
} }
/**
* Returns a list of commands to execute when the player creates or joins an island.
* @param world the World
* @return a list of commands
* @since 1.8.0
* @see #getOnLeaveCommands(World)
*/
@NonNull
public List<String> getOnJoinCommands(@NonNull World world) {
return gameModes.get(world).getWorldSettings().getOnJoinCommands();
}
/** /**
* @return the onLeaveResetMoney * @return the onLeaveResetMoney
*/ */
@ -583,6 +595,18 @@ public class IslandWorldManager {
return gameModes.get(world).getWorldSettings().isOnLeaveResetEnderChest(); return gameModes.get(world).getWorldSettings().isOnLeaveResetEnderChest();
} }
/**
* Returns a list of commands to execute when the player resets or leaves an island.
* @param world the World
* @return a list of commands
* @since 1.8.0
* @see #getOnJoinCommands(World)
*/
@NonNull
public List<String> getOnLeaveCommands(@NonNull World world) {
return gameModes.get(world).getWorldSettings().getOnLeaveCommands();
}
/** /**
* Get data folder for the addon that registered this world * Get data folder for the addon that registered this world
* *

View File

@ -670,7 +670,17 @@ public class IslandsManager {
} }
// If this is a new island, then run commands and do resets // If this is a new island, then run commands and do resets
if (newIsland) { if (newIsland) {
// TODO add command running // Execute commands
plugin.getIWM().getOnJoinCommands(world).forEach(command -> {
command = command.replace("[player]", player.getName());
if (command.startsWith("[SUDO]")) {
// Execute the command by the player
player.performCommand(command.substring(6));
} else {
// Otherwise execute as the server console
plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
});
// Remove money inventory etc. // Remove money inventory etc.
if (plugin.getIWM().isOnJoinResetEnderChest(world)) { if (plugin.getIWM().isOnJoinResetEnderChest(world)) {