Skip to content

Commit

Permalink
Make Vault optional for PlayerFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Sep 5, 2023
1 parent 65ff5be commit 7e92c0e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
27 changes: 10 additions & 17 deletions src/main/java/net/citizensnpcs/api/ai/speech/SpeechContext.java
Expand Up @@ -6,7 +6,6 @@
import java.util.List;

import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;

import net.citizensnpcs.api.ai.speech.event.NPCSpeechEvent;
import net.citizensnpcs.api.npc.NPC;
Expand All @@ -27,12 +26,13 @@ public SpeechContext() {
}

public SpeechContext(NPC talker, String message) {
if (talker != null)
if (talker != null) {
setTalker(talker.getEntity());
}
this.message = message;
}

public SpeechContext(NPC talker, String message, LivingEntity recipient) {
public SpeechContext(NPC talker, String message, Entity recipient) {
this(talker, message);
if (recipient != null) {
addRecipient(recipient);
Expand All @@ -43,10 +43,11 @@ public SpeechContext(String message) {
this.message = message;
}

public SpeechContext(String message, LivingEntity recipient) {
public SpeechContext(String message, Entity recipient) {
this.message = message;
if (recipient != null)
if (recipient != null) {
addRecipient(recipient);
}
}

/**
Expand All @@ -59,17 +60,13 @@ public SpeechContext(String message, LivingEntity recipient) {
*
*/
public SpeechContext addRecipient(Entity entity) {
if (recipients.isEmpty())
if (recipients.isEmpty()) {
recipients = new ArrayList<Talkable>();
}
recipients.add(new TalkableEntity(entity));
return this;
}

@Deprecated
public SpeechContext addRecipient(LivingEntity entity) {
return addRecipient((Entity) entity);
}

/**
* Adds a list of {@link Talkable} recipients. The {@link VocalChord} should use this information to correctly
* direct the message. Note: depending on the VocalChord, this list may not be inclusive as to who gets the message.
Expand All @@ -80,8 +77,9 @@ public SpeechContext addRecipient(LivingEntity entity) {
*
*/
public SpeechContext addRecipients(List<Talkable> talkables) {
if (recipients.isEmpty())
if (recipients.isEmpty()) {
recipients = new ArrayList<Talkable>();
}
recipients.addAll(talkables);
return this;
}
Expand Down Expand Up @@ -147,11 +145,6 @@ public void setTalker(Entity entity) {
this.talker = new TalkableEntity(entity);
}

@Deprecated
public void setTalker(LivingEntity entity) {
setTalker((Entity) entity);
}

/**
* @return number of recipients.
*/
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/net/citizensnpcs/api/trait/trait/PlayerFilter.java
Expand Up @@ -20,6 +20,7 @@
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.PermissionUtil;

@TraitName("playerfilter")
public class PlayerFilter extends Trait {
Expand Down Expand Up @@ -47,22 +48,16 @@ public PlayerFilter(BiConsumer<Player, Entity> hideFunction, BiConsumer<Player,
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;
}
if (groups != null && PermissionUtil.inGroup(groups, p) == true)
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;
}
if (groups != null && PermissionUtil.inGroup(groups, p) == false)
return true;

break;
}
return false;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/citizensnpcs/api/util/PermissionUtil.java
@@ -0,0 +1,24 @@
package net.citizensnpcs.api.util;

import java.util.Collection;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import net.milkbowl.vault.permission.Permission;

public class PermissionUtil {
public static Boolean inGroup(Collection<String> groups, Player player) {
if (!SUPPORT_PERMISSION)
return null;
try {
Permission permission = Bukkit.getServicesManager().getRegistration(Permission.class).getProvider();
return groups.stream().anyMatch(group -> permission.playerInGroup(player, group));
} catch (Throwable t) {
SUPPORT_PERMISSION = false;
return null;
}
}

private static boolean SUPPORT_PERMISSION = true;
}
Expand Up @@ -68,7 +68,10 @@ public void loadsCollections() {
@Test
public void loadsNullSets() {
SpecificCollectionClassTest test = PersistenceLoader.load(SpecificCollectionClassTest.class, root);
PersistenceLoader.save(test, root);
PersistenceLoader.load(test, root);
assertEquals(test.list, null);
assertEquals(test.set, null);
}

@Test
Expand Down

0 comments on commit 7e92c0e

Please sign in to comment.