Skip to content

Commit b43ed85

Browse files
authored
feature: Add better notification system (#176)
2 parents f9585dc + 201b18e commit b43ed85

18 files changed

+515
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ build/
1515
out/
1616
!**/src/main/**/out/
1717
!**/src/test/**/out/
18+
.idea/*
1819

1920
### Eclipse ###
2021
.apt_generated

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ dependencies {
6262
implementation(libs.cloud.command.annotations)
6363
implementation(libs.semver)
6464
implementation(libs.adventure.text.feature.pagination)
65+
implementation(libs.adventure.text.discord)
66+
implementation(libs.jda.webhook)
67+
implementation(libs.jda)
6568
implementation(libs.guice)
6669
implementation(libs.jakarta.inject)
6770
annotationProcessor(libs.cloud.command.annotations)

settings.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ dependencyResolutionManagement {
3131
version("cloudcommandExtras", "2.0.0-SNAPSHOT")
3232

3333
version("adventure-text-feature-pagination", "4.0.0-SNAPSHOT")
34+
version("adventure-text-discord-serializer", "4.3.0")
35+
version("jda", "5.6.1")
36+
version("jda-webhook", "0.8.4")
37+
3438
version("semver", "0.10.2")
3539
version("guice", "7.0.0")
3640
version("jakarta-inject", "2.0.1")
@@ -64,6 +68,9 @@ dependencyResolutionManagement {
6468

6569
library("paper", "io.papermc.paper", "paper-api").versionRef("paper")
6670
library("adventure.text.feature.pagination", "net.kyori", "adventure-text-feature-pagination").versionRef("adventure-text-feature-pagination")
71+
library("adventure.text.discord", "dev.vankka", "mcdiscordreserializer").versionRef("adventure-text-discord-serializer")
72+
library("jda", "net.dv8tion", "JDA").versionRef("jda")
73+
library("jda-webhook", "club.minnced", "discord-webhooks").versionRef("jda-webhook")
6774
library("bstats", "org.bstats", "bstats-bukkit").versionRef("bstats")
6875

6976
library("cloud.command.paper", "org.incendo", "cloud-paper").versionRef("cloudcommandPaper")

src/main/java/net/onelitefeather/antiredstoneclockremastered/AntiRedstoneClockRemastered.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public final class AntiRedstoneClockRemastered extends JavaPlugin {
3030
public void onLoad() {
3131
saveDefaultConfig();
3232
reloadConfig();
33+
saveConfig();
3334
injector = Guice.createInjector(Stage.PRODUCTION, Arrays.asList(
3435
new PlatformModule(this),
3536
new TranslationModule(),

src/main/java/net/onelitefeather/antiredstoneclockremastered/injection/ServiceModule.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
88
import net.onelitefeather.antiredstoneclockremastered.api.PlotsquaredSupport;
99
import net.onelitefeather.antiredstoneclockremastered.api.WorldGuardSupport;
10+
import net.onelitefeather.antiredstoneclockremastered.service.api.NotificationService;
1011
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
1112
import net.onelitefeather.antiredstoneclockremastered.service.api.RegionService;
1213
import net.onelitefeather.antiredstoneclockremastered.service.factory.RedstoneClockServiceFactory;
1314
import net.onelitefeather.antiredstoneclockremastered.service.UpdateService;
1415
import net.onelitefeather.antiredstoneclockremastered.service.api.TranslationService;
15-
import net.onelitefeather.antiredstoneclockremastered.service.impl.LegacyTranslationService;
16-
import net.onelitefeather.antiredstoneclockremastered.service.impl.ModernTranslationService;
16+
import net.onelitefeather.antiredstoneclockremastered.service.impl.*;
1717
import net.onelitefeather.antiredstoneclockremastered.utils.CheckTPS;
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
@@ -37,8 +37,24 @@ protected void configure() {
3737

3838
@Provides
3939
@Singleton
40-
public RedstoneClockService provideRedstoneClockService(AntiRedstoneClockRemastered plugin, RegionService regionService,
41-
PlotsquaredSupport plotsquaredSupport, WorldGuardSupport worldGuardSupport) {
42-
return RedstoneClockServiceFactory.createService(plugin, regionService, plotsquaredSupport, worldGuardSupport);
40+
public RedstoneClockService provideRedstoneClockService(AntiRedstoneClockRemastered plugin,
41+
RegionService regionService,
42+
PlotsquaredSupport plotsquaredSupport,
43+
WorldGuardSupport worldGuardSupport,
44+
NotificationService notificationService) {
45+
return RedstoneClockServiceFactory.createService(plugin, regionService, plotsquaredSupport, worldGuardSupport,
46+
notificationService);
4347
}
48+
49+
@Provides
50+
@Singleton
51+
public NotificationService providesNotificationService(AntiRedstoneClockRemastered antiRedstoneClockRemastered,
52+
RegionService regionService) {
53+
var adminNotifications = new AdminNotificationService(antiRedstoneClockRemastered, null);
54+
var consoleNotification = new ConsoleNotificationService(antiRedstoneClockRemastered, adminNotifications);
55+
var signNotifications = new SignNotificationService(antiRedstoneClockRemastered, consoleNotification, regionService);
56+
var discordNotification = new DiscordNotificationService(antiRedstoneClockRemastered, signNotifications);
57+
return discordNotification;
58+
}
59+
4460
}

src/main/java/net/onelitefeather/antiredstoneclockremastered/listener/PlayerListener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@ private void onBlockBreak(BlockBreakEvent blockBreakEvent) {
4646
break recheck;
4747
}
4848
}
49+
50+
@EventHandler(priority = EventPriority.HIGHEST)
51+
private void onSignBlockBreak(BlockBreakEvent blockBreakEvent) {
52+
var block = blockBreakEvent.getBlock();
53+
var hasKey = block.hasMetadata(Constants.META_KEY_ARCR_SIGN);
54+
if (hasKey) {
55+
blockBreakEvent.setDropItems(false);
56+
}
57+
}
4958
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.onelitefeather.antiredstoneclockremastered.service.api;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.bukkit.Location;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
/**
8+
* Service for sending notifications.
9+
*
10+
* @author TheMeinerLP
11+
* @version 1.0.0
12+
* @since 2.4.0
13+
*/
14+
public interface NotificationService {
15+
16+
/**
17+
* Get the notification message for the given location.
18+
* @param location where the notification gets sent from
19+
* @return the notification message
20+
*/
21+
@Nullable Component getNotificationMessage(Location location);
22+
23+
/**
24+
* Send a notification message to the given audience.
25+
* @param location where the notification gets sent from
26+
*/
27+
void sendNotificationMessage(Location location);
28+
29+
boolean isEnabled();
30+
}

src/main/java/net/onelitefeather/antiredstoneclockremastered/service/api/RegionService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public interface RegionService {
1919
*/
2020
void executeInRegion(Location location, Runnable task);
2121

22+
/**
23+
* Executes a task within the context of a specific region.
24+
* @param location The location to determine the region.
25+
* @param task The task to execute.
26+
* @param delay The delay in ticks before the task is executed.
27+
*/
28+
void executeInRegion(Location location, Runnable task, long delay);
29+
2230
/**
2331
* Checks if the current server instance is the owner of the region at the specified location.
2432
*

src/main/java/net/onelitefeather/antiredstoneclockremastered/service/factory/RedstoneClockServiceFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
44
import net.onelitefeather.antiredstoneclockremastered.api.PlotsquaredSupport;
55
import net.onelitefeather.antiredstoneclockremastered.api.WorldGuardSupport;
6+
import net.onelitefeather.antiredstoneclockremastered.service.api.NotificationService;
67
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
78
import net.onelitefeather.antiredstoneclockremastered.service.api.RegionService;
89
import net.onelitefeather.antiredstoneclockremastered.service.impl.BukkitRedstoneClockService;
@@ -35,7 +36,10 @@ private RedstoneClockServiceFactory() {
3536
* @return the appropriate RedstoneClockService implementation
3637
*/
3738
@NotNull
38-
public static RedstoneClockService createService(@NotNull AntiRedstoneClockRemastered plugin, RegionService regionService, PlotsquaredSupport plotsquaredSupport, WorldGuardSupport worldGuardSupport) {
39+
public static RedstoneClockService createService(@NotNull AntiRedstoneClockRemastered plugin,
40+
RegionService regionService, PlotsquaredSupport plotsquaredSupport,
41+
WorldGuardSupport worldGuardSupport,
42+
NotificationService notificationService) {
3943
if (FoliaHelper.isFolia()) {
4044
LOGGER.info("Folia detected - using FoliaRedstoneClockService");
4145
// Uncomment when ready to enable Folia support:
@@ -44,6 +48,6 @@ public static RedstoneClockService createService(@NotNull AntiRedstoneClockRemas
4448
}
4549

4650
LOGGER.info("Using BukkitRedstoneClockService");
47-
return new BukkitRedstoneClockService(plugin, regionService, worldGuardSupport, plotsquaredSupport);
51+
return new BukkitRedstoneClockService(plugin, regionService, worldGuardSupport, plotsquaredSupport, notificationService);
4852
}
4953
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.onelitefeather.antiredstoneclockremastered.service.impl;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.event.ClickEvent;
5+
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
6+
import net.onelitefeather.antiredstoneclockremastered.service.api.NotificationService;
7+
import net.onelitefeather.antiredstoneclockremastered.utils.Constants;
8+
import org.bukkit.Bukkit;
9+
import org.bukkit.Location;
10+
import org.bukkit.entity.Player;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
public final class AdminNotificationService implements NotificationService {
15+
16+
private final AntiRedstoneClockRemastered plugin;
17+
private final NotificationService notificationService;
18+
19+
public AdminNotificationService(@NotNull AntiRedstoneClockRemastered plugin,
20+
@Nullable NotificationService notificationService) {
21+
this.plugin = plugin;
22+
this.notificationService = notificationService;
23+
}
24+
25+
@Override
26+
public Component getNotificationMessage(@NotNull Location location) {
27+
return Component.translatable("service.notify.detected.clock")
28+
.arguments(AntiRedstoneClockRemastered.PREFIX,
29+
Component.text(location.getBlockX()),
30+
Component.text(location.getBlockY()),
31+
Component.text(location.getBlockZ()),
32+
Component.empty().clickEvent(ClickEvent.callback(audience -> {
33+
if (audience instanceof final Player executor) {
34+
executor.teleport(location);
35+
}
36+
})));
37+
}
38+
39+
@Override
40+
public void sendNotificationMessage(@NotNull Location location) {
41+
if (this.notificationService != null) {
42+
this.notificationService.sendNotificationMessage(location);
43+
}
44+
if (!isEnabled()) return;
45+
for (final Player player : Bukkit.getOnlinePlayers()) {
46+
if (!player.hasPermission(Constants.PERMISSION_NOTIFY) || !player.isOp()) continue;
47+
player.sendMessage(getNotificationMessage(location));
48+
}
49+
}
50+
51+
@Override
52+
public boolean isEnabled() {
53+
return this.plugin.getConfig().getBoolean("clock.notifyAdmins", true)
54+
|| this.plugin.getConfig().getStringList("notification.enabled").contains("admins");
55+
}
56+
}

0 commit comments

Comments
 (0)