Skip to content

Instantly share code, notes, and snippets.

@MercerK
Created January 11, 2022 01:21
Show Gist options
  • Save MercerK/116d3be78ca43be1f71f4f4614597f5e to your computer and use it in GitHub Desktop.
Save MercerK/116d3be78ca43be1f71f4f4614597f5e to your computer and use it in GitHub Desktop.
import { Command } from '../../helpers/commands'
import { announce } from '../../utility/common'
const placeholders = {
// TODO: Add test to make sure it has % at end and start
essentials_Afk: '%essentials_afk%',
jobsReborn_Jobs: '%jobsr_user_jobs%',
}
const __undefined__ = '__undefined__'
const booleanValues: Record<string, boolean> = {
yes: true,
YES: true,
no: false,
NO: false,
[__undefined__]: false,
}
const deps = ['essentials', 'server', 'luckperms']
const PlaceholderApiHandler = () => {
const api = core.type('me.clip.placeholderapi.PlaceholderAPI' as any)
function register() {
const registered: string[] = [...api.getRegisteredIdentifiers()]
let shouldReload = false
for (const dep of deps) {
if (!registered.includes(dep)) {
console.log(`Downloading ${dep}`)
server.dispatchCommand(server.getConsoleSender(), `papi ecloud download ${dep}`)
shouldReload = true
}
}
if (shouldReload) {
setTimeout(() => server.dispatchCommand(server.getConsoleSender(), 'papi reload'), 3000)
}
}
function initialize() {
register()
initializeCommands()
}
function initializeCommands() {
Command({
name: 'isAfk',
execute: (_, player) => {
announce(`${isAfk(server.getPlayer(player))}`)
},
})
Command({
name: 'getJob',
execute: (_, playerName) => {
const player = server.getPlayer(playerName)
announce(`${hasJob(player) ? getJob(player) : 'Has no job'}`)
},
})
}
function getPlaceholders(player: obePlayer, message: string): string {
if (!api) return __undefined__
const result = api?.setPlaceholders(player, message)
if (result === message) return __undefined__
return result
}
const isAfk = (player: obePlayer): boolean =>
booleanValues[getPlaceholders(player, placeholders.essentials_Afk)] ?? false
// https://github.com/Zrips/Jobs/wiki/Placeholders
const hasJob = (player: obePlayer) =>
getPlaceholders(player, placeholders.jobsReborn_Jobs).length > 0
const getJob = (player: obePlayer) => getPlaceholders(player, placeholders.jobsReborn_Jobs)
return {
initialize,
hasJob,
getJob,
isAfk,
}
}
export const PlaceholderApi = PlaceholderApiHandler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment