Adding custom Java code

Is there a way I can include a JAR (maybe by adding it into addons) and import the necessary classes into my rules and use them there?

The Rules DSL is great but I have a lot of repeating code that would be great to refactor into a class.

For bonus points, how would I be able to access Items in my Java code?

If you have a lot of repeating code, why not make it a lambda, which can be reused in other rules ?

How would you share a lambda across rules in different files?

which actually reminds me of another question I had - is there a global scope accessible somewhere? It would be very handy for declaring constants (val). I’ve noticed I cannot access Items in the rule file scope (ie variables declared at the top of the file - not within the rule scope).

lambdas cannot be shared across different files.

Did you look at this?

To answer your specific questions, the answer is no. You cannot create your own classes and use them in Rules DSL. There is no global context somewhere. Well, that last part isn’t strictly true. You do have access to a global context: Items.

There are lots of ways you can declare constants:

Depending on the nature of your repeated code, there are a many techniques that can be used to avoid that.

I’m tired of retyping this so I wrote a DP: Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL. It’s more of a meta-DP since everything is already documented in DPs and tutorials but people are either not finding them or not looking for them.

1 Like