Skip to content

Commit

Permalink
Remove unnecessary checks for permission values
Browse files Browse the repository at this point in the history
We shouldn't check for unknown enum values
And checking the sign for an unsigned int is bad
  • Loading branch information
MinnDevelopment committed Mar 8, 2021
1 parent b84333d commit a01ff2e
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,11 @@ public RoleData(long id)
* @param rawPermissions
* Raw permission value
*
* @throws java.lang.IllegalArgumentException
* If the provided permissions are negative or exceed the maximum permissions
*
* @return The current RoleData instance for chaining convenience
*/
@Nonnull
public RoleData setPermissionsRaw(@Nullable Long rawPermissions)
{
if (rawPermissions != null)
{
Checks.notNegative(rawPermissions, "Raw Permissions");
Checks.check(rawPermissions <= Permission.ALL_PERMISSIONS, "Provided permissions may not be greater than a full permission set!");
}
this.permissions = rawPermissions;
return this;
}
Expand Down Expand Up @@ -665,22 +657,14 @@ public ChannelData setPosition(@Nullable Integer position)
* The permissions to deny in the override
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided role is {@code null}</li>
* <li>If the provided allow value is negative or exceeds maximum permissions</li>
* <li>If the provided deny value is negative or exceeds maximum permissions</li>
* </ul>
* If the provided role is {@code null}
*
* @return This ChannelData instance for chaining convenience
*/
@Nonnull
public ChannelData addPermissionOverride(@Nonnull GuildActionImpl.RoleData role, long allow, long deny)
{
Checks.notNull(role, "Role");
Checks.notNegative(allow, "Granted permissions value");
Checks.notNegative(deny, "Denied permissions value");
Checks.check(allow <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
Checks.check(deny <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");
this.overrides.add(new PermOverrideData(PermOverrideData.ROLE_TYPE, role.id, allow, deny));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ default EnumSet<Permission> getInheritedPermissions()
* The <b>positive</b> bits representing the granted
* permissions for the new PermissionOverride
*
* @throws java.lang.IllegalArgumentException
* If the provided bits are negative
* or higher than {@link net.dv8tion.jda.api.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* on the channel and tries to set permissions it does not have in the channel
Expand Down Expand Up @@ -391,9 +388,6 @@ default PermissionOverrideAction grant(@Nonnull Permission... permissions)
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* on the channel and tries to set permissions it does not have in the channel
* @throws java.lang.IllegalArgumentException
* If the provided bits are negative
* or higher than {@link net.dv8tion.jda.api.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
*
* @return The current PermissionOverrideAction - for chaining convenience
*
Expand Down Expand Up @@ -608,9 +602,6 @@ default PermissionOverrideAction clear(@Nonnull Permission... permissions)
* An unsigned bitwise representation
* of denied Permissions
*
* @throws java.lang.IllegalArgumentException
* If any of the provided bits are negative
* or higher than {@link net.dv8tion.jda.api.Permission#ALL_PERMISSIONS Permission.ALL_PERMISSIONS}
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link Permission#MANAGE_PERMISSIONS Permission.MANAGE_PERMISSIONS}
* on the channel and tries to set permissions it does not have in the channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ default RoleAction setPermissions(@Nullable Collection<Permission> permissions)
* The raw {@link net.dv8tion.jda.api.Permission Permissions} value for the new role.
* To retrieve this use {@link net.dv8tion.jda.api.Permission#getRawValue()}
*
* @throws java.lang.IllegalArgumentException
* If the provided permission value is invalid
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not hold one of the specified permissions
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ public ChannelAction<T> syncPermissionOverrides()

private ChannelActionImpl<T> addOverride(long targetId, int type, long allow, long deny)
{
Checks.notNegative(allow, "Granted permissions value");
Checks.notNegative(deny, "Denied permissions value");
Checks.check(allow <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
Checks.check(deny <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");
Member selfMember = getGuild().getSelfMember();
boolean canSetRoles = selfMember.hasPermission(Permission.ADMINISTRATOR);
if (!canSetRoles && parent != null) // You can also set MANAGE_ROLES if you have it on the category (apparently?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.dv8tion.jda.internal.entities.AbstractChannelImpl;
import net.dv8tion.jda.internal.entities.PermissionOverrideImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.PermissionUtil;
import okhttp3.RequestBody;

Expand Down Expand Up @@ -187,8 +186,6 @@ public boolean isRole()
@CheckReturnValue
public PermissionOverrideActionImpl setAllow(long allowBits)
{
Checks.notNegative(allowBits, "Granted permissions value");
Checks.check(allowBits <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
checkPermissions(getCurrentAllow() ^ allowBits);
this.allow = allowBits;
this.deny &= ~allowBits;
Expand All @@ -201,8 +198,6 @@ public PermissionOverrideActionImpl setAllow(long allowBits)
@CheckReturnValue
public PermissionOverrideActionImpl setDeny(long denyBits)
{
Checks.notNegative(denyBits, "Denied permissions value");
Checks.check(denyBits <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");
checkPermissions(getCurrentDeny() ^ denyBits);
this.deny = denyBits;
this.allow &= ~denyBits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ public RoleActionImpl setPermissions(Long permissions)
{
if (permissions != null)
{
Checks.notNegative(permissions, "Raw Permissions");
Checks.check(permissions <= Permission.ALL_PERMISSIONS, "Provided permissions may not be greater than a full permission set!");
for (Permission p : Permission.getPermissions(permissions))
checkPermission(p);
}
Expand Down

0 comments on commit a01ff2e

Please sign in to comment.