From 1da3b3f08e82b3a3d46569255ed27e27eaa80917 Mon Sep 17 00:00:00 2001 From: triagonal <10545540+triagonal@users.noreply.github.com> Date: Sat, 19 Jun 2021 01:24:43 +1000 Subject: [PATCH] Implement home cleanup utility (#4244) --- .../commands/Commandessentials.java | 88 +++++++++++++++++-- .../src/main/resources/messages.properties | 10 +++ 2 files changed, 92 insertions(+), 6 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java index 8dfabcb8c..388a85299 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java @@ -13,8 +13,10 @@ import com.google.common.base.Charsets; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.Sound; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -29,6 +31,7 @@ import java.util.Locale; import java.util.Map; import java.util.UUID; import java.util.function.Supplier; +import java.util.stream.Collectors; import static com.earth2me.essentials.I18n.tl; @@ -38,6 +41,8 @@ public class Commandessentials extends EssentialsCommand { private static final Sound NOTE_HARP = EnumUtil.valueOf(Sound.class, "BLOCK_NOTE_BLOCK_HARP", "BLOCK_NOTE_HARP", "NOTE_PIANO"); private static final Sound MOO_SOUND = EnumUtil.valueOf(Sound.class, "COW_IDLE", "ENTITY_COW_MILK"); + private static final String HOMES_USAGE = "/ homes (fix | delete [world])"; + private static final String NYAN_TUNE = "1D#,1E,2F#,,2A#,1E,1D#,1E,2F#,2B,2D#,2E,2D#,2A#,2B,,2F#,,1D#,1E,2F#,2B,2C#,2A#,2B,2C#,2E,2D#,2E,2C#,,2F#,,2G#,,1D,1D#,,1C#,1D,1C#,1B,,1B,,1C#,,1D,,1D,1C#,1B,1C#,1D#,2F#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,1B,,1F#,1G#,1B,1B,1C#,1D#,1B,1F#,1G#,1F#,1B,,1B,1A#,1B,1F#,1G#,1B,1E,1D#,1E,2F#,1B,,1A#,,1B,,1F#,1G#,1B,,1F#,1G#,1B,1C#,1D#,1B,1E,1D#,1E,2F#,1B,,1B,,1F#,1G#,1B,1F#,1E,1D#,1C#,1B,,,,1F#,1B,,1F#,1G#,1B,,1F#,1G#,1B,1B,1C#,1D#,1B,1F#,1G#,1F#,1B,,1B,1A#,1B,1F#,1G#,1B,1E,1D#,1E,2F#,1B,,1B,,"; private static final String[] CONSOLE_MOO = new String[] {" (__)", " (oo)", " /------\\/", " / | ||", " * /\\---/\\", " ~~ ~~", "....\"Have you mooed today?\"..."}; private static final String[] PLAYER_MOO = new String[] {" (__)", " (oo)", " /------\\/", " / | | |", " * /\\---/\\", " ~~ ~~", "....\"Have you mooed today?\"..."}; @@ -86,12 +91,10 @@ public class Commandessentials extends EssentialsCommand { case "verbose": runDebug(server, sender, commandLabel, args); break; - case "ver": case "version": runVersion(server, sender, commandLabel, args); break; - case "cmd": case "commands": runCommands(server, sender, commandLabel, args); @@ -107,6 +110,9 @@ public class Commandessentials extends EssentialsCommand { case "cleanup": runCleanup(server, sender, commandLabel, args); break; + case "homes": + runHomes(server, sender, commandLabel, args); + break; case "uuidconvert": runUUIDConvert(server, sender, commandLabel, args); break; @@ -130,7 +136,7 @@ public class Commandessentials extends EssentialsCommand { // Displays the command's usage. private void showUsage(final CommandSource sender) throws Exception { - throw new NotEnoughArgumentsException("/ "); + throw new NotEnoughArgumentsException(); } // Lists commands that are being handed over to other plugins. @@ -253,7 +259,69 @@ public class Commandessentials extends EssentialsCommand { } sender.sendMessage(tl("cleaned")); }); + } + private void runHomes(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { + if (args.length < 2) { + sender.sendMessage("This sub-command provides a utility to mass-delete homes based on user options:"); + sender.sendMessage("Use \"fix\" to delete all homes inside non-existent or unloaded worlds."); + sender.sendMessage("Use \"delete\" to delete all existing homes."); + sender.sendMessage("Use \"delete \" to delete all homes inside a specific world."); + throw new Exception(HOMES_USAGE); + } + + final UserMap userMap = ess.getUserMap(); + switch (args[1]) { + case "fix": + sender.sendMessage(tl("fixingHomes")); + ess.runTaskAsynchronously(() -> { + for (final UUID u : userMap.getAllUniqueUsers()) { + final User user = ess.getUserMap().getUser(u); + if (user == null) { + continue; + } + for (String homeName : user.getHomes()) { + try { + if (user.getHome(homeName) == null) { + user.delHome(homeName); + } + } catch (Exception e) { + ess.getLogger().info("Unable to delete home " + homeName + " for " + user.getName()); + } + } + } + sender.sendMessage(tl("fixedHomes")); + }); + break; + case "delete": + final boolean filterByWorld = args.length >= 3; + if (filterByWorld && server.getWorld(args[2]) == null) { + throw new Exception(tl("invalidWorld")); + } + sender.sendMessage(filterByWorld ? tl("deletingHomesWorld", args[2]) : tl("deletingHomes")); + ess.runTaskAsynchronously(() -> { + for (final UUID u : userMap.getAllUniqueUsers()) { + final User user = ess.getUserMap().getUser(u); + if (user == null) { + continue; + } + for (String homeName : user.getHomes()) { + try { + final Location home = user.getHome(homeName); + if (!filterByWorld || (home != null && home.getWorld() != null && home.getWorld().getName().equals(args[2]))) { + user.delHome(homeName); + } + } catch (Exception e) { + ess.getLogger().info("Unable to delete home " + homeName + " for " + user.getName()); + } + } + } + sender.sendMessage(filterByWorld ? tl("deletedHomesWorld", args[2]) : tl("deletedHomes")); + }); + break; + default: + throw new Exception(HOMES_USAGE); + } } // Forces a rerun of userdata UUID conversion. @@ -406,12 +474,13 @@ public class Commandessentials extends EssentialsCommand { protected List getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) { if (args.length == 1) { final List options = Lists.newArrayList(); - options.add("debug"); - options.add("commands"); - options.add("version"); options.add("reload"); + options.add("version"); + options.add("commands"); + options.add("debug"); options.add("reset"); options.add("cleanup"); + options.add("homes"); //options.add("uuidconvert"); //options.add("uuidtest"); //options.add("nya"); @@ -438,6 +507,13 @@ public class Commandessentials extends EssentialsCommand { return Lists.newArrayList("-1", "0"); } break; + case "homes": + if (args.length == 2) { + return Lists.newArrayList("fix", "delete"); + } else if (args.length == 3 && args[1].equalsIgnoreCase("delete")) { + return server.getWorlds().stream().map(World::getName).collect(Collectors.toList()); + } + break; case "uuidconvert": if (args.length == 2) { return Lists.newArrayList("ignoreUFCache"); diff --git a/Essentials/src/main/resources/messages.properties b/Essentials/src/main/resources/messages.properties index b85d76d21..408287cc7 100644 --- a/Essentials/src/main/resources/messages.properties +++ b/Essentials/src/main/resources/messages.properties @@ -191,11 +191,15 @@ customtextCommandUsage=/ - Define in bukkit.yml day=day days=days defaultBanReason=The Ban Hammer has spoken\! +deletedHomes=All homes deleted. +deletedHomesWorld=All homes in {0} deleted. deleteFileError=Could not delete file\: {0} deleteHome=\u00a76Home\u00a7c {0} \u00a76has been removed. deleteJail=\u00a76Jail\u00a7c {0} \u00a76has been removed. deleteKit=\u00a76Kit\u00a7c {0} \u00a76has been removed. deleteWarp=\u00a76Warp\u00a7c {0} \u00a76has been removed. +deletingHomes=Deleting all homes... +deletingHomesWorld=Deleting all homes in {0}... delhomeCommandDescription=Removes a home. delhomeCommandUsage=/ [player:] delhomeCommandUsage1=/ @@ -278,6 +282,10 @@ essentialsCommandUsage4=/ debug essentialsCommandUsage4Description=Toggles Essentials' "debug mode" essentialsCommandUsage5=/ reset essentialsCommandUsage5Description=Resets the given player's userdata +essentialsCommandUsage6=/ cleanup +essentialsCommandUsage6Description=Cleans up old userdata +essentialsCommandUsage7=/ homes +essentialsCommandUsage7Description=Manages user homes essentialsHelp1=The file is broken and Essentials can''t open it. Essentials is now disabled. If you can''t fix the file yourself, go to http\://tiny.cc/EssentialsChat essentialsHelp2=The file is broken and Essentials can''t open it. Essentials is now disabled. If you can''t fix the file yourself, either type /essentialshelp in game or go to http\://tiny.cc/EssentialsChat essentialsReload=\u00a76Essentials reloaded\u00a7c {0}. @@ -329,6 +337,8 @@ fireworkCommandUsage4=/ fireworkCommandUsage4Description=Adds the given effect to the held firework fireworkEffectsCleared=\u00a76Removed all effects from held stack. fireworkSyntax=\u00a76Firework parameters\:\u00a7c color\: [fade\:] [shape\:] [effect\:]\n\u00a76To use multiple colors/effects, separate values with commas\: \u00a7cred,blue,pink\n\u00a76Shapes\:\u00a7c star, ball, large, creeper, burst \u00a76Effects\:\u00a7c trail, twinkle. +fixedHomes=Invalid homes deleted. +fixingHomes=Deleting invalid homes... flyCommandDescription=Take off, and soar! flyCommandUsage=/ [player] [on|off] flyCommandUsage1=/ [player]