1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-09 05:40:47 +02:00

Fix bukkit permission subscriptions

This commit is contained in:
Luck
2017-01-14 23:18:48 +00:00
parent cd4a684613
commit 6501e5cf8d
7 changed files with 127 additions and 31 deletions

View File

@@ -22,7 +22,6 @@
package me.lucko.luckperms.api.context;
import java.util.HashMap;
import java.util.Map;
/**
@@ -40,37 +39,9 @@ public interface IContextCalculator<T> {
* @param subject the subject to add contexts to
* @param accumulator a map of contexts to add to
* @return the map
* @deprecated in favour of {@link #giveApplicableContext(Object, MutableContextSet)}. Older implementations of this
* interface will still work, as the replacement method is given as a default, and falls back to using this method.
*/
@Deprecated
default Map<String, String> giveApplicableContext(T subject, Map<String, String> accumulator) {
MutableContextSet acc = new MutableContextSet();
giveApplicableContext(subject, acc);
accumulator.putAll(acc.toMap());
return accumulator;
}
/**
* Gives the subject all of the applicable contexts they meet
*
* <p><b>You MUST implement this method. The default is only provided for backwards compatibility with
* {@link #giveApplicableContext(Object, Map)}.</b>
*
* @param subject the subject to add contexts to
* @param accumulator a map of contexts to add to
* @return the map
* @since 2.13
*/
@SuppressWarnings("deprecation")
default MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator) {
Map<String, String> acc = new HashMap<>();
giveApplicableContext(subject, acc);
accumulator.addAll(acc.entrySet());
return accumulator;
}
MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator);
/**
* Checks to see if a context is applicable to a subject

View File

@@ -391,6 +391,14 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
}
}
@Override
public void onUserRefresh(User user) {
LPPermissible lpp = Injector.getPermissible(uuidCache.getExternalUUID(user.getUuid()));
if (lpp != null) {
lpp.updateSubscriptions();
}
}
@Override
public void doAsync(Runnable r) {
asyncExecutor.execute(r);

View File

@@ -77,6 +77,8 @@ public class Injector {
lpPermissible.recalculatePermissions();
lpPermissible.setOldPermissible(existing);
lpPermissible.updateSubscriptionsAsync();
HUMAN_ENTITY_FIELD.set(player, lpPermissible);
INJECTED_PERMISSIBLES.put(player.getUniqueId(), lpPermissible);
return true;
@@ -90,6 +92,10 @@ public class Injector {
try {
PermissibleBase permissible = (PermissibleBase) HUMAN_ENTITY_FIELD.get(player);
if (permissible instanceof LPPermissible) {
permissible.clearPermissions();
((LPPermissible) permissible).unsubscribeFromAllAsync();
if (dummy) {
HUMAN_ENTITY_FIELD.set(player, new DummyPermissibleBase());
} else {
@@ -104,7 +110,6 @@ public class Injector {
List<PermissionAttachment> newAttachments = (List<PermissionAttachment>) PERMISSIBLEBASE_ATTACHMENTS.get(newPb);
newAttachments.addAll(attachments);
attachments.clear();
lpp.clearPermissions();
HUMAN_ENTITY_FIELD.set(player, newPb);
}

View File

@@ -61,8 +61,12 @@ public class LPPermissible extends PermissibleBase {
@Getter
private final Player parent;
@Getter
private final LPBukkitPlugin plugin;
@Getter
private final SubscriptionManager subscriptions;
@Getter
@Setter
private PermissibleBase oldPermissible = null;
@@ -78,10 +82,28 @@ public class LPPermissible extends PermissibleBase {
this.user = user;
this.parent = parent;
this.plugin = plugin;
this.subscriptions = new SubscriptionManager(this);
recalculatePermissions();
}
public void updateSubscriptionsAsync() {
plugin.doAsync(this::updateSubscriptions);
}
public void updateSubscriptions() {
Set<String> ent = user.getUserData().getPermissionData(calculateContexts()).getImmutableBacking().keySet();
subscriptions.subscribe(ent);
}
public void unsubscribeFromAllAsync() {
plugin.doAsync(this::unsubscribeFromAll);
}
public void unsubscribeFromAll() {
subscriptions.subscribe(Collections.emptySet());
}
public void addAttachments(List<PermissionAttachment> attachments) {
for (PermissionAttachment attachment : attachments) {
this.attachments.add(attachment);

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.bukkit.model;
import lombok.RequiredArgsConstructor;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@RequiredArgsConstructor
public class SubscriptionManager {
private final LPPermissible permissible;
private Set<String> currentSubscriptions = ImmutableSet.of();
public synchronized void subscribe(Set<String> perms) {
Set<String> newPerms = ImmutableSet.copyOf(perms);
Map.Entry<Set<String>, Set<String>> changes = compareSets(newPerms, currentSubscriptions);
Set<String> toAdd = changes.getKey();
Set<String> toRemove = changes.getValue();
permissible.getPlugin().doSync(() -> {
for (String s : toAdd) {
permissible.getPlugin().getServer().getPluginManager().subscribeToPermission(s, permissible.getParent());
}
for (String s : toRemove) {
permissible.getPlugin().getServer().getPluginManager().unsubscribeFromPermission(s, permissible.getParent());
}
});
this.currentSubscriptions = newPerms;
}
/**
* Compares two sets
* @param local the local set
* @param remote the remote set
* @return the entries to add to remote, and the entries to remove from remote
*/
private static Map.Entry<Set<String>, Set<String>> compareSets(Set<String> local, Set<String> remote) {
// entries in local but not remote need to be added
// entries in remote but not local need to be removed
Set<String> toAdd = new HashSet<>(local);
toAdd.removeAll(remote);
Set<String> toRemove = new HashSet<>(remote);
toRemove.removeAll(local);
return Maps.immutableEntry(toAdd, toRemove);
}
}

View File

@@ -394,4 +394,13 @@ public interface LuckPermsPlugin {
}
/**
* Called when a users data is refreshed
*
* @param user the user
*/
default void onUserRefresh(User user) {
}
}

View File

@@ -117,6 +117,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
userData = new UserCache(this, getPlugin().getCalculatorFactory());
userData.preCalculate(getPlugin().getPreProcessContexts(op));
getPlugin().onUserRefresh(this);
}
/**
@@ -139,6 +140,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
ud.recalculatePermissions();
ud.recalculateMeta();
getPlugin().getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this)));
getPlugin().onUserRefresh(this);
}
/**