@Listener public void onModifyBlock(ChangeBlockEvent event, @First Player player) { logger.info("Call: " + event.getClass()); if (event .getCause() .get(NamedCause.NOTIFIER, Player.class) .map(p -> p.equals(player)) .orElse(false)) { logger.info("Player cause is notifier."); return; } WorldFallbackContext worldContext = worldContexts.get(event.getTargetWorld().getUniqueId()); boolean canModifyWild = Optional.ofNullable(worldContext) .flatMap(w -> w.getWilderness().getPublicPermission(MODIFY)) .orElse(MODIFY.isAllowedByDefaultOnTheWild()); Optional<Map<Vector3i, ClaimedChunk>> chunkMap = getChunkMap(event.getTargetWorld()); claimedCheck: if (chunkMap.isPresent()) { Map<Vector3i, ClaimedChunk> subMap = chunkMap.get(); Set<Vector3i> checkedChunks = new HashSet<>(2); for (Transaction<BlockSnapshot> transaction : event.getTransactions()) { Vector3i chunkPosition = blockToChunk(transaction.getOriginal().getPosition()); if (checkedChunks.contains(chunkPosition)) continue; ClaimedChunk claimedChunk = subMap.get(chunkPosition); if (claimedChunk != null && !claimedChunk.check(MODIFY, player)) { logger.info("Chunk modification cancelled: " + chunkPosition + " " + event.getCause()); event.setCancelled(true); return; } else if (!canModifyWild) break claimedCheck; checkedChunks.add(chunkPosition); logger.info("Chunk modification allowed: " + chunkPosition + " " + event); } return; } if (!canModifyWild) { if (worldContext != null) worldContext.getWilderness().notifyFailure(MODIFY, player); else MODIFY.notifyFailure(player, PlayerName.WILDERNESS); logger.info("Chunk modification cancelled because MODIFY is not allowed on unclaimed chunks"); event.setCancelled(true); } }
public Optional<WorldFallbackContext> getWorldContext(UUID worldId) { return Optional.ofNullable(worldContexts.get(worldId)); }
public Optional<Map<Vector3i, ClaimedChunk>> getChunkMap(UUID worldId) { Map<Vector3i, ClaimedChunk> subMap = claimedChunks.get(worldId); if (subMap == null) return Optional.empty(); return Optional.of(subMap); }