Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Make the plugin folia-compatible #844

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object Versions {
const val SPIGOT_API = "1.19.4-R0.1-SNAPSHOT"
const val PAPER_API = "1.19.4-R0.1-SNAPSHOT"

const val ETERNALCODE_COMMONS = "1.1.3"
const val ETERNALCODE_COMMONS = "1.1.4-SNAPSHOT"
const val MULTIFICATION = "1.1.3"

const val JETBRAINS_ANNOTATIONS = "24.1.0"
Expand All @@ -13,7 +13,7 @@ object Versions {

const val PAPERLIB = "1.0.8"
const val ADVENTURE_PLATFORM = "4.3.4"
const val ADVENTURE_TEXT_MINIMESSAGE = "4.18.0-SNAPSHOT"
const val ADVENTURE_TEXT_MINIMESSAGE = "4.17.0"
const val ADVENTURE_PLATFORM_FACET = "4.3.4"
const val CDN_CONFIGS = "1.14.5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repositories {
maven { url = uri("https://repo.papermc.io/repository/maven-public/") }
maven { url = uri("https://repo.panda-lang.org/releases/") }
maven { url = uri("https://repo.eternalcode.pl/releases/") }
maven { url = uri("https://repo.eternalcode.pl/snapshots/") }
maven { url = uri("https://repo.extendedclip.com/content/repositories/placeholderapi/") }
maven("https://repo.stellardrift.ca/repository/snapshots/")
}
6 changes: 4 additions & 2 deletions eternalcore-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ dependencies {
implementation("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE_TEXT_MINIMESSAGE}")

testImplementation("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}")

implementation("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}")
implementation("com.eternalcode:eternalcode-commons-folia:${Versions.ETERNALCODE_COMMONS}")
implementation("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}")
}

eternalShadow {
Expand All @@ -54,8 +58,6 @@ eternalShadow {
)

// EternalCode Commons
library("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}")
library("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}")
libraryRelocate(
"com.eternalcode.commons",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.liteskullapi.SkullAPI;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.annotations.scan.feature.FeatureDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
Expand All @@ -20,26 +21,28 @@ public class SudoCommand {

private final Server server;
private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
public SudoCommand(Server server, NoticeService noticeService) {
public SudoCommand(Server server, NoticeService noticeService, Scheduler scheduler) {
this.server = server;
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute(name = "-console")
@Permission("eternalcore.sudo.console")
@DescriptionDocs(description = "Execute command as console", arguments = "<command>")
void console(@Context Viewer viewer, @Join String command) {
this.server.dispatchCommand(this.server.getConsoleSender(), command);
this.scheduler.sync(() -> this.server.dispatchCommand(this.server.getConsoleSender(), command));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider handling command execution result

The dispatchCommand method returns a boolean indicating whether the command was successful. Consider checking this return value to handle unsuccessful command executions and provide feedback to the user.

Apply this diff to handle the command execution result:

-this.scheduler.sync(() -> this.server.dispatchCommand(this.server.getConsoleSender(), command));
+this.scheduler.sync(() -> {
+    boolean success = this.server.dispatchCommand(this.server.getConsoleSender(), command);
+    if (!success) {
+        viewer.sendMessage("Command execution failed.");
+    }
+});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.scheduler.sync(() -> this.server.dispatchCommand(this.server.getConsoleSender(), command));
this.scheduler.sync(() -> {
boolean success = this.server.dispatchCommand(this.server.getConsoleSender(), command);
if (!success) {
viewer.sendMessage("Command execution failed.");
}
});

this.sendSudoSpy(viewer, command);
}

@Execute
@Permission("eternalcore.sudo.player")
@DescriptionDocs(description = "Execute command as player", arguments = "<player> <command>")
void player(@Context Viewer viewer, @Arg Player target, @Join String command) {
this.server.dispatchCommand(target, command);
this.scheduler.sync(() -> this.server.dispatchCommand(target, command));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider handling command execution result

Similarly, consider checking the result of dispatchCommand when executing commands as a player to handle any failures.

Apply this diff to handle the command execution result:

-this.scheduler.sync(() -> this.server.dispatchCommand(target, command));
+this.scheduler.sync(() -> {
+    boolean success = this.server.dispatchCommand(target, command);
+    if (!success) {
+        viewer.sendMessage("Command execution failed.");
+    }
+});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.scheduler.sync(() -> this.server.dispatchCommand(target, command));
this.scheduler.sync(() -> {
boolean success = this.server.dispatchCommand(target, command);
if (!success) {
viewer.sendMessage("Command execution failed.");
}
});

this.sendSudoSpy(viewer, command);
}

Expand All @@ -53,6 +56,7 @@ private void sendSudoSpy(Viewer viewer, String command) {

this.server.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("eternalcore.sudo.spy"))
.filter(player -> !player.getUniqueId().equals(viewer.getUniqueId()))
.forEach(player -> this.noticeService.create()
.notice(translation -> translation.sudo().sudoMessage())
.placeholder("{COMMAND}", command)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.eternalcode.core.feature.essentials.time;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import org.bukkit.World;

@Command(name = "day")
@Permission("eternalcore.day")
class DayCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
DayCommand(NoticeService noticeService) {
DayCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute
Expand All @@ -35,7 +38,9 @@ void dayWorld(@Context Viewer viewer, @Arg World world) {
}

private void setDay(Viewer viewer, World world) {
world.setTime(100);
this.scheduler.sync(() -> {
world.setTime(100);
});

this.noticeService.create()
.viewer(viewer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.eternalcode.core.feature.essentials.time;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import org.bukkit.World;

@Command(name = "night")
@Permission("eternalcore.night")
class NightCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
NightCommand(NoticeService noticeService) {
NightCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute
Expand All @@ -35,7 +38,9 @@ void nightWorld(@Context Viewer viewer, @Arg World world) {
}

private void setNight(Viewer viewer, World world) {
world.setTime(13700);
this.scheduler.sync(() -> {
world.setTime(13700);
});
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure notice is sent after time is set

Currently, the notice is sent immediately after scheduling the time change, which might result in the notice being delivered before the world time is actually set due to asynchronous execution. To guarantee that the viewer receives the notice after the time has been updated, consider moving the notice-sending code inside the scheduler.sync block.

Apply this diff to adjust the code:

 private void setNight(Viewer viewer, World world) {
     this.scheduler.sync(() -> {
         world.setTime(13700);

+        this.noticeService.create()
+            .viewer(viewer)
+            .placeholder("{WORLD}", world.getName())
+            .notice(translation -> translation.timeAndWeather().timeSetNight())
+            .send();
     });
-
-    this.noticeService.create()
-        .viewer(viewer)
-        .placeholder("{WORLD}", world.getName())
-        .notice(translation -> translation.timeAndWeather().timeSetNight())
-        .send();
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.scheduler.sync(() -> {
world.setTime(13700);
});
private void setNight(Viewer viewer, World world) {
this.scheduler.sync(() -> {
world.setTime(13700);
this.noticeService.create()
.viewer(viewer)
.placeholder("{WORLD}", world.getName())
.notice(translation -> translation.timeAndWeather().timeSetNight())
.send();
});
}


this.noticeService.create()
.viewer(viewer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eternalcode.core.feature.essentials.time;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
Expand All @@ -17,10 +18,12 @@
class TimeCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
TimeCommand(NoticeService noticeService) {
TimeCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute(name = "add")
Expand All @@ -32,7 +35,9 @@ void add(@Context Player player, @Context Viewer viewer, @Arg(TimeArgument.KEY)
@Execute(name = "add")
@DescriptionDocs(description = "Add specified amount of time to specified world", arguments = "<time> <world>")
void add(@Context Viewer viewer, @Arg(TimeArgument.KEY) int time, @Arg World world) {
world.setTime(world.getTime() + time);
this.scheduler.sync(() -> {
world.setTime(world.getTime() + time);
});

this.noticeService.create()
.viewer(viewer)
Expand All @@ -50,7 +55,9 @@ void set(@Context Player player, @Context Viewer viewer, @Arg(TimeArgument.KEY)
@Execute(name = "set")
@DescriptionDocs(description = "Sets specified time to specified world", arguments = "<time> <world>")
void set(@Context Viewer viewer, @Arg(TimeArgument.KEY) int time, @Arg World world) {
world.setTime(time);
this.scheduler.sync(() -> {
world.setTime(time);
});

this.noticeService.create()
.viewer(viewer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.eternalcode.core.feature.essentials.weather;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import org.bukkit.World;

@Command(name = "rain")
@Permission("eternalcore.rain")
class RainCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
RainCommand(NoticeService noticeService) {
RainCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute
Expand All @@ -35,8 +38,10 @@ void rainWorld(@Context Viewer viewer, @Arg World world) {
}

private void setRain(Viewer viewer, World world) {
world.setStorm(true);
world.setThundering(false);
this.scheduler.sync(() -> {
world.setStorm(true);
world.setThundering(false);
});
Comment on lines +41 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider handling exceptions within the scheduled task

To enhance robustness, consider adding exception handling within the scheduler.sync() lambda to catch and log any unexpected exceptions that may occur during world.setStorm(true) and world.setThundering(false). This ensures any issues are appropriately logged and do not silently fail.

Apply this diff to add exception handling:

 private void setRain(Viewer viewer, World world) {
     this.scheduler.sync(() -> {
+        try {
             world.setStorm(true);
             world.setThundering(false);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     });

     this.noticeService.create()
         .viewer(viewer)
         .placeholder("{WORLD}", world.getName())
         .notice(translation -> translation.timeAndWeather().weatherSetRain())
         .send();
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.scheduler.sync(() -> {
world.setStorm(true);
world.setThundering(false);
});
this.scheduler.sync(() -> {
try {
world.setStorm(true);
world.setThundering(false);
} catch (Exception e) {
e.printStackTrace();
}
});


this.noticeService.create()
.viewer(viewer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.eternalcode.core.feature.essentials.weather;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.time.DurationParser;
import dev.rollczi.litecommands.time.TemporalAmountParser;
import org.bukkit.World;
Expand All @@ -20,10 +21,12 @@
class SunCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
SunCommand(NoticeService noticeService) {
SunCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute
Expand All @@ -43,9 +46,11 @@ void sunWorld(@Context Viewer viewer, @Arg World world) {
}

private void setSun(Viewer viewer, World world) {
world.setClearWeatherDuration(20 * 60 * 10);
world.setStorm(false);
world.setThundering(false);
this.scheduler.sync(() -> {
world.setClearWeatherDuration(20 * 60 * 10);
world.setStorm(false);
world.setThundering(false);
});

this.noticeService.create()
.viewer(viewer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.eternalcode.core.feature.essentials.weather;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import org.bukkit.World;

@Command(name = "thunder", aliases = "storm")
@Permission("eternalcore.thunder")
class ThunderCommand {

private final NoticeService noticeService;
private final Scheduler scheduler;

@Inject
ThunderCommand(NoticeService noticeService) {
ThunderCommand(NoticeService noticeService, Scheduler scheduler) {
this.noticeService = noticeService;
this.scheduler = scheduler;
}

@Execute
Expand All @@ -35,8 +38,10 @@ void thunderWorld(@Context Viewer viewer, @Arg World world) {
}

private void setThunder(Viewer viewer, World world) {
world.setStorm(true);
world.setThundering(true);
this.scheduler.sync(() -> {
world.setStorm(true);
world.setThundering(true);
});

this.noticeService.create()
.viewer(viewer)
Expand Down
Loading