Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 28, 2024
1 parent 30d3fad commit df814cc
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 20 deletions.
41 changes: 41 additions & 0 deletions src/main/java/world/bentobox/bentobox/api/hooks/NPCHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package world.bentobox.bentobox.api.hooks;

import java.util.List;
import java.util.Map;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import de.oliver.fancynpcs.api.Npc;
import lol.pyr.znpcsplus.api.npc.NpcEntry;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;

/**
* NPC Hooks
* @author tastybento
* @since 3.2.0
*/
public abstract class NPCHook extends Hook {

protected NPCHook(@NonNull String pluginName, @NonNull Material icon) {
super(pluginName, icon);
}

public abstract boolean spawnNpc(String yaml, Location pos) throws InvalidConfigurationException;

public abstract Map<? extends Vector, ? extends List<BlueprintEntity>> getNpcsInArea(World world,
List<Vector> vectorsToCopy, @Nullable Vector origin);

/**
* Remove all NPCs in chunk
* @param chunk chunk
*/
public abstract void removeNPCsInChunk(Chunk chunk);

}
29 changes: 26 additions & 3 deletions src/main/java/world/bentobox/bentobox/hooks/FancyNpcsHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
Expand All @@ -29,9 +30,10 @@
import de.oliver.fancynpcs.api.actions.NpcAction;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
import de.oliver.fancynpcs.api.utils.SkinFetcher;
import lol.pyr.znpcsplus.api.npc.NpcEntry;
import net.kyori.adventure.text.format.NamedTextColor;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.hooks.NPCHook;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;

