diff --git a/tests/utilities/NPCTestHelper.java b/tests/utilities/NPCTestHelper.java new file mode 100644 index 0000000000..df38e9e03d --- /dev/null +++ b/tests/utilities/NPCTestHelper.java @@ -0,0 +1,79 @@ +/*************************************************************************** + * Copyright © 2023 - Arianne * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package utilities; + +import java.util.ArrayList; +import java.util.List; + +import games.stendhal.server.core.engine.StendhalRPZone; +import games.stendhal.server.core.engine.StendhalRPWorld; +import games.stendhal.server.entity.npc.NPC; +import games.stendhal.server.maps.MockStendlRPWorld; +import marauroa.common.game.IRPZone; +import marauroa.common.game.RPObject; + + +public class NPCTestHelper { + + private static final StendhalRPWorld world = MockStendlRPWorld.get(); + + + /** + * Retrieves all NPCs currently in world. + */ + public static List getAllNPCs() { + final List npcs = new ArrayList<>(); + for (final IRPZone zone: world) { + for (final RPObject obj: zone) { + if (obj instanceof NPC) { + npcs.add((NPC) obj); + } + } + } + return npcs; + } + + /** + * Removes an NPC from world. + * + * @param npc + * NPC to be removed. + * @return + * true if NPC was removed. + */ + public static boolean removeNPC(final NPC npc) { + if (npc == null) { + return true; + } + final StendhalRPZone npczone = npc.getZone(); + if (npczone == null) { + return true; + } + npczone.remove(npc); + return !npczone.getNPCList().contains(npc); + } + + /** + * Removes all NPCs from world; + * + * @return + * true if world contains no NPCs. + */ + public static boolean removeAllNPCs() { + final List npcs = getAllNPCs(); + boolean removed = true; + for (final NPC npc: npcs) { + removed = removed && removeNPC(npc); + } + return removed && getAllNPCs().isEmpty(); + } +} diff --git a/tests/utilities/PlayerTestHelper.java b/tests/utilities/PlayerTestHelper.java index b90590eda4..3b703322eb 100644 --- a/tests/utilities/PlayerTestHelper.java +++ b/tests/utilities/PlayerTestHelper.java @@ -282,8 +282,8 @@ public abstract class PlayerTestHelper { * * @param npcName */ - public static void removeNPC(final String npcName) { - SingletonRepository.getNPCList().remove(npcName); + public static boolean removeNPC(final String npcName) { + return SpeakerNPCTestHelper.removeSpeakerNPC(npcName); } public static void addEmptySlots(final Player player) { diff --git a/tests/utilities/SpeakerNPCTestHelper.java b/tests/utilities/SpeakerNPCTestHelper.java index 322661b3b4..64498ada7f 100644 --- a/tests/utilities/SpeakerNPCTestHelper.java +++ b/tests/utilities/SpeakerNPCTestHelper.java @@ -17,6 +17,7 @@ import java.util.LinkedList; import java.util.List; import games.stendhal.common.constants.Events; +import games.stendhal.server.core.engine.SingletonRepository; import games.stendhal.server.entity.npc.SpeakerNPC; import marauroa.common.game.RPEvent; @@ -27,7 +28,7 @@ import marauroa.common.game.RPEvent; * avoid database access * */ -public abstract class SpeakerNPCTestHelper { +public abstract class SpeakerNPCTestHelper extends NPCTestHelper { public static SpeakerNPC createSpeakerNPC() { return createSpeakerNPC("bob"); @@ -38,6 +39,18 @@ public abstract class SpeakerNPCTestHelper { return new SpeakerNPC(name); } + public static SpeakerNPC getSpeakerNPC(final String name) { + return SingletonRepository.getNPCList().get(name); + } + + public static boolean removeSpeakerNPC(final SpeakerNPC npc) { + return removeNPC(npc); + } + + public static boolean removeSpeakerNPC(final String name) { + return removeSpeakerNPC(getSpeakerNPC(name)); + } + /** * Query the events for public visible text messages. *