Skip to content

Commit 7eca792

Browse files
committed
Update to findTag function
Better way of finding tag on entity
1 parent e9f59a5 commit 7eca792

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

scripts/modules/minecraft/lib/utils/others.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,15 @@ function findEntityAtPos([x, y, z], { dimension, entityIgnore } = {}) {
3737
};
3838
/**
3939
* @function findTag() - Check if an entity has a certain tag
40+
* @param {string} searchTag - Tag you are seraching for. WARNING: Color Coding with § is ignored
4041
* @param {string} entityRequirements - Requirements for the entity. Same syntax as Minecraft commands. For example: [type=player,r=10]
41-
* @param {string} searchTag - Tag you are seraching for
4242
* @returns boolean
4343
*/
4444
function findTag({ entityRequirements } = {}, { searchTag }) {
45-
try {
46-
const data = runCommand(`tag @e${entityRequirements ? `[${entityRequirements.replace(/\]|\[/g, '')}]` : ''} list`).result;
47-
const parsedData = data.statusMessage.replace(/(:(.§a))/g, ': ').replace(/(§r, §a)/g, ' ');
48-
const tagRegex = new RegExp(`:(.+)\\b${searchTag}\\b`, 'g');
49-
if(parsedData.match(tagRegex)) return true;
50-
} catch(err) {
51-
return { error: err };
52-
};
45+
const data = runCommand(`tag @e${entityRequirements ? `[${entityRequirements.replace(/\]|\[/g, '')}]` : ''} list`).result;
46+
if(data.error) return false;
47+
const allTags = data.statusMessage.match(/(?<=: ).*$/)[0].split('§r, §a');
48+
for(const tag of allTags) if(tag.replace(/§./g, '').match(new RegExp(`^${searchTag.replace(/§./g, '')}$`))) return true;
5349
};
5450
/**
5551
* @function getScore() - Get score of a player from a specific scoreboard

0 commit comments

Comments
 (0)