/**
Expand All @@ -40,13 +42,13 @@
* @author tastybento
* @since 3.1.0
*/
public class FancyNpcsHook extends Hook {
public class FancyNpcsHook extends NPCHook {

public FancyNpcsHook() {
super("FancyNpcs", Material.PLAYER_HEAD);
}

public String serializeNPC(Npc npc, Vector origin) {
String serializeNPC(Npc npc, Vector origin) {
if (npc == null) {
throw new IllegalArgumentException("NPC cannot be null.");
}
Expand Down Expand Up @@ -265,6 +267,26 @@ public String getFailureCause() {
return null; // The hook process shouldn't fail
}

/**
* Return all NPCs in the chunk
* @param chunk chunk
* @return list of NPCs
*/
public List<Npc> getNPCsInChunk(Chunk chunk) {
return FancyNpcsPlugin.get().getNpcManager().getAllNpcs().stream()
.filter(npc -> npc.getData().getLocation().getChunk().equals(chunk)).toList();
}

/**
* Remove all NPCs in chunk
* @param chunk chunk
*/
@Override
public void removeNPCsInChunk(Chunk chunk) {
getNPCsInChunk(chunk).forEach(npc -> npc.removeForAll());
}

@Override
public Map<? extends Vector, ? extends List<BlueprintEntity>> getNpcsInArea(World world, List<Vector> vectorsToCopy,
@Nullable Vector origin) {
Map<Vector, List<BlueprintEntity>> bpEntities = new HashMap<>();
Expand All @@ -290,4 +312,5 @@ public String getFailureCause() {
}
return bpEntities;
}

}
34 changes: 31 additions & 3 deletions src/main/java/world/bentobox/bentobox/hooks/ZNPCsPlusHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
Expand All @@ -17,7 +18,7 @@
import lol.pyr.znpcsplus.api.npc.NpcEntry;
import lol.pyr.znpcsplus.util.NpcLocation;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.hooks.NPCHook;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.util.Util;

Expand All @@ -27,21 +28,28 @@
* @author tastybento
* @since 3.2.0
*/
public class ZNPCsPlusHook extends Hook {
public class ZNPCsPlusHook extends NPCHook {

private static final String VERSION = "2.0.0-SNAPSHOT"; // Minimum version required

public ZNPCsPlusHook() {
super("ZNPCsPlus", Material.PLAYER_HEAD);
}

public String serializeNPC(NpcEntry entry, Vector origin) {
/**
* Serialize a NpcEntry
* @param entry NPC entry
* @param origin origin point of blueprint
* @return string serializing the NPC Entry
*/
String serializeNPC(NpcEntry entry, Vector origin) {
String result = NpcApiProvider.get().getNpcSerializerRegistry().getSerializer(YamlConfiguration.class)
.serialize(entry)
.saveToString();
return result;
}

@Override
public boolean spawnNpc(String yaml, Location pos) throws InvalidConfigurationException {
YamlConfiguration yaml2 = new YamlConfiguration();
yaml2.loadFromString(yaml);
Expand Down Expand Up @@ -75,6 +83,7 @@ public String getFailureCause() {
+ this.getPlugin().getDescription().getVersion();
}

@Override
public Map<? extends Vector, ? extends List<BlueprintEntity>> getNpcsInArea(World world, List<Vector> vectorsToCopy,
@Nullable Vector origin) {
Map<Vector, List<BlueprintEntity>> bpEntities = new HashMap<>();
Expand All @@ -101,4 +110,23 @@ public String getFailureCause() {
}
return bpEntities;
}

/**
* Get a list of all the NPC IDs in this chunk
* @param chunk chunk
* @return list of NPC IDs
*/
public List<String> getNPCsInChunk(Chunk chunk) {
return NpcApiProvider.get().getNpcRegistry().getAll().stream()
.filter(npc -> npc.getNpc().getWorld().equals(chunk.getWorld())) // Only NPCs in this world
.filter(npc -> npc.getNpc().getLocation().toBukkitLocation(chunk.getWorld()).getChunk().equals(chunk)) // Only in this chunk
.map(npc -> npc.getId()) // IDs
.toList();
}

@Override
public void removeNPCsInChunk(Chunk chunk) {
getNPCsInChunk(chunk).forEach(NpcApiProvider.get().getNpcRegistry()::delete);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.hooks.FancyNpcsHook;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.SlimefunHook;
import world.bentobox.bentobox.hooks.ZNPCsPlusHook;
import world.bentobox.bentobox.util.MyBiomeGrid;

/**
Expand All @@ -56,9 +58,18 @@
public abstract class CopyWorldRegenerator implements WorldRegenerator {

private final BentoBox plugin;
private Optional<FancyNpcsHook> npc;
private Optional<ZNPCsPlusHook> znpc;

protected CopyWorldRegenerator() {
this.plugin = BentoBox.getInstance();
// Fancy NPCs Hook
npc = plugin.getHooks().getHook("FancyNpcs").filter(FancyNpcsHook.class::isInstance)
.map(FancyNpcsHook.class::cast);
// ZNPCs Plus Hook
znpc = plugin.getHooks().getHook("ZNPCsPlus").filter(ZNPCsPlusHook.class::isInstance)
.map(ZNPCsPlusHook.class::cast);

}

/**
Expand Down Expand Up @@ -179,11 +190,20 @@ private CompletableFuture<Void> cleanChunk(CompletableFuture<Chunk> chunkFuture,
);

// Similarly, when the chunk is loaded, remove all the entities in the chunk apart from players
CompletableFuture<Void> entitiesFuture = chunkFuture.thenAccept(chunk ->
// Remove all entities in chunk, including any dropped items as a result of clearing the blocks above
Arrays.stream(chunk.getEntities())
.filter(e -> !(e instanceof Player) && di.inBounds(e.getLocation().getBlockX(), e.getLocation().getBlockZ()))
.forEach(Entity::remove));
CompletableFuture<Void> entitiesFuture = chunkFuture.thenAccept(chunk -> {
// Remove all entities in chunk, including any dropped items as a result of clearing the blocks above
Arrays.stream(chunk.getEntities())
.filter(e -> !(e instanceof Player)
&& di.inBounds(e.getLocation().getBlockX(), e.getLocation().getBlockZ()))
.forEach(Entity::remove);
// Remove any NPCs
// Fancy NPCs Hook
npc.ifPresent(hook -> hook.removeNPCsInChunk(chunk));
// ZNPCs Plus Hook
znpc.ifPresent(hook -> hook.removeNPCsInChunk(chunk));

});

return CompletableFuture.allOf(invFuture, entitiesFuture);
}

Expand Down Expand Up @@ -310,6 +330,10 @@ private void writeSign(Sign fromSign, Sign toSign, Side side) {

public CompletableFuture<Void> regenerateSimple(GameModeAddon gm, IslandDeletion di, World world) {
CompletableFuture<Void> bigFuture = new CompletableFuture<>();
if (world == null) {
bigFuture.complete(null);
return bigFuture;
}
new BukkitRunnable() {
private int chunkX = di.getMinXChunk();
private int chunkZ = di.getMinZChunk();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AdminBlueprintCopyCommandTest {

@Mock
private BentoBox plugin;
@Mock
private AdminBlueprintCommand ac;
@Mock
private GameModeAddon addon;
@Mock
private User user;
@Mock

private BlueprintClipboard clip;
private UUID uuid = UUID.randomUUID();
@Mock
Expand All @@ -64,10 +66,12 @@ public class AdminBlueprintCopyCommandTest {
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
// Set up plugin // Set up plugin
// Required for NamespacedKey
when(plugin.getName()).thenReturn("BentoBox");
Whitebox.setInternalState(BentoBox.class, "instance", plugin);

clip = mock(BlueprintClipboard.class);
// Blueprints Manager
when(plugin.getBlueprintsManager()).thenReturn(bm);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public static void beforeClass() {

@Before
public void setUp() throws Exception {
// Required for NamespacedKey
when(plugin.getName()).thenReturn("BentoBox");
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AdminBlueprintSaveCommandTest {

@Mock
private BentoBox plugin;
private AdminBlueprintSaveCommand absc;
@Mock
private AdminBlueprintCommand ac;
Expand All @@ -76,8 +78,8 @@ public static void beforeClass() {

@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
// Required for NamespacedKey
when(plugin.getName()).thenReturn("BentoBox");
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Hooks
HooksManager hooksManager = mock(HooksManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class BlueprintClipboardTest {
*/
@Before
public void setUp() throws Exception {
// Required for NamespacedKey
when(plugin.getName()).thenReturn("BentoBox");
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class BlueprintClipboardManagerTest {

@Mock
private BentoBox plugin;
@Mock

private BlueprintClipboard clipboard;

private File blueprintFolder;
Expand Down Expand Up @@ -129,15 +129,19 @@ private void zip(File targetFile) throws IOException {
*/
@Before
public void setUp() throws Exception {
// Set up plugin
// Required for NamespacedKey
when(plugin.getName()).thenReturn("BentoBox");
Whitebox.setInternalState(BentoBox.class, "instance", plugin);

clipboard = mock(BlueprintClipboard.class);

server = ServerMocks.newServer();
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);

blueprintFolder = new File("blueprints");
// Clear any residual files
tearDown();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Hooks
HooksManager hooksManager = mock(HooksManager.class);
when(hooksManager.getHook(anyString())).thenReturn(Optional.empty());
Expand Down

0 comments on commit df814cc

Please sign in to comment.