Skip to content

Instantly share code, notes, and snippets.

@lambdadog
Last active June 18, 2022 05:29
Show Gist options
  • Save lambdadog/2473d6cc77391f2196e0092f62e366d7 to your computer and use it in GitHub Desktop.
Save lambdadog/2473d6cc77391f2196e0092f62e366d7 to your computer and use it in GitHub Desktop.
Convenience function for using Java libraries in Grakkit

loadClasses - A function to simplify using external library JARs in Grakkit

Turns this:

const SC = {
  Socket: load('./plugins/grakkit/socketcluster-2.0.0.jar', 'io.github.sac.Socket'),
  BasicListener: load('./plugins/grakkit/socketcluster-2.0.0.jar', 'io.github.sac.BasicListener'),
  Emitter: {
    Listener: load('./plugins/grakkit/socketcluster-2.0.0.jar', 'io.github.sac.Emitter$Listener'),
  },
  ReconnectStrategy: load('./plugins/grakkit/socketcluster-2.0.0.jar', 'io.github.sac.ReconnectStrategy'),
}

into this:

const SC = loadClasses('./plugins/grakkit/socketcluster-2.0.0.jar', 'io.github.sac', [
  'Socket',
  'BasicListener',
  'Emitter.Listener',
  'ReconnectStrategy'
])
import { load } from '@grakkit/stdlib-paper'
// import { load } from '@grakkit/stdlib-minestom'
export const loadClasses = (jarPath: string, ns: string, classes: Array<string>): Object => {
let result = {}
for (const idx in classes) {
const className = classes[idx]
const classpath = `${ns}.${className.replaceAll('.','$')}`
const accu = load(jarPath, classpath)
const keys = className.split('.')
for (const key in keys.reverse()) {
const cache = accu
accu = {}
accu[keys[key]] = cache
}
result = { ...result, ...accu }
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment