From 816e0c73a1ac708ed4cad05c85be4d928db9d927 Mon Sep 17 00:00:00 2001 From: Krakenied Date: Fri, 12 Jul 2024 12:29:11 +0200 Subject: Clean up task type manager Mark some TaskType methods as final as they aren't really meant to be overridden --- .../quests/common/tasktype/TaskType.java | 6 +- .../quests/common/tasktype/TaskTypeManager.java | 182 ++++++++++++--------- 2 files changed, 104 insertions(+), 84 deletions(-) (limited to 'common/src/main/java/com') 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 getAliases() { + public final @NotNull Set 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 taskTypes = new HashMap<>(); - private final Map aliases = new HashMap<>(); private final Set exclusions; + private final Map taskTypes; + private final Map 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 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 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 aliasTypes = taskType.getAliases(); + final Set 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 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 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); } -- cgit v1.2.3-70-g09d2