Skip to content

Commit

Permalink
Half-assed merging of flags and world profile in event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Dec 28, 2024
1 parent 9c63c03 commit 4a0653f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 41 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> setCancelled,
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -1009,12 +1025,15 @@ private void onExplosionEvent(@NotNull World world, @NotNull Collection<Block> 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(
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cjburkey.claimchunk.flag;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,23 @@ public void loadFromConfig(@NotNull YamlConfiguration config) {
}

// Check if the deny message is set for this flag
List<String> 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<String> 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) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public void clearPermissionFlags(
FlagProtectResult result;

final Optional<String> flagNameOptional = Optional.of(flagName);
Function<FlagProtectResult, FlagProtectInfo> makeOutput = r -> new FlagProtectInfo(r, flagNameOptional);
Function<FlagProtectResult, FlagProtectInfo> makeOutput =
r -> new FlagProtectInfo(r, flagNameOptional);

if (applicableFlags.chunkPlayerFlags() != null) {
result = checkFlag(applicableFlags.chunkPlayerFlags(), flagName, protectWhen);
Expand Down Expand Up @@ -153,7 +154,8 @@ public void clearPermissionFlags(
return new FlagProtectInfo(FlagProtectResult.Unspecified, Optional.empty());
}

public record FlagProtectInfo(@NotNull FlagProtectResult result, @NotNull Optional<String> flagName) {
public record FlagProtectInfo(
@NotNull FlagProtectResult result, @NotNull Optional<String> flagName) {
public static FlagProtectInfo unspecified() {
return new FlagProtectInfo(FlagProtectResult.Unspecified, Optional.empty());
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 4a0653f

Please sign in to comment.