#interface
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Yes, you can use an interface without hardcoding a class for it by using dynamic proxy in Java. Dynamic proxy allows you to create a proxy object that implements the interface at runtime, without explicitly defining a class for it.
Here's an example of how you can use dynamic proxy to create an instance of an interface dynamically:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface MyInterface {
void doSomething();
}
class MyInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Dynamic Proxy: " + method.getName());
return null;
}
}
public class Main {
public static void main(String[] args) {
MyInvocationHandler handler = new MyInvocationHandler();
MyInterface myInterface = (MyInterface) Proxy.newProxyInstance(
Main.class.getClassLoader(),
new Class[]{MyInterface.class},
handler);
myInterface.doSomething(); // This will invoke the dynamic proxy
}
}
In this example, MyInvocationHandler is an implementation of InvocationHandler which handles the method invocations on the dynamic proxy object. The Proxy.newProxyInstance() method creates a new instance of the interface MyInterface using the provided invocation handler.
When you call methods on the myInterface object, it will be intercepted by the dynamic proxy and executed through the invoke() method in MyInvocationHandler. You can customize this method to perform any desired behavior.
Note that dynamic proxies can only be created for interfaces and not for concrete classes.
you cant instantiate an interface, but you can make have anonymous classes
not sure what you want to do though
ok so there is this papermc goal.java interface
but i wan't to use it dynamically if that makes sense
it requires these methods
to create a goal without having to pre-code it
what do you actually want to do?
whats wrong with creating a class that implements the goal
that would make it sorta (not really) hardcoded
i want to be able to create a new goal on the fly and add it

