#Modifying java ENUMS using ByteBuddy and interceptor at runtime

1 messages · Page 1 of 1 (latest)

stiff flameBOT
#

<@&1008423204219531294> please have a look, thanks.

solar spade
#

Modifying java ENUMS using ByteBuddy and interceptor at runtime

#

Well, recently I asked here if it is possible to modify an enum at runtime and if yes, how to do this.

Basically, what everyone said was that I should use Instrumentation to do this. Well, i'm using ByteBuddy.

Here's my Kotlin code and the error I'm receiving. Of course the error is because of the cast as Array<out Material>. But, how can I do this? I found almost nothing about modifying ENUMS using ByteBuddy. sadCat

data class MaterialInjectionData(
        val name: String,
        val maxStack: Int,
        val durability: Short,
        val legacy: Boolean,
        val key: NamespacedKey
    )

    internal fun injectNewMaterialValues(newValues: List<MaterialInjectionData>) {
        ByteBuddyAgent.install()

        val newEnumValues = ByteBuddy()
            .makeEnumeration(newValues.map { it.name })
            .make()
            .load(Material::class.java.classLoader)
            .loaded.enumConstants as Array<out Material>

        ByteBuddy()
            .redefine(Material::class.java)
            .method(ElementMatchers.named("values"))
            .intercept(FixedValue.value(newEnumValues))
            .make()
            .load(Material::class.java.classLoader, ClassReloadingStrategy.fromInstalledAgent())

        ByteBuddy()
            .redefine(Material::class.java)
            .method(ElementMatchers.named("valueOf"))
            .intercept(FixedValue.value(newEnumValues.associateBy { it.name }))
            .make()
            .load(Material::class.java.classLoader, ClassReloadingStrategy.fromInstalledAgent())
    }
lime geyser
#

first things first, address the warnings

#

this is a complex topic, the last thing we need are overlooked warnings

#

either solve it, or suppress it if you know its 100% not related to the issue

solar spade
lime geyser
#

look into that first

#

the less we see, the easier it is to help

#

and warnings could be highlighting logic errors

#

help us help you

solar spade
#

my question is literally how to cast it correctly

lime geyser
#

it cant be casted

solar spade
#

that's why it's not suppressed

lime geyser
#

look at the types at play

#

cant convert Enum$ByteBudy$... to Material

solar spade
solar spade
lime geyser
#

via ByteBuddy? no clue, i don't use ByteBuddy. idk why they wouldn't just inject it as the original type

#

there could be some configuration you could apply maybe

#

there could be a step you're missing

#

that step could be what transforms it from the ByteBuddy perspective to the actual perspective

#

it is possible to inject a value into an enum. for example, with ASM, you could just throw it in

#

apparently ByteBuddy has some intermediate system going on

solar spade
#

any project that uses it, anything

lime geyser
#

not even home rn, and when i get home, i got a whole list of things to do

#

i could write up an example by the end of the week

#

idk of any online existing examples

solar spade
#

I just need to this, no matter what I need to use

lime geyser
#

others here have experience with ASM too, they may be able to throw an example before i get to it

lime geyser
#

gotta be patient, or do some independent work

#

look into ASM

solar spade
#

alright

solar spade
lime geyser
#

all i know is that ASM is straight to the point. its not introducing any intermediate types like ByteBuddy did

#

you are working directly with the bytecode, no buddy

outer comet
# solar spade hmm

Also what are u trying to achive with modifying the bukkit material enum?

#

Block's aren't in registries they are static iirc

lime geyser
#

dynmaic updates. there may be new materials, which they want Material to support

#

having Material as an enum is already poor design, they're just working around it (by the looks of it)

solar spade
#

What I want to do is inject these new blocks into the Material so the client will be able to join the Paper server and use the blocks

#

"why just don't you use a modded server ..."

I need some plugins that only exists as plugins, there's no "mod version" of them

outer comet
#

Won't that still break esp w/ mc writing the world?

lime geyser
#

Nopox may be onto something. we are Java devs, not all MC devs

solar spade
outer comet
solar spade
#

lol

#

I never made something more complex like this

lime geyser
#

there are options like javassist, bytebuddy, etc... but when all else fails, ASM does not fail

#

and yeah, its not going to be the easiest. you are directly manipulating bytecode, no buddy involved

#

but its also not gonna be that hard, since all you wanna do is update an enum

outer comet
#

the enum is mostly meant as API

#

why aren't u using a fabric/forge server if you want to add custom blocks?

#

even when the spigot dev touches the enum everything breaks, so it'll definitely not work when u do it at runtime

#

MC itself uses registries, and those won't get updated if u change the enum

solar spade
#

I need to use plugins like fastasyncworldedit, gobrush, arceon, etc

solar spade
#

The problem is that plugins use the Material enum as the server blocks reference.

For example, FAWE (worldedit) uses the material enum to match a material when using its commands.

outer comet
#

mb skipped that part

#

fair enough

#

keep in mind at least paper will soon move away from that enum

#

not sure about spigot, spigot is not really relevant anyway

#

so those will have to move to the registry, probably, or some API to access the block list

solar spade
outer comet
#

yes

solar spade
#

why?

outer comet
#

because it sucks

solar spade
outer comet
#

I'll get a link 2 sec

solar spade
#

it would be perfect if paper had an API to add new blocks as I need

outer comet
#

well, mojang doesn't have that

#

it's supposed to support vanilla clients

#

but maybe soon

solar spade
outer comet
#

lots of things are becoming data driven

solar spade
solar spade
#

@outer comet when you found the link, ping me please

outer comet
#

yeah I thought I could find it quickly

#

I will

#

oh, apparently it's not gonna be that soon

#

I guess it's just the plan of paper for the restructure

#

I got thrown off by the fact they exposed the registries to paper plugins lately

#

ok so I guess this won't be soon enough to worry about

#

@solar spade

solar spade
outer comet
#

not sure that'd do what you want for the enum specifically

#

since the enum is API, people will probably use that over the registries for now

solar spade
outer comet
#

yes

#

with datapacks now as well though

solar spade
#

It's not possible to modify the blocks yet

outer comet
#

I don't think the block registries get sent to the client yet

#

hm

#

ok

solar spade
#

Well, I have a mod at the clientside with the custom blocks

#

i just need te server to "accept" them

outer comet
#

Yea
I don't think I can help you any further with this
I'd just go with a modded server at that point, there's mod alternatives for almost any plugin, and if not write it yourself ;]