#interface

1 messages · Page 1 of 1 (latest)

tranquil bluff
#

hey can i use a interface without having to (hardcode) a Class for it
i want to make it dynamic

main cypressBOT
#

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

main cypressBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

frigid quest
#

you cant instantiate an interface, but you can make have anonymous classes

#

not sure what you want to do though

tranquil bluff
#

but i wan't to use it dynamically if that makes sense

frigid quest
#

wdym with dynamically?

#

just make a class that implements it

tranquil bluff
#

to create a goal without having to pre-code it

frigid quest
#

what do you actually want to do?

#

whats wrong with creating a class that implements the goal

tranquil bluff
#

i want to be able to create a new goal on the fly and add it

frigid quest
frigid quest
frigid quest
tranquil bluff
frigid quest
#

wdym by that

tranquil bluff
#

wait gimme a sec

tranquil bluff
frigid quest
tranquil bluff
#

nothing

frigid quest
#

I dont get what you want

#

whats "dynamically"

tranquil bluff
#

lets say a user can create a new mob goal

frigid quest
#

you mean a user of your plugin?

tranquil bluff
#

yes for example

frigid quest
#

I think you want anonymous classes then

#

but you are very vague

#

it might not be what you want

tranquil bluff
#

let me make a classic userstories

i want to be able to create a new custom ai that i can add to a mob

frigid quest
#

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

tranquil bluff
#

and the @override methods

frigid quest
#

can you pls just say what you want to do, this sounds like an xy problem

tranquil bluff
#

xy?

main cypressBOT
#

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/.

tranquil bluff
#

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

frigid quest
#

how is the user supposed to provide the logic for the ai?

tranquil bluff
#

ill figure something

frigid quest
#

but the logic needs to come from somewhere

tranquil bluff
#

but right now i just wonder how ill do it in java

olive pelican
#

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

frigid quest
#

the question is if thats actually what he wants

olive pelican
#

oh shish

tranquil bluff
#

huh?

#

should this create a new Class that implements Goal<?>?

olive pelican
tranquil bluff
#

hmm yes
in the end all i need is the new goal Class that contains the functionality

#

and just apply it to a mob

olive pelican
#

yes. Mob will just accept Goal as their argument, so it will be implementation agnostic

tranquil bluff
tranquil bluff
#

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

tranquil bluff
#

but i'm just adding on to that language

#

You are what?

#

The reply is to 3 messages at once

#

nvm

tranquil bluff
#

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

tranquil bluff
tranquil bluff
tranquil bluff
#

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

tranquil bluff
#

¯_(ツ)_/¯

#

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

tranquil bluff
#

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

tranquil bluff
#

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

tranquil bluff
#

then it will run when it is ticked

#

imo create (registering) skript event kinda weird so i'm tryna wrap my head around it

tranquil bluff
#

should i make another method into the runable?

gloomy shadow
tranquil bluff
#

?

gloomy shadow
#

Ah ntg recently looked into strategy pattern, you might wanna look into that. Might be what you wanna do.