From addc423c2da3b783cd22fca53e3c1a1522358e80 Mon Sep 17 00:00:00 2001 From: CoderTim Date: Thu, 2 Oct 2014 22:15:12 -0400 Subject: [PATCH] Added /npc clear back --- .../core/npc/Commands/ClearCommand.java | 39 +++++++++++++++++++ .../core/npc/Commands/NpcCommand.java | 1 + .../src/mineplex/core/npc/NpcManager.java | 31 +++++++++++++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/ClearCommand.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/ClearCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/ClearCommand.java new file mode 100644 index 000000000..7ea556eec --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/ClearCommand.java @@ -0,0 +1,39 @@ +package mineplex.core.npc.Commands; + +import java.sql.SQLException; + +import org.bukkit.entity.Player; + +import mineplex.core.command.CommandBase; +import mineplex.core.common.Rank; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.npc.NpcManager; + +public class ClearCommand extends CommandBase +{ + public ClearCommand(NpcManager plugin) + { + super(plugin, Rank.DEVELOPER, "clear"); + } + + @Override + public void Execute(Player caller, String[] args) + { + if (args != null) + Plugin.Help(caller); + else + { + try + { + Plugin.ClearNpcs(); + + UtilPlayer.message(caller, F.main(Plugin.GetName(), "Cleared NPCs.")); + } + catch (SQLException e) + { + Plugin.Help(caller, "Database error."); + } + } + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/NpcCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/NpcCommand.java index 394dcdeca..270280954 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/NpcCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/npc/Commands/NpcCommand.java @@ -15,6 +15,7 @@ public class NpcCommand extends MultiCommandBase AddCommand(new AddCommand(plugin)); AddCommand(new DeleteCommand(plugin)); AddCommand(new HomeCommand(plugin)); + AddCommand(new ClearCommand(plugin)); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/npc/NpcManager.java b/Plugins/Mineplex.Core/src/mineplex/core/npc/NpcManager.java index 2f316742e..ba2142da5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/npc/NpcManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/npc/NpcManager.java @@ -121,9 +121,10 @@ public class NpcManager extends MiniPlugin public void Help(Player caller, String message) { UtilPlayer.message(caller, F.main(_moduleName, "Commands List:")); - UtilPlayer.message(caller, F.help("/npc add [radius] [adult] [name]", "Right click mob to attach npc.", Rank.DEVELOPER)); - UtilPlayer.message(caller, F.help("/npc del ", "Right click npc to delete", Rank.DEVELOPER)); - UtilPlayer.message(caller, F.help("/npc home", " Teleport npcs to home locations.", Rank.DEVELOPER)); + UtilPlayer.message(caller, F.help("/npc add [radius] [adult] [name]", "Create a new NPC.", Rank.DEVELOPER)); + UtilPlayer.message(caller, F.help("/npc del ", "Right click NPC to delete.", Rank.DEVELOPER)); + UtilPlayer.message(caller, F.help("/npc home", "Teleport NPCs to home locations.", Rank.DEVELOPER)); + UtilPlayer.message(caller, F.help("/npc clear", "Deletes all NPCs.", Rank.DEVELOPER)); if (message != null) UtilPlayer.message(caller, F.main(_moduleName, ChatColor.RED + message)); @@ -478,4 +479,28 @@ public class NpcManager extends MiniPlugin { return _npcDeletingPlayers; } + + public void ClearNpcs() throws SQLException + { + String serverType = _plugin.getClass().getSimpleName(); + + try (Connection connection = DBPool.getInstance().getConnection()) + { + DSL.using(connection) + .delete(Tables.npcs) + .where(Tables.npcs.server.eq(serverType)) + .execute(); + + for (World world : Bukkit.getWorlds()) + { + for (LivingEntity entity : world.getEntitiesByClass(LivingEntity.class)) + { + if (isNpc(entity)) + entity.remove(); + } + } + + _npcs.clear(); + } + } }