Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 5 additions & 123 deletions src/main/java/net/theevilreaper/aves/map/BaseMap.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -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();
}

Expand All @@ -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;
}
}
30 changes: 16 additions & 14 deletions src/main/java/net/theevilreaper/aves/map/BaseMapBuilder.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String> 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.
Expand All @@ -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()));
}
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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]));
}

Expand All @@ -124,7 +126,7 @@ protected BaseMapBuilder(@NotNull BaseMap baseMap) {
*
* @return a list of builder names
*/
public @NotNull List<String> getBuilders() {
public List<String> getBuilders() {
return builders;
}
}
9 changes: 4 additions & 5 deletions src/main/java/net/theevilreaper/aves/map/BaseMapEntry.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -23,15 +22,15 @@ 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.
*
* @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");
}
Expand Down Expand Up @@ -83,7 +82,7 @@ public boolean hasMapFile() {
* @return the root directory
*/
@Override
public @NotNull Path getDirectoryRoot() {
public Path getDirectoryRoot() {
return this.directory;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/theevilreaper/aves/map/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/theevilreaper/aves/map/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NotNullByDefault
package net.theevilreaper.aves.map;

import org.jetbrains.annotations.NotNullByDefault;
Loading