Skip to content

Commit 652f414

Browse files
committed
Offline exempt permissions with Vault
1 parent 63240e9 commit 652f414

File tree

4 files changed

+88
-62
lines changed

4 files changed

+88
-62
lines changed

src/main/java/me/leoko/advancedban/manager/UUIDManager.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public static UUIDManager get() {
3232
* Initially setup the uuid manager by determening which {@link FetcherMode} should be used
3333
* based on the configured preference and the servers capabilities.
3434
*/
35-
public void setup(){
36-
if(mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Dynamic", true)){
37-
if(!mi.isOnlineMode()) {
35+
public void setup() {
36+
if (mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Dynamic", true)) {
37+
if (!mi.isOnlineMode()) {
3838
mode = FetcherMode.DISABLED;
39-
}else{
40-
if(Universal.get().isBungee()){
39+
} else {
40+
if (Universal.get().isBungee()) {
4141
mode = FetcherMode.MIXED;
42-
}else{
42+
} else {
4343
mode = FetcherMode.INTERN;
4444
}
4545
}
46-
}else{
47-
if(!mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Enabled", true)) {
46+
} else {
47+
if (!mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Enabled", true)) {
4848
mode = FetcherMode.DISABLED;
49-
}else if(mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Intern", false)){
49+
} else if (mi.getBoolean(mi.getConfig(), "UUID-Fetcher.Intern", false)) {
5050
mode = FetcherMode.INTERN;
51-
}else{
51+
} else {
5252
mode = FetcherMode.RESTFUL;
5353
}
5454
}
@@ -63,12 +63,12 @@ public void setup(){
6363
*/
6464
public String getInitialUUID(String name) {
6565
name = name.toLowerCase();
66-
if(mode == FetcherMode.DISABLED)
66+
if (mode == FetcherMode.DISABLED)
6767
return name;
6868

69-
if(mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
69+
if (mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
7070
String internUUID = mi.getInternUUID(name);
71-
if(mode == FetcherMode.INTERN || internUUID != null)
71+
if (mode == FetcherMode.INTERN || internUUID != null)
7272
return internUUID;
7373
}
7474

@@ -106,11 +106,26 @@ public String getInitialUUID(String name) {
106106
* @param uuid the uuid
107107
*/
108108
public void supplyInternUUID(String name, UUID uuid) {
109-
if(mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
109+
if (mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
110110
activeUUIDs.put(name, uuid.toString().replace("-", ""));
111111
}
112112
}
113113

114+
/**
115+
* Convert String to UUID even if dashes are missing
116+
*
117+
* @param uuid
118+
* @return
119+
*/
120+
public UUID fromString(String uuid) {
121+
if (!uuid.contains("-") && uuid.length() == 32)
122+
uuid = uuid
123+
.replaceFirst(
124+
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5");
125+
126+
return uuid.length() == 36 && uuid.contains("-") ? UUID.fromString(uuid) : null;
127+
}
128+
114129
/**
115130
* Get the uuid to a name.
116131
*
@@ -125,18 +140,18 @@ public String getUUID(String name) {
125140
/**
126141
* Gets a uuid from a name only if AdvancedBan
127142
* already has the uuid/name mapping in memory.
128-
*
143+
*
129144
* @param name the player name
130145
* @return the nonhyphenated uuid or null if not found
131146
*/
132147
public String getInMemoryUUID(String name) {
133-
return activeUUIDs.get(name);
148+
return activeUUIDs.get(name);
134149
}
135150

136151
/**
137152
* Gets a name from a uuid only if AdvancedBan
138153
* already has the uuid/name mapping in memory.
139-
*
154+
*
140155
* @param uuid the uuid without hyphens
141156
* @return the player name or null if not found
142157
*/
@@ -160,17 +175,17 @@ public String getNameFromUUID(String uuid, boolean forceInitial) {
160175
if (mode == FetcherMode.DISABLED)
161176
return uuid;
162177

163-
if(mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
178+
if (mode == FetcherMode.INTERN || mode == FetcherMode.MIXED) {
164179
String internName = mi.getName(uuid);
165-
if(mode == FetcherMode.INTERN || internName != null)
180+
if (mode == FetcherMode.INTERN || internName != null)
166181
return internName;
167182
}
168183

169184
if (!forceInitial) {
170-
String inMemoryName = getInMemoryName(uuid);
171-
if (inMemoryName != null) {
172-
return inMemoryName;
173-
}
185+
String inMemoryName = getInMemoryName(uuid);
186+
if (inMemoryName != null) {
187+
return inMemoryName;
188+
}
174189
}
175190

176191
try (Scanner scanner = new Scanner(new URL("https://api.mojang.com/user/profiles/" + uuid + "/names").openStream(), "UTF-8")) {
@@ -182,6 +197,8 @@ public String getNameFromUUID(String uuid, boolean forceInitial) {
182197
}
183198
}
184199

200+
201+
185202
private String askAPI(String url, String name, String key) throws IOException {
186203
HttpURLConnection request = (HttpURLConnection) new URL(url.replaceAll("%NAME%", name).replaceAll("%TIMESTAMP%", new Date().getTime() + "")).openConnection();
187204
request.connect();
@@ -210,7 +227,7 @@ public FetcherMode getMode() {
210227
/**
211228
* The fetcher-mode describes how the {@link UUIDManager} resolves UUIDs.
212229
*/
213-
public enum FetcherMode{
230+
public enum FetcherMode {
214231
/**
215232
* No UUID Fetcher is used. The Username will be treated as an UUID.<br>
216233
* <b>Recommended for:</b> Servers running in offline mode (cracked).

src/main/java/me/leoko/advancedban/utils/commands/PunishmentProcessor.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import me.leoko.advancedban.utils.Punishment;
1010
import me.leoko.advancedban.utils.PunishmentType;
1111

12-
import java.sql.Time;
1312
import java.util.List;
1413
import java.util.function.Consumer;
14+
import java.util.function.Function;
1515

1616
import static me.leoko.advancedban.utils.CommandUtils.*;
1717

@@ -27,17 +27,17 @@ public void accept(Command.CommandInput input) {
2727
boolean silent = processTag(input, "-s");
2828
String name = input.getPrimary();
2929

30-
// is exempted
31-
if (processExempt(input, type))
32-
return;
33-
3430
// extract target
3531
String target = type.isIpOrientated()
3632
? processIP(input)
3733
: processName(input);
3834
if (target == null)
3935
return;
4036

37+
// is exempted
38+
if (processExempt(name, target, input.getSender(), type))
39+
return;
40+
4141
// calculate duration if necessary
4242
Long end = -1L;
4343
String timeTemplate = "";
@@ -48,7 +48,7 @@ public void accept(Command.CommandInput input) {
4848

4949
end = calculation.time;
5050

51-
if(calculation.template != null)
51+
if (calculation.template != null)
5252
timeTemplate = calculation.template;
5353
}
5454

@@ -90,57 +90,64 @@ private static TimeCalculation processTime(Command.CommandInput input, String uu
9090
List<String> timeLayout = mi.getStringList(mi.getLayouts(), "Time." + layout);
9191
String timeName = timeLayout.get(Math.min(i, timeLayout.size() - 1));
9292
if (timeName.equalsIgnoreCase("perma")) {
93-
return new TimeCalculation(layout, -1L);
93+
return new TimeCalculation(layout, -1L);
9494
}
95-
Long actualTime = TimeManager.getTime() + TimeManager.toMilliSec(timeName);
95+
Long actualTime = TimeManager.getTime() + TimeManager.toMilliSec(timeName);
9696
return new TimeCalculation(layout, actualTime);
9797
}
98-
long toAdd = TimeManager.toMilliSec(time);
99-
if (!Universal.get().hasPerms(input.getSender(), "ab." + type.getName() + ".dur.max")) {
100-
long max = -1;
101-
for (int i = 10; i >= 1; i--) {
102-
if (Universal.get().hasPerms(input.getSender(), "ab." + type.getName() + ".dur." + i) &&
103-
mi.contains(mi.getConfig(), "TempPerms." + i)) {
104-
max = mi.getLong(mi.getConfig(), "TempPerms." + i) * 1000;
105-
break;
106-
}
107-
}
108-
if (max != -1 && toAdd > max) {
109-
MessageManager.sendMessage(input.getSender(), type.getConfSection() + ".MaxDuration", true, "MAX", max / 1000 + "");
110-
return null;
111-
}
112-
}
113-
return new TimeCalculation(null, TimeManager.getTime() + toAdd);
98+
long toAdd = TimeManager.toMilliSec(time);
99+
if (!Universal.get().hasPerms(input.getSender(), "ab." + type.getName() + ".dur.max")) {
100+
long max = -1;
101+
for (int i = 10; i >= 1; i--) {
102+
if (Universal.get().hasPerms(input.getSender(), "ab." + type.getName() + ".dur." + i) &&
103+
mi.contains(mi.getConfig(), "TempPerms." + i)) {
104+
max = mi.getLong(mi.getConfig(), "TempPerms." + i) * 1000;
105+
break;
106+
}
107+
}
108+
if (max != -1 && toAdd > max) {
109+
MessageManager.sendMessage(input.getSender(), type.getConfSection() + ".MaxDuration", true, "MAX", max / 1000 + "");
110+
return null;
111+
}
112+
}
113+
return new TimeCalculation(null, TimeManager.getTime() + toAdd);
114114
}
115115

116116
// Checks whether target is exempted from punishment
117-
private static boolean processExempt(Command.CommandInput input, PunishmentType type) {
118-
String name = input.getPrimary();
117+
private static boolean processExempt(String name, String target, Object sender, PunishmentType type) {
119118
MethodInterface mi = Universal.get().getMethods();
120119
String dataName = name.toLowerCase();
121-
// ( isOnline && hasOnlineExempt ) || hasOfflineExempt
122-
if ((mi.isOnline(dataName) && !canPunish(input.getSender(), mi.getPlayer(dataName), type.getName()))
123-
|| Universal.get().isExemptPlayer(dataName)) {
124-
MessageManager.sendMessage(input.getSender(), type.getBasic().getConfSection() + ".Exempt",
120+
121+
boolean onlineExempt = false;
122+
if (mi.isOnline(dataName)) {
123+
Object onlineTarget = mi.getPlayer(dataName);
124+
onlineExempt = canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasPerms(onlineTarget, perms), type.getName());
125+
}
126+
127+
boolean offlineExempt = !onlineExempt && (Universal.get().isExemptPlayer(dataName) || canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasOfflinePerms(name, perms), type.getName()));
128+
129+
if (onlineExempt || offlineExempt) {
130+
MessageManager.sendMessage(sender, type.getBasic().getConfSection() + ".Exempt",
125131
true, "NAME", name);
126132
return true;
127133
}
128134
return false;
129135
}
130136

131137
// Check based on exempt level if some is able to ban a player
132-
public static boolean canPunish(Object operator, Object target, String path) {
138+
public static boolean canNotPunish(Function<String, Boolean> operatorHasPerms, Function<String, Boolean> targetHasPerms, String path) {
133139
final String perms = "ab." + path + ".exempt";
134-
if (Universal.get().hasPerms(target, perms))
135-
return false;
140+
if (targetHasPerms.apply(perms))
141+
return true;
142+
143+
int targetLevel = permissionLevel(targetHasPerms, perms);
136144

137-
int targetLevel = permissionLevel(target, perms);
138-
return targetLevel == 0 || permissionLevel(operator, perms) > targetLevel;
145+
return targetLevel != 0 && permissionLevel(operatorHasPerms, perms) <= targetLevel;
139146
}
140147

141-
private static int permissionLevel(Object subject, String permission){
148+
private static int permissionLevel(Function<String, Boolean> hasPerms, String permission) {
142149
for (int i = 10; i >= 1; i--)
143-
if(Universal.get().hasPerms(subject, permission+"."+i))
150+
if (hasPerms.apply(permission + "." + i))
144151
return i;
145152

146153
return 0;
@@ -164,7 +171,7 @@ private static boolean alreadyPunished(String target, PunishmentType type) {
164171
|| (type.getBasic() == PunishmentType.BAN && PunishmentManager.get().isBanned(target));
165172
}
166173

167-
private static class TimeCalculation{
174+
private static class TimeCalculation {
168175
private String template;
169176
private Long time;
170177

src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ MuteCommands:
6262

6363
# These players will not be able to get punished in any way
6464
# this also works if the player is offline
65+
# Use Vault to make exempt permissions also work for offline players
6566
ExemptPlayers:
6667
- 'Leoko'
6768
- 'md5'

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: ${project.artifactId}
22
version: ${project.version}
33
main: ${project.groupId}.bukkit.BukkitMain
44
author: Leoko
5+
softdepend: [Vault]
56

67
commands:
78
AdvancedBan:

0 commit comments

Comments
 (0)