Skip to content

Commit fc874a3

Browse files
committed
Add warnings for orphaned tags
1 parent f4b2f00 commit fc874a3

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

realty-backend/src/main/java/io/github/md5sha256/realty/database/mapper/RegionTagMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public interface RegionTagMapper {
1515

1616
int deleteByTagAndRegion(@NotNull String tagId, @NotNull String worldGuardRegionId);
1717

18+
@NotNull List<String> selectDistinctTagIds();
19+
1820
int deleteByTagIdNotIn(@NotNull Collection<String> tagIds);
1921

2022
int deleteAll();

realty-backend/src/main/java/io/github/md5sha256/realty/database/maria/mapper/MariaRegionTagMapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ int insert(@Param("tagId") @NotNull String tagId,
4545
int deleteByTagAndRegion(@Param("tagId") @NotNull String tagId,
4646
@Param("worldGuardRegionId") @NotNull String worldGuardRegionId);
4747

48+
@Override
49+
@Select("""
50+
SELECT DISTINCT tagId
51+
FROM RegionTag
52+
""")
53+
@NotNull List<String> selectDistinctTagIds();
54+
4855
@Override
4956
@Delete("""
5057
<script>

realty-paper/src/main/java/io/github/md5sha256/realty/Realty.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import io.github.md5sha256.realty.command.util.SafeLocationFinder;
5151
import io.github.md5sha256.realty.database.Database;
5252
import io.github.md5sha256.realty.database.RealtyBackendImpl;
53+
import io.github.md5sha256.realty.database.SqlSessionWrapper;
5354
import io.github.md5sha256.realty.database.maria.MariaDatabase;
5455
import io.github.md5sha256.realty.listener.SignInteractionListener;
5556
import io.github.md5sha256.realty.localisation.MessageContainer;
@@ -104,6 +105,7 @@
104105
import java.time.Duration;
105106
import java.util.HashMap;
106107
import java.util.List;
108+
import java.util.Set;
107109
import java.util.Map;
108110
import java.util.Objects;
109111
import java.util.concurrent.ExecutorService;
@@ -259,6 +261,7 @@ public void onEnable() {
259261
.register(RealtyBackend.class, this.logic, this, ServicePriority.Normal);
260262
getServer().getServicesManager()
261263
.register(RealtyPaperApi.class, this.paperApi, this, ServicePriority.Normal);
264+
warnOrphanedTags();
262265
getLogger().info("Plugin enabled successfully");
263266
}
264267

@@ -421,6 +424,25 @@ private void registerTagPermissions(@NotNull RealtyTags realtyTags) {
421424
}
422425
}
423426

427+
private void warnOrphanedTags() {
428+
executorState.dbExec().execute(() -> {
429+
try (SqlSessionWrapper session = database.openSession(true)) {
430+
List<String> dbTagIds = session.regionTagMapper().selectDistinctTagIds();
431+
Set<String> configTagIds = realtyTags.get().tagIds();
432+
List<String> orphaned = dbTagIds.stream()
433+
.filter(tagId -> !configTagIds.contains(tagId))
434+
.toList();
435+
if (!orphaned.isEmpty()) {
436+
getLogger().warning("Found orphaned tags in the database that are not in region-tags.yml: "
437+
+ String.join(", ", orphaned)
438+
+ ". Run /realty cleanup tags to remove them.");
439+
}
440+
} catch (Exception ex) {
441+
getLogger().warning("Failed to check for orphaned tags: " + ex.getMessage());
442+
}
443+
});
444+
}
445+
424446
private void reloadMessages() throws IOException {
425447
ConfigurationNode node = copyDefaultsYaml("messages");
426448
this.messageContainer.load(node);
@@ -470,6 +492,7 @@ private void performReload() throws IOException {
470492
configureRegionFlagService(this.regionFlagSettings.get());
471493
this.profileApplicator.applyAll(this.settings.get().profileReapplyPerTick());
472494
reloadMessages();
495+
warnOrphanedTags();
473496
}
474497

475498
private void registerCommands(

0 commit comments

Comments
 (0)