Refactor the HolderType enum

This commit is contained in:
Luck 2019-03-08 20:19:08 +00:00
parent 12b861db3f
commit 8846932cb6
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
21 changed files with 59 additions and 57 deletions

View File

@ -37,6 +37,7 @@ import me.lucko.luckperms.common.cacheddata.type.PermissionCache;
import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -404,13 +405,13 @@ public class LuckPermsVaultPermission extends AbstractVaultPermission {
}
boolean holderSave(PermissionHolder holder) {
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
User u = (User) holder;
// we don't need to join this call - the save operation
// can happen in the background.
this.plugin.getStorage().saveUser(u);
} else if (holder.getType().isGroup()) {
} else if (holder.getType() == HolderType.GROUP) {
Group g = (Group) holder;
// invalidate caches - they have potentially been affected by

View File

@ -31,6 +31,7 @@ import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
@ -286,11 +287,11 @@ public class ExtendedLogEntry implements LogEntry {
}
public Builder acted(PermissionHolder acted) {
if (acted.getType().isUser()) {
if (acted.getType() == HolderType.USER) {
actedName(((User) acted).getName().orElse("null"));
acted(((User) acted).getUuid());
type(Type.USER);
} else if (acted.getType().isGroup()) {
} else if (acted.getType() == HolderType.GROUP) {
actedName(((Group) acted).getName());
type(Type.GROUP);
}

View File

@ -44,6 +44,7 @@ import me.lucko.luckperms.api.caching.CachedData;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.NodeMapType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
@ -217,7 +218,7 @@ public class ApiPermissionHolder implements me.lucko.luckperms.api.PermissionHol
Objects.requireNonNull(group, "group");
Group g = ApiGroup.cast(group);
if (this.handle.getType().isGroup() && g.getName().equals(this.handle.getObjectName())) {
if (this.handle.getType() == HolderType.GROUP && g.getName().equals(this.handle.getObjectName())) {
return true;
}
@ -230,7 +231,7 @@ public class ApiPermissionHolder implements me.lucko.luckperms.api.PermissionHol
Objects.requireNonNull(contextSet, "contextSet");
Group g = ApiGroup.cast(group);
if (this.handle.getType().isGroup() && g.getName().equals(this.handle.getObjectName())) {
if (this.handle.getType() == HolderType.GROUP && g.getName().equals(this.handle.getObjectName())) {
return true;
}
@ -279,7 +280,7 @@ public class ApiPermissionHolder implements me.lucko.luckperms.api.PermissionHol
public void clearMatching(@NonNull Predicate<Node> test) {
Objects.requireNonNull(test, "test");
this.handle.removeIf(test);
if (this.handle.getType().isUser()) {
if (this.handle.getType() == HolderType.USER) {
this.handle.getPlugin().getUserManager().giveDefaultIfNeeded((User) this.handle, false);
}
}

View File

@ -31,6 +31,7 @@ import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.cacheddata.type.PermissionCache;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
@ -271,7 +272,7 @@ public final class ArgumentPermissions {
* @return true if the sender should NOT be allowed to act, true if they should
*/
public static boolean checkGroup(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, ContextSet contextSet) {
if (holder.getType().isGroup()) {
if (holder.getType() == HolderType.GROUP) {
return checkGroup(plugin, sender, ((Group) holder).getName(), contextSet);
}
return false;

View File

@ -29,6 +29,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
@ -166,10 +167,10 @@ public final class StorageAssistant {
}
public static void save(PermissionHolder holder, Sender sender, LuckPermsPlugin plugin) {
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
User user = ((User) holder);
save(user, sender, plugin);
} else if (holder.getType().isGroup()) {
} else if (holder.getType() == HolderType.GROUP) {
Group group = ((Group) holder);
save(group, sender, plugin);
} else {

View File

@ -38,6 +38,7 @@ import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.node.comparator.NodeWithContextComparator;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -166,7 +167,7 @@ public class MetaInfo extends SharedSubCommand {
"¥7Click to remove this " + type.name().toLowerCase() + " from " + holder.getFormattedDisplayName()
), '¥'));
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType().isGroup() ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType() == HolderType.GROUP ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command);
return component -> {
@ -190,7 +191,7 @@ public class MetaInfo extends SharedSubCommand {
"¥7Click to remove this meta pair from " + holder.getFormattedDisplayName()
), '¥'));
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType().isGroup() ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType() == HolderType.GROUP ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command);
return component -> {

View File

@ -37,6 +37,7 @@ import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@ -71,7 +72,7 @@ public class HolderShowTracks<T extends PermissionHolder> extends SubCommand<T>
List<Map.Entry<Track, String>> lines = new ArrayList<>();
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
// if the holder is a user, we want to query parent groups for tracks
Set<Node> nodes = holder.enduringData().immutable().values().stream()
.filter(Node::isGroupNode)

View File

@ -40,6 +40,7 @@ import me.lucko.luckperms.common.command.utils.StorageAssistant;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
@ -95,7 +96,7 @@ public class ParentClearTrack extends SharedSubCommand {
holder.removeIf(node -> node.isGroupNode() && node.getFullContexts().equals(context) && track.containsGroup(node.getGroupName()));
}
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
plugin.getUserManager().giveDefaultIfNeeded(((User) holder), false);
}

View File

@ -39,6 +39,7 @@ import me.lucko.luckperms.common.command.utils.SortType;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.node.comparator.NodeWithContextComparator;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -141,7 +142,7 @@ public class ParentInfo extends SharedSubCommand {
"&7Click to remove this parent from " + holder.getFormattedDisplayName()
), CommandManager.AMPERSAND_CHAR));
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType().isGroup() ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType() == HolderType.GROUP ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command);
return component -> {

View File

@ -42,6 +42,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -74,7 +75,7 @@ public class ParentRemove extends SharedSubCommand {
return CommandResult.NO_PERMISSION;
}
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
User user = (User) holder;
boolean shouldPrevent = plugin.getConfiguration().get(ConfigKeys.PREVENT_PRIMARY_GROUP_REMOVAL) &&
@ -96,7 +97,7 @@ public class ParentRemove extends SharedSubCommand {
.action("parent", "remove", groupName, context)
.build().submit(plugin, sender);
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
plugin.getUserManager().giveDefaultIfNeeded(((User) holder), false);
}

View File

@ -41,6 +41,7 @@ import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -80,7 +81,7 @@ public class ParentSet extends SharedSubCommand {
holder.clearParents(context, false);
holder.setPermission(NodeFactory.buildGroupNode(group.getName()).withExtraContext(context).build());
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
((User) holder).getPrimaryGroup().setStoredValue(group.getName());
}

View File

@ -39,6 +39,7 @@ import me.lucko.luckperms.common.command.utils.SortType;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.node.comparator.NodeWithContextComparator;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -143,7 +144,7 @@ public class PermissionInfo extends SharedSubCommand {
"¥7Click to remove this node from " + holder.getFormattedDisplayName()
), '¥'));
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType().isGroup() ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
String command = "/" + label + " " + NodeFactory.nodeAsCommand(node, holder.getType() == HolderType.GROUP ? holder.getObjectName() : holder.getFormattedDisplayName(), holder.getType(), false, !holder.getPlugin().getConfiguration().getContextsFile().getDefaultContexts().isEmpty());
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command);
return component -> {

View File

@ -78,6 +78,7 @@ import me.lucko.luckperms.common.event.model.EntitySourceImpl;
import me.lucko.luckperms.common.event.model.SenderEntity;
import me.lucko.luckperms.common.event.model.UnknownSource;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
@ -267,7 +268,7 @@ public final class EventFactory {
}
public void handleDataRecalculate(PermissionHolder holder) {
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
User user = (User) holder;
post(UserDataRecalculateEvent.class, () -> generate(UserDataRecalculateEvent.class, user.getApiDelegate(), user.getCachedData()));
} else {

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.common.inheritance;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.factory.NodeFactory;
@ -39,7 +40,7 @@ public class InheritanceComparator implements Comparator<Group> {
private static final Comparator<Group> NULL_ORIGIN = new InheritanceComparator(null);
public static Comparator<Group> getFor(PermissionHolder origin) {
if (origin.getType().isUser()) {
if (origin.getType() == HolderType.USER) {
return new InheritanceComparator(((User) origin));
}
return NULL_ORIGIN;

View File

@ -26,32 +26,11 @@
package me.lucko.luckperms.common.model;
public enum HolderType {
USER(true, false),
GROUP(false, true);
private final boolean user;
private final boolean group;
HolderType(boolean user, boolean group) {
this.user = user;
this.group = group;
}
public boolean matches(PermissionHolder holder) {
return holder.getType() == this;
}
USER,
GROUP;
@Override
public String toString() {
return name().toLowerCase();
}
public boolean isUser() {
return this.user;
}
public boolean isGroup() {
return this.group;
}
}

View File

@ -506,7 +506,7 @@ public abstract class PermissionHolder {
* @return a tristate, returns undefined if no match
*/
public Tristate hasPermission(NodeMapType type, Node node, NodeEqualityPredicate equalityPredicate) {
if (this.getType().isGroup() && node.isGroupNode() && node.getGroupName().equalsIgnoreCase(getObjectName())) {
if (this.getType() == HolderType.GROUP && node.isGroupNode() && node.getGroupName().equalsIgnoreCase(getObjectName())) {
return Tristate.TRUE;
}
@ -713,7 +713,7 @@ public abstract class PermissionHolder {
public boolean clearParents(boolean giveDefault) {
return removeIf(Node::isGroupNode, () -> {
if (this.getType().isUser() && giveDefault) {
if (this.getType() == HolderType.USER && giveDefault) {
this.plugin.getUserManager().giveDefaultIfNeeded((User) this, false);
}
});
@ -721,7 +721,7 @@ public abstract class PermissionHolder {
public boolean clearParents(ContextSet contextSet, boolean giveDefault) {
return removeIf(contextSet, Node::isGroupNode, () -> {
if (this.getType().isUser() && giveDefault) {
if (this.getType() == HolderType.USER && giveDefault) {
this.plugin.getUserManager().giveDefaultIfNeeded((User) this, false);
}
});

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.common.model.manager.user;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.Streams;
import me.lucko.luckperms.api.LocalizedNode;
import me.lucko.luckperms.api.Node;
@ -42,6 +43,7 @@ import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public abstract class AbstractUserManager<T extends User> extends AbstractManager<UserIdentifier, User, T> implements UserManager<T> {
@ -152,7 +154,12 @@ public abstract class AbstractUserManager<T extends User> extends AbstractManage
@Override
public CompletableFuture<Void> updateAllUsers() {
return CompletableFuture.runAsync(
() -> this.plugin.getBootstrap().getOnlinePlayers().forEach(u -> this.plugin.getStorage().loadUser(u, null).join()),
() -> {
Stream.concat(
getAll().keySet().stream().map(UserIdentifier::getUuid),
this.plugin.getBootstrap().getOnlinePlayers()
).forEach(u -> this.plugin.getStorage().loadUser(u, null).join());
},
this.plugin.getBootstrap().getScheduler().async()
);
}

View File

@ -72,7 +72,6 @@ public class SyncTask implements Runnable {
this.plugin.getStorage().loadAllTracks().join();
// Refresh all online users.
this.plugin.getUserManager().invalidateAllUserCaches();
CompletableFuture<Void> userUpdateFut = this.plugin.getUserManager().updateAllUsers();
if (!this.initialUpdate) {
userUpdateFut.join();

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.context.ContextSetJsonSerializer;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.model.NodeDataContainer;
@ -74,7 +75,7 @@ public final class WebEditor {
.add("id", getHolderIdentifier(holder))
.add("friendly", holder.getPlainDisplayName())
.consume(obj -> {
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
obj.add("uuid", ((User) holder).getUuid().toString());
}
}))
@ -111,7 +112,7 @@ public final class WebEditor {
}
private static String getHolderIdentifier(PermissionHolder holder) {
if (holder.getType().isUser()) {
if (holder.getType() == HolderType.USER) {
User user = ((User) holder);
return USER_ID_PATTERN + user.getUuid().toString();
} else {

View File

@ -36,6 +36,7 @@ import me.lucko.luckperms.common.calculator.processor.PermissionProcessor;
import me.lucko.luckperms.common.calculator.processor.RegexProcessor;
import me.lucko.luckperms.common.calculator.processor.WildcardProcessor;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.sponge.LPSpongePlugin;
public class SpongeCalculatorFactory implements CalculatorFactory {
@ -64,9 +65,9 @@ public class SpongeCalculatorFactory implements CalculatorFactory {
}
if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_SPONGE_DEFAULT_SUBJECTS)) {
if (metadata.getHolderType().isUser()) {
if (metadata.getHolderType() == HolderType.USER) {
processors.add(new UserDefaultsProcessor(this.plugin.getService(), contexts.getContexts().makeImmutable()));
} else if (metadata.getHolderType().isGroup()) {
} else if (metadata.getHolderType() == HolderType.GROUP) {
processors.add(new GroupDefaultsProcessor(this.plugin.getService(), contexts.getContexts().makeImmutable()));
}
}

View File

@ -36,6 +36,7 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.cacheddata.type.MetaAccumulator;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.NodeMapType;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.User;
@ -157,7 +158,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
return CompletableFuture.completedFuture(false);
}
if (this.holder.getType().isUser()) {
if (this.holder.getType() == HolderType.USER) {
this.service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) this.holder), false);
}
@ -183,7 +184,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
return CompletableFuture.completedFuture(false);
}
if (this.holder.getType().isUser()) {
if (this.holder.getType() == HolderType.USER) {
this.service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) this.holder), false);
}
@ -471,7 +472,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
private CompletableFuture<Void> save(PermissionHolder t) {
// if the holder is a group, invalidate caches.
if (t.getType().isGroup()) {
if (t.getType() == HolderType.GROUP) {
this.service.getPlugin().getGroupManager().invalidateAllGroupCaches();
this.service.getPlugin().getUserManager().invalidateAllUserCaches();
return CompletableFuture.completedFuture(null);
@ -482,7 +483,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
return CompletableFuture.completedFuture(null);
}
if (t.getType().isUser()) {
if (t.getType() == HolderType.USER) {
User user = ((User) t);
return this.service.getPlugin().getStorage().saveUser(user);
} else {