summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrakenied <Krakenied1@gmail.com>2023-05-26 16:35:52 +0200
committerKrakenied <Krakenied1@gmail.com>2023-05-26 16:35:52 +0200
commitc7a5e4e4203322a2bc3862a0f5ba7b3382db8901 (patch)
tree6612bf23efdcc554a7c3394510cffd8d8504ccc7
parente5e3e87c0c49403f60b74dfdbb2f5904050a55c2 (diff)
More TaskUtils improvements
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/StringUtils.java58
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java33
2 files changed, 90 insertions, 1 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/StringUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/StringUtils.java
index c6c2aa04..e51d645e 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/StringUtils.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/StringUtils.java
@@ -36,4 +36,62 @@ public class StringUtils {
return true;
}
+ public static boolean equals(final CharSequence cs1, final CharSequence cs2, final boolean ignoreCase) {
+ if (cs1 == cs2) {
+ return true;
+ }
+ if (cs1 == null || cs2 == null) {
+ return false;
+ }
+ if (cs1.length() != cs2.length()) {
+ return false;
+ }
+ return regionMatches(cs1, ignoreCase, 0, cs2, 0, cs1.length());
+ }
+
+ static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
+ final CharSequence substring, final int start, final int length) {
+ if (cs instanceof String && substring instanceof String) {
+ return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
+ }
+ int index1 = thisStart;
+ int index2 = start;
+ int tmpLen = length;
+
+ // Extract these first so we detect NPEs the same as the java.lang.String version
+ final int srcLen = cs.length() - thisStart;
+ final int otherLen = substring.length() - start;
+
+ // Check for invalid parameters
+ if (thisStart < 0 || start < 0 || length < 0) {
+ return false;
+ }
+
+ // Check that the regions are long enough
+ if (srcLen < length || otherLen < length) {
+ return false;
+ }
+
+ while (tmpLen-- > 0) {
+ final char c1 = cs.charAt(index1++);
+ final char c2 = substring.charAt(index2++);
+
+ if (c1 == c2) {
+ continue;
+ }
+
+ if (!ignoreCase) {
+ return false;
+ }
+
+ // The real same check as in String.regionMatches():
+ final char u1 = Character.toUpperCase(c1);
+ final char u2 = Character.toUpperCase(c2);
+ if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
}
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 be3234bb..8f0c09ee 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
@@ -4,6 +4,7 @@ import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import com.leonardobishop.quests.bukkit.item.ParsedQuestItem;
import com.leonardobishop.quests.bukkit.item.QuestItem;
import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType;
+import com.leonardobishop.quests.bukkit.util.chat.Chat;
import com.leonardobishop.quests.common.config.ConfigProblem;
import com.leonardobishop.quests.common.config.ConfigProblemDescriptions;
import com.leonardobishop.quests.common.player.QPlayer;
@@ -269,7 +270,7 @@ public class TaskUtils {
EntityType mob;
- for (final String mobName : checkMobs) {
+ for (String mobName : checkMobs) {
mob = EntityType.valueOf(mobName);
type.debug("Checking against mob " + mob, pendingTask.quest.getId(), task.getId(), player);
@@ -285,6 +286,36 @@ public class TaskUtils {
return false;
}
+ public static boolean matchName(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @Nullable String name, boolean legacyColor, boolean ignoreCase, @NotNull UUID player) {
+ Task task = pendingTask.task;
+
+ List<String> checkNames = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey("name") ? "name" : "names");
+ if (checkNames == null) {
+ return true;
+ } else if (checkNames.isEmpty()) {
+ return name == null;
+ }
+
+ if (name == null) {
+ return false;
+ }
+
+ for (String s : checkNames) {
+ type.debug("Checking against name " + s, pendingTask.quest.getId(), task.getId(), player);
+
+ s = Chat.legacyColor(s);
+
+ if (StringUtils.equals(s, name, ignoreCase)) {
+ type.debug("Name match", pendingTask.quest.getId(), task.getId(), player);
+ return true;
+ } else {
+ type.debug("Name mismatch", pendingTask.quest.getId(), task.getId(), player);
+ }
+ }
+
+ return false;
+ }
+
public static int[] getAmountsPerSlot(Player player, QuestItem qi) {
int[] slotToAmount = new int[37];
// idx 36 = total