fancynpcs: Fix tests

This commit is contained in:
Oliver
2025-09-02 21:30:02 +02:00
parent 0dd4af4811
commit 19bad7dabf
6 changed files with 155 additions and 86 deletions

View File

@@ -0,0 +1,19 @@
package de.oliver.plugintests.utils;
import java.util.concurrent.*;
public class Delay {
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(10);
public static void delay(Runnable runnable) {
ScheduledFuture<?> f = EXECUTOR.schedule(runnable, 10, TimeUnit.MILLISECONDS);
try {
f.get(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
System.out.println("Delay interrupted: " + e.getMessage());
}
}
}

View File

@@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.List;
import static de.oliver.plugintests.Expectable.expect;
import static de.oliver.plugintests.utils.Delay.delay;
public class ActionCMDTest {
@@ -47,9 +48,11 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " add " + actionType + " " + actionValue)).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(1);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(1);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
});
}
@FPTest(name = "Add action before")
@@ -63,11 +66,13 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " add_before 1 " + actionType + " " + actionValue)).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction.value());
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction.value());
});
}
@FPTest(name = "Add action after")
@@ -81,11 +86,13 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " add_after 1 " + actionType + " " + actionValue)).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(actionValue);
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(actionValue);
});
}
@FPTest(name = "Set action")
@@ -99,9 +106,11 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " set 1 " + actionType + " " + actionValue)).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(1);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(1);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(actionType);
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(actionValue);
});
}
@FPTest(name = "Remove action")
@@ -112,7 +121,9 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " remove 1")).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(0);
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(0);
});
}
@FPTest(name = "Move action up")
@@ -124,11 +135,13 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " move_up 2")).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction2.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction2.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction1.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction1.value());
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction2.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction2.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction1.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction1.value());
});
}
@FPTest(name = "Move action down")
@@ -140,11 +153,13 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " move_down 1")).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction2.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction2.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction1.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction1.value());
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(2);
expect(npc.getData().getActions(actionTrigger).getFirst().action().getName()).toEqual(existingAction2.action().getName());
expect(npc.getData().getActions(actionTrigger).getFirst().value()).toEqual(existingAction2.value());
expect(npc.getData().getActions(actionTrigger).getLast().action().getName()).toEqual(existingAction1.action().getName());
expect(npc.getData().getActions(actionTrigger).getLast().value()).toEqual(existingAction1.value());
});
}
@FPTest(name = "Clear actions")
@@ -155,7 +170,9 @@ public class ActionCMDTest {
expect(player.performCommand("npc action " + npcName + " " + actionTrigger + " clear")).toBe(true);
expect(npc.getData().getActions(actionTrigger).size()).toEqual(0);
delay(() -> {
expect(npc.getData().getActions(actionTrigger).size()).toEqual(0);
});
}
}

View File

@@ -13,6 +13,7 @@ import org.bukkit.entity.Player;
import java.util.UUID;
import static de.oliver.plugintests.Expectable.expect;
import static de.oliver.plugintests.utils.Delay.delay;
public class CreateCMDTest {
@@ -43,103 +44,121 @@ public class CreateCMDTest {
public void createNpc(Player player) {
expect(player.performCommand("npc create " + npcName)).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getData().getName()).toEqual(npcName);
expect(createdNpc.getData().getType()).toEqual(EntityType.PLAYER);
expect(createdNpc.getData().getLocation()).toBeDefined();
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(player.getWorld().getName());
expect(createdNpc.getData().getCreator()).toEqual(player.getUniqueId());
expect(createdNpc.getData().getName()).toEqual(npcName);
expect(createdNpc.getData().getType()).toEqual(EntityType.PLAYER);
expect(createdNpc.getData().getLocation()).toBeDefined();
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(player.getWorld().getName());
expect(createdNpc.getData().getCreator()).toEqual(player.getUniqueId());
});
}
@FPTest(name = "Create npc with type")
public void createNpcWithType(Player player) {
expect(player.performCommand("npc create " + npcName + " --type PIG")).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getData().getType()).toEqual(EntityType.PIG);
expect(createdNpc.getData().getType()).toEqual(EntityType.PIG);
});
}
@FPTest(name = "Create npc with location")
public void createNpcWithLocation(Player player) {
expect(player.performCommand("npc create " + npcName + " --location 12 154 842")).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getData().getLocation().x()).toEqual(12d);
expect(createdNpc.getData().getLocation().y()).toEqual(154d);
expect(createdNpc.getData().getLocation().z()).toEqual(842d);
expect(createdNpc.getData().getLocation().x()).toEqual(12d);
expect(createdNpc.getData().getLocation().y()).toEqual(154d);
expect(createdNpc.getData().getLocation().z()).toEqual(842d);
});
}
@FPTest(name = "Create npc with world")
public void createNpcWithWorld(Player player) {
String worldName = "world_the_nether";
if (Bukkit.getWorld(worldName) == null) {
String worldName;
if (Bukkit.getWorld("world_the_nether") == null) {
worldName = "world";
} else {
worldName = "world_the_nether";
}
expect(player.performCommand("npc create " + npcName + " --world " + worldName)).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(worldName);
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(worldName);
});
}
@FPTest(name = "Create npc with invalid name")
public void createNpcWithInvalidName(Player player) {
expect(player.performCommand("npc create " + "invalid.name")).toBe(true);
createdNpc = NPC_MANAGER.getNpc("invalid.name");
expect(createdNpc).toBeNull();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc("invalid.name");
expect(createdNpc).toBeNull();
});
}
@FPTest(name = "Create npc with existing name")
public void createNpcWithExistingName(Player player) {
expect(player.performCommand("npc create " + npcName)).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(player.performCommand("npc create " + npcName)).toBe(true);
expect(player.performCommand("npc create " + npcName)).toBe(true);
Npc existingNpc = NPC_MANAGER.getNpc(npcName);
expect(existingNpc).toBeDefined();
Npc existingNpc = NPC_MANAGER.getNpc(npcName);
expect(existingNpc).toBeDefined();
expect(existingNpc.getEntityId()).toBeGreaterThan(-1);
expect(existingNpc).toEqual(createdNpc);
expect(existingNpc.getEntityId()).toBeGreaterThan(-1);
expect(existingNpc).toEqual(createdNpc);
});
}
@FPTest(name = "Create npc with all flags")
public void createNpcWithAllFlags(Player player) {
String worldName = "world_the_nether";
if (Bukkit.getWorld(worldName) == null) {
String worldName;
if (Bukkit.getWorld("world_the_nether") == null) {
worldName = "world";
} else {
worldName = "world_the_nether";
}
expect(player.performCommand("npc create " + npcName + " --type COW --location 137 131 -571 --world " + worldName)).toBe(true);
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
delay(() -> {
createdNpc = NPC_MANAGER.getNpc(npcName);
expect(createdNpc).toBeDefined();
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getEntityId()).toBeGreaterThan(-1);
expect(createdNpc.getData().getType()).toEqual(EntityType.COW);
expect(createdNpc.getData().getLocation().x()).toEqual(137d);
expect(createdNpc.getData().getLocation().y()).toEqual(131d);
expect(createdNpc.getData().getLocation().z()).toEqual(-571d);
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(worldName);
expect(createdNpc.getData().getType()).toEqual(EntityType.COW);
expect(createdNpc.getData().getLocation().x()).toEqual(137d);
expect(createdNpc.getData().getLocation().y()).toEqual(131d);
expect(createdNpc.getData().getLocation().z()).toEqual(-571d);
expect(createdNpc.getData().getLocation().getWorld().getName()).toEqual(worldName);
});
}
}

