From 0e88c61b775f31cc430a2796bc322552ca200654 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 8 Mar 2020 21:37:07 +0100 Subject: [PATCH] Fix Towny resident rename, remove invalid residents Added a command to remove residents that aren't real players anymore because of a rename usually (#41) Name conflicts should be handled manually (#7) - possibly by removing the resident --- .../towny/RemoveResidentsCommand.java | 46 +++++++++++++++++++ .../core/component/towny/TownyComponent.java | 7 +-- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java new file mode 100644 index 0000000..03228f9 --- /dev/null +++ b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java @@ -0,0 +1,46 @@ +package buttondevteam.core.component.towny; + +import buttondevteam.lib.chat.Command2; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.ICommand2MC; +import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.towny.TownyUniverse; +import com.palmergames.bukkit.towny.object.TownyObject; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import java.util.AbstractMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@CommandClass(path = "chroma remresidents", modOnly = true, helpText = { + "Removes invalid Towny residents from their towns (usually after a rename that didn't get caught by the plugin)", + "If the delete eco account setting is off, then it will completely delete the resident", + "(The economy account could still be used by the player)" +}) +public class RemoveResidentsCommand extends ICommand2MC { + @Command2.Subcommand + public void def(CommandSender sender, @Command2.OptionalArg String remove) { + Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> { + sender.sendMessage("Starting..."); + var ds = TownyUniverse.getInstance().getDataSource(); + var res = ds.getResidents().stream() + .flatMap(r -> Stream.of(r) //https://stackoverflow.com/questions/37299312/in-java-8-lambdas-how-to-access-original-object-in-the-stream + .map(TownyObject::getName).map(Bukkit::getOfflinePlayer).filter(p -> !p.hasPlayedBefore()) + .map(p -> new AbstractMap.SimpleEntry<>(r, p))) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + sender.sendMessage("Residents to remove:"); + res.values().forEach(op -> sender.sendMessage(op.getName())); + if (TownySettings.isDeleteEcoAccount()) + sender.sendMessage("§bWill only remove from town, as delete eco account setting is on"); + else + sender.sendMessage("§eWill completely delete the resident, delete eco account setting is off"); + if (remove != null && remove.equalsIgnoreCase("remove")) { + sender.sendMessage("Removing residents..."); //Removes from town and deletes town if needed - doesn't delete the resident if the setting is on + //If it did, that could mean the player's economy is deleted too, unless this setting is false + res.keySet().forEach(TownySettings.isDeleteEcoAccount() ? ds::removeResident : ds::removeResidentList); + sender.sendMessage("Done"); + } + }); + } +} diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/towny/TownyComponent.java b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/TownyComponent.java index a6fdda4..0acf1ef 100644 --- a/Chroma-Core/src/main/java/buttondevteam/core/component/towny/TownyComponent.java +++ b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/TownyComponent.java @@ -17,6 +17,7 @@ import org.bukkit.Bukkit; public class TownyComponent extends Component { @Override protected void enable() { + registerCommand(new RemoveResidentsCommand()); } @Override @@ -38,9 +39,9 @@ public class TownyComponent extends Component { if (resident == null) { Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); TBMCCoreAPI.sendDebugMessage("Resident not found - couldn't rename in Towny."); - } else if (tu.getResidentMap().contains(newName.toLowerCase())) { - Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle - TBMCCoreAPI.sendDebugMessage("Target resident name is already in use."); + } else if (tu.getDataSource().hasResident(newName)) { + Bukkit.getLogger().warning("Target resident name is already in use."); + TBMCCoreAPI.sendDebugMessage("Target resident name is already in use. (" + oldName + " -> " + newName + ")"); } else try { tu.getDataSource().renamePlayer(resident, newName); //Fixed in Towny 0.91.1.2