Skip to content

Commit a6f1c6c

Browse files
Copilotmodpotato
andcommitted
Address code review feedback: improve code quality and reduce duplication
Co-authored-by: modpotato <81768237+modpotato@users.noreply.github.com>
1 parent 5885c8d commit a6f1c6c

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/main/java/top/modpotato/restoration/RestorationProgressTracker.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class RestorationProgressTracker {
2121
private final Map<UUID, Integer> lastPercentUpdate; // sessionId -> last percent milestone
2222
private final Set<UUID> globalOptOut; // Players who opted out of ALL feedback
2323
private Object taskId = null; // Can be Integer (Paper) or ScheduledTask (Folia)
24+
private boolean isFolia; // Cache the Folia check result
2425

2526
// Configuration
2627
private static final long TIME_UPDATE_INTERVAL_MS = 60000; // 60 seconds
@@ -38,6 +39,7 @@ public RestorationProgressTracker(Main plugin) {
3839
this.lastTimeUpdate = new ConcurrentHashMap<>();
3940
this.lastPercentUpdate = new ConcurrentHashMap<>();
4041
this.globalOptOut = ConcurrentHashMap.newKeySet();
42+
this.isFolia = checkFolia();
4143
}
4244

4345
/**
@@ -48,9 +50,6 @@ public void start() {
4850
return; // Already running
4951
}
5052

51-
// Check if running on Folia
52-
boolean isFolia = checkFolia();
53-
5453
if (isFolia) {
5554
// On Folia, use global region scheduler (runs every second = 20 ticks)
5655
taskId = Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, task -> {
@@ -70,12 +69,11 @@ public void stop() {
7069
return; // Not running
7170
}
7271

73-
boolean isFolia = checkFolia();
74-
7572
if (isFolia) {
7673
// On Folia, cancel the ScheduledTask
74+
// The ScheduledTask interface has a cancel() method
7775
try {
78-
// taskId is a ScheduledTask, call cancel() on it
76+
// Use reflection only as fallback for API compatibility
7977
taskId.getClass().getMethod("cancel").invoke(taskId);
8078
} catch (Exception e) {
8179
plugin.getLogger().warning("Failed to cancel Folia task: " + e.getMessage());

src/main/java/top/modpotato/util/DebrisStorage.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public RestorationSession scheduleRestoreAll(CommandSender initiator) {
529529
for (String locString : entry.getValue()) {
530530
try {
531531
Location loc = deserializeLocation(world, locString);
532-
uniqueChunks.add(world.getUID() + ":" + (loc.getBlockX() >> 4) + "," + (loc.getBlockZ() >> 4));
532+
uniqueChunks.add(getChunkKey(loc, true));
533533
} catch (Exception e) {
534534
// Ignore invalid locations
535535
}
@@ -571,7 +571,7 @@ public RestorationSession scheduleRestoreInWorld(CommandSender initiator, World
571571
for (String locString : locations) {
572572
try {
573573
Location loc = deserializeLocation(world, locString);
574-
uniqueChunks.add((loc.getBlockX() >> 4) + "," + (loc.getBlockZ() >> 4));
574+
uniqueChunks.add(getChunkKey(loc, false));
575575
} catch (Exception e) {
576576
// Ignore invalid locations
577577
}
@@ -911,4 +911,19 @@ private boolean checkFolia() {
911911
return false;
912912
}
913913
}
914+
915+
/**
916+
* Generates a chunk key for a location
917+
* @param location The location
918+
* @param includeWorld Whether to include the world UUID in the key
919+
* @return The chunk key
920+
*/
921+
private String getChunkKey(Location location, boolean includeWorld) {
922+
int chunkX = location.getBlockX() >> 4;
923+
int chunkZ = location.getBlockZ() >> 4;
924+
if (includeWorld && location.getWorld() != null) {
925+
return location.getWorld().getUID() + ":" + chunkX + "," + chunkZ;
926+
}
927+
return chunkX + "," + chunkZ;
928+
}
914929
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ commands:
1212
usage: |
1313
/<command> reload - Reload the configuration
1414
/<command> restore-debris [world] - Restore all replaced Ancient Debris (optionally in a specific world)
15+
/<command> restore-feedback <on|off> - Toggle restoration progress feedback
1516
/<command> debris-info - Show information about stored Ancient Debris locations
1617
/<command> get <setting> - Get a configuration value
1718
/<command> set <setting> <value> - Set a configuration value

0 commit comments

Comments
 (0)