View File

@@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import java.util.List;
import static de.oliver.plugintests.Expectable.expect;
import static de.oliver.plugintests.utils.Delay.delay;
public class DisplayNameCMDTest {
@@ -37,19 +38,28 @@ public class DisplayNameCMDTest {
public void setDisplayName(Player player) {
String displayName = "<red>Test Display Name";
expect(player.performCommand("npc displayname " + npcName + " " + displayName)).toBe(true);
expect(npc.getData().getDisplayName()).toEqual(displayName);
delay(() -> {
expect(npc.getData().getDisplayName()).toEqual(displayName);
});
}
@FPTest(name = "Set display name to none")
public void setDisplayNameToNone(Player player) {
expect(player.performCommand("npc displayname " + npcName + " @none")).toBe(true);
expect(npc.getData().getDisplayName()).toEqual("<empty>");
delay(() -> {
expect(npc.getData().getDisplayName()).toEqual("<empty>");
});
}
@FPTest(name = "Set display name to empty")
public void setDisplayNameToEmpty(Player player) {
expect(player.performCommand("npc displayname " + npcName + " <empty>")).toBe(true);
expect(npc.getData().getDisplayName()).toEqual("<empty>");
delay(() -> {
expect(npc.getData().getDisplayName()).toEqual("<empty>");
});
}
@FPTest(name = "Set display name with blocked command")
@@ -62,7 +72,9 @@ public class DisplayNameCMDTest {
expect(player.performCommand("npc displayname " + npcName + " <click:run_command:'/" + blockedCommand + "'>hello</click>")).toBe(true);
expect(npc.getData().getDisplayName()).toEqual(npcName);
delay(() -> {
expect(npc.getData().getDisplayName()).toEqual(npcName);
});
}
}

View File

@@ -8,6 +8,7 @@ import de.oliver.plugintests.annotations.FPTest;
import org.bukkit.entity.Player;
import static de.oliver.plugintests.Expectable.expect;
import static de.oliver.plugintests.utils.Delay.delay;
public class TurnToPlayerCMDTest {
@@ -33,7 +34,10 @@ public class TurnToPlayerCMDTest {
@FPTest(name = "Set turnToPlayer to true")
public void setTurnToPlayerToTrue(Player player) {
expect(player.performCommand("npc turn_to_player " + npcName + " true")).toBe(true);
expect(npc.getData().isTurnToPlayer()).toBe(true);
delay(() -> {
expect(npc.getData().isTurnToPlayer()).toBe(true);
});
}
@FPTest(name = "Set turnToPlayer to false")
@@ -41,7 +45,10 @@ public class TurnToPlayerCMDTest {
npc.getData().setTurnToPlayer(true);
expect(player.performCommand("npc turn_to_player " + npcName + " false")).toBe(true);
expect(npc.getData().isTurnToPlayer()).toBe(false);
delay(() -> {
expect(npc.getData().isTurnToPlayer()).toBe(false);
});
}
}

View File

@@ -9,6 +9,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import static de.oliver.plugintests.Expectable.expect;
import static de.oliver.plugintests.utils.Delay.delay;
public class TypeCMDTest {
@@ -34,15 +35,9 @@ public class TypeCMDTest {
@FPTest(name = "Set type to COW")
public void setTypeToCow(Player player) {
expect(player.performCommand("npc type " + npcName + " COW")).toBe(true);
expect(npc.getData().getType()).toBe(EntityType.COW);
}
@FPTest(name = "Set type to COW with showInTab")
public void setTypeToCowWithShowInTab(Player player) {
npc.getData().setShowInTab(true);
expect(player.performCommand("npc type " + npcName + " COW")).toBe(true);
expect(npc.getData().getType()).toBe(EntityType.COW);
expect(npc.getData().isShowInTab()).toBe(false);
delay(() -> {
expect(npc.getData().getType()).toBe(EntityType.COW);
});
}
}