diff --git a/src/main/java/net/theevilreaper/aves/map/BaseMap.java b/src/main/java/net/theevilreaper/aves/map/BaseMap.java index b5c2397d..588dfe91 100644 --- a/src/main/java/net/theevilreaper/aves/map/BaseMap.java +++ b/src/main/java/net/theevilreaper/aves/map/BaseMap.java @@ -1,9 +1,7 @@ package net.theevilreaper.aves.map; import net.minestom.server.coordinate.Pos; -import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -15,36 +13,10 @@ * It should be extended to add more values and methods to the map structure. * * @author theEvilReaper - * @version 1.0.5 + * @version 1.1.0 * @since 1.0.0 */ -public class BaseMap { - - private String name; - private String[] builders; - private Pos spawn; - - /** - * Empty constructor to create a new instance of the {@link BaseMap} with any values. - * Sometimes such constructors are required for serialization or deserialization. - */ - public BaseMap() { - } - - /** - * Creates a new reference from the {@link BaseMap}. - * It requires all values that are needed to create a map. - * - * @param name the name from the map - * @param builders the builders from the map - * @param spawn the spawn location from the map - */ - public BaseMap(@NotNull String name, Pos spawn, String... builders) { - Check.argCondition(name.trim().isEmpty(), "The name can not be null or empty"); - this.name = name; - this.builders = builders; - this.spawn = spawn; - } +public record BaseMap(String name, @Nullable Pos spawn, @Nullable String... builders) { /** * Creates a new instance of the {@link BaseMapBuilder} to build a new map. @@ -53,7 +25,7 @@ public BaseMap(@NotNull String name, Pos spawn, String... builders) { * @return a new instance of the {@link BaseMapBuilder} */ @Contract(pure = true) - public static @NotNull BaseMapBuilder builder() { + public static BaseMapBuilder builder() { return new BaseMapBuilder(); } @@ -64,107 +36,17 @@ public BaseMap(@NotNull String name, Pos spawn, String... builders) { * @return a new instance of the {@link BaseMapBuilder} with the given values */ @Contract(value = "_ -> new", pure = true) - public static @NotNull BaseMapBuilder builder(@NotNull BaseMap baseMap) { + public static BaseMapBuilder builder(BaseMap baseMap) { return new BaseMapBuilder(baseMap); } - /** - * Overrides the equal the method from the object class. - * - * @param o the object to compare - * @return true if the given object is the same otherwise false - */ - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BaseMap baseMap = (BaseMap) o; - return name.equals(baseMap.name); - } - - /** - * Returns a hash value from some data that are provided by the object. - * In general, the hash relies on the unique data. - * For the basic implementation that is only the name of the map. - * - * @return a created hash code - */ - @Override - public int hashCode() { - return Objects.hash(name); - } - - /** - * Set the name of a map - * - * @param name the name to set - */ - public void setName(@NotNull String name) { - Check.argCondition(name.trim().isEmpty(), "The name can not be null or empty"); - this.name = name; - } - - /** - * Set the builder of a map - * - * @param builders the builder's to set - */ - public void setBuilders(@Nullable String... builders) { - this.builders = builders; - } - - /** - * Set the spawn location of a map - * - * @param spawn the spawn location to set - */ - public void setSpawn(@Nullable Pos spawn) { - this.spawn = spawn; - } - - /** - * Checks if the map has a spawn location. - * - * @return true if the map has a spawn location otherwise false - */ - public boolean hasSpawn() { - return spawn != null; - } - - /** - * Returns the map name. - * - * @return the name of the map - */ - public @Nullable String getName() { - return name; - } - - /** - * Returns the builders. - * - * @return the builders of the map - */ - public @Nullable String[] getBuilders() { - return builders; - } - - /** - * Returns the spawn location - * - * @return the spawn of the map - */ - public @Nullable Pos getSpawn() { - return spawn; - } - /** * Returns the spawn location or the default spawn location if the spawn is null. * * @param defaultSpawn the default spawn location * @return the spawn location or the default spawn location */ - public @NotNull Pos getSpawnOrDefault(@NotNull Pos defaultSpawn) { + public Pos getSpawnOrDefault(Pos defaultSpawn) { return spawn != null ? spawn : defaultSpawn; } } \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/map/BaseMapBuilder.java b/src/main/java/net/theevilreaper/aves/map/BaseMapBuilder.java index 130caaaa..8a793623 100644 --- a/src/main/java/net/theevilreaper/aves/map/BaseMapBuilder.java +++ b/src/main/java/net/theevilreaper/aves/map/BaseMapBuilder.java @@ -1,6 +1,7 @@ package net.theevilreaper.aves.map; import net.minestom.server.coordinate.Pos; +import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,14 +14,14 @@ * If you want to create a custom map, you can extend this class and implement the required methods. * * @author theEvilReaper - * @version 1.0.0 + * @version 1.1.0 * @since 1.9.0 */ public class BaseMapBuilder { protected final List builders; - protected String name; - protected Pos spawn; + protected @Nullable String name; + protected @Nullable Pos spawn; /** * Constructs a new {@link BaseMapBuilder} instance with an empty list of builders. @@ -35,16 +36,16 @@ protected BaseMapBuilder() { * * @param baseMap the base map to copy properties from */ - protected BaseMapBuilder(@NotNull BaseMap baseMap) { - this.name = baseMap.getName(); - this.spawn = baseMap.getSpawn(); - if (baseMap.getBuilders() == null) { + protected BaseMapBuilder(BaseMap baseMap) { + this.name = baseMap.name(); + this.spawn = baseMap.spawn(); + if (baseMap.builders() == null) { this.builders = new ArrayList<>(); } else { // Copy the builders from the base map to the new list // This ensures that we do not modify the original list in the base map // and allows us to add new builders if needed. - this.builders = new ArrayList<>(List.of(baseMap.getBuilders())); + this.builders = new ArrayList<>(List.of(baseMap.builders())); } } @@ -54,7 +55,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * @param name the name of the map * @return the current instance of {@link BaseMapBuilder} for method chaining */ - public @NotNull BaseMapBuilder name(@NotNull String name) { + public BaseMapBuilder name(String name) { this.name = name; return this; } @@ -65,7 +66,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * @param builder the name of the builder to be added * @return the current instance of {@link BaseMapBuilder} for method chaining */ - public @NotNull BaseMapBuilder builder(@NotNull String builder) { + public BaseMapBuilder builder(String builder) { this.builders.add(builder); return this; } @@ -76,7 +77,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * @param builders the names of the builders to be added * @return the current instance of {@link BaseMapBuilder} for method chaining */ - public @NotNull BaseMapBuilder builders(@NotNull String... builders) { + public BaseMapBuilder builders(String... builders) { this.builders.addAll(List.of(builders)); return this; } @@ -87,7 +88,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * @param spawn the position where the map will spawn * @return the current instance of {@link BaseMapBuilder} for method chaining */ - public @NotNull BaseMapBuilder spawn(@Nullable Pos spawn) { + public BaseMapBuilder spawn(@Nullable Pos spawn) { this.spawn = spawn; return this; } @@ -97,7 +98,8 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * * @return a new instance of {@link BaseMap} */ - public @NotNull BaseMap build() { + public BaseMap build() { + Check.argCondition(this.name == null, "Name cannot be null"); return new BaseMap(name, spawn, builders.toArray(new String[0])); } @@ -124,7 +126,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) { * * @return a list of builder names */ - public @NotNull List getBuilders() { + public List getBuilders() { return builders; } } diff --git a/src/main/java/net/theevilreaper/aves/map/BaseMapEntry.java b/src/main/java/net/theevilreaper/aves/map/BaseMapEntry.java index 9cfe33e2..b664795f 100644 --- a/src/main/java/net/theevilreaper/aves/map/BaseMapEntry.java +++ b/src/main/java/net/theevilreaper/aves/map/BaseMapEntry.java @@ -1,6 +1,5 @@ package net.theevilreaper.aves.map; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +13,7 @@ * It will store the root directory of the map and the map file name. * * @since 1.6.0 - * @version 1.1.0 + * @version 1.2.0 * @author theEvilReaper */ public final class BaseMapEntry implements MapEntry { @@ -23,7 +22,7 @@ public final class BaseMapEntry implements MapEntry { private final String mapFileNaming; private final Path directory; - private Path mapFilePath; + private @Nullable Path mapFilePath; /** * Creates a new instance of the map entry. @@ -31,7 +30,7 @@ public final class BaseMapEntry implements MapEntry { * @param directory the root directory of the map * @param mapFileNaming the name of the map file */ - BaseMapEntry(@NotNull Path directory, @NotNull String mapFileNaming) { + BaseMapEntry(Path directory, String mapFileNaming) { if (!Files.isDirectory(directory)) { throw new IllegalArgumentException("The given path must be a directory"); } @@ -83,7 +82,7 @@ public boolean hasMapFile() { * @return the root directory */ @Override - public @NotNull Path getDirectoryRoot() { + public Path getDirectoryRoot() { return this.directory; } diff --git a/src/main/java/net/theevilreaper/aves/map/MapEntry.java b/src/main/java/net/theevilreaper/aves/map/MapEntry.java index 4a31e510..694e6e66 100644 --- a/src/main/java/net/theevilreaper/aves/map/MapEntry.java +++ b/src/main/java/net/theevilreaper/aves/map/MapEntry.java @@ -13,16 +13,16 @@ * When the creation doesn't receive the specific file ending a default one is used to avoid issues * * @author theEvilReaper - * @version 1.0.0 + * @version 1.1.0 * @since 1.6.0 */ public sealed interface MapEntry permits BaseMapEntry { /** - * The default file name which is used to store the map data. + * The default file name that is used to store the map data. * This file is used when no specific file name is given. */ - @NotNull String MAP_FILE = "map.json"; + String MAP_FILE = "map.json"; /** * Creates a new MapEntry from the given path. @@ -32,7 +32,7 @@ public sealed interface MapEntry permits BaseMapEntry { * @return the created reference */ @Contract(pure = true, value = "_ -> new") - static @NotNull MapEntry of(@NotNull Path directoryRoot) { + static MapEntry of(Path directoryRoot) { return new BaseMapEntry(directoryRoot, MAP_FILE); } @@ -44,7 +44,7 @@ public sealed interface MapEntry permits BaseMapEntry { * @return the created reference */ @Contract(pure = true, value = "_, _ -> new") - static @NotNull MapEntry of(@NotNull Path directoryRoot, @NotNull String mapFileNaming) { + static MapEntry of(Path directoryRoot, String mapFileNaming) { return new BaseMapEntry(directoryRoot, mapFileNaming); } @@ -72,7 +72,7 @@ public sealed interface MapEntry permits BaseMapEntry { * * @return the given path reference */ - @NotNull Path getDirectoryRoot(); + Path getDirectoryRoot(); /** * Returns the path to the file which stores information about a map. diff --git a/src/main/java/net/theevilreaper/aves/map/package-info.java b/src/main/java/net/theevilreaper/aves/map/package-info.java new file mode 100644 index 00000000..95a5eb04 --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/map/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.map; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/map/provider/AbstractMapProvider.java b/src/main/java/net/theevilreaper/aves/map/provider/AbstractMapProvider.java index acd64890..e835401c 100644 --- a/src/main/java/net/theevilreaper/aves/map/provider/AbstractMapProvider.java +++ b/src/main/java/net/theevilreaper/aves/map/provider/AbstractMapProvider.java @@ -10,7 +10,6 @@ import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceContainer; import net.minestom.server.instance.anvil.AnvilLoader; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnmodifiableView; import org.slf4j.Logger; @@ -34,7 +33,7 @@ *

* * @author theEvilReaper - * @version 1.0.0 + * @version 1.1.0 * @since 1.6.0 */ public abstract class AbstractMapProvider implements MapProvider { @@ -42,12 +41,11 @@ public abstract class AbstractMapProvider implements MapProvider { private static final Logger MAP_LOGGER = LoggerFactory.getLogger(AbstractMapProvider.class); private final PathFilter mapFilter; + protected final FileHandler fileHandler; + protected final List mapEntries; - protected FileHandler fileHandler; - protected List mapEntries; - - protected BaseMap activeMap; - protected InstanceContainer activeInstance; + protected @Nullable BaseMap activeMap; + protected @Nullable InstanceContainer activeInstance; /** * Constructs a BaseMapProvider with the specified FileHandler. @@ -55,9 +53,10 @@ public abstract class AbstractMapProvider implements MapProvider { * @param fileHandler the {@link FileHandler} used to load and save maps * @param mapFilter the filtering logic for the map entries */ - protected AbstractMapProvider(@NotNull FileHandler fileHandler, @NotNull PathFilter mapFilter) { + protected AbstractMapProvider(FileHandler fileHandler, PathFilter mapFilter) { this.fileHandler = fileHandler; this.mapFilter = mapFilter; + this.mapEntries = new ArrayList<>(); } /** @@ -67,7 +66,7 @@ protected AbstractMapProvider(@NotNull FileHandler fileHandler, @NotNull PathFil * @param instance the instance to be registered; must not be null * @param mapEntry the map entry representing the world data; must not be null */ - protected void registerInstance(@NotNull InstanceContainer instance, @NotNull MapEntry mapEntry) { + protected void registerInstance(InstanceContainer instance, MapEntry mapEntry) { instance.setChunkLoader(new AnvilLoader(mapEntry.getDirectoryRoot())); instance.enableAutoChunkLoad(true); instance.setTimeRate(0); @@ -80,38 +79,78 @@ protected void registerInstance(@NotNull InstanceContainer instance, @NotNull Ma * Handles IO exceptions gracefully by logging and reporting to the server exception manager. * * @param path the root directory containing map folders; must not be null - * @return a list of map entries found in the directory, possibly empty but never null */ - protected @NotNull List loadMapEntries(@NotNull Path path) { - List givenMaps = new ArrayList<>(); + protected void loadMapEntries(Path path) { + if (!this.mapEntries.isEmpty()) { + this.mapEntries.clear(); + } + try (Stream stream = Files.list(path)) { - givenMaps.addAll(this.mapFilter.filter(stream.filter(Files::isDirectory))); + this.mapEntries.addAll( + this.mapFilter.filter( + stream.filter(Files::isDirectory) + ) + ); } catch (IOException exception) { MinecraftServer.getExceptionManager().handleException(exception); MAP_LOGGER.error("Unable to load maps from path {}", path, exception); } - return givenMaps; } + /** + * {@inheritDoc} + */ @Override - public void teleportToSpawn(@NotNull Player player, boolean instanceSet) { - Pos pos = this.activeMap.getSpawnOrDefault(FALLBACK_POS); + public void teleportToSpawn(Player player, boolean instanceSet) { + Pos pos = activeMap().getSpawnOrDefault(FALLBACK_POS); + if (!instanceSet) { player.teleport(pos); return; } - player.setInstance(this.activeInstance, pos); - } + player.setInstance(activeInstance(), pos); + } + /** + * {@inheritDoc} + */ @Override - public @UnmodifiableView @NotNull List getEntries() { - if (this.mapEntries == null) return Collections.emptyList(); + public @UnmodifiableView List getEntries() { return Collections.unmodifiableList(this.mapEntries); } + /** + * {@inheritDoc} + */ @Override - public @NotNull Supplier<@Nullable Instance> getActiveInstance() { + public Supplier<@Nullable Instance> getActiveInstance() { return () -> this.activeInstance; } + + /** + * Returns the currently active map. + * + * @return the active map + * @throws IllegalStateException if no active map is set + */ + protected BaseMap activeMap() { + if (activeMap == null) { + throw new IllegalStateException("Active map has not been initialized yet"); + } + return activeMap; + } + + /** + * Returns the currently active instance. + * + * @return the active instance + * @throws IllegalStateException if no active instance is set + */ + protected InstanceContainer activeInstance() { + if (activeInstance == null) { + throw new IllegalStateException("Active instance has not been initialized yet"); + } + return activeInstance; + } } diff --git a/src/main/java/net/theevilreaper/aves/map/provider/MapProvider.java b/src/main/java/net/theevilreaper/aves/map/provider/MapProvider.java index 693ca98e..4a260ed5 100644 --- a/src/main/java/net/theevilreaper/aves/map/provider/MapProvider.java +++ b/src/main/java/net/theevilreaper/aves/map/provider/MapProvider.java @@ -5,7 +5,6 @@ import net.minestom.server.instance.Instance; import net.theevilreaper.aves.map.BaseMap; import net.theevilreaper.aves.map.MapEntry; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnmodifiableView; @@ -20,7 +19,7 @@ * This behavior is handled by another class. * * @author theEvilReaper - * @version 1.0.0 + * @version 1.1.0 * @since 1.6.0 */ public interface MapProvider { @@ -36,7 +35,7 @@ public interface MapProvider { * @param path the path where the map data should be saved * @param baseMap the map data which should be saved */ - void saveMap(@NotNull Path path, @NotNull BaseMap baseMap); + void saveMap(Path path, BaseMap baseMap); /** * Teleports a {@link Player} to the current active spawn position of the {@link Instance}. @@ -44,7 +43,7 @@ public interface MapProvider { * @param player the player which should be teleported * @param instanceSet if the instance should be set to the current active instance */ - void teleportToSpawn(@NotNull Player player, boolean instanceSet); + void teleportToSpawn(Player player, boolean instanceSet); /** * Returns a {@link List} which contains all available maps as {@link MapEntry} objects. @@ -52,12 +51,12 @@ public interface MapProvider { * @return the given list of maps */ @UnmodifiableView - @NotNull List getEntries(); + List getEntries(); /** * Returns the current active {@link Instance} which is used for the game. * * @return the current active instance */ - @NotNull Supplier<@Nullable Instance> getActiveInstance(); + Supplier<@Nullable Instance> getActiveInstance(); } diff --git a/src/main/java/net/theevilreaper/aves/map/provider/package-info.java b/src/main/java/net/theevilreaper/aves/map/provider/package-info.java new file mode 100644 index 00000000..c89baf56 --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/map/provider/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.map.provider; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/test/java/net/theevilreaper/aves/TestMapProvider.java b/src/test/java/net/theevilreaper/aves/TestMapProvider.java index 6d2b12fb..78080b09 100644 --- a/src/test/java/net/theevilreaper/aves/TestMapProvider.java +++ b/src/test/java/net/theevilreaper/aves/TestMapProvider.java @@ -22,7 +22,7 @@ public final class TestMapProvider extends AbstractMapProvider { */ public TestMapProvider(@NotNull Path path, @NotNull FileHandler fileHandler, @NotNull PathFilter mapFilter) { super(fileHandler, mapFilter); - this.mapEntries = loadMapEntries(path); + this.loadMapEntries(path); } public void loadMap(@NotNull MapEntry mapFile) { diff --git a/src/test/java/net/theevilreaper/aves/file/FileHandlerTest.java b/src/test/java/net/theevilreaper/aves/file/FileHandlerTest.java index 36df9e74..ea145a14 100644 --- a/src/test/java/net/theevilreaper/aves/file/FileHandlerTest.java +++ b/src/test/java/net/theevilreaper/aves/file/FileHandlerTest.java @@ -41,8 +41,7 @@ void testOtherConstructor() { @Test void testGsonFileHandlerWrite() { var path = tempDir.toPath().resolve(testMap); - var baseMap = new BaseMap("TestMap", null); - baseMap.setBuilders("Builder1", "Builder2"); + var baseMap = new BaseMap("TestMap", null, "Builder1", "Builder2"); fileHandler.save(path, baseMap); assertTrue(Files.exists(path)); } @@ -57,8 +56,8 @@ void testGsonFileHandler() { var map = optional.get(); - assertEquals("TestMap", map.getName()); - assertArrayEquals(new String[]{"Builder1", "Builder2"}, map.getBuilders()); + assertEquals("TestMap", map.name()); + assertArrayEquals(new String[]{"Builder1", "Builder2"}, map.builders()); } @Order(5) diff --git a/src/test/java/net/theevilreaper/aves/file/ModernFileHandlerTest.java b/src/test/java/net/theevilreaper/aves/file/ModernFileHandlerTest.java index d4203133..40482f7a 100644 --- a/src/test/java/net/theevilreaper/aves/file/ModernFileHandlerTest.java +++ b/src/test/java/net/theevilreaper/aves/file/ModernFileHandlerTest.java @@ -46,8 +46,7 @@ void testOtherConstructor() { @Test void testGsonFileHandlerWrite() { var path = tempDir.toPath().resolve(testMap); - var baseMap = new BaseMap("TestMap", null); - baseMap.setBuilders("Builder1", "Builder2"); + var baseMap = new BaseMap("TestMap", null, "Builder1", "Builder2"); fileHandler.save(path, baseMap, TypeToken.get(BaseMap.class)); assertTrue(Files.exists(path)); } @@ -62,8 +61,8 @@ void testGsonFileHandler() { var map = optional.get(); - assertEquals("TestMap", map.getName()); - assertArrayEquals(new String[]{"Builder1", "Builder2"}, map.getBuilders()); + assertEquals("TestMap", map.name()); + assertArrayEquals(new String[]{"Builder1", "Builder2"}, map.builders()); } @Order(5) diff --git a/src/test/java/net/theevilreaper/aves/map/BaseMapBuilderTest.java b/src/test/java/net/theevilreaper/aves/map/BaseMapBuilderTest.java index 1c07dd94..99fed7c1 100644 --- a/src/test/java/net/theevilreaper/aves/map/BaseMapBuilderTest.java +++ b/src/test/java/net/theevilreaper/aves/map/BaseMapBuilderTest.java @@ -7,6 +7,11 @@ class BaseMapBuilderTest { + @Test + void testMissingName() { + assertThrowsExactly(IllegalArgumentException.class, () -> BaseMap.builder().build()); + } + @Test void testMapCreationViaBuilder() { BaseMapBuilder builder = BaseMap.builder(); @@ -21,9 +26,9 @@ void testMapCreationViaBuilder() { assertNotNull(map); - assertEquals("TestMap", map.getName()); - assertArrayEquals(new String[]{"Author1", "Author2"}, map.getBuilders()); - assertEquals(new Pos(0, 64, 0), map.getSpawn()); + assertEquals("TestMap", map.name()); + assertArrayEquals(new String[]{"Author1", "Author2"}, map.builders()); + assertEquals(new Pos(0, 64, 0), map.spawn()); } @Test @@ -47,10 +52,10 @@ void testMapUpdateWithExistingMap() { BaseMap updatedMap = builder1.build(); assertNotNull(updatedMap); - assertEquals("TestMap", updatedMap.getName()); - assertArrayEquals(new String[]{"Author1", "Author2"}, updatedMap.getBuilders()); - assertEquals(new Pos(1, 65, 1), updatedMap.getSpawn()); - assertNotEquals(map.getSpawn(), updatedMap.getSpawn()); + assertEquals("TestMap", updatedMap.name()); + assertArrayEquals(new String[]{"Author1", "Author2"}, updatedMap.builders()); + assertEquals(new Pos(1, 65, 1), updatedMap.spawn()); + assertNotEquals(map.spawn(), updatedMap.spawn()); } @Test diff --git a/src/test/java/net/theevilreaper/aves/map/BaseMapTest.java b/src/test/java/net/theevilreaper/aves/map/BaseMapTest.java index 26b62afd..6fd1c81b 100644 --- a/src/test/java/net/theevilreaper/aves/map/BaseMapTest.java +++ b/src/test/java/net/theevilreaper/aves/map/BaseMapTest.java @@ -18,101 +18,55 @@ class BaseMapTest { @BeforeAll void init() { - this.builders = new String[]{"theEvilReaper, Tresson", "SeelenRetterin"}; - this.firstMap = new BaseMap("Test", null); - this.secondMap = new BaseMap("Test", new Pos(120, 51, 23), builders[0], builders[1]); - } - - @Test - void testOtherConstructor() { - var baseMap = new BaseMap("Test", Pos.ZERO); - assertSame("Test", baseMap.getName()); - assertSame(Pos.ZERO, baseMap.getSpawn()); - } - - @Test - void testMapIsSame() { - assertNotSame(this.firstMap, this.secondMap); - } - - @Test - void testHasSpawn() { - assertNotNull(this.secondMap.getSpawn()); - } - - @Test - void testOriginPos() { - assertFalse(this.secondMap.getSpawn() != null && this.secondMap.getSpawn().samePoint(Pos.ZERO)); - } + this.builders = new String[]{"theEvilReaper", "Tresson"}; - @Test - void testEmptyName() { - var exception = assertThrows(IllegalArgumentException.class, () -> this.firstMap.setName("")); - assertEquals("The name can not be null or empty", exception.getMessage()); - } - - @Test - void testEmptyNameWithStringWhichContainsSpace() { - assertThrowsExactly(IllegalArgumentException.class, () -> this.firstMap.setName(" "), "The name can not be empty"); - } - - @Test - void testNameSetForMaps() { - this.firstMap.setName("Granskoda"); - assertSame("Granskoda", this.firstMap.getName()); + this.firstMap = new BaseMap("Test", null); + this.secondMap = new BaseMap( + "Test", + new Pos(120, 51, 23), + builders + ); } @Test - void testEmptyNameConstructor() { - var exception = assertThrows(IllegalArgumentException.class, () -> new BaseMap("", null)); - assertEquals("The name can not be null or empty", exception.getMessage()); + void testName() { + assertEquals("Test", firstMap.name()); } @Test - void testSetBuilders() { - var mapBuilders = new String[]{"theEvilReaper, SeelenRetterin"}; - assertFalse(Arrays.equals(this.secondMap.getBuilders(), mapBuilders)); + void testSpawnNullable() { + assertNull(firstMap.spawn()); + assertNotNull(secondMap.spawn()); } @Test - void testBuilder() { - this.firstMap.setBuilders(this.builders); - assertArrayEquals(this.firstMap.getBuilders(), this.builders); + void testDifferentInstances() { + assertNotEquals(firstMap, secondMap); } @Test - void testNoSpawn() { - assertFalse(this.firstMap.hasSpawn()); - assertNull(this.firstMap.getSpawn()); + void testSpawnValue() { + assertEquals(new Pos(120, 51, 23), secondMap.spawn()); } @Test - void testSetSpawn() { - this.secondMap.setSpawn(new Pos(1, 2, 3)); - assertNotNull(this.secondMap.getSpawn()); + void testBuildersStored() { + assertArrayEquals(builders, secondMap.builders()); } @Test void testGetSpawnOrDefault() { - BaseMap baseMap = new BaseMap("Test", null); - assertNull(baseMap.getSpawn()); - - Pos defaultPos = new Pos(1, 2, 3); - assertEquals(defaultPos, baseMap.getSpawnOrDefault(defaultPos)); - } + BaseMap map = new BaseMap("Test", null); - @Test - void testEquals() { - assertNotSame(1203, this.firstMap.hashCode()); + Pos fallback = new Pos(1, 2, 3); + assertEquals(fallback, map.getSpawnOrDefault(fallback)); } @Test - void testEqualsWithSameObject() { - assertEquals(this.firstMap, this.firstMap); - } + void testEqualsDifferentName() { + BaseMap a = new BaseMap("A", null); + BaseMap b = new BaseMap("B", null); - @Test - void testEqualsWithNull() { - assertNotEquals(null, this.firstMap); + assertNotEquals(a, b); } } \ No newline at end of file diff --git a/src/test/java/net/theevilreaper/aves/map/provider/MapProviderIntegrationTest.java b/src/test/java/net/theevilreaper/aves/map/provider/MapProviderIntegrationTest.java index 5fc2bc2b..40e3405e 100644 --- a/src/test/java/net/theevilreaper/aves/map/provider/MapProviderIntegrationTest.java +++ b/src/test/java/net/theevilreaper/aves/map/provider/MapProviderIntegrationTest.java @@ -15,10 +15,8 @@ import net.minestom.testing.extension.MicrotusExtension; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -28,11 +26,7 @@ import java.nio.file.Path; import java.nio.file.Paths; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; @ExtendWith(MicrotusExtension.class) class MapProviderIntegrationTest { @@ -47,6 +41,7 @@ class MapProviderIntegrationTest { static void init() throws URISyntaxException { URL resourceUrl = MapProviderIntegrationTest.class.getClassLoader().getResource("map"); rootPath = Paths.get(resourceUrl.toURI()); + PositionGsonAdapter positionGsonAdapter = new PositionGsonAdapter(); fileHandler = new GsonFileHandler( new GsonBuilder() @@ -65,20 +60,14 @@ static void init() throws URISyntaxException { @BeforeEach void setUp() { this.mapProvider = new TestMapProvider(rootPath, fileHandler, pathFilter); + assertNotNull(mapProvider); assertInstanceOf(MapProvider.class, mapProvider); assertInstanceOf(AbstractMapProvider.class, mapProvider); - assertFalse(mapProvider.getEntries().isEmpty()); - assertEquals( - 1, - mapProvider.getEntries().size(), - "At provider instance creation, the map entries should be 1" - ); - } - @AfterEach - void tearDown() { - mapProvider = null; + assertFalse(mapProvider.getEntries().isEmpty()); + assertEquals(1, mapProvider.getEntries().size(), + "At provider instance creation, the map entries should be 1"); } @AfterAll @@ -87,11 +76,11 @@ static void tearDownAll() { pathFilter = null; } - @Disabled("Disabled due to long execution time") @Test void testMapHandlingLogic(@NotNull Env env) { InstanceContainer instance = (InstanceContainer) env.createFlatInstance(); Player player = env.createPlayer(instance); + assertNotNull(instance); MapEntry mapEntry = mapProvider.getEntries().getFirst(); @@ -99,7 +88,10 @@ void testMapHandlingLogic(@NotNull Env env) { ((TestMapProvider) mapProvider).loadMap(mapEntry); - assertNotNull(mapProvider.getActiveInstance()); + assertNotNull( + mapProvider.getActiveInstance().get(), + "Active instance should be initialized after loadMap" + ); mapProvider.teleportToSpawn(player, true); @@ -107,11 +99,7 @@ void testMapHandlingLogic(@NotNull Env env) { env.tick(); } - // assertNotEquals(instance.getUuid(), player.getInstance().getUuid()); - // assertNotEquals(Pos.ZERO, player.getPosition()); - player.teleport(Pos.ZERO); - assertEquals(Pos.ZERO, player.getPosition()); mapProvider.teleportToSpawn(player, false); @@ -121,5 +109,4 @@ void testMapHandlingLogic(@NotNull Env env) { env.destroyInstance(instance, true); env.destroyInstance(mapProvider.getActiveInstance().get(), true); } -} - +} \ No newline at end of file diff --git a/src/test/java/net/theevilreaper/aves/util/functional/BaseMapFunctionTest.java b/src/test/java/net/theevilreaper/aves/util/functional/BaseMapFunctionTest.java deleted file mode 100644 index 9486f659..00000000 --- a/src/test/java/net/theevilreaper/aves/util/functional/BaseMapFunctionTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.theevilreaper.aves.util.functional; - -import net.theevilreaper.aves.map.BaseMap; -import net.minestom.server.coordinate.Pos; -import org.junit.jupiter.api.Test; - -import java.util.function.Function; - -import static org.junit.jupiter.api.Assertions.*; - -class BaseMapFunctionTest { - - @Test - void testBaseMapFunctionalInterface() { - final BaseMapFunction function = baseMap -> baseMap == null ? null : baseMap.getSpawn(); - assertNull(function.apply(null)); - final BaseMap baseMap = new BaseMap(); - final Pos spawn = function.apply(baseMap); - assertNull(spawn); - } - - @Test - void testBaseMapBiFunctionApply() { - // Create an instance of BaseMapBiFunction - BaseMapBiFunction biFunction = (baseMap, str) -> { - // Example implementation: returning the length of the string - return str != null ? str.length() : 0; - }; - - // Create a dummy BaseMap instance - BaseMap baseMap = new BaseMap(); - - // Test the apply method - assertEquals(7, biFunction.apply(baseMap, "example")); - assertEquals(0, biFunction.apply(baseMap, null)); - } - - @Test - void testBaseMapBiFunctionApplyAndThen() { - // Create an instance of BaseMapBiFunction - BaseMapBiFunction biFunction = (baseMap, str) -> { - // Example implementation: returning the length of the string - return str != null ? str.length() : 0; - }; - - // Create an after function to convert the length to a string - Function afterFunction = Object::toString; - - // Create a composed function using andThen - BaseMapBiFunction composedFunction = biFunction.andThen(afterFunction); - - // Create a dummy BaseMap instance - BaseMap baseMap = new BaseMap(); - - // Test the composed function - assertEquals("7", composedFunction.apply(baseMap, "example")); - assertEquals("0", composedFunction.apply(baseMap, null)); - } - - @Test - void testAndThenWithNullAfter() { - // Create an instance of BaseMapBiFunction - BaseMapBiFunction biFunction = (baseMap, str) -> { - // Example implementation: returning the length of the string - return str != null ? str.length() : 0; - }; - - // Test the andThen method with a null after function - assertThrows(NullPointerException.class, () -> biFunction.andThen(null)); - } -}