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
This commit is contained in:
parent
425cbcd9d2
commit
0e88c61b77
2 changed files with 50 additions and 3 deletions
|
@ -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");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ import org.bukkit.Bukkit;
|
||||||
public class TownyComponent extends Component<MainPlugin> {
|
public class TownyComponent extends Component<MainPlugin> {
|
||||||
@Override
|
@Override
|
||||||
protected void enable() {
|
protected void enable() {
|
||||||
|
registerCommand(new RemoveResidentsCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,9 +39,9 @@ public class TownyComponent extends Component<MainPlugin> {
|
||||||
if (resident == null) {
|
if (resident == null) {
|
||||||
Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny.");
|
Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny.");
|
||||||
TBMCCoreAPI.sendDebugMessage("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())) {
|
} else if (tu.getDataSource().hasResident(newName)) {
|
||||||
Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle
|
Bukkit.getLogger().warning("Target resident name is already in use.");
|
||||||
TBMCCoreAPI.sendDebugMessage("Target resident name is already in use.");
|
TBMCCoreAPI.sendDebugMessage("Target resident name is already in use. (" + oldName + " -> " + newName + ")");
|
||||||
} else
|
} else
|
||||||
try {
|
try {
|
||||||
tu.getDataSource().renamePlayer(resident, newName); //Fixed in Towny 0.91.1.2
|
tu.getDataSource().renamePlayer(resident, newName); //Fixed in Towny 0.91.1.2
|
||||||
|
|
Loading…
Reference in a new issue