diff --git a/api/src/main/java/me/lucko/luckperms/api/LPConfiguration.java b/api/src/main/java/me/lucko/luckperms/api/LPConfiguration.java new file mode 100644 index 00000000..4345c5ee --- /dev/null +++ b/api/src/main/java/me/lucko/luckperms/api/LPConfiguration.java @@ -0,0 +1,50 @@ +package me.lucko.luckperms.api; + +import me.lucko.luckperms.api.data.MySQLConfiguration; + +/** + * A wrapper interface for the internal LuckPerms configuration, providing read only access. + */ +public interface LPConfiguration { + + /** + * @return the name of this server + */ + String getServer(); + + /** + * @return how often a sync task will run in minutes + */ + int getSyncTime(); + + /** + * @return the default group, in a node representation + */ + String getDefaultGroupNode(); + + /** + * @return the name of the default group + */ + String getDefaultGroupName(); + + /** + * @return if the users on this server will have their global permissions applied + */ + boolean getIncludeGlobalPerms(); + + /** + * @return the online mode setting in the config + */ + boolean getOnlineMode(); + + /** + * @return the database values set in the configuration + */ + MySQLConfiguration getDatabaseValues(); + + /** + * @return the storage method string from the configuration + */ + String getStorageMethod(); + +} diff --git a/api/src/main/java/me/lucko/luckperms/api/LuckPermsApi.java b/api/src/main/java/me/lucko/luckperms/api/LuckPermsApi.java index 144cd3a4..32b6f9d7 100644 --- a/api/src/main/java/me/lucko/luckperms/api/LuckPermsApi.java +++ b/api/src/main/java/me/lucko/luckperms/api/LuckPermsApi.java @@ -1,5 +1,6 @@ package me.lucko.luckperms.api; +import java.util.Optional; import java.util.UUID; /** @@ -18,6 +19,12 @@ public interface LuckPermsApi { */ String getVersion(); + /** + * Gets a wrapped {@link LPConfiguration} instance, with read only access + * @return a configuration instance + */ + LPConfiguration getConfiguration(); + /** * Gets a wrapped {@link Datastore} instance, with somewhat limited access * @return a datastore instance @@ -30,6 +37,12 @@ public interface LuckPermsApi { */ Logger getLogger(); + /** + * Gets a wrapped {@link UuidCache} instance, providing read access to the LuckPerms internal uuid caching system + * @return a uuidcache instance + */ + UuidCache getUuidCache(); + /** * Gets a wrapped user object from the user storage * @param uuid the uuid of the user to get @@ -37,6 +50,13 @@ public interface LuckPermsApi { */ User getUser(UUID uuid); + /** + * Gets a wrapped user object from the user storage. This method does not return null, unlike {@link #getUser(UUID)} + * @param uuid the uuid of the user to get + * @return an optional {@link User} object + */ + Optional getUserSafe(UUID uuid); + /** * Gets a wrapped user object from the user storage * @param name the username of the user to get @@ -44,6 +64,13 @@ public interface LuckPermsApi { */ User getUser(String name); + /** + * Gets a wrapped user object from the user storage. This method does not return null, unlike {@link #getUser(String)} + * @param name the username of the user to get + * @return an optional {@link User} object + */ + Optional getUserSafe(String name); + /** * Check if a user is loaded in memory * @param uuid the uuid to check for @@ -58,6 +85,13 @@ public interface LuckPermsApi { */ Group getGroup(String name); + /** + * Gets a wrapped group object from the group storage. This method does not return null, unlike {@link #getGroup} + * @param name the name of the group to get + * @return an optional {@link Group} object + */ + Optional getGroupSafe(String name); + /** * Check if a group is loaded in memory * @param name the name to check for @@ -72,6 +106,13 @@ public interface LuckPermsApi { */ Track getTrack(String name); + /** + * Gets a wrapped tracj object from the track storage. This method does not return null, unlike {@link #getTrack} + * @param name the name of the track to get + * @return an optional {@link Track} object + */ + Optional getTrackSafe(String name); + /** * Check if a track is loaded in memory * @param name the name to check for diff --git a/api/src/main/java/me/lucko/luckperms/api/UuidCache.java b/api/src/main/java/me/lucko/luckperms/api/UuidCache.java new file mode 100644 index 00000000..b359ebb8 --- /dev/null +++ b/api/src/main/java/me/lucko/luckperms/api/UuidCache.java @@ -0,0 +1,32 @@ +package me.lucko.luckperms.api; + +import java.util.UUID; + +/** + * This UuidCache is a means of allowing users to have the same internal UUID across a network of offline mode servers + * or mixed offline mode and online mode servers. Platforms running in offline mode generate a random UUID for a user when + * they first join the server, but this UUID will then not be consistent across the network. LuckPerms will instead check + * the datastore cache, to get a UUID for a user that is consistent across an entire network. + * + * If you want to get a user object from the datastore using the api on a server in offline mode, you will need to use this cache, + * OR use Datastore#getUUID, for users that are not online. + * + * WARNING: THIS IS ONLY EFFECTIVE FOR ONLINE PLAYERS. USE THE DATASTORE METHODS FOR OFFLINE PLAYERS. + */ +public interface UuidCache { + + /** + * Gets a users internal "LuckPerms" UUID, from the one given by the server. + * @param external the UUID assigned by the server, through Player#getUniqueId or ProxiedPlayer#getUniqueId + * @return the corresponding internal UUID + */ + UUID getUUID(UUID external); + + /** + * Gets a users external, server assigned or Mojang assigned unique id, from the internal one used within LuckPerms. + * @param internal the UUID used within LuckPerms, through User#getUuid + * @return the corresponding external UUID + */ + UUID getExternalUUID(UUID internal); + +} diff --git a/api/src/main/java/me/lucko/luckperms/api/data/MySQLConfiguration.java b/api/src/main/java/me/lucko/luckperms/api/data/MySQLConfiguration.java new file mode 100644 index 00000000..888641e5 --- /dev/null +++ b/api/src/main/java/me/lucko/luckperms/api/data/MySQLConfiguration.java @@ -0,0 +1,9 @@ +package me.lucko.luckperms.api.data; + +public interface MySQLConfiguration { + + String getAddress(); + String getDatabase(); + String getUsername(); + String getPassword(); +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java index 7567d75a..fbe743a6 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java @@ -10,9 +10,6 @@ import me.lucko.luckperms.users.User; import me.lucko.luckperms.utils.PermissionHolder; import net.milkbowl.vault.permission.Permission; -/** - * - */ class VaultPermissionHook extends Permission { @Setter diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/ApiProvider.java b/common/src/main/java/me/lucko/luckperms/api/implementation/ApiProvider.java index 26e88390..061ca970 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/ApiProvider.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/ApiProvider.java @@ -4,11 +4,9 @@ import lombok.AllArgsConstructor; import lombok.NonNull; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.*; -import me.lucko.luckperms.api.implementation.internal.DatastoreLink; -import me.lucko.luckperms.api.implementation.internal.GroupLink; -import me.lucko.luckperms.api.implementation.internal.TrackLink; -import me.lucko.luckperms.api.implementation.internal.UserLink; +import me.lucko.luckperms.api.implementation.internal.*; +import java.util.Optional; import java.util.UUID; /** @@ -28,11 +26,21 @@ public class ApiProvider implements LuckPermsApi { return plugin.getVersion(); } + @Override + public LPConfiguration getConfiguration() { + return new LPConfigurationLink(plugin.getConfiguration()); + } + @Override public Datastore getDatastore() { return new DatastoreLink(plugin.getDatastore()); } + @Override + public UuidCache getUuidCache() { + return new UuidCacheLink(plugin.getUuidCache()); + } + @Override public Logger getLogger() { return plugin.getLog(); @@ -48,6 +56,11 @@ public class ApiProvider implements LuckPermsApi { return new UserLink(user); } + @Override + public Optional getUserSafe(UUID uuid) { + return Optional.ofNullable(getUser(uuid)); + } + @Override public User getUser(@NonNull String name) { final me.lucko.luckperms.users.User user = plugin.getUserManager().getUser(name); @@ -58,6 +71,11 @@ public class ApiProvider implements LuckPermsApi { return new UserLink(user); } + @Override + public Optional getUserSafe(String name) { + return Optional.ofNullable(getUser(name)); + } + @Override public boolean isUserLoaded(@NonNull UUID uuid) { return plugin.getUserManager().isLoaded(uuid); @@ -73,6 +91,11 @@ public class ApiProvider implements LuckPermsApi { return new GroupLink(group); } + @Override + public Optional getGroupSafe(String name) { + return Optional.ofNullable(getGroup(name)); + } + @Override public boolean isGroupLoaded(@NonNull String name) { return plugin.getGroupManager().isLoaded(name); @@ -88,6 +111,11 @@ public class ApiProvider implements LuckPermsApi { return new TrackLink(track); } + @Override + public Optional getTrackSafe(String name) { + return Optional.ofNullable(getTrack(name)); + } + @Override public boolean isTrackLoaded(@NonNull String name) { return plugin.getTrackManager().isLoaded(name); diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/DatastoreLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/DatastoreLink.java index eba1d361..293f1d07 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/DatastoreLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/DatastoreLink.java @@ -7,10 +7,11 @@ import me.lucko.luckperms.api.Group; import me.lucko.luckperms.api.Track; import me.lucko.luckperms.api.User; import me.lucko.luckperms.api.data.Callback; -import me.lucko.luckperms.utils.Patterns; import java.util.UUID; +import static me.lucko.luckperms.api.implementation.internal.Utils.*; + /** * Provides a link between {@link Datastore} and {@link me.lucko.luckperms.data.Datastore} */ @@ -27,22 +28,6 @@ public class DatastoreLink implements Datastore { this.sync = new Sync(master); } - private static String checkUsername(String s) { - if (s.length() > 16 || Patterns.NON_USERNAME.matcher(s).find()) { - throw new IllegalArgumentException("Invalid username entry '" + s + "'. Usernames must be less than 16 chars" + - " and only contain 'a-z A-Z 1-9 _'."); - } - return s; - } - - private static String checkName(String s) { - if (s.length() > 36 || Patterns.NON_ALPHA_NUMERIC.matcher(s).find()) { - throw new IllegalArgumentException("Invalid name entry '" + s + "'. Names must be less than 37 chars" + - " and only contain 'a-z A-Z 1-9'."); - } - return s.toLowerCase(); - } - private static Callback checkCallback(Callback c) { // If no callback was given, just send an empty one if (c == null) { @@ -87,7 +72,7 @@ public class DatastoreLink implements Datastore { @Override public void saveUser(@NonNull User user, Callback callback) { - Utils.checkUser(user); + checkUser(user); master.saveUser(((UserLink) user).getMaster(), checkCallback(callback)); } @@ -108,13 +93,13 @@ public class DatastoreLink implements Datastore { @Override public void saveGroup(@NonNull Group group, Callback callback) { - Utils.checkGroup(group); + checkGroup(group); master.saveGroup(((GroupLink) group).getMaster(), checkCallback(callback)); } @Override public void deleteGroup(@NonNull Group group, Callback callback) { - Utils.checkGroup(group); + checkGroup(group); master.deleteGroup(((GroupLink) group).getMaster(), checkCallback(callback)); } @@ -135,13 +120,13 @@ public class DatastoreLink implements Datastore { @Override public void saveTrack(@NonNull Track track, Callback callback) { - Utils.checkTrack(track); + checkTrack(track); master.saveTrack(((TrackLink) track).getMaster(), checkCallback(callback)); } @Override public void deleteTrack(@NonNull Track track, Callback callback) { - Utils.checkTrack(track); + checkTrack(track); master.deleteTrack(((TrackLink) track).getMaster(), checkCallback(callback)); } @@ -172,7 +157,7 @@ public class DatastoreLink implements Datastore { @Override public boolean saveUser(@NonNull User user) { - Utils.checkUser(user); + checkUser(user); return master.saveUser(((UserLink) user).getMaster()); } @@ -193,13 +178,13 @@ public class DatastoreLink implements Datastore { @Override public boolean saveGroup(@NonNull Group group) { - Utils.checkGroup(group); + checkGroup(group); return master.saveGroup(((GroupLink) group).getMaster()); } @Override public boolean deleteGroup(@NonNull Group group) { - Utils.checkGroup(group); + checkGroup(group); return master.deleteGroup(((GroupLink) group).getMaster()); } @@ -220,13 +205,13 @@ public class DatastoreLink implements Datastore { @Override public boolean saveTrack(@NonNull Track track) { - Utils.checkTrack(track); + checkTrack(track); return master.saveTrack(((TrackLink) track).getMaster()); } @Override public boolean deleteTrack(@NonNull Track track) { - Utils.checkTrack(track); + checkTrack(track); return master.deleteTrack(((TrackLink) track).getMaster()); } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java index 150e036c..25c786fc 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java @@ -9,6 +9,8 @@ import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import static me.lucko.luckperms.api.implementation.internal.Utils.*; + /** * Provides a link between {@link Group} and {@link me.lucko.luckperms.groups.Group} */ @@ -30,91 +32,91 @@ public class GroupLink extends PermissionHolderLink implements Group { @Override public boolean inheritsGroup(@NonNull Group group) { - Utils.checkGroup(group); + checkGroup(group); return master.inheritsGroup(((GroupLink) group).getMaster()); } @Override public boolean inheritsGroup(@NonNull Group group, @NonNull String server) { - Utils.checkGroup(group); + checkGroup(group); return master.inheritsGroup(((GroupLink) group).getMaster(), server); } @Override public boolean inheritsGroup(@NonNull Group group, @NonNull String server, @NonNull String world) { - Utils.checkGroup(group); + checkGroup(group); return master.inheritsGroup(((GroupLink) group).getMaster(), server, world); } @Override public void setInheritGroup(@NonNull Group group) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster()); } @Override public void setInheritGroup(@NonNull Group group, @NonNull String server) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server)); } @Override public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world); } @Override public void setInheritGroup(@NonNull Group group, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster(), checkTime(expireAt)); } @Override public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), checkTime(expireAt)); } @Override public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world, checkTime(expireAt)); } @Override public void unsetInheritGroup(@NonNull Group group) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster()); } @Override public void unsetInheritGroup(@NonNull Group group, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster(), temporary); } @Override public void unsetInheritGroup(@NonNull Group group, @NonNull String server) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server)); } @Override public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world); } @Override public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), temporary); } @Override public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world, temporary); } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/LPConfigurationLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/LPConfigurationLink.java new file mode 100644 index 00000000..a27e50b1 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/LPConfigurationLink.java @@ -0,0 +1,53 @@ +package me.lucko.luckperms.api.implementation.internal; + +import lombok.AllArgsConstructor; +import me.lucko.luckperms.api.LPConfiguration; +import me.lucko.luckperms.api.data.MySQLConfiguration; + +/** + * Provides a link between {@link me.lucko.luckperms.api.LPConfiguration} and {@link me.lucko.luckperms.utils.LPConfiguration} + */ +@AllArgsConstructor +public class LPConfigurationLink implements LPConfiguration { + private final me.lucko.luckperms.utils.LPConfiguration master; + + @Override + public String getServer() { + return master.getServer(); + } + + @Override + public int getSyncTime() { + return master.getSyncTime(); + } + + @Override + public String getDefaultGroupNode() { + return master.getDefaultGroupNode(); + } + + @Override + public String getDefaultGroupName() { + return master.getDefaultGroupName(); + } + + @Override + public boolean getIncludeGlobalPerms() { + return master.getIncludeGlobalPerms(); + } + + @Override + public boolean getOnlineMode() { + return master.getOnlineMode(); + } + + @Override + public MySQLConfiguration getDatabaseValues() { + return master.getDatabaseValues(); + } + + @Override + public String getStorageMethod() { + return master.getStorageMethod(); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/PermissionHolderLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/PermissionHolderLink.java index f11cbc08..efad7d84 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/PermissionHolderLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/PermissionHolderLink.java @@ -6,13 +6,13 @@ import lombok.NonNull; import me.lucko.luckperms.api.PermissionHolder; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; -import me.lucko.luckperms.utils.DateUtil; -import me.lucko.luckperms.utils.Patterns; import java.util.Collections; import java.util.List; import java.util.Map; +import static me.lucko.luckperms.api.implementation.internal.Utils.*; + /** * Provides a link between {@link PermissionHolder} and {@link me.lucko.luckperms.utils.PermissionHolder} */ @@ -23,27 +23,6 @@ class PermissionHolderLink implements PermissionHolder { @NonNull private final me.lucko.luckperms.utils.PermissionHolder master; - static String checkServer(String s) { - if (Patterns.NON_ALPHA_NUMERIC.matcher(s).find()) { - throw new IllegalArgumentException("Invalid server entry '" + s + "'. Server names can only contain alphanumeric characters."); - } - return s; - } - - private static String checkNode(String s) { - if (s.contains("/") || s.contains("$")) { - throw new IllegalArgumentException("Invalid node entry '" + s + "'. Nodes cannot contain '/' or '$' characters."); - } - return s; - } - - static long checkTime(long l) { - if (DateUtil.shouldExpire(l)) { - throw new IllegalArgumentException("Unix time '" + l + "' is invalid, as it has already passed."); - } - return l; - } - @Override public String getObjectName() { return master.getObjectName(); diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/TrackLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/TrackLink.java index 773b3349..eca6514d 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/TrackLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/TrackLink.java @@ -11,6 +11,8 @@ import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import static me.lucko.luckperms.api.implementation.internal.Utils.checkGroup; + /** * Provides a link between {@link Track} and {@link me.lucko.luckperms.tracks.Track} */ @@ -39,31 +41,31 @@ public class TrackLink implements Track { @Override public String getNext(@NonNull Group current) throws ObjectLacksException { - Utils.checkGroup(current); + checkGroup(current); return master.getNext(((GroupLink) current).getMaster()); } @Override public String getPrevious(@NonNull Group current) throws ObjectLacksException { - Utils.checkGroup(current); + checkGroup(current); return master.getPrevious(((GroupLink) current).getMaster()); } @Override public void appendGroup(@NonNull Group group) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.appendGroup(((GroupLink) group).getMaster()); } @Override public void insertGroup(@NonNull Group group, @NonNull int position) throws ObjectAlreadyHasException, IndexOutOfBoundsException { - Utils.checkGroup(group); + checkGroup(group); master.insertGroup(((GroupLink) group).getMaster(), position); } @Override public void removeGroup(@NonNull Group group) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster()); } @@ -74,7 +76,7 @@ public class TrackLink implements Track { @Override public boolean containsGroup(@NonNull Group group) { - Utils.checkGroup(group); + checkGroup(group); return master.containsGroup(((GroupLink) group).getMaster()); } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UserLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UserLink.java index 336480ab..2c87d22f 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UserLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UserLink.java @@ -11,6 +11,8 @@ import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; import java.util.UUID; +import static me.lucko.luckperms.api.implementation.internal.Utils.*; + /** * Provides a link between {@link User} and {@link me.lucko.luckperms.users.User} */ @@ -60,91 +62,91 @@ public class UserLink extends PermissionHolderLink implements User { @Override public boolean isInGroup(@NonNull Group group) { - Utils.checkGroup(group); + checkGroup(group); return master.isInGroup(((GroupLink) group).getMaster()); } @Override public boolean isInGroup(@NonNull Group group, @NonNull String server) { - Utils.checkGroup(group); + checkGroup(group); return master.isInGroup(((GroupLink) group).getMaster(), server); } @Override public boolean isInGroup(@NonNull Group group, @NonNull String server, @NonNull String world) { - Utils.checkGroup(group); + checkGroup(group); return master.isInGroup(((GroupLink) group).getMaster(), server, world); } @Override public void addGroup(@NonNull Group group) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster()); } @Override public void addGroup(@NonNull Group group, @NonNull String server) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster(), checkServer(server)); } @Override public void addGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster(), checkServer(server), world); } @Override public void addGroup(@NonNull Group group, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster(), checkTime(expireAt)); } @Override public void addGroup(@NonNull Group group, @NonNull String server, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster(), checkServer(server), checkTime(expireAt)); } @Override public void addGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull long expireAt) throws ObjectAlreadyHasException { - Utils.checkGroup(group); + checkGroup(group); master.addGroup(((GroupLink) group).getMaster(), checkServer(server), world, checkTime(expireAt)); } @Override public void removeGroup(@NonNull Group group) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster()); } @Override public void removeGroup(@NonNull Group group, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster(), temporary); } @Override public void removeGroup(@NonNull Group group, @NonNull String server) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster(), checkServer(server)); } @Override public void removeGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster(), checkServer(server), world); } @Override public void removeGroup(@NonNull Group group, @NonNull String server, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster(), checkServer(server), temporary); } @Override public void removeGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull boolean temporary) throws ObjectLacksException { - Utils.checkGroup(group); + checkGroup(group); master.removeGroup(((GroupLink) group).getMaster(), checkServer(server), world, temporary); } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/Utils.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/Utils.java index 338ee449..674867c0 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/Utils.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/Utils.java @@ -4,6 +4,8 @@ import lombok.experimental.UtilityClass; import me.lucko.luckperms.api.Group; import me.lucko.luckperms.api.Track; import me.lucko.luckperms.api.User; +import me.lucko.luckperms.utils.DateUtil; +import me.lucko.luckperms.utils.Patterns; @UtilityClass class Utils { @@ -26,4 +28,41 @@ class Utils { } } + static String checkUsername(String s) { + if (s.length() > 16 || Patterns.NON_USERNAME.matcher(s).find()) { + throw new IllegalArgumentException("Invalid username entry '" + s + "'. Usernames must be less than 16 chars" + + " and only contain 'a-z A-Z 1-9 _'."); + } + return s; + } + + static String checkName(String s) { + if (s.length() > 36 || Patterns.NON_ALPHA_NUMERIC.matcher(s).find()) { + throw new IllegalArgumentException("Invalid name entry '" + s + "'. Names must be less than 37 chars" + + " and only contain 'a-z A-Z 1-9'."); + } + return s.toLowerCase(); + } + + static String checkServer(String s) { + if (Patterns.NON_ALPHA_NUMERIC.matcher(s).find()) { + throw new IllegalArgumentException("Invalid server entry '" + s + "'. Server names can only contain alphanumeric characters."); + } + return s; + } + + static String checkNode(String s) { + if (s.contains("/") || s.contains("$")) { + throw new IllegalArgumentException("Invalid node entry '" + s + "'. Nodes cannot contain '/' or '$' characters."); + } + return s; + } + + static long checkTime(long l) { + if (DateUtil.shouldExpire(l)) { + throw new IllegalArgumentException("Unix time '" + l + "' is invalid, as it has already passed."); + } + return l; + } + } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UuidCacheLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UuidCacheLink.java new file mode 100644 index 00000000..89712521 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/UuidCacheLink.java @@ -0,0 +1,24 @@ +package me.lucko.luckperms.api.implementation.internal; + +import lombok.AllArgsConstructor; +import me.lucko.luckperms.api.UuidCache; + +import java.util.UUID; + +/** + * Provides a link between {@link me.lucko.luckperms.api.UuidCache} and {@link me.lucko.luckperms.utils.UuidCache} + */ +@AllArgsConstructor +public class UuidCacheLink implements UuidCache { + private final me.lucko.luckperms.utils.UuidCache master; + + @Override + public UUID getUUID(UUID external) { + return master.getUUID(external); + } + + @Override + public UUID getExternalUUID(UUID internal) { + return master.getExternalUUID(internal); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/data/MySQLConfiguration.java b/common/src/main/java/me/lucko/luckperms/data/MySQLConfiguration.java index 26ea5024..de44dfbd 100644 --- a/common/src/main/java/me/lucko/luckperms/data/MySQLConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/data/MySQLConfiguration.java @@ -5,7 +5,7 @@ import lombok.Getter; @Getter @AllArgsConstructor -public class MySQLConfiguration { +public class MySQLConfiguration implements me.lucko.luckperms.api.data.MySQLConfiguration { private final String address; private final String database; diff --git a/common/src/main/java/me/lucko/luckperms/utils/AbstractListener.java b/common/src/main/java/me/lucko/luckperms/utils/AbstractListener.java index dacbfd0f..4eb4ef9f 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/AbstractListener.java +++ b/common/src/main/java/me/lucko/luckperms/utils/AbstractListener.java @@ -35,14 +35,6 @@ public class AbstractListener { } } - protected void onLogin(UUID uuid, String username) { - - } - - protected void onJoin(UUID uuid, String username) { - - } - protected void onLeave(UUID uuid) { final UuidCache cache = plugin.getUuidCache(); diff --git a/common/src/main/java/me/lucko/luckperms/utils/UuidCache.java b/common/src/main/java/me/lucko/luckperms/utils/UuidCache.java index 73a135bb..3725b4d5 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/UuidCache.java +++ b/common/src/main/java/me/lucko/luckperms/utils/UuidCache.java @@ -7,6 +7,15 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +/** + * This UuidCache is a means of allowing users to have the same internal UUID across a network of offline mode servers + * or mixed offline mode and online mode servers. Platforms running in offline mode generate a random UUID for a user when + * they first join the server, but this UUID will then not be consistent across the network. LuckPerms will instead check + * the datastore cache, to get a UUID for a user that is consistent across an entire network. + * + * If you want to get a user object from the datastore using the api on a server in offline mode, you will need to use this cache, + * OR use Datastore#getUUID, for users that are not online. + */ public class UuidCache { // External UUID --> Internal UUID