Skip to content

Commit 16dfea6

Browse files
authored
Fix velocity crash #3 (#10)
* 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
1 parent 4d8e6ec commit 16dfea6

4 files changed

Lines changed: 51 additions & 26 deletions

File tree

src/main/java/net/simplyvanilla/simplyperms/SimplePermProvider.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@
1010
import java.util.UUID;
1111

1212
public class SimplePermProvider implements PermissionProvider {
13+
protected final SimplyPerms plugin;
14+
15+
public SimplePermProvider(SimplyPerms plugin) {
16+
this.plugin = plugin;
17+
}
18+
1319
@Override
1420
public PermissionFunction createFunction(final PermissionSubject subject) {
15-
return new SimplePermFunction(subject);
21+
return new SimplePermFunction(this.plugin, subject);
1622
}
1723

18-
private record SimplePermFunction(PermissionSubject subject) implements PermissionFunction {
24+
private record SimplePermFunction(SimplyPerms plugin, PermissionSubject subject) implements PermissionFunction {
1925

2026
@Override
2127
public Tristate getPermissionValue(final String permission) {
2228
if (this.subject instanceof Player) {
2329
UUID uuid = ((Player) this.subject).getUniqueId();
24-
User user = User.getUser(uuid);
30+
User user = this.plugin.getUsersManager().getUser(uuid);
2531
return user.getPermissionState(permission);
2632
}
2733
return Tristate.TRUE;

src/main/java/net/simplyvanilla/simplyperms/SimplyPerms.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.simplyvanilla.simplyperms.listeners.CommandListener;
1414
import net.simplyvanilla.simplyperms.users.User;
1515
import net.simplyvanilla.simplyperms.users.UserYAMLParser;
16+
import net.simplyvanilla.simplyperms.users.UsersManager;
1617
import net.simplyvanilla.simplyperms.utils.GroupUtils;
1718
import it.fulminazzo.yamlparser.configuration.ConfigurationSection;
1819
import it.fulminazzo.yamlparser.configuration.FileConfiguration;
@@ -44,6 +45,7 @@ public class SimplyPerms {
4445
private final File dataDirectory;
4546

4647
private FileConfiguration configuration;
48+
private UsersManager usersManager;
4749

4850
static {
4951
FileConfiguration.addParsers(new GroupYAMLParser());
@@ -62,6 +64,8 @@ public SimplyPerms(final ProxyServer proxyServer, final Logger logger, final @Da
6264
public void onEnable(final ProxyInitializeEvent event) {
6365
this.configuration = loadConfiguration("config.yml");
6466

67+
this.usersManager = new UsersManager();
68+
6569
loadGroups();
6670
loadUsers();
6771

@@ -76,7 +80,7 @@ public void onDisable(final ProxyShutdownEvent event) {
7680

7781
@Subscribe
7882
public void on(PermissionsSetupEvent event) {
79-
event.setProvider(new SimplePermProvider());
83+
event.setProvider(new SimplePermProvider(this));
8084
}
8185

8286
public List<String> getAllowedCommands() {
@@ -108,7 +112,7 @@ private void loadUsers() {
108112
final ConfigurationSection usersSection = this.configuration.getConfigurationSection("users");
109113
if (usersSection != null)
110114
for (final String key : usersSection.getKeys()) {
111-
usersSection.get(key, User.class);
115+
this.usersManager.addUser(usersSection.get(key, User.class));
112116
}
113117
}
114118

@@ -117,6 +121,6 @@ private void unloadGroups() {
117121
}
118122

119123
private void unloadUsers() {
120-
User.clearUsers();
124+
this.usersManager.clearUsers();
121125
}
122126
}

src/main/java/net/simplyvanilla/simplyperms/users/User.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22

33
import com.velocitypowered.api.permission.Tristate;
44
import it.fulminazzo.fulmicollection.objects.Printable;
5+
import lombok.Getter;
56
import net.simplyvanilla.simplyperms.PermissionHolder;
67
import net.simplyvanilla.simplyperms.groups.Group;
7-
import lombok.Getter;
88

99
import java.util.*;
1010
import java.util.stream.Collectors;
1111

1212
@Getter
1313
public class User extends Printable implements PermissionHolder {
14-
private static final List<User> USERS = new LinkedList<>();
1514
private final UUID uniqueId;
1615
Set<String> groups = new LinkedHashSet<>();
1716

18-
public User(final String rawId) {
17+
User(final String rawId) {
1918
try {
2019
this.uniqueId = UUID.fromString(rawId);
2120
} catch (Exception e) {
2221
throw new IllegalArgumentException(String.format("'%s' is not a valid UUID", rawId));
2322
}
24-
25-
USERS.add(this);
2623
}
2724

2825
@Override
@@ -56,19 +53,4 @@ public Set<Group> getGroups() {
5653
.sorted(Comparator.comparing(g -> -Group.getWeight(g)))
5754
.collect(Collectors.toCollection(LinkedHashSet::new));
5855
}
59-
60-
public static User getUser(final UUID uniqueId) {
61-
return uniqueId == null ? null : getUsers().stream()
62-
.filter(g -> g.getUniqueId().equals(uniqueId))
63-
.findFirst().orElseGet(() -> new User(uniqueId.toString()));
64-
}
65-
66-
public static List<User> getUsers() {
67-
USERS.removeIf(Objects::isNull);
68-
return new LinkedList<>(USERS);
69-
}
70-
71-
public static void clearUsers() {
72-
USERS.clear();
73-
}
7456
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.simplyvanilla.simplyperms.users;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.UUID;
7+
8+
public class UsersManager {
9+
private final List<User> users;
10+
11+
public UsersManager() {
12+
this.users = new LinkedList<>();
13+
}
14+
15+
public void addUser(User user) {
16+
this.users.add(user);
17+
}
18+
19+
public User getUser(final UUID uniqueId) {
20+
return uniqueId == null ? null : getUsers().stream()
21+
.filter(g -> g.getUniqueId().equals(uniqueId))
22+
.findFirst().orElseGet(() -> new User(uniqueId.toString()));
23+
}
24+
25+
public List<User> getUsers() {
26+
this.users.removeIf(Objects::isNull);
27+
return new LinkedList<>(this.users);
28+
}
29+
30+
public void clearUsers() {
31+
this.users.clear();
32+
}
33+
}

0 commit comments

Comments
 (0)