Skip to content
Merged
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
35 changes: 23 additions & 12 deletions src/main/java/net/onelitefeather/guira/SetupDataService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.onelitefeather.guira;

import net.theevilreaper.aves.map.BaseMap;
import net.onelitefeather.guira.data.SetupData;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand All @@ -12,50 +11,62 @@
import java.util.UUID;

/**
* The (@link SetupDataService} is responsible for managing setup data during a runtime session.
* The {@link SetupDataService} is responsible for managing setup data during a runtime session.
* It allows adding, removing, and retrieving setup data associated with a unique identifier (UUID).
*
* @param <T> the type of setup data

* @author thEvilReaper
* @version 1.0.0
* @since 0.1.0
*/
public interface SetupDataService<T extends SetupData<? extends BaseMap>> {
public interface SetupDataService {

/**
* Creates a new instance of SetupDataService.
*
* @param <T> the type of setup data
* @return a new instance of SetupDataService
*/
@Contract(pure = true)
static @NotNull <T extends SetupData<? extends BaseMap>> SetupDataService<T> create() {
return new SetupDataServiceImpl<>();
static @NotNull SetupDataService create() {
return new SetupDataServiceImpl();
}

/**
* Adds a new setup data to the service.
* Checks if the service contains any setup data.
*
* @return true if the service is empty, false otherwise
*/
boolean isEmpty();

/**
* Clears all setup data from the service.
* This method removes all entries from the setup data map.
*/
void clear();

/**
* Adds new setup data to the service.
*
* @param uuid the unique identifier for the setup data
* @param data the setup data to add
*/
void add(@NotNull UUID uuid, @NotNull T data);
void add(@NotNull UUID uuid, @NotNull SetupData data);

/**
* Removes the setup data associated with the given UUID.
*
* @param uuid the unique identifier for the setup data
* @return an optional containing the removed setup data, or empty if not found
*/
@NotNull Optional<@Nullable T> remove(@NotNull UUID uuid);
@NotNull Optional<@Nullable SetupData> remove(@NotNull UUID uuid);

/**
* Retrieves the setup data associated with the given UUID.
*
* @param uuid the unique identifier for the setup data
* @return an optional containing the setup data, or empty if not found
*/
@NotNull Optional<@Nullable T> get(@NotNull UUID uuid);
@NotNull Optional<@Nullable SetupData> get(@NotNull UUID uuid);

/**
* Returns an unmodifiable view of the setup data map.
Expand All @@ -64,5 +75,5 @@ public interface SetupDataService<T extends SetupData<? extends BaseMap>> {
*/
@NotNull
@UnmodifiableView
Map<UUID, T> getView();
Map<UUID, SetupData> getView();
}
39 changes: 32 additions & 7 deletions src/main/java/net/onelitefeather/guira/SetupDataServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.onelitefeather.guira;

import net.theevilreaper.aves.map.BaseMap;
import net.onelitefeather.guira.data.SetupData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;
Expand All @@ -11,31 +10,57 @@
import java.util.Optional;
import java.util.UUID;

public final class SetupDataServiceImpl<T extends SetupData<? extends BaseMap>> implements SetupDataService<T> {
public final class SetupDataServiceImpl implements SetupDataService {

private final Map<UUID, T> dataMap;
private final Map<UUID, SetupData> dataMap;

SetupDataServiceImpl() {
this.dataMap = new HashMap<>();
}

@Override
public void add(@NotNull UUID uuid, @NotNull T data) {
public void clear() {
if (this.dataMap.isEmpty()) return;
this.dataMap.clear();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isEmpty() {
return this.dataMap.isEmpty();
}

/**
* {@inheritDoc}
*/
@Override
public void add(@NotNull UUID uuid, @NotNull SetupData data) {
dataMap.put(uuid, data);
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull Optional<T> remove(@NotNull UUID uuid) {
public @NotNull Optional<SetupData> remove(@NotNull UUID uuid) {
return Optional.ofNullable(this.dataMap.remove(uuid));
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull Optional<T> get(@NotNull UUID uuid) {
public @NotNull Optional<SetupData> get(@NotNull UUID uuid) {
return Optional.ofNullable(this.dataMap.get(uuid));
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull @UnmodifiableView Map<UUID, T> getView() {
public @NotNull @UnmodifiableView Map<UUID, SetupData> getView() {
return Collections.unmodifiableMap(this.dataMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
import java.util.UUID;

/**
* The {@link BaseSetupData} is a generic implementation of the {@link SetupData} interface.
* The {@link MapSetupData} is a generic implementation of the {@link SetupData} interface.
* It provides a basic implementation and needs to be extended by a specific setup data class.
* @param <T> the reference type of the map
* @version 1.0.0
* @since 0.1.0
* @author theEvilReaper
*/
public abstract non-sealed class BaseSetupData<T extends BaseMap> implements SetupData<T> {
public abstract class MapSetupData<T extends BaseMap> implements SetupData {

protected final UUID uuid;
protected final MapEntry mapEntry;
protected T map;

protected BaseSetupData(@NotNull UUID uuid, @NotNull MapEntry mapEntry) {
protected MapSetupData(@NotNull UUID uuid, @NotNull MapEntry mapEntry) {
this.uuid = uuid;
this.mapEntry = mapEntry;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof BaseSetupData<?> that)) return false;
if (!(o instanceof MapSetupData<?> that)) return false;
return Objects.equals(uuid, that.uuid);
}

Expand All @@ -38,22 +38,33 @@ public int hashCode() {
return Objects.hashCode(uuid);
}

@Override
/**
* Returns if the setup data has a map file.
*
* @return {@code true} if the setup data has a map
*/
public boolean hasMapFile() {
return this.mapEntry.hasMapFile();
}

@Override
/**
* Returns the map entry of the setup data.
*
* @return the map entry as {@link MapEntry}
*/
public @NotNull MapEntry getEntry() {
return this.mapEntry;
}

@Override
/**
* Returns the map of the setup data.
*
* @return the map as {@link BaseMap}
*/
public @NotNull UUID getId() {
return this.uuid;
}

@Override
public @NotNull Optional<T> getMap() {
return Optional.ofNullable(this.map);
}
Expand Down
30 changes: 4 additions & 26 deletions src/main/java/net/onelitefeather/guira/data/SetupData.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package net.onelitefeather.guira.data;

import net.theevilreaper.aves.map.BaseMap;
import net.theevilreaper.aves.map.MapEntry;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

/**
* The {@link SetupData} interface defines an object which contains all relevant values about a setup instance.
*
* @param <T> the reference type of the map
* @author theEvilReaper
* @version 1.2.0
* @since 0.1.0
*/
public sealed interface SetupData<T extends BaseMap> permits BaseSetupData {
public interface SetupData {

/**
* Can be used to save the data from the setup into another format.
Expand All @@ -29,31 +28,10 @@ public sealed interface SetupData<T extends BaseMap> permits BaseSetupData {
*/
void loadData();

/**
* Returns if the setup data has a map file.
*
* @return {@code true} if the setup data has a map
*/
boolean hasMapFile();

/**
* Returns the owner of the setup data.
*
* @return the owner as {@link UUID}
*/
@NotNull UUID getId();

/**
* Returns the map of the setup data.
*
* @return the map as {@link BaseMap}
*/
@NotNull Optional<T> getMap();

/**
* Returns the map entry of the setup data.
*
* @return the map entry as {@link MapEntry}
*/
@NotNull MapEntry getEntry();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.onelitefeather.guira.event;

import net.theevilreaper.aves.map.BaseMap;
import net.minestom.server.event.Event;
import net.minestom.server.event.trait.CancellableEvent;
import net.onelitefeather.guira.data.SetupData;
Expand All @@ -10,17 +9,16 @@
* The {@link SetupCreateEvent} can be used to indicate that a setup has started by the owner.
* You need to call it on your own and the handling of it.
*
* @param <T> the reference from the data
* @author thEvilReaper
* @version 1.0.0
* @since 0.1.0
*/
public class SetupCreateEvent<T extends SetupData<? extends BaseMap>> implements Event, CancellableEvent {
public class SetupCreateEvent implements Event, CancellableEvent {

private boolean cancelled;
private final T data;
private final SetupData data;

public SetupCreateEvent(@NotNull T data) {
public SetupCreateEvent(@NotNull SetupData data) {
this.data = data;
}

Expand All @@ -29,7 +27,7 @@ public SetupCreateEvent(@NotNull T data) {
*
* @return the reference
*/
public T getData() {
public SetupData getData() {
return data;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package net.onelitefeather.guira.event;

import net.theevilreaper.aves.map.BaseMap;
import net.minestom.server.event.Event;
import net.minestom.server.event.trait.CancellableEvent;
import net.onelitefeather.guira.data.SetupData;
import org.jetbrains.annotations.NotNull;

/**
* The {@link SetupFinishEvent} can be used to indicate that a setup process has been finished by the user.
* If your use case has some condition which needs to be checked before the setup process is finished, you can cancel
* If your use case has some condition that needs to be checked before the setup process is finished, you can cancel
* the event. But you need to handle the cancellation yourself.
*
* @param <T> the reference type of the map
* @author theEvilReaper
* @version 1.0.0
* @since 0.1.0
*/
public class SetupFinishEvent<T extends SetupData<? extends BaseMap>> implements Event, CancellableEvent {
public class SetupFinishEvent implements Event, CancellableEvent {

private final @NotNull T setupData;
private final SetupData setupData;
private boolean cancelled;

/**
* Creates a new instance of the {@link SetupFinishEvent} class.
*
* @param setupData the setup data of the event
*/
public SetupFinishEvent(@NotNull T setupData) {
public SetupFinishEvent(@NotNull SetupData setupData) {
this.setupData = setupData;
}

Expand Down Expand Up @@ -55,7 +53,7 @@ public boolean isCancelled() {
*
* @return the setup data
*/
public @NotNull T getData() {
public @NotNull SetupData getData() {
return setupData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public abstract class BasicDataTest {
public abstract class BasicTestMapData {

protected static Path tempDir;
protected static MapEntry emptyMapEntry;

@BeforeAll
static void beforeAll() throws URISyntaxException {
tempDir = Paths.get(BasicDataTest.class.getResource("/emptyFolder").toURI());
tempDir = Paths.get(BasicTestMapData.class.getResource("/emptyFolder").toURI());
assertNotNull(tempDir, "The test class for the SetupData requires a folder with the name 'emptyFolder'");
emptyMapEntry = MapEntry.of(tempDir);
assertFalse(emptyMapEntry.hasMapFile(), "The emptyFolder for the test should not contain any files!");
Expand Down
Loading
Loading