Skip to content

Commit

Permalink
More TaskUtils improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakenied committed May 26, 2023
1 parent e5e3e87 commit c7a5e4e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Expand Up @@ -36,4 +36,62 @@ public static boolean isNumeric(final CharSequence cs) {
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;
}
}
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -269,7 +270,7 @@ public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull Pending

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);
Expand All @@ -285,6 +286,36 @@ public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull Pending
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
Expand Down

0 comments on commit c7a5e4e

Please sign in to comment.