The set of quest-takers on the given stage
+ * @param questThe quest to spawn mobs for
+ * @param stageThe stage to spawn mobs for
+ * @param indexThe index of the kill location to spawn mobs for
+ */ + private void spawnMobsForStage(@NotNull MapThe location of the kill location's centre
- * @param radiusThe radius of the kill zone
- * @param entityTypeThe type of entity to kill in the kill zone
+ * @param radiusThe radius of the kill zone
+ * @param entityTypeThe type of entity to kill in the kill zone
* @return */ - private int entitiesInKillZone(Location killLocation, int radius, EntityType entityType) { + private int entitiesInKillZone(@NotNull Location killLocation, int radius, @NotNull EntityType entityType) { if (killLocation.getWorld() == null) { return 0; } @@ -195,8 +206,9 @@ public final class QuestMobSpawns extends JavaPlugin { * @param maxMobsThe maximum amount of mobs allowed in the kill zone
* @returnThe number of mobs to spawn
*/ - private int calculateMobNumber(MapThe entity with stored quest data
* @param playerA player
* @returnTrue if the player has the quest stored in the entity
*/ - private Boolean hasQuest(Entity entity, Player player) { + private Boolean hasQuest(@NotNull Entity entity, @NotNull Player player) { IQuester questPlayer = questsAPI.getQuester(player.getUniqueId()); - IQuest quest = getQuest(QuestMobHelper.getQuestId(entity)); + String questId = QuestMobHelper.getQuestId(entity); + if (questId == null) { + return false; + } + IQuest quest = getQuest(questId); if (quest == null) { // Remove the entity if the quest it belongs to no longer exists entity.remove(); return false; } - IStage stage = quest.getStage(QuestMobHelper.getStageId(entity)); - int index = QuestMobHelper.getIndex(entity); + Integer stageId = QuestMobHelper.getStageId(entity); + if (stageId == null) { + return false; + } + IStage stage = quest.getStage(stageId); + Integer index = QuestMobHelper.getIndex(entity); + if (index == null) { + return false; + } return questPlayer.getQuestData().containsKey(quest) && questPlayer.getCurrentStage(quest) == stage && questPlayer.getQuestData(quest).getMobNumKilled().get(index) < stage.getMobNumToKill().get(index); } - private IQuest getQuest(String questId) { + /** + * Gets the quest with the given id + * + * @param questIdThe id of the quest to get
+ * @returnThe quest, or null if no such quest exists
+ */ + private @Nullable IQuest getQuest(@NotNull String questId) { for (IQuest quest : questsAPI.getLoadedQuests()) { if (quest.getId().equalsIgnoreCase(questId)) { return quest; @@ -111,8 +133,14 @@ public class MobBlockDamageListener implements Listener { } return null; } - - private void cancelIfQuestMob(Cancellable cancellable, Entity entity) { + + /** + * Cancels a cancellable event if the given entity has been spawned by this plugin + * + * @param cancellableA cancellable event
+ * @param entityThe entity to check
+ */ + private void cancelIfQuestMob(@NotNull Cancellable cancellable, @NotNull Entity entity) { if (QuestMobHelper.isSpawnedMob(entity)) { cancellable.setCancelled(true); } diff --git a/src/main/java/net/knarcraft/questmobspawns/listener/MobDeathListener.java b/src/main/java/net/knarcraft/questmobspawns/listener/MobDeathListener.java index b3e492e..168f1ac 100644 --- a/src/main/java/net/knarcraft/questmobspawns/listener/MobDeathListener.java +++ b/src/main/java/net/knarcraft/questmobspawns/listener/MobDeathListener.java @@ -6,11 +6,15 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDropItemEvent; +import org.jetbrains.annotations.NotNull; +/** + * A listener for the death of quest mobs + */ public class MobDeathListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onMobDeath(EntityDeathEvent deathEvent) { + public void onMobDeath(@NotNull EntityDeathEvent deathEvent) { if (QuestMobHelper.isSpawnedMob(deathEvent.getEntity())) { deathEvent.getDrops().clear(); deathEvent.setDroppedExp(0); @@ -18,7 +22,7 @@ public class MobDeathListener implements Listener { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onMobDrop(EntityDropItemEvent dropEvent) { + public void onMobDrop(@NotNull EntityDropItemEvent dropEvent) { if (QuestMobHelper.isSpawnedMob(dropEvent.getEntity())) { dropEvent.setCancelled(true); } diff --git a/src/main/java/net/knarcraft/questmobspawns/util/QuestMobHelper.java b/src/main/java/net/knarcraft/questmobspawns/util/QuestMobHelper.java index 67dabd3..c49705c 100644 --- a/src/main/java/net/knarcraft/questmobspawns/util/QuestMobHelper.java +++ b/src/main/java/net/knarcraft/questmobspawns/util/QuestMobHelper.java @@ -6,7 +6,12 @@ import org.bukkit.NamespacedKey; import org.bukkit.entity.Entity; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +/** + * A helper class for quest mobs + */ public class QuestMobHelper { private static final NamespacedKey questMobKey = new NamespacedKey(QuestMobSpawns.getPlugin(), "questMob"); @@ -15,23 +20,57 @@ public class QuestMobHelper { private static final NamespacedKey mobIndexKey = new NamespacedKey(QuestMobSpawns.getPlugin(), "mobIndex"); private static final NamespacedKey killZoneLocationKey = new NamespacedKey(QuestMobSpawns.getPlugin(), "killZoneLocationKey"); - public static boolean isSpawnedMob(Entity entity) { + /** + * Checks whether the given entity has been spawned by this plugin + * + * @param entityThe entity to check
+ * @returnTrue if the entity was spawned by this plugin
+ */ + public static boolean isSpawnedMob(@NotNull Entity entity) { return Boolean.TRUE.equals(entity.getPersistentDataContainer().get(questMobKey, PersistentDataType.BOOLEAN)); } - public static void setSpawnedMob(Entity entity) { + /** + * Marks the given entity as spawned by this plugin + * + * @param entityThe entity to mark
+ */ + public static void setSpawnedMob(@NotNull Entity entity) { entity.getPersistentDataContainer().set(questMobKey, PersistentDataType.BOOLEAN, true); } - public static String getQuestId(Entity entity) { + /** + * Gets the id of the quest the given entity was spawned for + * + * @param entityThe entity to get the quest id from
+ * @returnThe quest id, or null if not a quest mob
+ */ + public static @Nullable String getQuestId(@NotNull Entity entity) { return entity.getPersistentDataContainer().get(questIdKey, PersistentDataType.STRING); } - public static Integer getStageId(Entity entity) { + /** + * Gets the id of the stage the given entity was spawned for + * + *The stage id is relative to the quest the stage belongs to.
+ * + * @param entityThe entity to get the stage id for
+ * @returnThe stage id, or null if not a quest mob
+ */ + public static @Nullable Integer getStageId(Entity entity) { return entity.getPersistentDataContainer().get(stageIdKey, PersistentDataType.INTEGER); } - public static Integer getIndex(Entity entity) { + /** + * Gets the index of this entity in the quest's "mobs to kill" array + * + *This property is necessary in order to check whether the player has already reached the required number of + * mob kills. This matters if the stage has some other clear property other than killing this type of mob.
+ * + * @param entityThe entity to check
+ * @returnThe entity's index, or null if not a quest mob
+ */ + public static @Nullable Integer getIndex(@NotNull Entity entity) { return entity.getPersistentDataContainer().get(mobIndexKey, PersistentDataType.INTEGER); } @@ -41,7 +80,7 @@ public class QuestMobHelper { * @param entityThe entity to get data for
* @returnAn array containing x,y,z,radius
*/ - public static int[] getKillZoneLocation(Entity entity) { + public static int[] getKillZoneLocation(@NotNull Entity entity) { return entity.getPersistentDataContainer().get(killZoneLocationKey, PersistentDataType.INTEGER_ARRAY); } @@ -52,7 +91,8 @@ public class QuestMobHelper { * @param questIdThe id of the quest the mob was spawned for
* @param killLocationInfoInformation about the kill location (x, y, z, range)
*/ - public static void setQuestData(Entity entity, String questId, int stageId, int mobIndex, int[] killLocationInfo) { + public static void setQuestData(@NotNull Entity entity, @NotNull String questId, int stageId, int mobIndex, + int[] killLocationInfo) { PersistentDataContainer persistentDataContainer = entity.getPersistentDataContainer(); persistentDataContainer.set(questIdKey, PersistentDataType.STRING, questId); persistentDataContainer.set(stageIdKey, PersistentDataType.INTEGER, stageId); @@ -67,7 +107,7 @@ public class QuestMobHelper { * @param location2The second location
* @returnThe 2d distance between the locations
*/ - public static double get2dDistance(Location location1, Location location2) { + public static double get2dDistance(@NotNull Location location1, @NotNull Location location2) { return Math.sqrt(Math.pow(location2.getX() - location1.getX(), 2) + Math.pow(location2.getZ() - location1.getZ(), 2)); }