Skip to content

Commit 99c9239

Browse files
CopilotTheMeinerLP
andcommitted
Implement Service Layer Architecture for RedstoneClockService
Co-authored-by: TheMeinerLP <[email protected]>
1 parent 399105c commit 99c9239

File tree

5 files changed

+201
-9
lines changed

5 files changed

+201
-9
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import net.onelitefeather.antiredstoneclockremastered.listener.*;
1313
import net.onelitefeather.antiredstoneclockremastered.plotsquared.v6.PlotSquaredLegacySupport;
1414
import net.onelitefeather.antiredstoneclockremastered.plotsquared.v7.PlotSquaredModernSupport;
15-
import net.onelitefeather.antiredstoneclockremastered.service.RedstoneClockService;
15+
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
16+
import net.onelitefeather.antiredstoneclockremastered.service.factory.RedstoneClockServiceFactory;
1617
import net.onelitefeather.antiredstoneclockremastered.service.UpdateService;
1718
import net.onelitefeather.antiredstoneclockremastered.service.api.TranslationService;
1819
import net.onelitefeather.antiredstoneclockremastered.service.impl.LegacyTranslationService;
@@ -234,7 +235,7 @@ private void registerEvents() {
234235
}
235236

236237
private void enableRedstoneClockService() {
237-
this.redstoneClockService = new RedstoneClockService(this);
238+
this.redstoneClockService = RedstoneClockServiceFactory.createService(this);
238239
}
239240

