Moonflower-Mods / allium

Lua script loader and API for easy Minecraft modding
MIT License
44 stars 9 forks source link

Refactored your factory™ Improve lua <-> java type conversion, allow creation of basic instances #3

Closed Patbox closed 2 years ago

Patbox commented 2 years ago

For a demo, enable commented out libraries/maven in gradle and use this

-- Allium Test Plugin
-- (c) hugeblank 2022
-- no rights reserved
-- Test plugin that demonstrates the capabilities of Allium.

local placeholder_api = java.getRawClass("eu.pb4.placeholders.PlaceholderAPI"):getMethod("parseText", { java.getRawClass("net.minecraft.text.Text"), java.getRawClass("net.minecraft.server.network.ServerPlayerEntity") })
local text_parser = java.getRawClass("eu.pb4.placeholders.TextParser"):getMethod("parse", { java.getRawClass("java.lang.String") })

print(placeholder_api)

allium.onEvent("chat_message", function(e, player, message)
    -- this gets logged under the "test" namespace because the anonymous function gets called after the plugin returns
    -- the information that registers it
    print(player:getName():asString().." said "..message)

    player:sendMessage(placeholder_api:invoke(nil, { text_parser:invoke(nil, { "Formatted ->"..message }), player }), false)
end)

allium.onEvent("player_join", function(e, player)
    print(game.listPlayers()[1])
end)

local i = 0
allium.onEvent("player_tick", function(e, player)
    player:setExperienceLevel(i)
    i=(i+1)%40
    local world = player:getWorld()
    world:setBlockState(player:getBlockPos():down(), game.getBlock("minecraft:diorite"):getDefaultState())
end)

local arraylist = java.create("java.util.ArrayList", 4)

arraylist:add("Test")
arraylist:add("Hello World!")

print(arraylist:get(1))
print(java.getRawClass("eu.pb4.placeholders.PlaceholderResult"))
local placeholder_result = java.getRawClass("eu.pb4.placeholders.PlaceholderResult"):getMethod("value", { java.getRawClass("java.lang.String") })

java.getRawClass("eu.pb4.placeholders.PlaceholderAPI")
        :getMethod("register", { java.getRawClass("net.minecraft.util.Identifier"), java.getRawClass("eu.pb4.placeholders.PlaceholderHandler") })
        :invoke(nil, { java.create("net.minecraft.util.Identifier", "lua:placeholder"), java.cast("eu.pb4.placeholders.PlaceholderHandler", function(ctx)
            print("Called!")
            return placeholder_result:invoke(nil, {"Hello lua!"} )
        end
)})

-- This doesn't get logged under the "test" namespace since Allium doesn't know what plugin is running until the return
--print("Loading test plugin!\n", "Test", 1, 2, 3)

local rawReg = java.getRawClass("net.minecraft.util.registry.Registry")
print(rawReg)

local item = java.create("eu.pb4.polymer.api.item.SimplePolymerItem",
        java.create("net.minecraft.item.Item$Settings"),
        java.getRawClass("net.minecraft.item.Items"):getField("TNT"):get(nil)
)
print(item)
print({ rawReg, java.getRawClass("java.lang.String")})

rawReg:getMethod("register", { rawReg, java.getRawClass("java.lang.String"), java.getRawClass("java.lang.Object") } )
        :invoke(nil, { rawReg:getField("ITEM"):get(nil), "test:item", item } )

return {
    id = "test",
    version = "0.1.0",
    name = "Allium Test Plugin"
}
hugeblank commented 2 years ago

my refactored factory looks amazing thank you 🙏 Looking through the rest of the logic right now, looks great!