diff --git a/src/main/java/net/simplyvanilla/simplyperms/SimplePermProvider.java b/src/main/java/net/simplyvanilla/simplyperms/SimplePermProvider.java index a67fd7a..f07a758 100644 --- a/src/main/java/net/simplyvanilla/simplyperms/SimplePermProvider.java +++ b/src/main/java/net/simplyvanilla/simplyperms/SimplePermProvider.java @@ -10,18 +10,24 @@ import java.util.UUID; public class SimplePermProvider implements PermissionProvider { + protected final SimplyPerms plugin; + + public SimplePermProvider(SimplyPerms plugin) { + this.plugin = plugin; + } + @Override public PermissionFunction createFunction(final PermissionSubject subject) { - return new SimplePermFunction(subject); + return new SimplePermFunction(this.plugin, subject); } - private record SimplePermFunction(PermissionSubject subject) implements PermissionFunction { + private record SimplePermFunction(SimplyPerms plugin, PermissionSubject subject) implements PermissionFunction { @Override public Tristate getPermissionValue(final String permission) { if (this.subject instanceof Player) { UUID uuid = ((Player) this.subject).getUniqueId(); - User user = User.getUser(uuid); + User user = this.plugin.getUsersManager().getUser(uuid); return user.getPermissionState(permission); } return Tristate.TRUE; diff --git a/src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java b/src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java index 6ddd727..b9cc16a 100644 --- a/src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java +++ b/src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java @@ -13,6 +13,7 @@ import net.simplyvanilla.simplyperms.listeners.CommandListener; import net.simplyvanilla.simplyperms.users.User; import net.simplyvanilla.simplyperms.users.UserYAMLParser; +import net.simplyvanilla.simplyperms.users.UsersManager; import net.simplyvanilla.simplyperms.utils.GroupUtils; import it.fulminazzo.yamlparser.configuration.ConfigurationSection; import it.fulminazzo.yamlparser.configuration.FileConfiguration; @@ -44,6 +45,7 @@ public class SimplyPerms { private final File dataDirectory; private FileConfiguration configuration; + private UsersManager usersManager; static { FileConfiguration.addParsers(new GroupYAMLParser()); @@ -62,6 +64,8 @@ public SimplyPerms(final ProxyServer proxyServer, final Logger logger, final @Da public void onEnable(final ProxyInitializeEvent event) { this.configuration = loadConfiguration("config.yml"); + this.usersManager = new UsersManager(); + loadGroups(); loadUsers(); @@ -76,7 +80,7 @@ public void onDisable(final ProxyShutdownEvent event) { @Subscribe public void on(PermissionsSetupEvent event) { - event.setProvider(new SimplePermProvider()); + event.setProvider(new SimplePermProvider(this)); } public List getAllowedCommands() { @@ -108,7 +112,7 @@ private void loadUsers() { final ConfigurationSection usersSection = this.configuration.getConfigurationSection("users"); if (usersSection != null) for (final String key : usersSection.getKeys()) { - usersSection.get(key, User.class); + this.usersManager.addUser(usersSection.get(key, User.class)); } } @@ -117,6 +121,6 @@ private void unloadGroups() { } private void unloadUsers() { - User.clearUsers(); + this.usersManager.clearUsers(); } } diff --git a/src/main/java/net/simplyvanilla/simplyperms/users/User.java b/src/main/java/net/simplyvanilla/simplyperms/users/User.java index 0f112ea..b24e39a 100644 --- a/src/main/java/net/simplyvanilla/simplyperms/users/User.java +++ b/src/main/java/net/simplyvanilla/simplyperms/users/User.java @@ -2,27 +2,24 @@ import com.velocitypowered.api.permission.Tristate; import it.fulminazzo.fulmicollection.objects.Printable; +import lombok.Getter; import net.simplyvanilla.simplyperms.PermissionHolder; import net.simplyvanilla.simplyperms.groups.Group; -import lombok.Getter; import java.util.*; import java.util.stream.Collectors; @Getter public class User extends Printable implements PermissionHolder { - private static final List USERS = new LinkedList<>(); private final UUID uniqueId; Set groups = new LinkedHashSet<>(); - public User(final String rawId) { + User(final String rawId) { try { this.uniqueId = UUID.fromString(rawId); } catch (Exception e) { throw new IllegalArgumentException(String.format("'%s' is not a valid UUID", rawId)); } - - USERS.add(this); } @Override @@ -56,19 +53,4 @@ public Set getGroups() { .sorted(Comparator.comparing(g -> -Group.getWeight(g))) .collect(Collectors.toCollection(LinkedHashSet::new)); } - - public static User getUser(final UUID uniqueId) { - return uniqueId == null ? null : getUsers().stream() - .filter(g -> g.getUniqueId().equals(uniqueId)) - .findFirst().orElseGet(() -> new User(uniqueId.toString())); - } - - public static List getUsers() { - USERS.removeIf(Objects::isNull); - return new LinkedList<>(USERS); - } - - public static void clearUsers() { - USERS.clear(); - } } diff --git a/src/main/java/net/simplyvanilla/simplyperms/users/UsersManager.java b/src/main/java/net/simplyvanilla/simplyperms/users/UsersManager.java new file mode 100644 index 0000000..88e8576 --- /dev/null +++ b/src/main/java/net/simplyvanilla/simplyperms/users/UsersManager.java @@ -0,0 +1,33 @@ +package net.simplyvanilla.simplyperms.users; + +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +public class UsersManager { + private final List users; + + public UsersManager() { + this.users = new LinkedList<>(); + } + + public void addUser(User user) { + this.users.add(user); + } + + public User getUser(final UUID uniqueId) { + return uniqueId == null ? null : getUsers().stream() + .filter(g -> g.getUniqueId().equals(uniqueId)) + .findFirst().orElseGet(() -> new User(uniqueId.toString())); + } + + public List getUsers() { + this.users.removeIf(Objects::isNull); + return new LinkedList<>(this.users); + } + + public void clearUsers() { + this.users.clear(); + } +}