Help with "constant loop"

Hey everyone,

I am new to Java and Sponge Plugin developpement so I need some help.
I would like to ake a plugin that “constantly” check every player in a specific list.
I want to apply this specific players some potion effects when they are nearby each other, but i want the effect to stop once the players are not close enough.

i don’t know how to make this loop that is constantly running and i don’t want to make the server lag either.

Thanks a lot for any help.
If there is any question, about what i want, just ask.

Try using the scheduler. It allows you to delay a task and/or make repeating tasks.

Scheduler scheduler = Sponge.getScheduler();
Task.Builder taskBuilder = scheduler.createTaskBuilder();

taskBuilder.execute(
    () -> {
        // Check for players near each other and give/take away potion effects
    }).intervalTicks(1) // Run every tick. Increase this to make this run less often
    .submit();

Is this what you need or do you also need help with applying potion effects?


Disclaimer:
I have not tried this code. I have not even used the scheduler before. I am just reading off of the Sponge Docs.

1 Like

Ok thanks.
I thought there were another, like “clean”, way to do that. I will try this.

I must admit that I haven’t been searching a lot for the potion effects yet. I tought it would have been something as easy as using a “setPotionEffect(…)” function from the Player class, but it seems it doesn’t exist ^^

I would appreciate that you help me with that one too :smiley:

I tried the method of the documentation you game me, but I couldn’t figure out why Eclipse underlined this part in red:

 () -> {

So I tried to use the other method, and here is what I have now:

Scheduler scheduler = Sponge.getScheduler();
Task.Builder taskBuilder = scheduler.createTaskBuilder();
taskBuilder.execute(new Runnable() {
____ public void run() {
________ checkingFunction(); // the funtion that will check the players in the list
____ }
}).interval(5, TimeUnit.SECONDS).submit(this);

I had to put this code in the initialisation event in order for the code to work. I don’t know if that is a good or a bad thing though.

About the potion effect, in found another topic that was about it. So here is what i have:

Player list[]; //this list will be modified by another function
private void checkingFunction() {
for(int i=0; i<list.length; i++){
____ List<PotionEffect> potion = new ArrayList<PotionEffect>();
____ potion.add(PotionEffect.builder().particles(false)
________ .potionType(PotionEffectTypes.STRENGTH)
________ .amplifier(1).duration(5).build());
____ list[i].offer(Keys.POTION_EFFECTS,potion);
____ }
}

It seems to work fine by now. But i would like to now if I have done something bad. If something can have a negativ effect on the server.

Thanks a lot for the help.

(By the way, as you may have noticed, i can’t figure out how to quote a piece of code correcly. sorry about that)

You must set the language level to Java 8, Sponge requires Java 8. (Right click project -> Properties -> Java Compiler -> Compiler compliance level)

Put it in the GameStartingServerEvent, then cancel the task (save the task object to a field or something) when the GameStoppingServerEvent is fired.

use three backticks (```) to denote a code block. The language can also be given
```java
System.out.println(“Hello World”);
```
Produces:

System.out.println("Hello World");

Hey,

Thanks for the help. I have been fixing some of the issues i had thanks to you.
I recreated the project using java 8 correctly and now Eclipse seems to better understand the taskBuider thing. But now is wants me to add an Object as argument for the .submit()

taskBuilder.execute(
                () -> {
                    System.out.print("________"); // something to see if that works
                }).interval(5, TimeUnit.SECONDS).submit( );

i found this in the javadocs:

Task submit(Object plugin)
Submits the task to the scheduler and returns the task that was created.

Parameters:plugin - The owner of the task

but i can’t understand what that means. i tried giving “plugin”, or the project’s name, the name of the class, but nothing works

You need to have the Object of your main class. You could use this if you’re doing this from your main class.
.submit(this);
If you’re not on your main plugin class, then you could pass around your main plugin class, or access a singleton of it.
Ex with singleton:
.submit(MyMainClass.instance)
Where instance is stored similarly to this:

public class MyMainClass {
     public static MyMainClass instance;

     @Listener
         public void init(GameInitializationEvent event) {
              instance = this;
         }
}