Move stuff into commons, rename PermissionObject, add more javadocs to api
This commit is contained in:
@@ -33,6 +33,27 @@
|
||||
<version>1.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- HikariCP -->
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>2.4.7</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- slf4j library -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.9</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!-- gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.7</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import me.lucko.luckperms.api.Logger;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.tracks.TrackManager;
|
||||
@@ -11,6 +12,10 @@ import me.lucko.luckperms.utils.UuidCache;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Main internal interface for LuckPerms plugins, allowing the luckperms-common module to bind with the plugin instance.
|
||||
* All plugin platforms implement this interface.
|
||||
*/
|
||||
public interface LuckPermsPlugin {
|
||||
|
||||
/**
|
||||
@@ -65,7 +70,7 @@ public interface LuckPermsPlugin {
|
||||
* @param uuid The player's uuid
|
||||
* @return a formatted status string
|
||||
*/
|
||||
String getPlayerStatus(UUID uuid);
|
||||
Message getPlayerStatus(UUID uuid);
|
||||
|
||||
/**
|
||||
* Gets the number of users online on the platform
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
* Provides a link between {@link Group} and {@link me.lucko.luckperms.groups.Group}
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class GroupLink extends PermissionObjectLink implements Group {
|
||||
public class GroupLink extends PermissionHolderLink implements Group {
|
||||
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final me.lucko.luckperms.groups.Group master;
|
||||
|
||||
+4
-4
@@ -3,7 +3,7 @@ package me.lucko.luckperms.api.implementation.internal;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.PermissionObject;
|
||||
import me.lucko.luckperms.api.PermissionHolder;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.utils.DateUtil;
|
||||
@@ -14,14 +14,14 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Provides a link between {@link PermissionObject} and {@link me.lucko.luckperms.utils.PermissionObject}
|
||||
* Provides a link between {@link PermissionHolder} and {@link me.lucko.luckperms.utils.PermissionHolder}
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@AllArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
class PermissionObjectLink implements PermissionObject {
|
||||
class PermissionHolderLink implements PermissionHolder {
|
||||
|
||||
@NonNull
|
||||
private final me.lucko.luckperms.utils.PermissionObject master;
|
||||
private final me.lucko.luckperms.utils.PermissionHolder master;
|
||||
|
||||
static String checkServer(String s) {
|
||||
if (Patterns.NON_ALPHA_NUMERIC.matcher(s).find()) {
|
||||
@@ -15,7 +15,7 @@ import java.util.UUID;
|
||||
* Provides a link between {@link User} and {@link me.lucko.luckperms.users.User}
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class UserLink extends PermissionObjectLink implements User {
|
||||
public class UserLink extends PermissionHolderLink implements User {
|
||||
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final me.lucko.luckperms.users.User master;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package me.lucko.luckperms.api.implementation.internal;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import me.lucko.luckperms.api.Group;
|
||||
import me.lucko.luckperms.api.Track;
|
||||
import me.lucko.luckperms.api.User;
|
||||
|
||||
@UtilityClass
|
||||
class Utils {
|
||||
|
||||
static void checkUser(User user) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package me.lucko.luckperms.commands;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public abstract class SenderFactory<T> {
|
||||
|
||||
protected abstract void sendMessage(T t, String s);
|
||||
protected abstract boolean hasPermission(T t, String node);
|
||||
|
||||
public Sender wrap(T t) {
|
||||
final SenderFactory<T> factory = this;
|
||||
return new Sender() {
|
||||
final WeakReference<T> cs = new WeakReference<>(t);
|
||||
|
||||
@Override
|
||||
public void sendMessage(String s) {
|
||||
final T c = cs.get();
|
||||
if (c != null) {
|
||||
factory.sendMessage(c, s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String node) {
|
||||
final T c = cs.get();
|
||||
return c != null && factory.hasPermission(c, node);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,9 @@ public enum Message {
|
||||
*/
|
||||
PREFIX("&7&l[&b&lL&a&lP&7&l] &c", false),
|
||||
EMPTY("%s", true),
|
||||
PLAYER_ONLINE("&aOnline", false),
|
||||
PLAYER_OFFLINE("&cOffline", false),
|
||||
LOADING_ERROR("Permissions data could not be loaded. Please contact an administrator.", true),
|
||||
|
||||
COMMAND_NOT_RECOGNISED("Command not recognised.", true),
|
||||
COMMAND_NO_PERMISSION("You do not have permission to use this command!", true),
|
||||
@@ -225,7 +228,7 @@ public enum Message {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return message;
|
||||
return Util.color(showPrefix ? PREFIX + message : message);
|
||||
}
|
||||
|
||||
public void send(Sender sender, Object... objects) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.utils.Patterns;
|
||||
import me.lucko.luckperms.utils.PermissionObject;
|
||||
import me.lucko.luckperms.utils.PermissionHolder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
@ToString(of = {"name"})
|
||||
@EqualsAndHashCode(of = {"name"}, callSuper = false)
|
||||
public class Group extends PermissionObject {
|
||||
public class Group extends PermissionHolder {
|
||||
|
||||
/**
|
||||
* The name of the group
|
||||
@@ -263,7 +263,7 @@ public class Group extends PermissionObject {
|
||||
Map<String, Boolean> perms = getPermissions(server, world, null, includeGlobal);
|
||||
return perms.keySet().stream()
|
||||
.filter(s -> Patterns.GROUP_MATCH.matcher(s).matches())
|
||||
.map(s -> Patterns.DOT_SPLIT.split(s, 2)[1])
|
||||
.map(s -> Patterns.DOT.split(s, 2)[1])
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.groups.Group;
|
||||
import me.lucko.luckperms.utils.Patterns;
|
||||
import me.lucko.luckperms.utils.PermissionObject;
|
||||
import me.lucko.luckperms.utils.PermissionHolder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -18,7 +18,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
@ToString(of = {"uuid"})
|
||||
@EqualsAndHashCode(of = {"uuid"}, callSuper = false)
|
||||
public abstract class User extends PermissionObject {
|
||||
public abstract class User extends PermissionHolder {
|
||||
|
||||
/**
|
||||
* The users Mojang UUID
|
||||
@@ -294,7 +294,7 @@ public abstract class User extends PermissionObject {
|
||||
Map<String, Boolean> perms = getPermissions(server, world, null, includeGlobal);
|
||||
return perms.keySet().stream()
|
||||
.filter(s -> Patterns.GROUP_MATCH.matcher(s).matches())
|
||||
.map(s -> Patterns.DOT_SPLIT.split(s, 2)[1])
|
||||
.map(s -> Patterns.DOT.split(s, 2)[1])
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.users.User;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class AbstractListener {
|
||||
private final LuckPermsPlugin plugin;
|
||||
|
||||
protected void onAsyncLogin(UUID u, String username) {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
|
||||
final UuidCache cache = plugin.getUuidCache();
|
||||
if (!cache.isOnlineMode()) {
|
||||
UUID uuid = plugin.getDatastore().getUUID(username);
|
||||
if (uuid != null) {
|
||||
cache.addToCache(u, uuid);
|
||||
} else {
|
||||
// No previous data for this player
|
||||
cache.addToCache(u, u);
|
||||
plugin.getDatastore().saveUUIDData(username, u, b -> {});
|
||||
}
|
||||
} else {
|
||||
// Online mode, no cache needed. This is just for name -> uuid lookup.
|
||||
plugin.getDatastore().saveUUIDData(username, u, b -> {});
|
||||
}
|
||||
|
||||
plugin.getDatastore().loadOrCreateUser(cache.getUUID(u), username);
|
||||
final long time = System.currentTimeMillis() - startTime;
|
||||
if (time >= 1000) {
|
||||
plugin.getLog().warn("Processing login for " + username + " took " + time + "ms.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void onLogin(UUID uuid, String username) {
|
||||
|
||||
}
|
||||
|
||||
protected void onJoin(UUID uuid, String username) {
|
||||
|
||||
}
|
||||
|
||||
protected void onLeave(UUID uuid) {
|
||||
final UuidCache cache = plugin.getUuidCache();
|
||||
|
||||
// Unload the user from memory when they disconnect;
|
||||
cache.clearCache(uuid);
|
||||
|
||||
final User user = plugin.getUserManager().getUser(cache.getUUID(uuid));
|
||||
plugin.getUserManager().unloadUser(user);
|
||||
}
|
||||
|
||||
protected void refreshPlayer(UUID uuid) {
|
||||
final User user = plugin.getUserManager().getUser(plugin.getUuidCache().getUUID(uuid));
|
||||
if (user != null) {
|
||||
user.refreshPermissions();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@ import java.util.regex.Pattern;
|
||||
|
||||
@UtilityClass
|
||||
public class Patterns {
|
||||
public static final Pattern SPACE_SPLIT = Pattern.compile(" ");
|
||||
public static final Pattern SERVER_SPLIT = Pattern.compile("\\/");
|
||||
public static final Pattern WORLD_SPLIT = Pattern.compile("\\-");
|
||||
public static final Pattern TEMP_SPLIT = Pattern.compile("\\$");
|
||||
public static final Pattern DOT_SPLIT = Pattern.compile("\\.");
|
||||
public static final Pattern SPACE = Pattern.compile(" ");
|
||||
public static final Pattern SERVER_DELIMITER = Pattern.compile("\\/");
|
||||
public static final Pattern WORLD_DELIMITER = Pattern.compile("\\-");
|
||||
public static final Pattern TEMP_DELIMITER = Pattern.compile("\\$");
|
||||
public static final Pattern DOT = Pattern.compile("\\.");
|
||||
public static final Pattern GROUP_MATCH = Pattern.compile("group\\..*");
|
||||
public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[^A-Za-z0-9]");
|
||||
public static final Pattern NON_USERNAME = Pattern.compile("[^A-Za-z0-9_]");
|
||||
|
||||
+20
-20
@@ -17,7 +17,7 @@ import java.util.stream.Collectors;
|
||||
* For example a User or a Group
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public abstract class PermissionObject {
|
||||
public abstract class PermissionHolder {
|
||||
|
||||
/**
|
||||
* The UUID of the user / name of the group.
|
||||
@@ -53,11 +53,11 @@ public abstract class PermissionObject {
|
||||
return b ? toQuery.containsKey(node) && toQuery.get(node) : toQuery.containsKey(node) && !toQuery.get(node);
|
||||
}
|
||||
|
||||
node = Patterns.TEMP_SPLIT.split(node)[0];
|
||||
node = Patterns.TEMP_DELIMITER.split(node)[0];
|
||||
|
||||
for (Map.Entry<String, Boolean> e : toQuery.entrySet()) {
|
||||
if (e.getKey().contains("$")) {
|
||||
String[] parts = Patterns.TEMP_SPLIT.split(e.getKey());
|
||||
String[] parts = Patterns.TEMP_DELIMITER.split(e.getKey());
|
||||
if (parts[0].equalsIgnoreCase(node)) {
|
||||
return b ? e.getValue() : !e.getValue();
|
||||
}
|
||||
@@ -146,7 +146,7 @@ public abstract class PermissionObject {
|
||||
public boolean inheritsPermission(String node, boolean b) {
|
||||
if (node.contains("/")) {
|
||||
// Use other method
|
||||
final String[] parts = Patterns.SERVER_SPLIT.split(node, 2);
|
||||
final String[] parts = Patterns.SERVER_DELIMITER.split(node, 2);
|
||||
return inheritsPermission(parts[1], b, parts[0]);
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ public abstract class PermissionObject {
|
||||
public boolean inheritsPermission(String node, boolean b, String server) {
|
||||
if (server.contains("-")) {
|
||||
// Use other method
|
||||
final String[] parts = Patterns.WORLD_SPLIT.split(server, 2);
|
||||
final String[] parts = Patterns.WORLD_DELIMITER.split(server, 2);
|
||||
return inheritsPermission(node, b, parts[0], parts[1]);
|
||||
}
|
||||
|
||||
@@ -306,7 +306,7 @@ public abstract class PermissionObject {
|
||||
|
||||
if (temporary) {
|
||||
match = this.nodes.keySet().stream()
|
||||
.filter(n -> n.contains("$")).filter(n -> Patterns.TEMP_SPLIT.split(n)[0].equalsIgnoreCase(fNode))
|
||||
.filter(n -> n.contains("$")).filter(n -> Patterns.TEMP_DELIMITER.split(n)[0].equalsIgnoreCase(fNode))
|
||||
.findFirst();
|
||||
} else {
|
||||
if (this.nodes.containsKey(fNode)) {
|
||||
@@ -401,7 +401,7 @@ public abstract class PermissionObject {
|
||||
*/
|
||||
public Map<Map.Entry<String, Boolean>, Long> getTemporaryNodes() {
|
||||
return this.nodes.entrySet().stream().filter(e -> e.getKey().contains("$")).map(e -> {
|
||||
final String[] parts = Patterns.TEMP_SPLIT.split(e.getKey());
|
||||
final String[] parts = Patterns.TEMP_DELIMITER.split(e.getKey());
|
||||
final long expiry = Long.parseLong(parts[1]);
|
||||
return new AbstractMap.SimpleEntry<Map.Entry<String, Boolean>, Long>(new AbstractMap.SimpleEntry<>(parts[0], e.getValue()), expiry);
|
||||
|
||||
@@ -423,7 +423,7 @@ public abstract class PermissionObject {
|
||||
public void auditTemporaryPermissions() {
|
||||
this.nodes.keySet().stream()
|
||||
.filter(s -> s.contains("$"))
|
||||
.filter(s -> DateUtil.shouldExpire(Long.parseLong(Patterns.TEMP_SPLIT.split(s)[1])))
|
||||
.filter(s -> DateUtil.shouldExpire(Long.parseLong(Patterns.TEMP_DELIMITER.split(s)[1])))
|
||||
.forEach(s -> this.nodes.remove(s));
|
||||
}
|
||||
|
||||
@@ -484,12 +484,12 @@ public abstract class PermissionObject {
|
||||
for (Map.Entry<String, Boolean> node : convertTemporaryPerms().entrySet()) {
|
||||
serverSpecific:
|
||||
if (node.getKey().contains("/")) {
|
||||
String[] parts = Patterns.SERVER_SPLIT.split(node.getKey(), 2);
|
||||
String[] parts = Patterns.SERVER_DELIMITER.split(node.getKey(), 2);
|
||||
// 0=server(+world) 1=node
|
||||
|
||||
// WORLD SPECIFIC
|
||||
if (parts[0].contains("-")) {
|
||||
String[] serverParts = Patterns.WORLD_SPLIT.split(parts[0], 2);
|
||||
String[] serverParts = Patterns.WORLD_DELIMITER.split(parts[0], 2);
|
||||
// 0=server 1=world
|
||||
|
||||
if ((!serverParts[0].equalsIgnoreCase("global") || !includeGlobal) && (!serverParts[0].equalsIgnoreCase(server))) {
|
||||
@@ -540,7 +540,7 @@ public abstract class PermissionObject {
|
||||
// Could be here if the server was set to global.
|
||||
String n = node.getKey();
|
||||
if (n.contains("/")) {
|
||||
n = Patterns.SERVER_SPLIT.split(n, 2)[1];
|
||||
n = Patterns.SERVER_DELIMITER.split(n, 2)[1];
|
||||
}
|
||||
|
||||
if (Patterns.GROUP_MATCH.matcher(n).matches()) {
|
||||
@@ -556,14 +556,14 @@ public abstract class PermissionObject {
|
||||
// If a group is negated at a higher priority, the group should not then be applied at a lower priority
|
||||
serverWorldSpecificGroups.entrySet().stream().filter(node -> !node.getValue()).forEach(node -> {
|
||||
groupNodes.remove(node.getKey());
|
||||
groupNodes.remove(Patterns.SERVER_SPLIT.split(node.getKey(), 2)[1]);
|
||||
groupNodes.remove(Patterns.SERVER_DELIMITER.split(node.getKey(), 2)[1]);
|
||||
serverSpecificGroups.remove(node.getKey());
|
||||
serverSpecificGroups.remove(Patterns.SERVER_SPLIT.split(node.getKey(), 2)[1]);
|
||||
serverSpecificGroups.remove(Patterns.WORLD_SPLIT.split(node.getKey(), 2)[0] + "/" + Patterns.SERVER_SPLIT.split(node.getKey(), 2)[1]);
|
||||
serverSpecificGroups.remove(Patterns.SERVER_DELIMITER.split(node.getKey(), 2)[1]);
|
||||
serverSpecificGroups.remove(Patterns.WORLD_DELIMITER.split(node.getKey(), 2)[0] + "/" + Patterns.SERVER_DELIMITER.split(node.getKey(), 2)[1]);
|
||||
});
|
||||
serverSpecificGroups.entrySet().stream().filter(node -> !node.getValue()).forEach(node -> {
|
||||
groupNodes.remove(node.getKey());
|
||||
groupNodes.remove(Patterns.SERVER_SPLIT.split(node.getKey(), 2)[1]);
|
||||
groupNodes.remove(Patterns.SERVER_DELIMITER.split(node.getKey(), 2)[1]);
|
||||
});
|
||||
|
||||
// Apply lowest priority: groupNodes
|
||||
@@ -574,7 +574,7 @@ public abstract class PermissionObject {
|
||||
// Don't add negated groups
|
||||
if (!groupNode.getValue()) continue;
|
||||
|
||||
String groupName = Patterns.DOT_SPLIT.split(groupNode.getKey(), 2)[1];
|
||||
String groupName = Patterns.DOT.split(groupNode.getKey(), 2)[1];
|
||||
if (!excludedGroups.contains(groupName)) {
|
||||
Group group = plugin.getGroupManager().getGroup(groupName);
|
||||
if (group != null) {
|
||||
@@ -589,7 +589,7 @@ public abstract class PermissionObject {
|
||||
// Apply next priorities: serverSpecificGroups and then serverWorldSpecificGroups
|
||||
for (Map<String, Boolean> m : Arrays.asList(serverSpecificGroups, serverWorldSpecificGroups)) {
|
||||
for (Map.Entry<String, Boolean> groupNode : m.entrySet()) {
|
||||
final String rawNode = Patterns.SERVER_SPLIT.split(groupNode.getKey())[1];
|
||||
final String rawNode = Patterns.SERVER_DELIMITER.split(groupNode.getKey())[1];
|
||||
|
||||
// Add the actual group perm node, so other plugins can hook
|
||||
perms.put(rawNode, groupNode.getValue());
|
||||
@@ -597,7 +597,7 @@ public abstract class PermissionObject {
|
||||
// Don't add negated groups
|
||||
if (!groupNode.getValue()) continue;
|
||||
|
||||
String groupName = Patterns.DOT_SPLIT.split(rawNode, 2)[1];
|
||||
String groupName = Patterns.DOT.split(rawNode, 2)[1];
|
||||
if (!excludedGroups.contains(groupName)) {
|
||||
Group group = plugin.getGroupManager().getGroup(groupName);
|
||||
if (group != null) {
|
||||
@@ -616,7 +616,7 @@ public abstract class PermissionObject {
|
||||
// Apply final priorities: serverSpecificNodes and then serverWorldSpecificNodes
|
||||
for (Map<String, Boolean> m : Arrays.asList(serverSpecificNodes, serverWorldSpecificNodes)) {
|
||||
for (Map.Entry<String, Boolean> node : m.entrySet()) {
|
||||
final String rawNode = Patterns.SERVER_SPLIT.split(node.getKey())[1];
|
||||
final String rawNode = Patterns.SERVER_DELIMITER.split(node.getKey())[1];
|
||||
perms.put(rawNode, node.getValue());
|
||||
}
|
||||
}
|
||||
@@ -626,7 +626,7 @@ public abstract class PermissionObject {
|
||||
|
||||
private static String stripTime(String s) {
|
||||
if (s.contains("$")) {
|
||||
return Patterns.TEMP_SPLIT.split(s)[0];
|
||||
return Patterns.TEMP_DELIMITER.split(s)[0];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
Reference in New Issue
Block a user