aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrakenied <Krakenied1@gmail.com>2024-07-12 12:29:11 +0200
committerKrakenied <46192742+Krakenied@users.noreply.github.com>2024-08-28 11:37:11 +0200
commit816e0c73a1ac708ed4cad05c85be4d928db9d927 (patch)
tree6b192e2a4acf4b7e34e98550a0b2f410492cb03b
parent63c874ec96fa7235ec2d85a3e3a04012eeaac73b (diff)
Clean up task type manager
Mark some TaskType methods as final as they aren't really meant to be overridden
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java20
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskType.java6
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskTypeManager.java182
3 files changed, 118 insertions, 90 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java
index d87caace..3067bed0 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java
@@ -9,6 +9,7 @@ import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
+import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@@ -21,22 +22,28 @@ public final class BukkitTaskTypeManager extends TaskTypeManager {
private final BukkitQuestsPlugin plugin;
/**
- * Constructs a new BukkitTaskTypeManager.
+ * Constructs a new BukkitTaskTypeManager with exclusions.
*
* @param plugin the Bukkit plugin instance
+ * @param exclusions the set of task type exclusions
*/
- public BukkitTaskTypeManager(final @NotNull BukkitQuestsPlugin plugin) {
+ public BukkitTaskTypeManager(final @NotNull BukkitQuestsPlugin plugin, final @NotNull Set<String> exclusions) {
+ super(exclusions);
+ Objects.requireNonNull(plugin, "plugin cannot be null");
+
this.plugin = plugin;
}
/**
- * Constructs a new BukkitTaskTypeManager with exclusions.
+ * Constructs a new BukkitTaskTypeManager.
*
* @param plugin the Bukkit plugin instance
- * @param exclusions the set of task type exclusions
*/
- public BukkitTaskTypeManager(final @NotNull BukkitQuestsPlugin plugin, final @NotNull Set<String> exclusions) {
- super(exclusions);
+ @SuppressWarnings("unused")
+ public BukkitTaskTypeManager(final @NotNull BukkitQuestsPlugin plugin) {
+ super();
+ Objects.requireNonNull(plugin, "plugin cannot be null");
+
this.plugin = plugin;
}
@@ -71,6 +78,7 @@ public final class BukkitTaskTypeManager extends TaskTypeManager {
* @param taskId the task ID
* @param associatedPlayer the UUID of the associated player
*/
+ @Override
public void sendDebug(final @NotNull String message, final @NotNull String taskType, final @NotNull String questId, final @NotNull String taskId, final @NotNull UUID associatedPlayer) {
String chatHeader = null;
diff --git a/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskType.java b/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskType.java
index b05e5ab8..526b7444 100644
--- a/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskType.java
+++ b/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskType.java
@@ -110,7 +110,7 @@ public abstract class TaskType {
*
* @return the author of this task type, or null if not specified
*/
- public @Nullable String getAuthor() {
+ public final @Nullable String getAuthor() {
return this.author;
}
@@ -119,7 +119,7 @@ public abstract class TaskType {
*
* @return the description of this task type, or null if not specified
*/
- public @Nullable String getDescription() {
+ public final @Nullable String getDescription() {
return this.description;
}
@@ -128,7 +128,7 @@ public abstract class TaskType {
*
* @return a set of aliases of this task type
*/
- public @NotNull Set<String> getAliases() {
+ public final @NotNull Set<String> getAliases() {
return this.aliases;
}
diff --git a/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskTypeManager.java b/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskTypeManager.java
index d5ee9687..c8aee763 100644
--- a/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskTypeManager.java
+++ b/common/src/main/java/com/leonardobishop/quests/common/tasktype/TaskTypeManager.java
@@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
+import java.util.UUID;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
@@ -22,38 +23,47 @@ import java.util.function.Supplier;
@SuppressWarnings("UnusedReturnValue")
public abstract class TaskTypeManager {
- private final Map<String, TaskType> taskTypes = new HashMap<>();
- private final Map<String, String> aliases = new HashMap<>();
private final Set<String> exclusions;
+ private final Map<String, TaskType> taskTypes;
+ private final Map<String, String> aliases;
+ private boolean registrationsOpen;
private int registered;
private int skipped;
private int unsupported;
- private boolean registrationsOpen;
-
- public TaskTypeManager() {
- this.registrationsOpen = true;
- this.exclusions = Collections.emptySet();
- }
+ /**
+ * Constructs a TaskTypeManager with a specified set of exclusions.
+ *
+ * @param exclusions a set of task type names to exclude from registration
+ */
public TaskTypeManager(final @NotNull Set<String> exclusions) {
- this.registrationsOpen = true;
+ Objects.requireNonNull(exclusions, "exclusions cannot be null");
+
this.exclusions = exclusions;
+ this.taskTypes = new HashMap<>();
+ this.aliases = new HashMap<>();
+ this.registrationsOpen = true;
+ this.registered = 0;
+ this.skipped = 0;
+ this.unsupported = 0;
}
/**
- * Closes the task type registrations. This is typically done after start-up.
+ * Constructs a TaskTypeManager with an empty set of exclusions.
+ * Use this constructor when no exclusions are needed.
*/
- public void closeRegistrations() {
- this.registrationsOpen = false;
+ public TaskTypeManager() {
+ this(Set.of());
}
/**
- * Checks if registrations are still open.
+ * Returns an immutable set containing all task type exclusions.
*
- * @return true if registrations are open, false otherwise
+ * @return immutable {@link Set} containing all task type exclusions
*/
- public boolean areRegistrationsOpen() {
- return this.registrationsOpen;
+ @SuppressWarnings("unused")
+ public @NotNull Set<String> getExclusions() {
+ return Collections.unmodifiableSet(this.exclusions);
}
/**
@@ -66,12 +76,39 @@ public abstract class TaskTypeManager {
}
/**
- * Resets all quest to task type registrations. This does not clear the task types registered to the task type manager.
+ * Gets a registered task type by type.
+ *
+ * @param type the type to check
+ * @return the {@link TaskType} if found, null otherwise
*/
- public void resetTaskTypes() {
- for (final TaskType taskType : this.taskTypes.values()) {
- taskType.unregisterAll();
+ public @Nullable TaskType getTaskType(final @NotNull String type) {
+ Objects.requireNonNull(type, "type cannot be null");
+
+ final TaskType taskType = this.taskTypes.get(type);
+ if (taskType != null) {
+ return taskType;
}
+
+ final String aliasType = this.aliases.get(type);
+ if (aliasType != null) {
+ return this.taskTypes.get(aliasType);
+ }
+
+ return null;
+ }
+
+ /**
+ * Gets the actual name of a task type, following aliases.
+ *
+ * @param type name of task type
+ * @return the actual name of the task type, or null if not found
+ */
+ public @Nullable String resolveTaskTypeName(final @NotNull String type) {
+ Objects.requireNonNull(type, "type cannot be null");
+
+ return this.taskTypes.containsKey(type)
+ ? type
+ : this.aliases.get(type);
}
/**
@@ -88,12 +125,12 @@ public abstract class TaskTypeManager {
}
final String type = taskType.getType();
- final Set<String> aliasTypes = taskType.getAliases();
+ final Set<String> aliases = taskType.getAliases();
if (this.exclusions.contains(type) || this.taskTypes.containsKey(type)
- || !Collections.disjoint(this.exclusions, aliasTypes)
- || !Collections.disjoint(this.taskTypes.keySet(), aliasTypes)
- || !Collections.disjoint(this.aliases.keySet(), aliasTypes)) {
+ || !Collections.disjoint(this.exclusions, aliases)
+ || !Collections.disjoint(this.taskTypes.keySet(), aliases)
+ || !Collections.disjoint(this.aliases.keySet(), aliases)) {
this.skipped++;
return false;
}
@@ -116,6 +153,7 @@ public abstract class TaskTypeManager {
*/
public boolean registerTaskType(final @NotNull Supplier<TaskType> taskTypeSupplier, final @NotNull BooleanSupplier @NotNull ... compatibilitySuppliers) {
Objects.requireNonNull(taskTypeSupplier, "taskTypeSupplier cannot be null");
+ Objects.requireNonNull(compatibilitySuppliers, "compatibilitySuppliers cannot be null");
if (!this.registrationsOpen) {
throw new IllegalStateException("No longer accepting new task types (must be done before quests are loaded)");
@@ -132,69 +170,19 @@ public abstract class TaskTypeManager {
}
/**
- * Registers a quest with its task types. This will register the quest to each task type it contains.
- *
- * @param quest the quest to register
- */
- public void registerQuestTasksWithTaskTypes(final @NotNull Quest quest) {
- Objects.requireNonNull(quest, "quest cannot be null");
-
- if (this.registrationsOpen) {
- throw new IllegalStateException("Still accepting new task types (type registrations must be closed before registering quests)");
- }
-
- for (final Task task : quest.getTasks()) {
- final TaskType taskType = this.getTaskType(task.getType());
-
- if (taskType != null) {
- taskType.registerQuest(quest);
- }
- }
- }
-
- /**
- * Gets a registered task type by type.
- *
- * @param type the type to check
- * @return the {@link TaskType} if found, null otherwise
- */
- public @Nullable TaskType getTaskType(final @NotNull String type) {
- Objects.requireNonNull(type, "type cannot be null");
-
- final TaskType taskType = this.taskTypes.get(type);
- if (taskType != null) {
- return taskType;
- }
-
- final String aliasType = this.aliases.get(type);
- if (aliasType != null) {
- return this.taskTypes.get(aliasType);
- }
-
- return null;
- }
-
- /**
- * Gets the actual name of a task type, following aliases.
- *
- * @param type name of task type
- * @return the actual name of the task type, or null if not found
+ * Closes the task type registrations. This is typically done after start-up.
*/
- public @Nullable String resolveTaskTypeName(final @NotNull String type) {
- Objects.requireNonNull(type, "type cannot be null");
-
- return this.taskTypes.containsKey(type)
- ? type
- : this.aliases.get(type);
+ public void closeRegistrations() {
+ this.registrationsOpen = false;
}
/**
- * Returns an immutable set containing all task type exclusions.
+ * Checks if registrations are still open.
*
- * @return immutable {@link Set} containing all task type exclusions
+ * @return true if registrations are open, false otherwise
*/
- public @NotNull Set<String> getExclusions() {
- return Collections.unmodifiableSet(this.exclusions);
+ public boolean areRegistrationsOpen() {
+ return this.registrationsOpen;
}
/**
@@ -223,4 +211,36 @@ public abstract class TaskTypeManager {
public int getUnsupported() {
return this.unsupported;
}
+
+ /**
+ * Registers a quest with its task types. This will register the quest to each task type it contains.
+ *
+ * @param quest the quest to register
+ */
+ public void registerQuestTasksWithTaskTypes(final @NotNull Quest quest) {
+ Objects.requireNonNull(quest, "quest cannot be null");
+
+ if (this.registrationsOpen) {
+ throw new IllegalStateException("Still accepting new task types (type registrations must be closed before registering quests)");
+ }
+
+ for (final Task task : quest.getTasks()) {
+ final TaskType taskType = this.getTaskType(task.getType());
+
+ if (taskType != null) {
+ taskType.registerQuest(quest);
+ }
+ }
+ }
+
+ /**
+ * Resets all quest to task type registrations. This does not clear the task types registered to the task type manager.
+ */
+ public void resetTaskTypes() {
+ for (final TaskType taskType : this.taskTypes.values()) {
+ taskType.unregisterAll();
+ }
+ }
+
+ public abstract void sendDebug(final @NotNull String message, final @NotNull String taskType, final @NotNull String questId, final @NotNull String taskId, final @NotNull UUID associatedPlayer);
}