diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-07-10 18:28:14 +0200 |
|---|---|---|
| committer | Krakenied <46192742+Krakenied@users.noreply.github.com> | 2024-08-28 11:37:11 +0200 |
| commit | b68f28a5ce382ff91834be8cf79e104396751d41 (patch) | |
| tree | faf50bb8c660b25296be5b4beb7c1f439ab5bef8 | |
| parent | 195b63109bc932334df78fc27dd108866d47f111 (diff) | |
Add dedicated getter for enums
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java | 5 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java | 102 |
2 files changed, 36 insertions, 71 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java index 2640a63c..c0d63901 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java @@ -8,6 +8,7 @@ import com.leonardobishop.quests.common.player.QPlayer; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.DyeColor; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -42,6 +43,8 @@ public final class ShearingTaskType extends BukkitTaskType { } final Entity entity = event.getEntity(); + final Colorable colorable = entity instanceof Colorable ? (Colorable) entity : null; + final DyeColor color = colorable != null ? colorable.getColor() : null; for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { Quest quest = pendingTask.quest(); @@ -55,7 +58,7 @@ public final class ShearingTaskType extends BukkitTaskType { continue; } - if (entity instanceof final Colorable colorable && !TaskUtils.matchColorable(this, pendingTask, colorable, player.getUniqueId())) { + if (colorable != null && !TaskUtils.matchEnum(DyeColor.class, this, pendingTask, color, player.getUniqueId(), "color", "colors")) { super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java index a2e76e11..14da111e 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java @@ -28,18 +28,16 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.material.Colorable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.UUID; -@SuppressWarnings("deprecation") +@SuppressWarnings({"deprecation", "BooleanMethodIsAlwaysInverted"}) public class TaskUtils { public static final String TASK_ATTRIBUTION_STRING = "<built-in>"; @@ -349,44 +347,6 @@ public class TaskUtils { return false; } - public static boolean matchColorable(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Colorable colorable, @NotNull UUID player) { - return matchColorable(type, pendingTask, colorable, player, "color", "colors"); - } - - public static boolean matchColorable(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Colorable colorable, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) { - Task task = pendingTask.task; - - DyeColor colorableColor = colorable.getColor(); - - List<String> checkColors = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey(stringKey) ? stringKey : listKey); - if (checkColors == null) { - return true; - } else if (checkColors.isEmpty()) { - return colorableColor == null; - } - - if (colorableColor == null) { - return false; - } - - DyeColor color; - - for (String colorName : checkColors) { - color = DyeColor.valueOf(colorName); - - type.debug("Checking against color " + color, pendingTask.quest.getId(), task.getId(), player); - - if (color == colorableColor) { - type.debug("Color match", pendingTask.quest.getId(), task.getId(), player); - return true; - } else { - type.debug("Color mismatch", pendingTask.quest.getId(), task.getId(), player); - } - } - - return false; - } - public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player) { return matchEntity(type, pendingTask, entity, player, "mob", "mobs"); } @@ -431,53 +391,55 @@ public class TaskUtils { static { try { - getEntitySpawnReasonMethod = Entity.class.getMethod("getEntitySpawnReason"); - } catch (NoSuchMethodException ignored) { + TaskUtils.getEntitySpawnReasonMethod = Entity.class.getMethod("getEntitySpawnReason"); + } catch (final NoSuchMethodException ignored) { // server version cannot support the method (doesn't work on Spigot) } } - public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player) { - return matchSpawnReason(type, pendingTask, entity, player, "spawn-reason", "spawn-reasons"); - } - - public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) { - Task task = pendingTask.task; - - List<String> checkSpawnReasons = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey(stringKey) ? stringKey : listKey); - if (checkSpawnReasons == null) { - return true; - } else if (checkSpawnReasons.isEmpty()) { - return false; - } - - if (getEntitySpawnReasonMethod == null) { - type.debug("Spawn reason is specified but the server software doesn't have the method necessary to get it", pendingTask.quest.getId(), task.getId(), player); + public static boolean matchSpawnReason(final @NotNull BukkitTaskType type, final @NotNull PendingTask pendingTask, final @NotNull Entity entity, + final @NotNull UUID player) { + if (TaskUtils.getEntitySpawnReasonMethod == null) { + type.debug("Spawn reason is specified but the server software doesn't have the method necessary to get it", pendingTask.quest.getId(), pendingTask.task.getId(), player); - // it is supported only on Paper so we simply ignore it + // it is supported only on Paper, so we simply ignore it return true; } - CreatureSpawnEvent.SpawnReason spawnReason; + final CreatureSpawnEvent.SpawnReason spawnReason; try { - spawnReason = (CreatureSpawnEvent.SpawnReason) getEntitySpawnReasonMethod.invoke(entity); - } catch (IllegalAccessException | InvocationTargetException e) { + spawnReason = (CreatureSpawnEvent.SpawnReason) TaskUtils.getEntitySpawnReasonMethod.invoke(entity); + } catch (final IllegalAccessException | InvocationTargetException e) { // it should never happen return false; } - CreatureSpawnEvent.SpawnReason reason; + return TaskUtils.matchEnum(CreatureSpawnEvent.SpawnReason.class, type, pendingTask, spawnReason, player, "spawn-reason", "spawn-reasons"); + } + + public static <E extends Enum<E>> boolean matchEnum(final @NotNull Class<E> enumClass, final @NotNull BukkitTaskType type, final @NotNull PendingTask pendingTask, + final @Nullable E enumValue, final @NotNull UUID player, final @NotNull String stringKey, final @NotNull String listKey) { + final Task task = pendingTask.task; + + final List<String> checkValueStrings = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey(stringKey) ? stringKey : listKey); + if (checkValueStrings == null) { + return true; + } else if (checkValueStrings.isEmpty()) { + return enumValue == null; + } + + E checkValue; - for (String spawnReasonName : checkSpawnReasons) { - reason = CreatureSpawnEvent.SpawnReason.valueOf(spawnReasonName); + for (final String checkValueString : checkValueStrings) { + checkValue = Enum.valueOf(enumClass, checkValueString); - type.debug("Checking against spawn reason " + reason, pendingTask.quest.getId(), task.getId(), player); + type.debug("Checking against enum value " + checkValue, pendingTask.quest.getId(), task.getId(), player); - if (reason == spawnReason) { - type.debug("Spawn reason match", pendingTask.quest.getId(), task.getId(), player); + if (checkValue == enumValue) { + type.debug("Enum value match", pendingTask.quest.getId(), task.getId(), player); return true; } else { - type.debug("Spawn reason mismatch", pendingTask.quest.getId(), task.getId(), player); + type.debug("Enum value mismatch", pendingTask.quest.getId(), task.getId(), player); } } |
