/
PermissionField.java
303 lines (271 loc) · 11 KB
/
PermissionField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.managers.fields;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.core.utils.Checks;
import java.util.*;
import java.util.function.Supplier;
/**
* Specification Manager Field for
* a {@link net.dv8tion.jda.core.Permission Permission} depending Field.
*
* <p>The specification for this Field will automatically check provided
* {@link net.dv8tion.jda.core.Permission Permissions} for access by using
* {@link net.dv8tion.jda.core.utils.PermissionUtil#checkPermission(Member, Permission...) PermissionUtil.checkPermission(Member, Permission...)}
* on the current account's Member instance for the specified {@link net.dv8tion.jda.core.entities.Guild Guild}.
*
* <p><b>This class is an extension of {@link net.dv8tion.jda.core.managers.fields.RoleField RoleField}</b>
* <br>It provides specific convenience Methods to modify the Permissions for a Role or equal.
*
* @since 3.0
*/
@Deprecated
public class PermissionField extends RoleField<Long>
{
Set<Permission> permsGiven = new HashSet<>();
Set<Permission> permsRevoked = new HashSet<>();
public PermissionField(net.dv8tion.jda.core.managers.RoleManagerUpdatable manager, Supplier<Long> originalValue)
{
super(manager, originalValue);
}
/**
* Sets the value which should be used in the update
* operation for the Manager instance.
* <br>This will cause {@link #isSet()} to return {@code true}!
*
* @param value
* The value that should be used by the update operation
*
* @throws IllegalArgumentException
* If the provided value is null
*
* @return The specific manager instance for chaining convenience
*
* @see #isSet()
*/
@Override
public net.dv8tion.jda.core.managers.RoleManagerUpdatable setValue(Long value)
{
checkValue(value);
this.value = value;
this.set = true;
this.permsGiven.clear();
this.permsRevoked.clear();
return manager;
}
/**
* Sets the permissions for this PermissionField.
* <br>Convenience method to provide multiple permissions with a single
* method.
*
* @param permissions
* The {@link net.dv8tion.jda.core.Permission Permissions} to use
*
* @throws IllegalArgumentException
* If the provided permission collection or any of the permissions within
* it are null
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the permissions provided require other permissions
* to be available
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable setPermissions(Permission... permissions)
{
return setPermissions(Arrays.asList(permissions));
}
/**
* Sets the permissions for this PermissionField.
* <br>Convenience method to provide multiple permissions with a single
* method.
*
* @param permissions
* The {@link net.dv8tion.jda.core.Permission Permissions} to use
*
* @throws IllegalArgumentException
* If the provided permission collection or any of the permissions within
* it are null
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the permissions provided require other permissions
* to be available
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable setPermissions(Collection<Permission> permissions)
{
Checks.notNull(permissions, "permissions Collection");
permissions.forEach(p ->
Checks.notNull(p, "Permission in the Collection"));
return setValue(Permission.getRaw(permissions));
}
@Override
public void checkValue(Long value)
{
Checks.notNull(value, "permission value");
Permission.getPermissions(value).forEach(p ->
{
checkPermission(p);
});
}
/**
* Adds the specified permissions to the result value
* <br>If any of the specified permissions is present in the revoked permissions it will be removed!
* <br><b>This does not apply immediately - it is applied in the value returned by {@link #getValue()}</b>
*
* @param permissions
* Permissions that should be granted
*
* @throws IllegalArgumentException
* If any of the provided Permissions is {@code null}
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable givePermissions(Permission... permissions)
{
return givePermissions(Arrays.asList(permissions));
}
/**
* Adds the specified permissions to the result value
* <br>If any of the specified permissions is present in the revoked permissions it will be removed!
* <br><b>This does not apply immediately - it is applied in the value returned by {@link #getValue()}</b>
*
* @param permissions
* Permissions that should be granted
*
* @throws IllegalArgumentException
* If any of the provided Permissions is {@code null}
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable givePermissions(Collection<Permission> permissions)
{
Checks.notNull(permissions, "Permission Collection");
permissions.forEach(p ->
{
Checks.notNull(p, "Permission in the Collection");
checkPermission(p);
});
permsGiven.addAll(permissions);
permsRevoked.removeAll(permissions);
set = true;
return manager;
}
/**
* Adds the specified permissions to the result value
* <br>These will override permissions that are given through {@link #givePermissions(Collection)} and {@link #givePermissions(Permission...)}!
* <br><b>This does not apply immediately - it is applied in the value returned by {@link #getValue()}</b>
*
* @param permissions
* Permissions that should be revoked
*
* @throws IllegalArgumentException
* If any of the provided Permissions is {@code null}
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable revokePermissions(Permission... permissions)
{
return revokePermissions(Arrays.asList(permissions));
}
/**
* Adds the specified permissions to the result value
* <br>These will override permissions that are given through {@link #givePermissions(Collection)} and {@link #givePermissions(Permission...)}!
* <br><b>This does not apply immediately - it is applied in the value returned by {@link #getValue()}</b>
*
* @param permissions
* Permissions that should be revoked
*
* @throws IllegalArgumentException
* If any of the provided Permissions is {@code null}
*
* @return The {@link net.dv8tion.jda.core.managers.RoleManagerUpdatable RoleManagerUpdatable} instance
* for this PermissionField for chaining convenience
*/
public net.dv8tion.jda.core.managers.RoleManagerUpdatable revokePermissions(Collection<Permission> permissions)
{
Checks.notNull(permissions, "Permission Collection");
permissions.forEach(p ->
{
Checks.notNull(p, "Permission in the Collection");
checkPermission(p);
});
permsRevoked.addAll(permissions);
permsGiven.removeAll(permissions);
set = true;
return manager;
}
@Override
public Long getValue()
{
if (!isSet())
return null;
long perms;
if (value != null) //If we have a set based value, use that
perms = value;
else
perms = getOriginalValue(); //Otherwise, assume we are adding and removing from the original value;
long given = Permission.getRaw(permsGiven);
long removed = Permission.getRaw(permsRevoked);
perms = perms | given; //Apply all of the bits that were given
perms = perms & ~removed; //Removed all the removed perm bits
return perms;
}
@Override
public net.dv8tion.jda.core.managers.RoleManagerUpdatable reset()
{
super.reset();
this.permsGiven.clear();
this.permsRevoked.clear();
return manager;
}
/**
* An immutable list of {@link net.dv8tion.jda.core.Permission Permissions}
* that are calculated from {@link #getValue()} using {@link Permission#getPermissions(long)}
*
* @return An immutable list of the currently set permissions
*
* @see #getOriginalPermissions()
*/
public List<Permission> getPermissions()
{
Long perms = getValue();
return perms != null ? Permission.getPermissions(perms) : null;
}
/**
* An immutable list of {@link net.dv8tion.jda.core.Permission Permissions}
* that have been calculated {@link #getOriginalValue()} using {@link Permission#getPermissions(long)}
*
* @return An immutable list of the originally set permissions
*
* @see #getPermissions()
*/
public List<Permission> getOriginalPermissions()
{
return Permission.getPermissions(getOriginalValue());
}
protected void checkPermission(Permission perm)
{
if (!manager.getGuild().getSelfMember().hasPermission(perm))
throw new InsufficientPermissionException(perm, "Cannot give / revoke the permission because the logged in account does not have access to it! Permission: " + perm);
}
}