9
9
import me .leoko .advancedban .utils .Punishment ;
10
10
import me .leoko .advancedban .utils .PunishmentType ;
11
11
12
- import java .sql .Time ;
13
12
import java .util .List ;
14
13
import java .util .function .Consumer ;
14
+ import java .util .function .Function ;
15
15
16
16
import static me .leoko .advancedban .utils .CommandUtils .*;
17
17
@@ -27,17 +27,17 @@ public void accept(Command.CommandInput input) {
27
27
boolean silent = processTag (input , "-s" );
28
28
String name = input .getPrimary ();
29
29
30
- // is exempted
31
- if (processExempt (input , type ))
32
- return ;
33
-
34
30
// extract target
35
31
String target = type .isIpOrientated ()
36
32
? processIP (input )
37
33
: processName (input );
38
34
if (target == null )
39
35
return ;
40
36
37
+ // is exempted
38
+ if (processExempt (name , target , input .getSender (), type ))
39
+ return ;
40
+
41
41
// calculate duration if necessary
42
42
Long end = -1L ;
43
43
String timeTemplate = "" ;
@@ -48,7 +48,7 @@ public void accept(Command.CommandInput input) {
48
48
49
49
end = calculation .time ;
50
50
51
- if (calculation .template != null )
51
+ if (calculation .template != null )
52
52
timeTemplate = calculation .template ;
53
53
}
54
54
@@ -90,57 +90,64 @@ private static TimeCalculation processTime(Command.CommandInput input, String uu
90
90
List <String > timeLayout = mi .getStringList (mi .getLayouts (), "Time." + layout );
91
91
String timeName = timeLayout .get (Math .min (i , timeLayout .size () - 1 ));
92
92
if (timeName .equalsIgnoreCase ("perma" )) {
93
- return new TimeCalculation (layout , -1L );
93
+ return new TimeCalculation (layout , -1L );
94
94
}
95
- Long actualTime = TimeManager .getTime () + TimeManager .toMilliSec (timeName );
95
+ Long actualTime = TimeManager .getTime () + TimeManager .toMilliSec (timeName );
96
96
return new TimeCalculation (layout , actualTime );
97
97
}
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 );
114
114
}
115
115
116
116
// 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 ) {
119
118
MethodInterface mi = Universal .get ().getMethods ();
120
119
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" ,
125
131
true , "NAME" , name );
126
132
return true ;
127
133
}
128
134
return false ;
129
135
}
130
136
131
137
// 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 ) {
133
139
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 );
136
144
137
- int targetLevel = permissionLevel (target , perms );
138
- return targetLevel == 0 || permissionLevel (operator , perms ) > targetLevel ;
145
+ return targetLevel != 0 && permissionLevel (operatorHasPerms , perms ) <= targetLevel ;
139
146
}
140
147
141
- private static int permissionLevel (Object subject , String permission ){
148
+ private static int permissionLevel (Function < String , Boolean > hasPerms , String permission ) {
142
149
for (int i = 10 ; i >= 1 ; i --)
143
- if ( Universal . get (). hasPerms ( subject , permission + "." + i ))
150
+ if ( hasPerms . apply ( permission + "." + i ))
144
151
return i ;
145
152
146
153
return 0 ;
@@ -164,7 +171,7 @@ private static boolean alreadyPunished(String target, PunishmentType type) {
164
171
|| (type .getBasic () == PunishmentType .BAN && PunishmentManager .get ().isBanned (target ));
165
172
}
166
173
167
- private static class TimeCalculation {
174
+ private static class TimeCalculation {
168
175
private String template ;
169
176
private Long time ;
170
177
0 commit comments