From da6d6d64c2e7c6fc6c38516e3458e152f8ff0628 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 18 Dec 2020 23:57:34 +0800 Subject: [PATCH] Add purge command. --- .../commands_acf/PurgeCommand.java | 65 +++++++++++++++++++ .../commands_helper/MVCommandCompletions.java | 12 +++- .../commands_helper/MVCommandManager.java | 2 + 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands_acf/PurgeCommand.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/PurgeCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/PurgeCommand.java new file mode 100644 index 00000000..2f287bcc --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/PurgeCommand.java @@ -0,0 +1,65 @@ +package com.onarandombox.MultiverseCore.commands_acf; + +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Flags; +import co.aikar.commands.annotation.Split; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +@CommandAlias("mv") +public class PurgeCommand extends MultiverseCommand { + + public PurgeCommand(MultiverseCore plugin) { + super(plugin); + } + + @Subcommand("purgeall") + @CommandPermission("multiverse.core.purge") + @Syntax("") + @CommandCompletion("all|animals|monsters|@livingEntities") + @Description("Removed the specified type of mob from all worlds.") + public void onPurgeAllCommand(@NotNull CommandSender sender, + @NotNull @Split(",") String[] targetEntities) { + + doPurge(sender, this.plugin.getMVWorldManager().getMVWorlds(), targetEntities); + } + + @Subcommand("purge") + @CommandPermission("multiverse.core.purge") + @Syntax("") + @CommandCompletion("@MVWorlds|all|animals|monsters|@livingEntities all|animals|monsters|@livingEntities") + @Description("Removed the specified type of mob from the specified world.") + public void onPurgeCommand(@NotNull CommandSender sender, + @NotNull @Flags("other,defaultself,fallbackself") MultiverseWorld world, + @NotNull @Split(",") String[] targetEntities) { + + doPurge(sender, Collections.singleton(world), targetEntities); + } + + private void doPurge(@NotNull CommandSender sender, + @NotNull Collection targetWorlds, + @NotNull String[] targetEntities) { + + List thingsToKill = Arrays.stream(targetEntities) + .map(String::toUpperCase) + .collect(Collectors.toList()); + + targetWorlds.forEach(w -> this.plugin.getMVWorldManager() + .getTheWorldPurger() + .purgeWorld(w, thingsToKill, false, false, sender)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandCompletions.java index 0206f216..c295acf8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandCompletions.java @@ -8,6 +8,7 @@ import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.enums.AddProperties; import org.bukkit.GameRule; import org.bukkit.World; +import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -33,8 +34,9 @@ public class MVCommandCompletions extends PaperCommandCompletions { registerStaticCompletion("gameRules", suggestGameRules()); registerStaticCompletion("environments", suggestEnvironments()); registerStaticCompletion("addProperties", suggestAddProperties()); + registerStaticCompletion("livingEntities", suggestEntities()); - //TODO: Change to destinations + //TODO: Destinations } @NotNull @@ -105,4 +107,12 @@ public class MVCommandCompletions extends PaperCommandCompletions { .map(p -> p.toString().toLowerCase()) .collect(Collectors.toList()); } + + @NotNull + private List suggestEntities() { + return Arrays.stream(EntityType.values()) + .filter(e -> e.isAlive() && e.isSpawnable()) + .map(e -> e.toString().toLowerCase()) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandManager.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandManager.java index 363453c2..8348ebcf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/MVCommandManager.java @@ -25,6 +25,7 @@ import com.onarandombox.MultiverseCore.commands_acf.ImportCommand; import com.onarandombox.MultiverseCore.commands_acf.InfoCommand; import com.onarandombox.MultiverseCore.commands_acf.ListCommand; import com.onarandombox.MultiverseCore.commands_acf.LoadCommand; +import com.onarandombox.MultiverseCore.commands_acf.PurgeCommand; import com.onarandombox.MultiverseCore.commands_acf.RegenCommand; import com.onarandombox.MultiverseCore.commands_acf.ReloadCommand; import com.onarandombox.MultiverseCore.commands_acf.RemoveCommand; @@ -90,6 +91,7 @@ public class MVCommandManager extends PaperCommandManager { registerCommand(new RegenCommand(plugin)); registerCommand(new TeleportCommand(plugin)); registerCommand(new SilentCommand(plugin)); + registerCommand(new PurgeCommand(plugin)); } @Override