Coder Social home page Coder Social logo

dynamicjs's Introduction

DynamicJS

A (maybe) maintained, (partially) typed and (should be) production-ready dynamic class generator for KubeJS, powered by ByteBuddy.

DynamicJS allows you to create Java class dynamically at runtime via KubeJS, the class created can inherit any class, implement any interface, and override/implement methods declared by those classes/interfaces.

This means that it's possible for you to make custom items that implement certain interface, like IDrillHead in Immersive Engineering, which turns the item into a drill head usable by IE's oil drill:

StartupEvents.registry("item", event => {
    /**
     * @type {Internal.IDrillHead}
     */
    let $IDrillHead = Java.loadClass("blusunrize.immersiveengineering.api.tool.IDrillHead")
    let $Tiers = Java.loadClass("net.minecraft.world.item.Tiers")
    let $ImmutableList = Java.loadClass("com.google.common.collect.ImmutableList")

    let $Foo = DynamicClass
        //Inherits from the item class
        .wraps(Java.loadClass("net.minecraft.world.item.Item"))
        //Can override everything in the original class
        .override("getMod", callinfo => callinfo.parent.getMod())
        //Declare that we need to implement such interface for this class,
        //and implement everything we need in declared interfaces...
        .impl($IDrillHead, implementation => implementation
            .override("beforeBlockbreak", callinfo => { })
            .override("afterBlockbreak", callinfo => { })
            //Return type will be converted by Rhino, but sometimes it might fail
            .override("getExtraBlocksDug", callinfo => $ImmutableList.of())
            .override("getMiningLevel", callinfo => $Tiers.WOOD)
            .override("getMiningSpeed", callinfo => {
                return callinfo.args[0].getOrCreateTag().getInt("sus") + 1
            })
            .override("getAttackDamage", callinfo => 100)
            .override("getHeadDamage", callinfo => 0)
            .override("getMaximumHeadDamage", callinfo => 1000)
            .override("damageHead", callinfo => {
                /**
                 * @type {Internal.ItemStack}
                 */
                let stack = callinfo.args[0]
                let susAmount = stack.getOrCreateTag().getInt("sus")
                stack.nbt.putInt("sus", susAmount + 1)
            })
            .override("getDrillTexture", callinfo => new ResourceLocation("kubejs", "sus"))
        )
        //Build the class to get everything done
        .build()

    let inst = new $Foo(new $ItemProperties()
        .tab(CREATIVE_TABS["kubejs.kubejs"])
        .stacksTo(1))
    console.info(inst.getClass())
    console.info(inst.getMod())
    console.info(inst instanceof $IDrillHead)
    event.custom("sus_drill", inst)
})

Will give you:

lol

Yes, a sussy drill.

Installation of ProbeJS is highly recommended, as there are internal docs describing detail type hinting for every method and parameter. Actually, there's even completion for method names you need to override!

The type hinting of arg lists in callback is not done yet, but after some time, it should be implemented as ProbeJS will have an update in docs handling.

dynamicjs's People

Contributors

prunoideae avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.