Remove code duplication between kick and leave command

This commit is contained in:
tastybento 2020-12-24 14:24:42 -08:00
parent f5b2b136e9
commit 372f149f3d
3 changed files with 50 additions and 62 deletions

View File

@ -85,43 +85,9 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
target.sendMessage("commands.island.team.kick.owner-kicked", TextVariables.GAMEMODE, getAddon().getDescription().getName()); target.sendMessage("commands.island.team.kick.owner-kicked", TextVariables.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 // Clean the target player
Util.runCommands(target, getIWM().getOnLeaveCommands(oldIsland.getWorld()), "leave"); getPlayers().cleanLeavingPlayer(getWorld(), target);
// Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
if (target.isOnline()) {
target.getPlayer().getEnderChest().clear();
}
else {
getPlayers().getPlayer(targetUUID).addToPendingKick(getWorld());
getPlayers().save(targetUUID);
}
}
if (getIWM().isOnLeaveResetInventory(getWorld()) && !getIWM().isKickedKeepInventory(getWorld())) {
if (target.isOnline()) {
target.getPlayer().getInventory().clear();
} else {
getPlayers().getPlayer(targetUUID).addToPendingKick(getWorld());
getPlayers().save(targetUUID);
}
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.withdraw(target, vault.getBalance(target)));
}
// Reset the health
if (getIWM().isOnLeaveResetHealth(getWorld())) {
Util.resetHealth(target.getPlayer());
}
// Reset the hunger
if (getIWM().isOnLeaveResetHunger(getWorld())) {
target.getPlayer().setFoodLevel(20);
}
// Reset the XP
if (getIWM().isOnLeaveResetXP(getWorld())) {
target.getPlayer().setTotalExperience(0);
}
user.sendMessage("commands.island.team.kick.success", TextVariables.NAME, target.getName()); user.sendMessage("commands.island.team.kick.success", TextVariables.NAME, target.getName());
// Fire event // Fire event
TeamEvent.builder() TeamEvent.builder()

View File

@ -11,7 +11,6 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
public class IslandTeamLeaveCommand extends ConfirmableCommand { public class IslandTeamLeaveCommand extends ConfirmableCommand {
@ -72,32 +71,9 @@ 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 // Clean the player
Util.runCommands(user, getIWM().getOnLeaveCommands(island.getWorld()), "leave"); getPlayers().cleanLeavingPlayer(getWorld(), user);
// Remove money inventory etc.
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
user.getPlayer().getEnderChest().clear();
}
if (getIWM().isOnLeaveResetInventory(getWorld())) {
user.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user)));
}
// Reset the health
if (getIWM().isOnLeaveResetHealth(getWorld())) {
Util.resetHealth(user.getPlayer());
}
// Reset the hunger
if (getIWM().isOnLeaveResetHunger(getWorld())) {
user.getPlayer().setFoodLevel(20);
}
// Reset the XP
if (getIWM().isOnLeaveResetXP(getWorld())) {
user.getPlayer().setTotalExperience(0);
}
// Add cooldown for this player and target // Add cooldown for this player and target
if (getSettings().getInviteCooldown() > 0 && getParent() != null) { if (getSettings().getInviteCooldown() > 0 && getParent() != null) {
// Get the invite class from the parent // Get the invite class from the parent

View File

@ -15,6 +15,7 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Names; import world.bentobox.bentobox.database.objects.Names;
import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.util.Util;
public class PlayersManager { public class PlayersManager {
@ -507,4 +508,49 @@ public class PlayersManager {
playerCache.values().removeIf(p -> player.getUniqueId().toString().equals(p.getUniqueId())); playerCache.values().removeIf(p -> player.getUniqueId().toString().equals(p.getUniqueId()));
} }
/**
* Cleans the player when leaving an island
* @param world - island world
* @param target - target user
* @since 1.15.4
*/
public void cleanLeavingPlayer(World world, User target) {
// Execute commands when leaving
Util.runCommands(target, plugin.getIWM().getOnLeaveCommands(world), "leave");
// Remove money inventory etc.
if (plugin.getIWM().isOnLeaveResetEnderChest(world)) {
if (target.isOnline()) {
target.getPlayer().getEnderChest().clear();
} else {
getPlayer(target.getUniqueId()).addToPendingKick(world);
}
}
if (plugin.getIWM().isOnLeaveResetInventory(world) && !plugin.getIWM().isKickedKeepInventory(world)) {
if (target.isOnline()) {
target.getPlayer().getInventory().clear();
} else {
getPlayer(target.getUniqueId()).addToPendingKick(world);
}
}
if (plugin.getSettings().isUseEconomy() && plugin.getIWM().isOnLeaveResetMoney(world)) {
plugin.getVault().ifPresent(vault -> vault.withdraw(target, vault.getBalance(target)));
}
// Reset the health
if (plugin.getIWM().isOnLeaveResetHealth(world)) {
Util.resetHealth(target.getPlayer());
}
// Reset the hunger
if (plugin.getIWM().isOnLeaveResetHunger(world)) {
target.getPlayer().setFoodLevel(20);
}
// Reset the XP
if (plugin.getIWM().isOnLeaveResetXP(world)) {
target.getPlayer().setTotalExperience(0);
}
// Save player
save(target.getUniqueId());
}
} }