-
Notifications
You must be signed in to change notification settings - Fork 53
Add include/exclude list support to MANUAL visibility mode #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TSERATO
wants to merge
2
commits into
FancyInnovations:main
Choose a base branch
from
TSERATO:manual-lists-npc-visibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,8 @@ public enum NpcVisibility { | |
| (player, npc) -> player.hasPermission("fancynpcs.npc." + npc.getData().getName() + ".see") | ||
| ), | ||
| /** | ||
| * The player needs to be added manually through the API | ||
| * The player visibility is controlled manually through the API. | ||
| * You can use either include list (allowlist) or exclude list (blocklist), or both. | ||
| */ | ||
| MANUAL(ManualNpcVisibility::canSee); | ||
|
|
||
|
|
@@ -46,41 +47,107 @@ public interface VisibilityPredicate { | |
| } | ||
|
|
||
| /** | ||
| * Handling of NpcVisibility.MANUAL | ||
| * Handling of NpcVisibility.MANUAL mode with include and exclude lists | ||
| */ | ||
| public static class ManualNpcVisibility { | ||
| private static final HashMultimap<String, UUID> distantViewers = HashMultimap.create(); | ||
|
|
||
| private static final HashMultimap<String, UUID> includeList = HashMultimap.create(); | ||
| private static final HashMultimap<String, UUID> excludeList = HashMultimap.create(); | ||
|
|
||
| /** | ||
| * Check if player can see NPC based on include/exclude lists. | ||
| * Logic: | ||
| * - If include list is not empty: player must be in include list to see | ||
| * - If exclude list has entries: player in exclude list cannot see | ||
| * - Include list takes priority over exclude list | ||
| */ | ||
| public static boolean canSee(Player player, Npc npc) { | ||
| return npc.isShownFor(player) || distantViewers.containsEntry(npc.getData().getName(), player.getUniqueId()); | ||
| String npcName = npc.getData().getName(); | ||
| UUID playerId = player.getUniqueId(); | ||
|
|
||
| boolean hasIncludeList = includeList.containsKey(npcName); | ||
| boolean hasExcludeList = excludeList.containsKey(npcName); | ||
|
|
||
| // If include list exists and is not empty, player MUST be in it | ||
| if (hasIncludeList) { | ||
| return includeList.containsEntry(npcName, playerId); | ||
| } | ||
|
|
||
| // If only exclude list exists, player must NOT be in it | ||
| if (hasExcludeList) { | ||
| return !excludeList.containsEntry(npcName, playerId); | ||
| } | ||
|
|
||
| // If neither list has entries, use default visibility | ||
| return npc.isShownFor(player); | ||
| } | ||
|
|
||
| // Include list management | ||
| public static void addToIncludeList(Npc npc, UUID uuid) { | ||
| addToIncludeList(npc.getData().getName(), uuid); | ||
|
||
| } | ||
|
|
||
| public static void addToIncludeList(String npcName, UUID uuid) { | ||
| includeList.put(npcName, uuid); | ||
| } | ||
|
|
||
| public static void removeFromIncludeList(Npc npc, UUID uuid) { | ||
| removeFromIncludeList(npc.getData().getName(), uuid); | ||
| } | ||
|
|
||
| public static void removeFromIncludeList(String npcName, UUID uuid) { | ||
| includeList.remove(npcName, uuid); | ||
| } | ||
|
|
||
| // Exclude list management | ||
| public static void addToExcludeList(Npc npc, UUID uuid) { | ||
| addToExcludeList(npc.getData().getName(), uuid); | ||
| } | ||
|
|
||
| public static void addToExcludeList(String npcName, UUID uuid) { | ||
| excludeList.put(npcName, uuid); | ||
| } | ||
|
|
||
| public static void removeFromExcludeList(Npc npc, UUID uuid) { | ||
| removeFromExcludeList(npc.getData().getName(), uuid); | ||
| } | ||
|
|
||
| public static void removeFromExcludeList(String npcName, UUID uuid) { | ||
| excludeList.remove(npcName, uuid); | ||
| } | ||
|
|
||
| // Old methods for backward compatibility | ||
| @Deprecated | ||
| public static void addDistantViewer(Npc npc, UUID uuid) { | ||
| addDistantViewer(npc.getData().getName(), uuid); | ||
| addToIncludeList(npc, uuid); | ||
| } | ||
|
|
||
| @Deprecated | ||
| public static void addDistantViewer(String npcName, UUID uuid) { | ||
| distantViewers.put(npcName, uuid); | ||
| addToIncludeList(npcName, uuid); | ||
| } | ||
|
|
||
| @Deprecated | ||
| public static void removeDistantViewer(Npc npc, UUID uuid) { | ||
| removeDistantViewer(npc.getData().getName(), uuid); | ||
| removeFromIncludeList(npc, uuid); | ||
| } | ||
|
|
||
| @Deprecated | ||
| public static void removeDistantViewer(String npcName, UUID uuid) { | ||
| distantViewers.remove(npcName, uuid); | ||
| removeFromIncludeList(npcName, uuid); | ||
| } | ||
|
|
||
| public static void remove(Npc npc) { | ||
| remove(npc.getData().getName()); | ||
| } | ||
|
|
||
| public static void remove(String npcName) { | ||
| distantViewers.removeAll(npcName); | ||
| includeList.removeAll(npcName); | ||
| excludeList.removeAll(npcName); | ||
| } | ||
|
|
||
| public static void clear() { | ||
| distantViewers.clear(); | ||
| includeList.clear(); | ||
| excludeList.clear(); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"or both", it can be only either include or exclude, right?