Skip to content

Commit

Permalink
ListTag.find_match
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 8, 2022
1 parent 1439748 commit 049671c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
@@ -1,5 +1,6 @@
package com.denizenscript.denizencore.objects.core;

import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.exceptions.TagProcessingException;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Fetchable;
Expand Down Expand Up @@ -1484,17 +1485,40 @@ else if (input.size() > 1) {
// returns all the numbered locations of elements that contain the text within a list,
// or an empty list if the list does not contain that item.
// For example: .find_all_partial[tw] on a list of "one|two|three|two" will return "2|4".
// TODO: Take multiple inputs? Or a matcher?
// -->
tagProcessor.registerStaticTag(ListTag.class, "find_all_partial", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find_all_partial[...] must have a value.");
return null;
}
String test = attribute.getParam().toUpperCase();
String test = CoreUtilities.toLowerCase(attribute.getParam());
ListTag positions = new ListTag();
for (int i = 0; i < object.size(); i++) {
if (object.get(i).toUpperCase().contains(test)) {// TODO: Efficiency
if (CoreUtilities.toLowerCase(object.get(i)).contains(test)) {
positions.add(String.valueOf(i + 1));
}
}
return positions;
});

// <--[tag]
// @attribute <ListTag.find_all_matches[<matcher>]>
// @returns ListTag
// @description
// returns all the numbered indices of elements that match within a list,
// using the system behind <@link language Advanced Script Event Matching>,
// or an empty list if the list does not contain that item.
// For example: .find_all_matches[t*] on a list of "one|two|three" will return "2|3".
// -->
tagProcessor.registerStaticTag(ListTag.class, "find_all_matches", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find_all_matches[...] must have a value.");
return null;
}
ListTag positions = new ListTag();
ScriptEvent.MatchHelper matcher = ScriptEvent.createMatcher(attribute.getParam());
for (int i = 0; i < object.size(); i++) {
if (matcher.doesMatch(object.get(i))) {
positions.add(String.valueOf(i + 1));
}
}
Expand All @@ -1505,19 +1529,19 @@ else if (input.size() > 1) {
// @attribute <ListTag.find_all[<element>]>
// @returns ListTag
// @description
// returns all the numbered locations of elements that match the text within a list,
// returns all the numbered indices of all entries that match the text within a list,
// or an empty list if the list does not contain that item.
// For example: .find_all[two] on a list of "one|two|three|two" will return "2|4".
// TODO: Take multiple inputs? Or a matcher?
// -->
tagProcessor.registerStaticTag(ListTag.class, "find_all", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find_all[...] must have a value.");
return null;
}
ListTag positions = new ListTag();
String test = CoreUtilities.toLowerCase(attribute.getParam());
for (int i = 0; i < object.size(); i++) {
if (object.get(i).equalsIgnoreCase(attribute.getParam())) {
if (CoreUtilities.toLowerCase(object.get(i)).equals(test)) {
positions.add(String.valueOf(i + 1));
}
}
Expand All @@ -1528,19 +1552,41 @@ else if (input.size() > 1) {
// @attribute <ListTag.find_partial[<element>]>
// @returns ElementTag(Number)
// @description
// returns the numbered location of the first partially matching element within a list,
// returns the numbered index of the first partially matching entry within a list,
// or -1 if the list does not contain that item.
// For example: .find_partial[tw] on a list of "one|two|three" will return "2".
// TODO: Take multiple inputs? Or a matcher?
// -->
tagProcessor.registerStaticTag(ElementTag.class, "find_partial", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find_partial[...] must have a value.");
return null;
}
String test = attribute.getParam().toUpperCase();
String test = CoreUtilities.toLowerCase(attribute.getParam());
for (int i = 0; i < object.size(); i++) {
if (object.get(i).toUpperCase().contains(test)) { // TODO: Efficiency
if (CoreUtilities.toLowerCase(object.get(i)).contains(test)) {
return new ElementTag(i + 1);
}
}
return new ElementTag(-1);
});

// <--[tag]
// @attribute <ListTag.find_match[<matcher>]>
// @returns ElementTag(Number)
// @description
// returns the numbered index of the first match within a list,
// using the system behind <@link language Advanced Script Event Matching>,
// or -1 if the list does not contain that item.
// For example: .find_match[t*] on a list of "one|two|three" will return "2".
// -->
tagProcessor.registerStaticTag(ElementTag.class, "find_match", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find_match[...] must have a value.");
return null;
}
ScriptEvent.MatchHelper matcher = ScriptEvent.createMatcher(attribute.getParam());
for (int i = 0; i < object.size(); i++) {
if (matcher.doesMatch(object.get(i))) {
return new ElementTag(i + 1);
}
}
Expand All @@ -1551,47 +1597,40 @@ else if (input.size() > 1) {
// @attribute <ListTag.find[<element>]>
// @returns ElementTag(Number)
// @description
// returns the numbered location of an element within a list,
// returns the numbered index of an entry within a list,
// or -1 if the list does not contain that item.
// For example: .find[two] on a list of "one|two|three" will return "2".
// TODO: Take multiple inputs? Or a matcher?
// -->
tagProcessor.registerStaticTag(ElementTag.class, "find", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.find[...] must have a value.");
return null;
}
String test = CoreUtilities.toLowerCase(attribute.getParam());
for (int i = 0; i < object.size(); i++) {
if (object.get(i).equalsIgnoreCase(attribute.getParam())) {
if (CoreUtilities.toLowerCase(object.get(i)).equals(test)) {
return new ElementTag(i + 1);
}
}
// TODO: This should be find_partial or something
/*
for (int i = 0; i < size(); i++) {
if (get(i).toUpperCase().contains(attribute.getParam().toUpperCase()))
return new Element(i + 1);
}
*/
return new ElementTag(-1);
});

// <--[tag]
// @attribute <ListTag.count[<element>]>
// @returns ElementTag(Number)
// @description
// returns how many times in the sub-list occurs.
// returns how many times a value in the list occurs.
// For example: a list of "one|two|two|three" .count[two] returns 2.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "count", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag ListTag.count[...] must have a value.");
return null;
}
String element = attribute.getParam();
String test = CoreUtilities.toLowerCase(attribute.getParam());
int count = 0;
for (String s : object) {
if (CoreUtilities.equalsIgnoreCase(s, element)) {
if (CoreUtilities.toLowerCase(s).equals(test)) {
count++;
}
}
Expand Down
Expand Up @@ -153,11 +153,11 @@ public ObjectTag adjust(ObjectTag object, Mechanism mechanism, ScriptEntry entry
object = ObjectFetcher.pickObjectFor(objectString, entry.context);
if (object instanceof ElementTag) {
FlaggableObject altObject = DenizenCore.implementation.simpleWordToFlaggable(objectString, entry);
if (altObject != null && !(altObject instanceof ElementTag)) {
return altObject;
if (altObject == null || (altObject instanceof ElementTag)) {
Debug.echoError("Unable to determine what object to adjust (missing object notation?), for: " + objectString);
return object;
}
Debug.echoError("Unable to determine what object to adjust (missing object notation?), for: " + objectString);
return object;
object = altObject;
}
}
if (object instanceof ListTag) {
Expand Down

0 comments on commit 049671c

Please sign in to comment.