diff options
| author | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-06-29 20:34:55 +0100 |
|---|---|---|
| committer | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-06-29 20:34:55 +0100 |
| commit | c8bef615fd0a117daa0919e9927a0512e2c3d0a4 (patch) | |
| tree | 4b80fb1e81955cc5053239251b1fdb9518dab1f6 /common/src | |
| parent | 5ac115087ae3c6d38da3e2bcd6ab01e71e4d3d29 (diff) | |
Add JetBrains annotations
Diffstat (limited to 'common/src')
12 files changed, 475 insertions, 110 deletions
diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java index f9f46078..d8b40cce 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java @@ -5,7 +5,10 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFi import com.leonardobishop.quests.common.plugin.Quests; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.questcontroller.QuestController; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.UUID; /** @@ -27,7 +30,12 @@ public class QPlayer { this.questController = questController; } - public UUID getPlayerUUID() { + /** + * Get the player UUID associated with this quest player. The player may not be online. + * + * @return uuid + */ + public @NotNull UUID getPlayerUUID() { return this.uuid; } @@ -38,7 +46,9 @@ public class QPlayer { * @param quest the quest to complete * @return true (always) */ - public boolean completeQuest(Quest quest) { + public boolean completeQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + return questController.completeQuestForPlayer(this, quest); } @@ -47,7 +57,7 @@ public class QPlayer { ** * @param quest the quest to track */ - public void trackQuest(Quest quest) { + public void trackQuest(@Nullable Quest quest) { questController.trackQuestForPlayer(this, quest); } @@ -57,7 +67,9 @@ public class QPlayer { * @param quest the quest to test for * @return true if the quest is started or quest autostart is enabled and the quest is ready to start, false otherwise */ - public boolean hasStartedQuest(Quest quest) { + public boolean hasStartedQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + return questController.hasPlayerStartedQuest(this, quest); } @@ -70,7 +82,9 @@ public class QPlayer { * @return the quest start result -- {@code QuestStartResult.QUEST_SUCCESS} indicates success */ // TODO PlaceholderAPI support - public QuestStartResult startQuest(Quest quest) { + public @NotNull QuestStartResult startQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + return questController.startQuestForPlayer(this, quest); } @@ -80,7 +94,9 @@ public class QPlayer { * @param quest the quest to start * @return true if the quest was cancelled, false otherwise */ - public boolean cancelQuest(Quest quest) { + public boolean cancelQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + return questController.cancelQuestForPlayer(this, quest); } @@ -92,24 +108,48 @@ public class QPlayer { * @param quest the quest to check * @return the quest start result */ - public QuestStartResult canStartQuest(Quest quest) { + public @NotNull QuestStartResult canStartQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + return questController.canPlayerStartQuest(this, quest); } - - public QuestProgressFile getQuestProgressFile() { + /** + * Get this players associated {@link QuestProgressFile} + * + * @return the quest progress file + */ + public @NotNull QuestProgressFile getQuestProgressFile() { return questProgressFile; } - public QPlayerPreferences getPlayerPreferences() { + /** + * Get this players associated {@link QPlayerPreferences} + * + * @return the players preferences + */ + public @NotNull QPlayerPreferences getPlayerPreferences() { return playerPreferences; } - public QuestController getQuestController() { + /** + * Get this players associated {@link QuestController}, usually the servers active quest controller + * + * @see QPlayerManager#getActiveQuestController() + * @return the quest controller for this player + */ + public @NotNull QuestController getQuestController() { return questController; } - public void setQuestController(QuestController questController) { + /** + * Sets this players associated {@link QuestController} + * + * @param questController new quest controller + */ + public void setQuestController(@NotNull QuestController questController) { + Objects.requireNonNull(questController, "questController cannot be null"); + this.questController = questController; } diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerManager.java b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerManager.java index 3f256c05..eac0f255 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerManager.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerManager.java @@ -5,9 +5,13 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFi import com.leonardobishop.quests.common.plugin.Quests; import com.leonardobishop.quests.common.questcontroller.QuestController; import com.leonardobishop.quests.common.storage.StorageProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.Collections; import java.util.Map; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -32,9 +36,11 @@ public class QPlayerManager { * Gets the QPlayer from a given UUID. * * @param uuid the uuid - * @return {@link QPlayer} if they are loaded + * @return {@link QPlayer} if they are loaded, otherwise null */ - public QPlayer getPlayer(UUID uuid) { + public @Nullable QPlayer getPlayer(@NotNull UUID uuid) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + QPlayer qPlayer = qPlayers.get(uuid); if (qPlayer == null) { plugin.getQuestsLogger().debug("QPlayer of " + uuid + " is null, but was requested:"); @@ -50,7 +56,9 @@ public class QPlayerManager { * * @param uuid the uuid of the player */ - public void removePlayer(UUID uuid) { + public void removePlayer(@NotNull UUID uuid) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + plugin.getQuestsLogger().debug("Unloading and saving player " + uuid + "."); qPlayers.computeIfPresent(uuid, (mapUUID, qPlayer) -> { savePlayer(uuid); @@ -64,7 +72,9 @@ public class QPlayerManager { * * @param uuid the uuid of the player */ - public void savePlayer(UUID uuid) { + public void savePlayer(@NotNull UUID uuid) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + QPlayer qPlayer = getPlayer(uuid); if (qPlayer == null) return; savePlayer(uuid, qPlayer.getQuestProgressFile()); @@ -77,7 +87,10 @@ public class QPlayerManager { * @param uuid the uuid of the player * @param originalProgressFile the quest progress file to associate with and save */ - public void savePlayer(UUID uuid, QuestProgressFile originalProgressFile) { + public void savePlayer(@NotNull UUID uuid, @NotNull QuestProgressFile originalProgressFile) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + Objects.requireNonNull(originalProgressFile, "originalProgressFile cannot be null"); + QuestProgressFile clonedProgressFile = new QuestProgressFile(originalProgressFile); originalProgressFile.resetModified(); plugin.getScheduler().doAsync(() -> save(uuid, clonedProgressFile)); @@ -89,7 +102,9 @@ public class QPlayerManager { * * @param uuid the uuid of the player */ - public void savePlayerSync(UUID uuid) { + public void savePlayerSync(@NotNull UUID uuid) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + QPlayer qPlayer = getPlayer(uuid); if (qPlayer == null) return; savePlayerSync(uuid, qPlayer.getQuestProgressFile()); @@ -102,11 +117,14 @@ public class QPlayerManager { * @param uuid the uuid of the player * @param questProgressFile the quest progress file to associate with and save */ - public void savePlayerSync(UUID uuid, QuestProgressFile questProgressFile) { + public void savePlayerSync(@NotNull UUID uuid, @NotNull QuestProgressFile questProgressFile) { save(uuid, questProgressFile); } - private void save(UUID uuid, QuestProgressFile questProgressFile) { + private void save(@NotNull UUID uuid, @NotNull QuestProgressFile questProgressFile) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + Objects.requireNonNull(questProgressFile, "questProgressFile cannot be null"); + plugin.getQuestsLogger().debug("Saving player " + uuid + "."); storageProvider.saveProgressFile(uuid, questProgressFile); } @@ -116,13 +134,20 @@ public class QPlayerManager { * * @param uuid the uuid of the player */ - public void dropPlayer(UUID uuid) { + public void dropPlayer(@NotNull UUID uuid) { + Objects.requireNonNull(uuid, "uuid cannot be null"); + plugin.getQuestsLogger().debug("Dropping player " + uuid + "."); qPlayers.remove(uuid); } + /** + * Gets all QPlayers loaded on the server + * + * @return immutable map of quest players + */ public Collection<QPlayer> getQPlayers() { - return qPlayers.values(); + return Collections.unmodifiableCollection(qPlayers.values()); } /** diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerPreferences.java b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerPreferences.java index 5278ae09..db1e9230 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerPreferences.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayerPreferences.java @@ -1,5 +1,7 @@ package com.leonardobishop.quests.common.player; +import org.jetbrains.annotations.Nullable; + public class QPlayerPreferences { private String trackedQuestId; @@ -8,11 +10,11 @@ public class QPlayerPreferences { this.trackedQuestId = trackedQuestId; } - public String getTrackedQuestId() { + public @Nullable String getTrackedQuestId() { return trackedQuestId; } - public void setTrackedQuestId(String trackedQuestId) { + public void setTrackedQuestId(@Nullable String trackedQuestId) { this.trackedQuestId = trackedQuestId; } } diff --git a/common/src/main/java/com/leonardobishop/quests/common/plugin/Quests.java b/common/src/main/java/com/leonardobishop/quests/common/plugin/Quests.java index fbfd6813..862d9b95 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/plugin/Quests.java +++ b/common/src/main/java/com/leonardobishop/quests/common/plugin/Quests.java @@ -10,6 +10,7 @@ import com.leonardobishop.quests.common.scheduler.ServerScheduler; import com.leonardobishop.quests.common.storage.StorageProvider; import com.leonardobishop.quests.common.tasktype.TaskTypeManager; import com.leonardobishop.quests.common.updater.Updater; +import org.jetbrains.annotations.NotNull; public interface Quests { @@ -19,7 +20,7 @@ public interface Quests { * @see QuestsLogger * @return quests logger */ - QuestsLogger getQuestsLogger(); + @NotNull QuestsLogger getQuestsLogger(); /** * Obtain an instance of the QuestManager. @@ -27,7 +28,7 @@ public interface Quests { * @see QuestManager * @return quest manager */ - QuestManager getQuestManager(); + @NotNull QuestManager getQuestManager(); /** * Obtain an instance of the QPlayerManager. @@ -35,7 +36,7 @@ public interface Quests { * @see QPlayerManager * @return quest player manager */ - QPlayerManager getPlayerManager(); + @NotNull QPlayerManager getPlayerManager(); /** * Obtain an instance of the QuestController. @@ -43,7 +44,7 @@ public interface Quests { * @see QuestController * @return quest controller */ - QuestController getQuestController(); + @NotNull QuestController getQuestController(); /** * Obtain an instance of the TaskTypeManager. @@ -51,7 +52,7 @@ public interface Quests { * @see TaskTypeManager * @return task type manager */ - TaskTypeManager getTaskTypeManager(); + @NotNull TaskTypeManager getTaskTypeManager(); /** * Obtain an instance of the QuestCompleter. @@ -59,7 +60,7 @@ public interface Quests { * @see QuestCompleter * @return quest completer */ - QuestCompleter getQuestCompleter(); + @NotNull QuestCompleter getQuestCompleter(); /** * Obtain an instance of the QuestConfig. @@ -67,7 +68,7 @@ public interface Quests { * @see QuestsConfig * @return quest config */ - QuestsConfig getQuestsConfig(); + @NotNull QuestsConfig getQuestsConfig(); /** * Obtain an instance of the Updater. @@ -75,7 +76,7 @@ public interface Quests { * @see Updater * @return updater */ - Updater getUpdater(); + @NotNull Updater getUpdater(); /** * Obtain an instance of the ServerScheduler. @@ -83,7 +84,7 @@ public interface Quests { * @see ServerScheduler * @return server scheduler */ - ServerScheduler getScheduler(); + @NotNull ServerScheduler getScheduler(); /** * Obtain an instance of the StorageProvider. @@ -91,7 +92,7 @@ public interface Quests { * @see StorageProvider * @return storage provider */ - StorageProvider getStorageProvider(); + @NotNull StorageProvider getStorageProvider(); /** * Performs a full reload of the plugin, unloading and re-registering quests to their task types. diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/Category.java b/common/src/main/java/com/leonardobishop/quests/common/quest/Category.java index 7bd8f10d..25abcef0 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/Category.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/Category.java @@ -1,7 +1,11 @@ package com.leonardobishop.quests.common.quest; +import org.jetbrains.annotations.NotNull; + import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Objects; public class Category { @@ -14,20 +18,42 @@ public class Category { this.permissionRequired = permissionRequired; } - public String getId() { + /** + * Get the id of this category. + * + * @return id + */ + public @NotNull String getId() { return id; } + /** + * Get if a specific permission is required to open this category and start quests within it. + * This permission will be in the form of "quests.category.[category id]". + * + * @return boolean + */ public boolean isPermissionRequired() { return permissionRequired; } - public void registerQuestId(String questid) { - registeredQuestIds.add(questid); + /** + * Register a new quest ID to this category + * + * @param questId quest id to register + */ + public void registerQuestId(@NotNull String questId) { + Objects.requireNonNull(questId, "questId cannot be null"); + registeredQuestIds.add(questId); } - public List<String> getRegisteredQuestIds() { - return registeredQuestIds; + /** + * Get quest IDs which are registered to this category + * + * @return immutable list of quest ids + */ + public @NotNull List<String> getRegisteredQuestIds() { + return Collections.unmodifiableList(registeredQuestIds); } } diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java b/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java index c1f6f47f..6a591e1d 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java @@ -1,15 +1,20 @@ package com.leonardobishop.quests.common.quest; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class Quest implements Comparable<Quest> { private final Map<String, Task> tasks = new HashMap<>(); + private final Map<String, List<Task>> tasksByType = new HashMap<>(); private String id; private List<String> rewards; private List<String> requirements; @@ -26,83 +31,196 @@ public class Quest implements Comparable<Quest> { private Quest() { } - public void registerTask(Task task) { + /** + * Register a task to this quest. + * + * @param task the task to register + */ + public void registerTask(@NotNull Task task) { + Objects.requireNonNull(task, "task cannot be null"); + tasks.put(task.getId(), task); + tasksByType.compute(task.getType(), (type, list) -> { + if (list == null) { + return new ArrayList<>(Collections.singletonList(task)); + } else { + list.add(task); + return list; + } + }); } - public Collection<Task> getTasks() { - return tasks.values(); + /** + * Get all tasks registered to this quest. + * + * @return immutable list containing all {@link Task} + */ + public @NotNull Collection<Task> getTasks() { + return Collections.unmodifiableCollection(tasks.values()); } - public Task getTaskById(String id) { + /** + * Get a specific task registered to this quest. + * + * @param id task id + * @return {@link Task}, or null if not exists + */ + public @Nullable Task getTaskById(@NotNull String id) { + Objects.requireNonNull(id, "id cannot be null"); + return tasks.get(id); } - public List<Task> getTasksOfType(String type) { - List<Task> tasks = new ArrayList<>(); - for (Task task : getTasks()) { - if (task.getType().equals(type)) { - tasks.add(task); - } - } - return tasks; + /** + * Get a list of all task of a specific task type. + * + * @param type the task type + * @return list containing all tasks of type + */ + public @NotNull List<Task> getTasksOfType(String type) { + Objects.requireNonNull(type, "type cannot be null"); + + List<Task> list = tasksByType.get(type); + return list == null ? Collections.emptyList() : Collections.unmodifiableList(list); } + /** + * Get if a specific permission is required to start this quest. + * This permission will be in the form of "quests.quest.[quest id]". + * + * @return boolean + */ public boolean isPermissionRequired() { return permissionRequired; } - public List<String> getRewardString() { - return rewardString; + /** + * Get the reward string of the quest. + * The reward string is a series of messages sent to the player upon completing the quest. + * + * @return immutable list of messages to send + */ + public @NotNull List<String> getRewardString() { + return Collections.unmodifiableList(rewardString); } - public List<String> getStartString() { - return startString; + /** + * Get the start string of the quest. + * The start string is a series of messages sent to the player upon starting the quest. + * + * @return immutable list of messages to send + */ + public @NotNull List<String> getStartString() { + return Collections.unmodifiableList(startString); } - public String getId() { + /** + * Get the id of this quest. + * + * @return id + */ + public @NotNull String getId() { return id; } - public List<String> getRewards() { - return rewards; + /** + * Get the rewards for this quest. + * The rewards is a list of commands to be executed upon completing the quest. + * + * @return immutable list of rewards + */ + public @NotNull List<String> getRewards() { + return Collections.unmodifiableList(rewards); } - public List<String> getRequirements() { - return requirements; + /** + * Get the requirements for this quest. + * The requirements is a list of quests ids the player must have completed at least once to start this quest. + * The quest ids may or may not represent actual quests and are only validated by the plugin with a warning. + * + * @return immutable list of quest requirements + */ + public @NotNull List<String> getRequirements() { + return Collections.unmodifiableList(requirements); } + /** + * Get if this quest can be repeated after completion. + * + * @return boolean + */ public boolean isRepeatable() { return repeatEnabled; } + /** + * Get if this quest has a cooldown enabled after completion. + * Whether or not the quest enters a cooldown phase for the player depends + * on if it is repeatable in the first place: {@link Quest#isRepeatable()} + * + * @return boolean + */ public boolean isCooldownEnabled() { return cooldownEnabled; } + /** + * Get the cooldown for this quest between completing and restarting the quest. + * Whether or not this cooldown is in use depends on {@link Quest#isCooldownEnabled()}. + * + * @return the cooldown, in seconds + */ public int getCooldown() { return cooldown; } - public String getCategoryId() { + /** + * Get the category id this quest is in. + * + * @return the category id, or null + */ + public @Nullable String getCategoryId() { return categoryid; } - public Map<String, String> getPlaceholders() { - return placeholders; + /** + * Get the local placeholders for this quest, which is exposed to PlaceholderAPI. + * + * @return immutable map of placeholders + */ + public @NotNull Map<String, String> getPlaceholders() { + return Collections.unmodifiableMap(placeholders); } + /** + * Get the sort order for this quest in the GUI. + * Numbers closer to Integer.MIN_VALUE have greater priority. + * + * @return any integer, both negative or positive + */ public int getSortOrder() { return sortOrder; } + /** + * Get if quest-specific autostart is enabled for this quest. + * + * @return boolean + */ public boolean isAutoStartEnabled() { return autoStartEnabled; } + /** + * Compare the sort orders for this quest with another quest. + * + * @see Comparable#compareTo(Object) + * @param quest the quest to compare with + * @return a negative integer, zero, or a positive integer + */ @Override - public int compareTo(Quest quest) { + public int compareTo(@NotNull Quest quest) { return (sortOrder - quest.sortOrder); } diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/QuestCompleter.java b/common/src/main/java/com/leonardobishop/quests/common/quest/QuestCompleter.java index cc498df9..704ec6e9 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/QuestCompleter.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/QuestCompleter.java @@ -2,6 +2,7 @@ package com.leonardobishop.quests.common.quest; import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile; +import org.jetbrains.annotations.NotNull; /** * The quest completer is responsible for checking each player for completed quests. Implementations may split @@ -9,7 +10,18 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFi */ public interface QuestCompleter { - void queueSingular(QuestProgress questProgress); - void queueFullCheck(QuestProgressFile questProgressFile); + /** + * Queue an individual quest progress to check if the quest is completed. + * + * @param questProgress the questprogress to check + */ + void queueSingular(@NotNull QuestProgress questProgress); + + /** + * Queue a quest progress file for a full check if they have completed any quests. + * + * @param questProgressFile the questprogressfile to check + */ + void queueFullCheck(@NotNull QuestProgressFile questProgressFile); } diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/QuestManager.java b/common/src/main/java/com/leonardobishop/quests/common/quest/QuestManager.java index af2afa52..2a569b2e 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/QuestManager.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/QuestManager.java @@ -1,11 +1,15 @@ package com.leonardobishop.quests.common.quest; import com.leonardobishop.quests.common.plugin.Quests; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * The quests manager stores all present Quests and Categories on the server and is used as a registry. @@ -13,40 +17,81 @@ import java.util.Map; public class QuestManager { private final Quests plugin; + private final Map<String, Quest> quests = new LinkedHashMap<>(); + private final List<Category> categories = new ArrayList<>(); public QuestManager(Quests plugin) { this.plugin = plugin; } - private Map<String, Quest> quests = new LinkedHashMap<>(); - private List<Category> categories = new ArrayList<>(); + /** + * Register a quest with the quest manager + * + * @param quest the category to register + */ + public void registerQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); - public void registerQuest(Quest quest) { quests.put(quest.getId(), quest); } - public Quest getQuestById(String id) { + /** + * @param id id to match + * @return {@link Quest}, or null + */ + public @Nullable Quest getQuestById(@NotNull String id) { + Objects.requireNonNull(id, "id cannot be null"); + return quests.get(id); } - public Map<String, Quest> getQuests() { - return quests; + /** + * Get a map of id-quest of all quests registered + * @return immutable map of all {@link Quest} + */ + public @NotNull Map<String, Quest> getQuests() { + return Collections.unmodifiableMap(quests); } - public void registerCategory(Category category) { categories.add(category); } + /** + * Register a category with the quest manager + * + * @param category the category to register + */ + public void registerCategory(@NotNull Category category) { + Objects.requireNonNull(category, "category cannot be null"); + + categories.add(category); + } - public List<Category> getCategories() { - return categories; + /** + * @return immutable list of all {@link Category} + */ + public @NotNull List<Category> getCategories() { + return Collections.unmodifiableList(categories); } - public Category getCategoryById(String id) { + /** + * Get a specific category by id + * + * @param id the id + * @return {@link Category}, or null + */ + public @Nullable Category getCategoryById(@NotNull String id) { + Objects.requireNonNull(id, "id cannot be null"); + for (Category category : categories) { if (category.getId().equals(id)) return category; } return null; } - public Quests getPlugin() { - return this.plugin; + /** + * Reset the quest manager and clears all registered quests and categories + */ + public void clear() { + quests.clear(); + categories.clear(); } + } diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/Task.java b/common/src/main/java/com/leonardobishop/quests/common/quest/Task.java index 8189e06e..6e27aef6 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/Task.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/Task.java @@ -1,7 +1,12 @@ package com.leonardobishop.quests.common.quest; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class Task { @@ -14,27 +19,62 @@ public class Task { this.type = type; } - public String getId() { + /** + * @return the id of this task + */ + public @NotNull String getId() { return id; } - public String getType() { + /** + * @return the configured task type for this task + */ + public @NotNull String getType() { return type; } - public Object getConfigValue(String key) { + /** + * Get a specific configuration value for this task + * + * @param key key of config value to get + * @return config value, or null + */ + public @Nullable Object getConfigValue(@NotNull String key) { + Objects.requireNonNull(key, "key cannot be null"); + return configValues.getOrDefault(key, null); //??? this will return null without the need of `OrDefault(key, null)` } - public Object getConfigValue(String key, Object def) { + /** + * Get a specific configuration value for this task + * + * @param key key of config value to get + * @param def default value if null + * @return config value, or null + */ + public @Nullable Object getConfigValue(@NotNull String key, @Nullable Object def) { + Objects.requireNonNull(key, "key cannot be null"); + return configValues.getOrDefault(key, def); } - public Map<String, Object> getConfigValues() { - return configValues; + /** + * @return immutable list containing all config values + */ + public @NotNull Map<String, Object> getConfigValues() { + return Collections.unmodifiableMap(configValues); } - public void addConfigValue(String key, Object value) { + /** + * Add a key-value pair to this tasks configuration + * + * @param key key + * @param value value + */ + public void addConfigValue(@NotNull String key, @NotNull Object value) { + Objects.requireNonNull(key, "key cannot be null"); + Objects.requireNonNull(value, "value cannot be null"); + configValues.put(key, value); } diff --git a/common/src/main/java/com/leonardobishop/quests/common/storage/StorageProvider.java b/common/src/main/java/com/leonardobishop/quests/common/storage/StorageProvider.java index 26cca4be..bd558c4e 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/storage/StorageProvider.java +++ b/common/src/main/java/com/leonardobishop/quests/common/storage/StorageProvider.java @@ -1,6 +1,8 @@ package com.leonardobishop.quests.common.storage; import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.UUID; @@ -14,8 +16,19 @@ public interface StorageProvider { void shutdown(); - QuestProgressFile loadProgressFile(UUID uuid); + /** + * Load a QuestProgressFile from the data source by a specific UUID + * + * @param uuid the UUID to load + * @return {@link QuestProgressFile} or null + */ + @Nullable QuestProgressFile loadProgressFile(@NotNull UUID uuid); - void saveProgressFile(UUID uuid, QuestProgressFile questProgressFile); + /** + * Save a QuestProgressFile to the data source with a specific UUID + * @param uuid the uuid to match the file to + * @param questProgressFile the file to save + */ + void saveProgressFile(@NotNull UUID uuid, @NotNull QuestProgressFile questProgressFile); } 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 79c7a0b0..efa62733 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 @@ -3,11 +3,14 @@ package com.leonardobishop.quests.common.tasktype; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.UUID; /** @@ -28,8 +31,8 @@ public abstract class TaskType { * @param author the name of the person (or people) who wrote it * @param description a short, simple description of the task type */ - public TaskType(String type, String author, String description) { - this.type = type; + public TaskType(@NotNull String type, String author, String description) { + this(type); this.author = author; this.description = description; } @@ -37,7 +40,9 @@ public abstract class TaskType { /** * @param type the name of the task type, should not contain spaces */ - public TaskType(String type) { + public TaskType(@NotNull String type) { + Objects.requireNonNull(type, "type cannot be null"); + this.type = type; } @@ -47,7 +52,9 @@ public abstract class TaskType { * * @param quest the {@link Quest} to register. */ - public final void registerQuest(Quest quest) { + public final void registerQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + if (!quests.contains(quest)) { quests.add(quest); } @@ -56,39 +63,39 @@ public abstract class TaskType { /** * Clears the list which contains the registered quests. */ - public final void unregisterAll() { + protected final void unregisterAll() { quests.clear(); } /** - * @return {@link List} of type {@link Quest} of all registered quests. + * @return immutable {@link List} of type {@link Quest} of all registered quests. */ - public final List<Quest> getRegisteredQuests() { - return quests; + public final @NotNull List<Quest> getRegisteredQuests() { + return Collections.unmodifiableList(quests); } - public final String getType() { + public final @NotNull String getType() { return type; } - public String getAuthor() { + public @Nullable String getAuthor() { return author; } - public String getDescription() { + public @Nullable String getDescription() { return description; } /** - * Called when Quests has finished registering all quests to the task type - * May be called several times if an operator uses /quests admin reload + * Called when Quests has finished registering all quests to the task type. + * May be called several times if an operator uses /quests admin reload. */ public void onReady() { // not implemented here } /** - * Called when a player starts a quest containing a task of this type + * Called when a player starts a quest containing a task of this type. */ public void onStart(Quest quest, Task task, UUID playerUUID) { // not implemented here @@ -99,9 +106,12 @@ public abstract class TaskType { } /** - * Called when Quests reloads the configuration - used to detect errors in the configuration of your task type + * Called when Quests reloads the configuration - used to detect errors in the configuration of your task type. + * + * @param root the root path for the config + * @param config the config itself */ - public List<ConfigProblem> validateConfig(String root, HashMap<String, Object> config) { + public @NotNull List<ConfigProblem> validateConfig(@NotNull String root, @NotNull HashMap<String, Object> config) { // not implemented here return Collections.emptyList(); } 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 284a1e0a..775d635a 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 @@ -3,8 +3,13 @@ package com.leonardobishop.quests.common.tasktype; import com.leonardobishop.quests.common.plugin.Quests; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; /** * The task type manager stores all registered task types and registers individual quests to each task type. @@ -30,17 +35,30 @@ public abstract class TaskTypeManager { return allowRegistrations; } - public ArrayList<TaskType> getTaskTypes() { - return taskTypes; + /** + * @return immutable {@link List} containing all registered {@link TaskType} + */ + public @NotNull List<TaskType> getTaskTypes() { + return Collections.unmodifiableList(taskTypes); } + /** + * Resets all quest to task type registrations. This does not clear the task types registered to the task type manager. + */ public void resetTaskTypes() { for (TaskType taskType : taskTypes) { - taskType.getRegisteredQuests().clear(); + taskType.unregisterAll(); } } - public void registerTaskType(TaskType taskType) { + /** + * Register a task type with the task type manager. + * + * @param taskType the task type to register + */ + public void registerTaskType(@NotNull TaskType taskType) { + Objects.requireNonNull(taskType, "taskType cannot be null"); + if (!allowRegistrations) { throw new IllegalStateException("No longer accepting new task types (must be done before quests are loaded)"); } @@ -48,7 +66,14 @@ public abstract class TaskTypeManager { taskTypes.add(taskType); } - public void registerQuestTasksWithTaskTypes(Quest quest) { + /** + * Register 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(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + if (allowRegistrations) { throw new IllegalStateException("Still accepting new task types (type registrations must be closed before registering quests)"); } @@ -60,9 +85,17 @@ public abstract class TaskTypeManager { } } - public TaskType getTaskType(String string) { + /** + * Get a registered task type by type + * + * @param type the type to check + * @return {@link TaskType} + */ + public @Nullable TaskType getTaskType(@NotNull String type) { + Objects.requireNonNull(type, "type cannot be null"); + for (TaskType taskType : taskTypes) { - if (taskType.getType().equalsIgnoreCase(string)) { + if (taskType.getType().equalsIgnoreCase(type)) { return taskType; } } |