240241
private void enableTPSChecker() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.kyori.adventure.text.Component;
44
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
5-
import net.onelitefeather.antiredstoneclockremastered.service.RedstoneClockService;
5+
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
66
import net.onelitefeather.antiredstoneclockremastered.utils.Constants;
77
import org.bukkit.block.BlockFace;
88
import org.bukkit.event.EventHandler;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package net.onelitefeather.antiredstoneclockremastered.service.api;
2+
3+
import net.onelitefeather.antiredstoneclockremastered.model.RedstoneClock;
4+
import org.bukkit.Location;
5+
import org.bukkit.block.Block;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Collection;
10+
import java.util.Map;
11+
12+
/**
13+
* Service interface for managing redstone clock detection and handling.
14+
* This abstraction allows for different implementations (e.g., Bukkit, Folia).
15+
*/
16+
public interface RedstoneClockService {
17+
18+
/**
19+
* Check and update clock state with manual active state control.
20+
*
21+
* @param location the location to check
22+
* @param state the active state to set
23+
*/
24+
void checkAndUpdateClockStateWithActiveManual(@NotNull Location location, boolean state);
25+
26+
/**
27+
* Check and update clock state with manual active state control.
28+
*
29+
* @param block the block to check
30+
* @param state the active state to set
31+
*/
32+
void checkAndUpdateClockStateWithActiveManual(@NotNull Block block, boolean state);
33+
34+
/**
35+
* Check and update clock state with automatic active state detection.
36+
*
37+
* @param block the block to check
38+
*/
39+
void checkAndUpdateClockStateWithActive(@NotNull Block block);
40+
41+
/**
42+
* Check and update clock state with automatic active state detection.
43+
*
44+
* @param location the location to check
45+
*/
46+
void checkAndUpdateClockStateWithActive(@NotNull Location location);
47+
48+
/**
49+
* Check and update clock state without active state management.
50+
*
51+
* @param block the block to check
52+
*/
53+
void checkAndUpdateClockState(@NotNull Block block);
54+
55+
/**
56+
* Check and update clock state without active state management.
57+
*
58+
* @param location the location to check
59+
*/
60+
void checkAndUpdateClockState(@NotNull Location location);
61+
62+
/**
63+
* Add a new redstone clock test at the specified location.
64+
*
65+
* @param location the location to test
66+
*/
67+
void addRedstoneClockTest(@NotNull Location location);
68+
69+
/**
70+
* Reload the service configuration.
71+
*/
72+
void reload();
73+
74+
/**
75+
* Remove a clock by its location.
76+
*
77+
* @param location the location of the clock to remove
78+
*/
79+
void removeClockByLocation(@NotNull Location location);
80+
81+
/**
82+
* Remove a clock by the clock object itself.
83+
*
84+
* @param redstoneClock the clock to remove
85+
*/
86+
void removeClockByClock(@NotNull RedstoneClock redstoneClock);
87+
88+
/**
89+
* Check if the service contains a clock at the specified location.
90+
*
91+
* @param location the location to check
92+
* @return true if a clock exists at the location, false otherwise
93+
*/
94+
boolean containsLocation(@NotNull Location location);
95+
96+
/**
97+
* Get the clock at the specified location.
98+
*
99+
* @param location the location to check
100+
* @return the clock at the location, or null if none exists
101+
*/
102+
@Nullable
103+
RedstoneClock getClockByLocation(@NotNull Location location);
104+
105+
/**
106+
* Get all active redstone clocks.
107+
*
108+
* @return an unmodifiable collection of all redstone clocks
109+
*/
110+
@NotNull
111+
Collection<RedstoneClock> getRedstoneClocks();
112+
113+
/**
114+
* Get all locations with active redstone clocks.
115+
*
116+
* @return an unmodifiable collection of all clock locations
117+
*/
118+
@NotNull
119+
Collection<Location> getRedstoneClockLocations();
120+
121+
/**
122+
* Get all active clock testers as a map.
123+
*
124+
* @return a copy of the active testers map
125+
*/
126+
@NotNull
127+
Map<Location, RedstoneClock> getActiveTester();
128+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.onelitefeather.antiredstoneclockremastered.service.factory;
2+
3+
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
4+
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
5+
import net.onelitefeather.antiredstoneclockremastered.service.impl.BukkitRedstoneClockService;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
/**
9+
* Factory for creating RedstoneClockService implementations.
10+
* This factory determines the appropriate implementation based on the server platform.
11+
*/
12+
public final class RedstoneClockServiceFactory {
13+
14+
private RedstoneClockServiceFactory() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Creates the appropriate RedstoneClockService implementation for the current platform.
20+
*
21+
* @param plugin the main plugin instance
22+
* @return the appropriate RedstoneClockService implementation
23+
*/
24+
@NotNull
25+
public static RedstoneClockService createService(@NotNull AntiRedstoneClockRemastered plugin) {
26+
// For now, only Bukkit implementation is available
27+
// Future implementations (e.g., Folia) can be added here based on platform detection
28+
return new BukkitRedstoneClockService(plugin);
29+
}
30+
31+
/**
32+
* Detects if the server is running on Folia.
33+
* This method can be extended in the future to detect Folia and return a FoliaRedstoneClockService.
34+
*
35+
* @return true if Folia is detected, false otherwise
36+
*/
37+
private static boolean isFolia() {
38+
try {
39+
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
40+
return true;
41+
} catch (ClassNotFoundException e) {
42+
return false;
43+
}
44+
}
45+
}

src/main/java/net/onelitefeather/antiredstoneclockremastered/service/RedstoneClockService.java renamed to src/main/java/net/onelitefeather/antiredstoneclockremastered/service/impl/BukkitRedstoneClockService.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package net.onelitefeather.antiredstoneclockremastered.service;
1+
package net.onelitefeather.antiredstoneclockremastered.service.impl;
22

33
import net.kyori.adventure.text.Component;
44
import net.kyori.adventure.text.event.ClickEvent;
5-
import net.kyori.adventure.text.minimessage.MiniMessage;
65
import net.onelitefeather.antiredstoneclockremastered.AntiRedstoneClockRemastered;
76
import net.onelitefeather.antiredstoneclockremastered.model.RedstoneClock;
7+
import net.onelitefeather.antiredstoneclockremastered.service.api.RedstoneClockService;
88
import net.onelitefeather.antiredstoneclockremastered.utils.Constants;
99
import org.bukkit.Bukkit;
1010
import org.bukkit.Location;
@@ -23,7 +23,11 @@
2323
import java.util.concurrent.ConcurrentHashMap;
2424
import java.util.logging.Level;
2525

26-
public final class RedstoneClockService {
26+
/**
27+
* Bukkit/Paper implementation of the RedstoneClockService.
28+
* This implementation uses the standard Bukkit scheduler and APIs.
29+
*/
30+
public final class BukkitRedstoneClockService implements RedstoneClockService {
2731

2832
private final @NotNull AntiRedstoneClockRemastered antiRedstoneClockRemastered;
2933
private int endTimeDelay;
@@ -37,7 +41,7 @@ public final class RedstoneClockService {
3741
private final ConcurrentHashMap<Location, RedstoneClock> activeClockTesters = new ConcurrentHashMap<>();
3842
private final ItemStack SILK_TOUCH_PICKAXE = new ItemStack(Material.DIAMOND_PICKAXE);
3943

40-
public RedstoneClockService(@NotNull AntiRedstoneClockRemastered antiRedstoneClockRemastered) {
44+
public BukkitRedstoneClockService(@NotNull AntiRedstoneClockRemastered antiRedstoneClockRemastered) {
4145
this.antiRedstoneClockRemastered = antiRedstoneClockRemastered;
4246
this.endTimeDelay = antiRedstoneClockRemastered.getConfig().getInt("clock.endDelay", 300);
4347
this.maxClockCount = antiRedstoneClockRemastered.getConfig().getInt("clock.maxCount", 150);
@@ -49,6 +53,7 @@ public RedstoneClockService(@NotNull AntiRedstoneClockRemastered antiRedstoneClo
4953
SILK_TOUCH_PICKAXE.addEnchantment(Enchantment.SILK_TOUCH, 1);
5054
}
5155

56+
@Override
5257
public void checkAndUpdateClockStateWithActiveManual(@NotNull Location location, boolean state) {
5358
if (this.ignoredWorlds.contains(location.getWorld().getName())) return;
5459
if (this.antiRedstoneClockRemastered.getWorldGuardSupport() != null && this.antiRedstoneClockRemastered.getWorldGuardSupport().isRegionAllowed(location))
@@ -76,14 +81,17 @@ public void checkAndUpdateClockStateWithActiveManual(@NotNull Location location,
7681
addRedstoneClockTest(location);
7782
}
7883

84+
@Override
7985
public void checkAndUpdateClockStateWithActiveManual(@NotNull Block block, boolean state) {
8086
checkAndUpdateClockStateWithActiveManual(block.getLocation(), state);
8187
}
8288

89+
@Override
8390
public void checkAndUpdateClockStateWithActive(@NotNull Block block) {
8491
checkAndUpdateClockStateWithActive(block.getLocation());
8592
}
8693

94+
@Override
8795
public void checkAndUpdateClockStateWithActive(@NotNull Location location) {
8896
if (this.ignoredWorlds.contains(location.getWorld().getName())) return;
8997
if (this.antiRedstoneClockRemastered.getWorldGuardSupport() != null && this.antiRedstoneClockRemastered.getWorldGuardSupport().isRegionAllowed(location))
@@ -111,10 +119,12 @@ public void checkAndUpdateClockStateWithActive(@NotNull Location location) {
111119
addRedstoneClockTest(location);
112120
}
113121

122+
@Override
114123
public void checkAndUpdateClockState(@NotNull Block block) {
115124
checkAndUpdateClockState(block.getLocation());
116125
}
117126

127+
@Override
118128
public void checkAndUpdateClockState(@NotNull Location location) {
119129
if (this.ignoredWorlds.contains(location.getWorld().getName())) return;
120130
if (this.antiRedstoneClockRemastered.getWorldGuardSupport() != null && this.antiRedstoneClockRemastered.getWorldGuardSupport().isRegionAllowed(location))
@@ -181,10 +191,12 @@ private void breakBlock(@NotNull Location location) {
181191

182192
}
183193

194+
@Override
184195
public void addRedstoneClockTest(@NotNull Location location) {
185196
this.activeClockTesters.putIfAbsent(location, new RedstoneClock(location, (System.currentTimeMillis() / 1000) + endTimeDelay));
186197
}
187198

199+
@Override
188200
public void reload() {
189201
this.antiRedstoneClockRemastered.reloadConfig();
190202
this.endTimeDelay = antiRedstoneClockRemastered.getConfig().getInt("clock.endDelay", 300);
@@ -196,36 +208,42 @@ public void reload() {
196208
this.ignoredWorlds = antiRedstoneClockRemastered.getConfig().getStringList("check.ignoredWorlds");
197209
}
198210

211+
@Override
199212
public void removeClockByLocation(@NotNull Location location) {
200213
this.activeClockTesters.remove(location);
201214
}
202215

216+
@Override
203217
public void removeClockByClock(@NotNull RedstoneClock redstoneClock) {
204218
removeClockByLocation(redstoneClock.getLocation());
205219
}
206220

221+
@Override
207222
public boolean containsLocation(@NotNull Location location) {
208223
return this.activeClockTesters.containsKey(location);
209224
}
210225

226+
@Override
211227
@Nullable
212228
public RedstoneClock getClockByLocation(@NotNull Location location) {
213229
return this.activeClockTesters.get(location);
214230
}
215231

232+
@Override
216233
@NotNull
217234
public Collection<RedstoneClock> getRedstoneClocks() {
218235
return Collections.unmodifiableCollection(this.activeClockTesters.values());
219236
}
220237

238+
@Override
221239
@NotNull
222240
public Collection<Location> getRedstoneClockLocations() {
223241
return Collections.unmodifiableCollection(this.activeClockTesters.keySet());
224242
}
225243

244+
@Override
226245
@NotNull
227246
public Map<Location, RedstoneClock> getActiveTester() {
228247
return Map.copyOf(this.activeClockTesters);
229248
}
230-
231-
}
249+
}

0 commit comments

Comments
 (0)