From 4a0653fba5634d4af0f0580920f18f50adae9976 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Sat, 28 Dec 2024 17:27:58 -0500 Subject: [PATCH] Half-assed merging of flags and world profile in event handler --- build.gradle.kts | 2 + .../event/WorldProfileEventHandler.java | 81 ++++++++++++------- .../com/cjburkey/claimchunk/flag/CCFlags.java | 1 - .../cjburkey/claimchunk/flag/CCPermFlags.java | 23 ++++-- .../cjburkey/claimchunk/flag/FlagHandler.java | 8 +- .../claimchunk/i18n/V2JsonMessages.java | 3 +- 6 files changed, 77 insertions(+), 41 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a4199c5..4f7a35c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -45,6 +45,7 @@ object DepData { const val SLF4J_VERSION = "1.7.25" const val BSTATS_VERSION = "3.0.2" const val SORMULA_VERSION = "4.3" + const val MOCK_BUKKIT_VERSION = "4.21.0" // Directories const val TEST_SERVER_DIR = "run" @@ -321,6 +322,7 @@ dependencies { testImplementation("org.slf4j:slf4j-simple:${DepData.SLF4J_VERSION}") testImplementation("org.junit.jupiter:junit-jupiter:${DepData.JUNIT_VERSION}") + testImplementation("org.mockbukkit.mockbukkit:mockbukkit-v1.21:${DepData.MOCK_BUKKIT_VERSION}") testImplementation("org.spigotmc:spigot-api:${DepData.SPIGOT_VERSION}") testRuntimeOnly("org.junit.platform:junit-platform-launcher:${DepData.JUNIT_LAUNCHER_VERSION}") } diff --git a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java index 43d6603..588adab 100644 --- a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java @@ -24,7 +24,6 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; -import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -52,9 +51,7 @@ public WorldProfileEventHandler(ClaimChunk claimChunk) { this.claimChunk = claimChunk; } - private void fmtAndSendErrToPly(@NotNull Player player, @Nullable String flagName) { - - } + private void fmtAndSendErrToPly(@NotNull Player player, @Nullable String flagName) {} private void handleCancelEntity( @NotNull Consumer setCancelled, @@ -99,7 +96,9 @@ private void handleCancelEntity( worldProfile.getEntityAccess(chunkOwner != null, chunkPos.world(), entity); if (!allowEntityAccess.apply(entityAccess)) { setCancelled.accept(true); - if (accessor != null) {fmtAndSendErrToPly(accessor, null);} + if (accessor != null) { + fmtAndSendErrToPly(accessor, null); + } } } @@ -129,7 +128,9 @@ private void shouldCancelBlock( chunkOwner, accessorUuid, chunkPos, block, interactionType); if (protection.result().doesProtect(false)) { setCancelled.accept(true); - if (accessor != null) {fmtAndSendErrToPly(accessor, protection.flagName().orElse(null));} + if (accessor != null) { + fmtAndSendErrToPly(accessor, protection.flagName().orElse(null)); + } return; } } @@ -138,7 +139,9 @@ private void shouldCancelBlock( var blockAccess = worldProfile.getBlockAccess(chunkOwner != null, chunkPos.world(), block); if (!allowBlockAccess.apply(blockAccess)) { setCancelled.accept(true); - if (accessor != null) {fmtAndSendErrToPly(accessor, null);} + if (accessor != null) { + fmtAndSendErrToPly(accessor, null); + } } } @@ -343,8 +346,10 @@ public void onPearlLaunch(ProjectileLaunchEvent event) { } final ChunkPos chunkPos = new ChunkPos(chunk); - if (!flagHandler.queryProtectionSimple( - chunkOwner, ply, chunkPos, permFlags.pearlFlag)) { + if (!flagHandler + .queryProtectionSimple(chunkOwner, ply, chunkPos, permFlags.pearlFlag) + .result() + .doesProtect(false)) { return; } @@ -731,8 +736,11 @@ private void onEntityEvent( final ChunkPos chunkPos = new ChunkPos(chunk); CCPermFlags permFlags = claimChunk.getPermFlags(); FlagHandler flagHandler = claimChunk.getFlagHandler(); - if (!flagHandler.queryEntityProtection( - chunkOwner, ply, chunkPos, entity.getType(), entityAccessType)) { + if (!flagHandler + .queryEntityProtection( + chunkOwner, ply, chunkPos, entity.getType(), entityAccessType) + .result() + .doesProtect(false)) { return; } } @@ -870,8 +878,10 @@ private boolean onBlockEvent( final ChunkPos chunkPos = new ChunkPos(chunk); CCPermFlags permFlags = claimChunk.getPermFlags(); FlagHandler flagHandler = claimChunk.getFlagHandler(); - if (!flagHandler.queryBlockProtection( - chunkOwner, ply, chunkPos, blockType, blockAccessType)) { + if (!flagHandler + .queryBlockProtection(chunkOwner, ply, chunkPos, blockType, blockAccessType) + .result() + .doesProtect(false)) { return false; } } @@ -943,12 +953,15 @@ private void onExplosionForEntityEvent(@NotNull Runnable cancel, @NotNull Entity // TODO: WORLD PROFILE REWRITE && chunkOwner != null && shouldProtectOwnerChunks(chunkOwner, claimChunk.getServer(), profile) - && flagHandler.queryEntityProtection( - chunkOwner, - null, - chunkPos, - entity.getType(), - CCFlags.EntityFlagType.EXPLODE)) { + && flagHandler + .queryEntityProtection( + chunkOwner, + null, + chunkPos, + entity.getType(), + CCFlags.EntityFlagType.EXPLODE) + .result() + .doesProtect()) { cancel.run(); } } @@ -969,12 +982,15 @@ private void onExplosionForBlockEvent(@NotNull Runnable cancel, @NotNull Block b if (profile.enabled && chunkOwner != null && shouldProtectOwnerChunks(chunkOwner, claimChunk.getServer(), profile) - && flagHandler.queryBlockProtection( - chunkOwner, - null, - chunkPos, - block.getType(), - CCFlags.BlockFlagType.EXPLODE)) { + && flagHandler + .queryBlockProtection( + chunkOwner, + null, + chunkPos, + block.getType(), + CCFlags.BlockFlagType.EXPLODE) + .result() + .doesProtect()) { cancel.run(); } } @@ -1009,12 +1025,15 @@ private void onExplosionEvent(@NotNull World world, @NotNull Collection b // Check if this type of block should be protected if (shouldProtectOwnerChunks(owner, claimChunk.getServer(), worldProfile) - && flagHandler.queryBlockProtection( - owner, - null, - chunkPos, - block.getType(), - CCFlags.BlockFlagType.EXPLODE)) { + && flagHandler + .queryBlockProtection( + owner, + null, + chunkPos, + block.getType(), + CCFlags.BlockFlagType.EXPLODE) + .result() + .doesProtect()) { // Try to remove the block from the explosion list if (!blockList.remove(block)) { Utils.err( diff --git a/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java b/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java index 9d69936..0f13a55 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java @@ -1,7 +1,6 @@ package com.cjburkey.claimchunk.flag; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.Set; diff --git a/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java b/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java index cff96c8..ee7add5 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java @@ -230,12 +230,23 @@ public void loadFromConfig(@NotNull YamlConfiguration config) { } // Check if the deny message is set for this flag - List msgs = flagEntries.stream().filter(map -> map.containsKey("denyMessage")).map(map -> { - if (map.size() > 1) { - Utils.warn("The deny message map entry for flag %s has other values specified, but they will be ignored! \"for\" entries cannot be combined with their own cancel messages!", flagName); - } - return (String) map.get("denyMessage"); - }).toList(); + List msgs = + flagEntries.stream() + .filter(map -> map.containsKey("denyMessage")) + .map( + map -> { + if (map.size() > 1) { + Utils.warn( + "The deny message map entry for flag %s has" + + " other values specified, but they will" + + " be ignored! \"for\" entries cannot be" + + " combined with their own cancel" + + " messages!", + flagName); + } + return (String) map.get("denyMessage"); + }) + .toList(); @Nullable String protectionMessage = null; if (!msgs.isEmpty()) { if (msgs.size() > 1) { diff --git a/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java b/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java index 064b009..fc30620 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java @@ -124,7 +124,8 @@ public void clearPermissionFlags( FlagProtectResult result; final Optional flagNameOptional = Optional.of(flagName); - Function makeOutput = r -> new FlagProtectInfo(r, flagNameOptional); + Function makeOutput = + r -> new FlagProtectInfo(r, flagNameOptional); if (applicableFlags.chunkPlayerFlags() != null) { result = checkFlag(applicableFlags.chunkPlayerFlags(), flagName, protectWhen); @@ -153,7 +154,8 @@ public void clearPermissionFlags( return new FlagProtectInfo(FlagProtectResult.Unspecified, Optional.empty()); } - public record FlagProtectInfo(@NotNull FlagProtectResult result, @NotNull Optional flagName) { + public record FlagProtectInfo( + @NotNull FlagProtectResult result, @NotNull Optional flagName) { public static FlagProtectInfo unspecified() { return new FlagProtectInfo(FlagProtectResult.Unspecified, Optional.empty()); } @@ -168,6 +170,8 @@ public boolean isSpecified() { return this != Unspecified; } + // RETURNS `true` IF THE RESULT IS UNSPECIFIED + // DON'T EVER CHANGE THIS! public boolean doesProtect() { return doesProtect(true); } diff --git a/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java b/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java index efe53a0..8f40acf 100644 --- a/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java +++ b/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java @@ -157,7 +157,8 @@ public final class V2JsonMessages { public String chunkLeaveSelf = "&6Exiting your territory"; // Permission flag default messages - public String permFlagDenyDefault = "&c&e%%ACTION%%&c is disabled for &e%%TARGET%%&c in &e%%OWNER%%&c's chunk"; + public String permFlagDenyDefault = + "&c&e%%ACTION%%&c is disabled for &e%%TARGET%%&c in &e%%OWNER%%&c's chunk"; // Protection localization public String chunkCancelAdjacentPlace =