Skip to content

Commit 806b9d7

Browse files
committed
add some perm helpers
1 parent 67a182f commit 806b9d7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/brickperms/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
1+
import { Player } from '@minecraft/server';
2+
import { checkPerm } from './permission.js';
13
import * as bricklib from '../bricklib/index.js';
24
export * from './permission.js';
35

46
bricklib.plugin.newPlugin('brickperms', () => {
57
/* no-op */
68
});
9+
10+
11+
/**
12+
* Returns an array of permission tags a player has.
13+
* @param plr The player.
14+
* @returns Permission tags of the player.
15+
*/
16+
export function getPermTags(plr: Player): string[]
17+
{
18+
return plr
19+
.getTags()?.filter(t => t.startsWith('p:')).map(v => v.slice(2)) ?? [];
20+
}
21+
22+
/**
23+
* Check if a player has a certain permission.
24+
* @param perm The permission to check.
25+
* @param plr The player to check for.
26+
* @returns True if the player has permission.
27+
*/
28+
export function hasPermission(perm: string, plr: Player): boolean
29+
{
30+
return checkPerm(perm, getPermTags(plr));
31+
}
32+
33+
/**
34+
* Asserts if a player has permission.
35+
* @param perm The perm node.
36+
* @param plr The player.
37+
* @throws This will throw an error if the player doesn't have permission.
38+
*/
39+
export function assertPermission(perm: string, plr: Player): void
40+
{
41+
if (!hasPermission(perm, plr))
42+
throw 'brickperms: no permission: ' + perm;
43+
}

0 commit comments

Comments
 (0)