how is that hardcoded?
then create an instance of the class that implements Goal
i mean more like pre-coded
wdym by that
wait gimme a sec
ok so a example of a mob goal
this is how you would use a interface
but can i make one of these mob goals 'dynamically'
yeah and whats wrong with this?
nothing
^
lets say a user can create a new mob goal
you mean a user of your plugin?
yes for example
I think you want anonymous classes then
but you are very vague
it might not be what you want
let me make a classic userstories
i want to be able to create a new custom ai that i can add to a mob
yeah then you would just code it
make a class, give it a fitting name, implement Goal, and create the implementation of the missing methods of Goal
yes but dynamically?
and the @override methods
can you pls just say what you want to do, this sounds like an xy problem
xy?
What is it?
The XY problem is asking about your attempted solution rather than your actual problem. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.
* User wants to do X.
* User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
* User doesn't know how to do Y either.
* User asks for help with Y.
* Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
* After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.
* The problem occurs when people get stuck on what they believe is the solution and are unable step back and explain the issue in full.
What to do about it?
1. Always include information about a broader picture along with any attempted solution.
2. If someone asks for more information, do provide details.
3. If there are other solutions you've already ruled out, share why you've ruled them out. This gives more information about your requirements.
Remember that if your diagnostic theories were accurate, you wouldn't be asking for help right?
For examples, please refer to https://xyproblem.info/.
ok ill dumb it down
lets say i'm a user
i will create a new mob
create a ai for it
apply it & summon
how is the user supposed to provide the logic for the ai?
ill figure something
but the logic needs to come from somewhere
but right now i just wonder how ill do it in java
you want to use strategy pattern
Create static factory and use it
// Goal.java
public static Goal create(Supplier<Boolean> shouldActivate,
Supplier<Boolean> shouldStayActive,
Runnable start,
Runnable stop,
Runnable tick,
GoalKey<T> key, // if it's constant
EnumSet<GoalType> goalTypes) { // if they're constant
return new Goal() {
@Override
public boolean shouldActivate() {
return shouldActivate.get();
}
// and so on
}
}
When you'll going to "create an instance on the fly", you're just going to call this factory, which will create an Anonymous Instance
Goal.create(
new Random()::nextBoolean,
() -> false,
() -> System.out.println("I just had started!"),
() -> System.out.println("I just had stopped :("),
() -> System.out.println("tick"),
new GoalKey<>("Key"),
EnumSet.of(new GoalKey<>("Key"))
)
look up Lambda Functions, Functional Interfaces, Strategy Pattern
Anonymous Class Instances
the question is if thats actually what he wants
oh shish
User of your library will create an instance of Goal in their code. Creating an implementation of an interface doesn't mean hardcoding it. You're just providing an interface which your library code is using, and implementation can come from anywhere
exactly
hmm yes
in the end all i need is the new goal Class that contains the functionality
and just apply it to a mob
yes. Mob will just accept Goal as their argument, so it will be implementation agnostic
hey what am i supposed to do with GoalKey<T> key more specifically <T>
This is still not what OP wants I believe
Using this, you still have to write the code in java
But he wants the end user, who doesn't code in java, to make custom goals
i can convert it using a middle
thing
You will have to extend the Goal class, because you need to add your logic
But letting the user write some form of 'code' is quite hard
You basically have to create a small programming language
So yeah that's quite complex
i am
but i'm just adding on to that language
You are what?
The reply is to 3 messages at once
nvm
what do you mean by this?
you are creating a language?
The first thing you need to know is how the user is going to write the goals
You can't start coding before knowing that
i can first make this function
This doesn't help
^^
i'm sorry if i'm annoying you
but the full context of what i'm trying to do is quite large
Dw
But I feel like you're trying to do something too big
maybe, but ill try anyway
Have you done smaller java projects?
public static Goal create(Supplier<Boolean> shouldActivate,
Supplier<Boolean> shouldStayActive,
Runnable start,
Runnable stop,
Runnable tick,
GoalKey<?> key, // if it's constant
EnumSet<GoalType> goalTypes) { // if they're constant
return new Goal() {
@Override
public boolean shouldActivate() {
return shouldActivate.get();
}
@Override
public boolean shouldStayActive() {
return shouldStayActive.get();
}
@Override
public void start() {
stop.run();
}
@Override
public void stop() {
stop.run();
}
@Override
public void tick() {
tick.run();
}
@Override
public @NotNull GoalKey getKey() {
return key;
}
@Override
public @NotNull EnumSet<GoalType> getTypes() {
return goalTypes;
}
// and so on
};
}
is this wrong?
As I said that suggestion doesn't work for what you are trying to do
It needs to be clear what you want to do first
i prefer overloading my brain rather than just filling in method parameters
¯_(ツ)_/¯
You do you
You won't get much good guidance unless you specify what your end goal is / how it needs to look
but where it stands right now i need to be able to create a new Goal in runtime
with custom functionality
so maybe either reflection to create the new class or something like this
No, you're already too far ahead
This won't work like you think it will
Pls tell us first how the user input is gonna look like for the custom behaviour
are they gonna write plain Java?
or in a yaml or json file?
the user will write Skript code
that took long enough lol
ok
sorry but i dunno if explaining the framework of skript was relevant
everything is relevant
especially if I ask for it lol
ok sorry lol
ok i might be explainer but ill try
but then this should be a Skript extension of some sorts right
yes
but ill worry about that later
i'm just trying to understand the logics right now
hmm
wait what if i flip it around
what if on goalkey tick -> skript section/event
Well, idk much about Skript
You may be able to get some Runnable / Predicate from a bit Skript code, and then implement a Goal class that runs those Runnables / whatever for example when the entity ticks
i can make a skript event and throw some eventargs into it
or that, but then you'd have to have some way to distinguish between different entities
make i would have to know when tick() for example is called and then get the mob and key -> skript event
hmm could i make a new Goal (just for the key i guess)
and then when that Goal calls tick() i can call a skript event
Yeah, that seems like a good approach
hmm but how
how would i know when tick() is called
? theres a tick method in the goal class
you can override it
it will get called when the goal is ticked
yeah i need to create the Goal class but also know when the methods get called
if that makes sense
.. the method get's called when it gets called
you can put your code to run the Skript event inside that method
then it will run when it is ticked
imo create (registering) skript event kinda weird so i'm tryna wrap my head around it
source code for a onFirstJoin event
should i make another method into the runable?
Smells like a strategy 👀
?
Ah ntg recently looked into strategy pattern, you might wanna look into that. Might be what you wanna do.