Skip to content

Commit

Permalink
Fix velocity crash #3 (#10)
Browse files Browse the repository at this point in the history
* Fixed [Velocity crash](https://gist.github.com/Netherwhal/5e37bd8a1656dbb9ac8cc88530144a65) happening when users in list are null

* Added forced stack trace to individuate error on Velocity restart

* Removed internal static list and reworked constructor

* Created UsersManager to keep track of loaded users

* Added usersManager field

* Added addUser method

* Updated some methods to use users manager

* Updated to use users manager to retrieve users

* Updated SimplePermProvider
  • Loading branch information
fulminazzo authored Mar 11, 2024
1 parent 4d8e6ec commit 16dfea6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class SimplyPerms {
private final File dataDirectory;

private FileConfiguration configuration;
private UsersManager usersManager;

static {
FileConfiguration.addParsers(new GroupYAMLParser());
Expand All @@ -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();

Expand All @@ -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<String> getAllowedCommands() {
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -117,6 +121,6 @@ private void unloadGroups() {
}

private void unloadUsers() {
User.clearUsers();
this.usersManager.clearUsers();
}
}
22 changes: 2 additions & 20 deletions src/main/java/net/simplyvanilla/simplyperms/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> USERS = new LinkedList<>();
private final UUID uniqueId;
Set<String> 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
Expand Down Expand Up @@ -56,19 +53,4 @@ public Set<Group> 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<User> getUsers() {
USERS.removeIf(Objects::isNull);
return new LinkedList<>(USERS);
}

public static void clearUsers() {
USERS.clear();
}
}
Original file line number Diff line number Diff line change
@@ -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<User> 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<User> getUsers() {
this.users.removeIf(Objects::isNull);
return new LinkedList<>(this.users);
}

public void clearUsers() {
this.users.clear();
}
}

0 comments on commit 16dfea6

Please sign in to comment.