#Modifying java ENUMS using ByteBuddy and interceptor at runtime
1 messages · Page 1 of 1 (latest)
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. 
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())
}
no need to delete the original message
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
I always suppress warnings lol, I didn't just to take the screenshot

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
my question is literally how to cast it correctly
it cant be casted
that's why it's not suppressed
so is there another way to do what I need? In this case, modify an existent enum during runtime using bytebuddy, as I said.
yeah I know
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
can you send me any example of doing it using ASM so?
any project that uses it, anything
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
I just need to this, no matter what I need to use
others here have experience with ASM too, they may be able to throw an example before i get to it
everyone needs something
gotta be patient, or do some independent work
look into ASM
alright
hmm
there may be some, i didnt look
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
Also what are u trying to achive with modifying the bukkit material enum?
Block's aren't in registries they are static iirc
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)
Well, I have a mod that add new blocks, dynamic download them from an API
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

Won't that still break esp w/ mc writing the world?
now you're speaking greek to me. it may be best to ask an MC related server, if they have any experience with it
Nopox may be onto something. we are Java devs, not all MC devs
I don't believe so. Bukkit uses the Material enum as a base of everything, even for the registry of the blocks/items.
I do mc dev stuff sometimes 🙋
Well, it's funny to say that i'm a MC plugin developer for more than 4 years and idk how to inject this
lol
I never made something more complex like this
ASM is the go-to tool
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
"no buddy involved" 😢
yeah
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
As I said...
...
I need to use plugins like fastasyncworldedit, gobrush, arceon, etc
About this, I can just modify the registry, this is not a problem.
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.
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
Paper will remove the Material enum? wt
yes
why?
because it sucks
yeah I agree lol
I'll get a link 2 sec
yeah
it would be perfect if paper had an API to add new blocks as I need

well, mojang doesn't have that
it's supposed to support vanilla clients
but maybe soon
yeah I know
lots of things are becoming data driven
please
the paper API itself is changing a lot
@outer comet when you found the link, ping me please

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
they do
still experimental though, and probably not referenced by plugins
so, it's possible by doing this? i'll take a look
it's possible to modify the registries if you opt in to the paper plugin stuff
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
well, it's possible to register new enchantments
It's not possible to modify the blocks yet
Well, I have a mod at the clientside with the custom blocks
i just need te server to "accept" them
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 ;]