Implemented admin delete_group command.
This commit is contained in:
		@@ -95,6 +95,7 @@
 | 
			
		||||
 | 
			
		||||
	<!-- ADMIN DELETE -->
 | 
			
		||||
	<admin command="admin_delete" accessLevel="100" />
 | 
			
		||||
	<admin command="admin_delete_group" accessLevel="100" confirmDlg="true" />
 | 
			
		||||
 | 
			
		||||
	<!-- ADMIN DESTROY ITEMS -->
 | 
			
		||||
	<admin command="admin_destroy_items" accessLevel="100" confirmDlg="true" />
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,11 @@
 | 
			
		||||
 */
 | 
			
		||||
package handlers.admincommandhandlers;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.l2jmobius.gameserver.data.SpawnTable;
 | 
			
		||||
import org.l2jmobius.gameserver.handler.AdminCommandHandler;
 | 
			
		||||
import org.l2jmobius.gameserver.handler.IAdminCommandHandler;
 | 
			
		||||
import org.l2jmobius.gameserver.instancemanager.DBSpawnManager;
 | 
			
		||||
import org.l2jmobius.gameserver.model.Spawn;
 | 
			
		||||
@@ -24,23 +28,32 @@ import org.l2jmobius.gameserver.model.World;
 | 
			
		||||
import org.l2jmobius.gameserver.model.WorldObject;
 | 
			
		||||
import org.l2jmobius.gameserver.model.actor.Npc;
 | 
			
		||||
import org.l2jmobius.gameserver.model.actor.Player;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.NpcSpawnTemplate;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.SpawnGroup;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.SpawnTemplate;
 | 
			
		||||
import org.l2jmobius.gameserver.model.zone.type.SpawnTerritory;
 | 
			
		||||
import org.l2jmobius.gameserver.util.BuilderUtil;
 | 
			
		||||
import org.l2jmobius.gameserver.util.Util;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class handles following admin commands: - delete = deletes target
 | 
			
		||||
 * @author Mobius
 | 
			
		||||
 */
 | 
			
		||||
