Skip to content

Commit

Permalink
Rework playerfilter to be a state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 11, 2023
1 parent 5645e14 commit f8ee5f3
Showing 1 changed file with 89 additions and 110 deletions.
199 changes: 89 additions & 110 deletions src/main/java/net/citizensnpcs/api/trait/trait/PlayerFilter.java
Expand Up @@ -9,7 +9,6 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import com.google.common.collect.Sets;

Expand All @@ -20,17 +19,15 @@

@TraitName("playerfilter")
public class PlayerFilter extends Trait {
@Persist
private Set<UUID> allowlist = null;
private Function<Player, Boolean> filter;
@Persist
private Set<String> groupAllowlist = null;
@Persist
private Set<String> groupHidden = null;
@Persist
private Set<UUID> hidden = null;
private Set<String> groups = null;
private final Set<UUID> hiddenPlayers = Sets.newHashSet();
private BiConsumer<Player, Entity> hideFunction;
@Persist
private Mode mode = Mode.DENYLIST;
@Persist
private Set<UUID> players = null;
private BiConsumer<Player, Entity> viewFunction;
private final Set<UUID> viewingPlayers = Sets.newHashSet();

Expand All @@ -41,21 +38,27 @@ public PlayerFilter() {
public PlayerFilter(BiConsumer<Player, Entity> hideFunction, BiConsumer<Player, Entity> viewFunction) {
this();
this.filter = p -> {
if (allowlist != null && !allowlist.contains(p.getUniqueId()))
return true;
if (hidden != null && hidden.contains(p.getUniqueId()))
return true;
if (groupAllowlist != null || groupHidden != null) {
RegisteredServiceProvider<net.milkbowl.vault.permission.Permission> groups = Bukkit.getServicesManager()
.getRegistration(net.milkbowl.vault.permission.Permission.class);
if (groups != null
&& !groupAllowlist.stream().anyMatch(group -> groups.getProvider().playerInGroup(p, group))) {
return true;
}
if (groups != null
&& groupHidden.stream().anyMatch(group -> groups.getProvider().playerInGroup(p, group))) {
return true;
}
switch (mode) {
case DENYLIST:
if (players != null && players.contains(p.getUniqueId()))
return true;
if (groups != null) {
net.milkbowl.vault.permission.Permission permission = Bukkit.getServicesManager()
.getRegistration(net.milkbowl.vault.permission.Permission.class).getProvider();
if (groups.stream().anyMatch(group -> permission.playerInGroup(p, group)))
return true;
}
break;
case ALLOWLIST:
if (players != null && !players.contains(p.getUniqueId()))
return true;
if (groups != null) {
net.milkbowl.vault.permission.Permission permission = Bukkit.getServicesManager()
.getRegistration(net.milkbowl.vault.permission.Permission.class).getProvider();
if (!groups.stream().anyMatch(group -> permission.playerInGroup(p, group)))
return true;
}
break;
}
return false;
};
Expand All @@ -64,64 +67,58 @@ public PlayerFilter(BiConsumer<Player, Entity> hideFunction, BiConsumer<Player,
}

/**
* Clears all set UUID filters.
* Hides the NPC from the given permissions group
*/
public void clear() {
hidden = allowlist = null;
groupAllowlist = groupHidden = null;
public void addGroup(String group) {
if (groups == null) {
groups = Sets.newHashSet();
}
groups.add(group);
recalculate();
}

/**
* Implementation detail: may change in the future.
* Hides the NPC from the given Player UUID.
*
* @param uuid
*/
public Set<UUID> getAllowlist() {
return allowlist;
public void addPlayer(UUID uuid) {
if (players == null) {
players = Sets.newHashSet();
}
players.add(uuid);
getSet().add(uuid);
recalculate();
}

/**
* Implementation detail: may change in the future.
* Clears all set UUID filters.
*/
public Set<String> getGroupAllowlist() {
return groupAllowlist;
public void clear() {
players = null;
groups = null;
}

/**
* Implementation detail: may change in the future.
*/
public Set<String> getGroupHidden() {
return groupHidden;
public Set<String> getGroups() {
return groups;
}

/**
* Implementation detail: may change in the future.
*/
public Set<UUID> getHidden() {
return hidden;
private Set<UUID> getInverseSet() {
return mode == Mode.ALLOWLIST ? viewingPlayers : hiddenPlayers;
}

/**
* Hides the NPC from the given Player UUID.
*
* @param uuid
* Implementation detail: may change in the future.
*/
public void hide(UUID uuid) {
if (hidden == null) {
hidden = Sets.newHashSet();
}
hidden.add(uuid);
viewingPlayers.add(uuid);
recalculate();
public Set<UUID> getPlayerUUIDs() {
return players;
}

/**
* Hides the NPC from the given permissions group
*/
public void hideGroup(String group) {
if (groupHidden == null) {
groupHidden = Sets.newHashSet();
}
groupHidden.add(group);
recalculate();
private Set<UUID> getSet() {
return mode == Mode.DENYLIST ? viewingPlayers : hiddenPlayers;
}

/**
Expand All @@ -137,28 +134,6 @@ public void onDespawn() {
viewingPlayers.clear();
}

/**
* Only the given {@link Player} identified by their {@link UUID} should see the {@link NPC}.
*/
public void only(UUID uuid) {
if (allowlist == null) {
allowlist = Sets.newHashSet();
}
allowlist.add(uuid);
recalculate();
}

/**
* Only the given permissions group should see the NPC.
*/
public void onlyGroup(String group) {
if (groupAllowlist == null) {
groupAllowlist = Sets.newHashSet();
}
groupAllowlist.add(group);
recalculate();
}

/**
* For internal use. Method signature may be changed at any time.
*/
Expand Down Expand Up @@ -202,26 +177,45 @@ public void recalculate() {
}
}

/**
* Unhides the given permissions group
*/
public void removeGroup(String group) {
if (groups != null) {
groups.remove(group);
}
recalculate();
}

/**
* Unhides the given Player UUID
*/
public void removePlayer(UUID uuid) {
if (players != null) {
players.remove(uuid);
}
getInverseSet().add(uuid);
recalculate();
}

@Override
public void run() {
if (!npc.isSpawned() || !npc.isUpdating(NPCUpdate.PACKET))
return;
recalculate();
}

/**
* Implementation detail: may change in the future.
*/
public void setAllowlist(Set<UUID> allowlist) {
this.allowlist = allowlist == null ? null : Sets.newHashSet(allowlist);
public void setAllowlist() {
this.mode = Mode.ALLOWLIST;
recalculate();
}

/**
* Implementation detail: may change in the future.
*/

public void setHiddenFrom(Set<UUID> hidden) {
this.hidden = hidden == null ? null : Sets.newHashSet(hidden);
public void setDenylist() {
this.mode = Mode.DENYLIST;
recalculate();
}

/**
Expand All @@ -234,29 +228,14 @@ public void setPlayerFilter(Function<Player, Boolean> filter) {
}

/**
* Unhides the given Player UUID
* Implementation detail: may change in the future.
*/
public void unhide(UUID uuid) {
if (hidden != null) {
hidden.remove(uuid);
}
if (allowlist != null) {
allowlist.remove(uuid);
}
hiddenPlayers.add(uuid);
recalculate();
public void setPlayers(Set<UUID> players) {
this.players = players == null ? null : Sets.newHashSet(players);
}

/**
* Unhides the given permissions group
*/
public void unhideGroup(String group) {
if (groupHidden != null) {
groupHidden.remove(group);
}
if (groupAllowlist != null) {
groupAllowlist = null;
}
recalculate();
public enum Mode {
ALLOWLIST,
DENYLIST;
}
}

0 comments on commit f8ee5f3

Please sign in to comment.