Add NPCTestHelper class

This commit is contained in:
Jordan Irwin 2023-01-29 21:46:50 -08:00
parent 9e2310cbb6
commit ca1102ae2f
3 changed files with 95 additions and 3 deletions

View file

@ -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<NPC> getAllNPCs() {
final List<NPC> 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
* <code>true</code> 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
* <code>true</code> if world contains no NPCs.
*/
public static boolean removeAllNPCs() {
final List<NPC> npcs = getAllNPCs();
boolean removed = true;
for (final NPC npc: npcs) {
removed = removed && removeNPC(npc);
}
return removed && getAllNPCs().isEmpty();
}
}

View file

@ -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) {

View file

@ -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.
*