public class AdminDelete implements IAdminCommandHandler
 | 
			
		||||
{
 | 
			
		||||
	private static final String[] ADMIN_COMMANDS =
 | 
			
		||||
	{
 | 
			
		||||
		"admin_delete"
 | 
			
		||||
		"admin_delete", // supports range parameter
 | 
			
		||||
		"admin_delete_group" // for territory spawns
 | 
			
		||||
	};
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean useAdminCommand(String command, Player activeChar)
 | 
			
		||||
	{
 | 
			
		||||
		if (command.startsWith("admin_delete"))
 | 
			
		||||
		if (command.contains("group"))
 | 
			
		||||
		{
 | 
			
		||||
			handleDeleteGroup(activeChar);
 | 
			
		||||
		}
 | 
			
		||||
		else if (command.startsWith("admin_delete"))
 | 
			
		||||
		{
 | 
			
		||||
			final String[] split = command.split(" ");
 | 
			
		||||
			handleDelete(activeChar, (split.length > 1) && Util.isDigit(split[1]) ? Integer.parseInt(split[1]) : 0);
 | 
			
		||||
@@ -48,48 +61,125 @@ public class AdminDelete implements IAdminCommandHandler
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void handleDelete(Player activeChar, int range)
 | 
			
		||||
	private void handleDelete(Player player, int range)
 | 
			
		||||
	{
 | 
			
		||||
		if (range > 0)
 | 
			
		||||
		{
 | 
			
		||||
			World.getInstance().forEachVisibleObjectInRange(activeChar, Npc.class, range, target ->
 | 
			
		||||
			{
 | 
			
		||||
				deleteNpc(activeChar, target);
 | 
			
		||||
			});
 | 
			
		||||
			World.getInstance().forEachVisibleObjectInRange(player, Npc.class, range, target -> deleteNpc(player, target));
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		final WorldObject obj = activeChar.getTarget();
 | 
			
		||||
		final WorldObject obj = player.getTarget();
 | 
			
		||||
		if (obj instanceof Npc)
 | 
			
		||||
		{
 | 
			
		||||
			deleteNpc(activeChar, (Npc) obj);
 | 
			
		||||
			deleteNpc(player, (Npc) obj);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			BuilderUtil.sendSysMessage(activeChar, "Incorrect target.");
 | 
			
		||||
			BuilderUtil.sendSysMessage(player, "Incorrect target.");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void deleteNpc(Player activeChar, Npc target)
 | 
			
		||||
	private void handleDeleteGroup(Player player)
 | 
			
		||||
	{
 | 
			
		||||
		final WorldObject obj = player.getTarget();
 | 
			
		||||
		if (obj instanceof Npc)
 | 
			
		||||
		{
 | 
			
		||||
			deleteGroup(player, (Npc) obj);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			BuilderUtil.sendSysMessage(player, "Incorrect target.");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void deleteNpc(Player player, Npc target)
 | 
			
		||||
	{
 | 
			
		||||
		target.deleteMe();
 | 
			
		||||
		
 | 
			
		||||
		final Spawn spawn = target.getSpawn();
 | 
			
		||||
		if (spawn != null)
 | 
			
		||||
		{
 | 
			
		||||
			spawn.stopRespawn();
 | 
			
		||||
			
 | 
			
		||||
			if (DBSpawnManager.getInstance().isDefined(spawn.getId()))
 | 
			
		||||
			final NpcSpawnTemplate npcSpawnTemplate = spawn.getNpcSpawnTemplate();
 | 
			
		||||
			final SpawnGroup group = npcSpawnTemplate != null ? npcSpawnTemplate.getGroup() : null;
 | 
			
		||||
			List<SpawnTerritory> territories = group != null ? group.getTerritories() : Collections.emptyList();
 | 
			
		||||
			if (territories.isEmpty())
 | 
			
		||||
			{
 | 
			
		||||
				DBSpawnManager.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				final SpawnTemplate spawnTemplate = npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnTemplate() : null;
 | 
			
		||||
				if (spawnTemplate != null)
 | 
			
		||||
				{
 | 
			
		||||
					territories = spawnTemplate.getTerritories();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if (territories.isEmpty())
 | 
			
		||||
			{
 | 
			
		||||
				target.deleteMe();
 | 
			
		||||
				spawn.stopRespawn();
 | 
			
		||||
				if (DBSpawnManager.getInstance().isDefined(spawn.getId()))
 | 
			
		||||
				{
 | 
			
		||||
					DBSpawnManager.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				}
 | 
			
		||||
				else
 | 
			
		||||
				{
 | 
			
		||||
					SpawnTable.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				}
 | 
			
		||||
				BuilderUtil.sendSysMessage(player, "Deleted " + target.getName() + " from " + target.getObjectId() + ".");
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				SpawnTable.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				AdminCommandHandler.getInstance().useAdminCommand(player, AdminDelete.ADMIN_COMMANDS[1], true);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void deleteGroup(Player player, Npc target)
 | 
			
		||||
	{
 | 
			
		||||
		final Spawn spawn = target.getSpawn();
 | 
			
		||||
		if (spawn != null)
 | 
			
		||||
		{
 | 
			
		||||
			final NpcSpawnTemplate npcSpawnTemplate = spawn.getNpcSpawnTemplate();
 | 
			
		||||
			final SpawnGroup group = npcSpawnTemplate != null ? npcSpawnTemplate.getGroup() : null;
 | 
			
		||||
			List<SpawnTerritory> territories = group != null ? group.getTerritories() : Collections.emptyList();
 | 
			
		||||
			boolean simpleTerritory = false;
 | 
			
		||||
			if (territories.isEmpty())
 | 
			
		||||
			{
 | 
			
		||||
				final SpawnTemplate spawnTemplate = npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnTemplate() : null;
 | 
			
		||||
				if (spawnTemplate != null)
 | 
			
		||||
				{
 | 
			
		||||
					territories = spawnTemplate.getTerritories();
 | 
			
		||||
					simpleTerritory = true;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if (territories.isEmpty())
 | 
			
		||||
			{
 | 
			
		||||
				BuilderUtil.sendSysMessage(player, "Incorrect target.");
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				target.deleteMe();
 | 
			
		||||
				spawn.stopRespawn();
 | 
			
		||||
				if (DBSpawnManager.getInstance().isDefined(spawn.getId()))
 | 
			
		||||
				{
 | 
			
		||||
					DBSpawnManager.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				}
 | 
			
		||||
				else
 | 
			
		||||
				{
 | 
			
		||||
					SpawnTable.getInstance().deleteSpawn(spawn, true);
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
				if (group != null)
 | 
			
		||||
				{
 | 
			
		||||
					for (NpcSpawnTemplate template : group.getSpawns())
 | 
			
		||||
					{
 | 
			
		||||
						template.despawn();
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				else if (simpleTerritory && (npcSpawnTemplate != null))
 | 
			
		||||
				{
 | 
			
		||||
					npcSpawnTemplate.despawn();
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
				BuilderUtil.sendSysMessage(player, "Deleted " + target.getName() + " group from " + target.getObjectId() + ".");
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		BuilderUtil.sendSysMessage(activeChar, "Deleted " + target.getName() + " from " + target.getObjectId() + ".");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ import java.io.File;
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
import java.io.FileWriter;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.concurrent.ConcurrentHashMap;
 | 
			
		||||
@@ -33,6 +34,9 @@ import org.l2jmobius.gameserver.data.xml.NpcData;
 | 
			
		||||
import org.l2jmobius.gameserver.model.Spawn;
 | 
			
		||||
import org.l2jmobius.gameserver.model.World;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.NpcSpawnTemplate;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.SpawnGroup;
 | 
			
		||||
import org.l2jmobius.gameserver.model.spawns.SpawnTemplate;
 | 
			
		||||
import org.l2jmobius.gameserver.model.zone.type.SpawnTerritory;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Spawn data retriever.
 | 
			
		||||
@@ -202,50 +206,104 @@ public class SpawnTable
 | 
			
		||||
			{
 | 
			
		||||
				final BufferedReader reader = new BufferedReader(new FileReader(spawnFile));
 | 
			
		||||
				final BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
 | 
			
		||||
				final String spawnId = String.valueOf(spawn.getId());
 | 
			
		||||
				final String spawnX = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getX() : spawn.getX());
 | 
			
		||||
				final String spawnY = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getY() : spawn.getY());
 | 
			
		||||
				final String spawnZ = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getZ() : spawn.getZ());
 | 
			
		||||
				
 | 
			
		||||
				boolean found = false; // in XML you can have more than one spawn with same coords
 | 
			
		||||
				boolean isMultiLine = false; // in case spawn has more stats
 | 
			
		||||
				boolean lastLineFound = false; // used to check for empty file
 | 
			
		||||
				int lineCount = 0;
 | 
			
		||||
				String currentLine;
 | 
			
		||||
				while ((currentLine = reader.readLine()) != null)
 | 
			
		||||
				
 | 
			
		||||
				final SpawnGroup group = npcSpawnTemplate != null ? npcSpawnTemplate.getGroup() : null;
 | 
			
		||||
				List<SpawnTerritory> territories = group != null ? group.getTerritories() : Collections.emptyList();
 | 
			
		||||
				boolean simpleTerritory = false;
 | 
			
		||||
				if (territories.isEmpty())
 | 
			
		||||
				{
 | 
			
		||||
					if (!found)
 | 
			
		||||
					final SpawnTemplate spawnTemplate = npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnTemplate() : null;
 | 
			
		||||
					if (spawnTemplate != null)
 | 
			
		||||
					{
 | 
			
		||||
						if (isMultiLine)
 | 
			
		||||
						{
 | 
			
		||||
							if (currentLine.contains("</npc>"))
 | 
			
		||||
							{
 | 
			
		||||
								found = true;
 | 
			
		||||
							}
 | 
			
		||||
							continue;
 | 
			
		||||
						}
 | 
			
		||||
						if (currentLine.contains(spawnId) && currentLine.contains(spawnX) && currentLine.contains(spawnY) && currentLine.contains(spawnZ))
 | 
			
		||||
						{
 | 
			
		||||
							if (!currentLine.contains("/>") && !currentLine.contains("</npc>"))
 | 
			
		||||
							{
 | 
			
		||||
								isMultiLine = true;
 | 
			
		||||
							}
 | 
			
		||||
							else
 | 
			
		||||
							{
 | 
			
		||||
								found = true;
 | 
			
		||||
							}
 | 
			
		||||
							continue;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
					writer.write(currentLine + Config.EOL);
 | 
			
		||||
					if (currentLine.contains("</list>"))
 | 
			
		||||
					{
 | 
			
		||||
						lastLineFound = true;
 | 
			
		||||
					}
 | 
			
		||||
					if (!lastLineFound)
 | 
			
		||||
					{
 | 
			
		||||
						lineCount++;
 | 
			
		||||
						territories = spawnTemplate.getTerritories();
 | 
			
		||||
						simpleTerritory = true;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
				if (territories.isEmpty())
 | 
			
		||||
				{
 | 
			
		||||
					final String spawnId = String.valueOf(spawn.getId());
 | 
			
		||||
					final String spawnX = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getX() : spawn.getX());
 | 
			
		||||
					final String spawnY = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getY() : spawn.getY());
 | 
			
		||||
					final String spawnZ = String.valueOf(npcSpawnTemplate != null ? npcSpawnTemplate.getSpawnLocation().getZ() : spawn.getZ());
 | 
			
		||||
					
 | 
			
		||||
					while ((currentLine = reader.readLine()) != null)
 | 
			
		||||
					{
 | 
			
		||||
						if (!found)
 | 
			
		||||
						{
 | 
			
		||||
							if (isMultiLine)
 | 
			
		||||
							{
 | 
			
		||||
								if (currentLine.contains("</npc>"))
 | 
			
		||||
								{
 | 
			
		||||
									found = true;
 | 
			
		||||
								}
 | 
			
		||||
								continue;
 | 
			
		||||
							}
 | 
			
		||||
							if (currentLine.contains(spawnId) && currentLine.contains(spawnX) && currentLine.contains(spawnY) && currentLine.contains(spawnZ))
 | 
			
		||||
							{
 | 
			
		||||
								if (!currentLine.contains("/>") && !currentLine.contains("</npc>"))
 | 
			
		||||
								{
 | 
			
		||||
									isMultiLine = true;
 | 
			
		||||
								}
 | 
			
		||||
								else
 | 
			
		||||
								{
 | 
			
		||||
									found = true;
 | 
			
		||||
								}
 | 
			
		||||
								continue;
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						writer.write(currentLine + Config.EOL);
 | 
			
		||||
						if (currentLine.contains("</list>"))
 | 
			
		||||
						{
 | 
			
		||||
							lastLineFound = true;
 | 
			
		||||
						}
 | 
			
		||||
						if (!lastLineFound)
 | 
			
		||||
						{
 | 
			
		||||
							lineCount++;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				else
 | 
			
		||||
				{
 | 
			
		||||
					SEARCH: while ((currentLine = reader.readLine()) != null)
 | 
			
		||||
					{
 | 
			
		||||
						if (!found)
 | 
			
		||||
						{
 | 
			
		||||
							if (isMultiLine)
 | 
			
		||||
							{
 | 
			
		||||
								if (currentLine.contains("</group>") || (simpleTerritory && currentLine.contains("<territories>")))
 | 
			
		||||
								{
 | 
			
		||||
									found = true;
 | 
			
		||||
								}
 | 
			
		||||
								continue;
 | 
			
		||||
							}
 | 
			
		||||
							for (SpawnTerritory territory : territories)
 | 
			
		||||
							{
 | 
			
		||||
								if (currentLine.contains('"' + territory.getName() + '"'))
 | 
			
		||||
								{
 | 
			
		||||
									isMultiLine = true;
 | 
			
		||||
									continue SEARCH;
 | 
			
		||||
								}
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						writer.write(currentLine + Config.EOL);
 | 
			
		||||
						if (currentLine.contains("</list>"))
 | 
			
		||||
						{
 | 
			
		||||
							lastLineFound = true;
 | 
			
		||||
						}
 | 
			
		||||
						if (!lastLineFound)
 | 
			
		||||
						{
 | 
			
		||||
							lineCount++;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
				writer.close();
 | 
			
		||||
				reader.close();
 | 
			
		||||
				spawnFile.delete();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user