/
SPlayerRole.java
152 lines (120 loc) · 4.26 KB
/
SPlayerRole.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
package com.bgsoftware.superiorskyblock.island;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.PlayerRole;
import com.bgsoftware.superiorskyblock.island.permissions.RolePermissionNode;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
@SuppressWarnings("WeakerAccess")
public final class SPlayerRole implements PlayerRole {
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private final String name;
private final String displayName;
private final int id;
private final int weight;
private final RolePermissionNode defaultPermissions;
public SPlayerRole(String name, @Nullable String displayName, int id, int weight, List<String> defaultPermissions,
SPlayerRole previousRole) {
this.name = name;
this.displayName = displayName == null ? name : displayName;
this.id = id;
this.weight = weight;
StringBuilder permissions = new StringBuilder();
defaultPermissions.forEach(perm -> permissions.append(";").append(perm));
this.defaultPermissions = new RolePermissionNode(null,
previousRole == null ? RolePermissionNode.EmptyRolePermissionNode.INSTANCE : previousRole.defaultPermissions,
permissions.length() == 0 ? "" : permissions.substring(1));
}
public static PlayerRole defaultRole() {
return plugin.getRoles().getDefaultRole();
}
public static PlayerRole lastRole() {
return plugin.getRoles().getLastRole();
}
public static PlayerRole guestRole() {
return plugin.getRoles().getGuestRole();
}
public static PlayerRole coopRole() {
return plugin.getRoles().getCoopRole();
}
public static PlayerRole of(int weight) {
return plugin.getRoles().getPlayerRole(weight);
}
public static PlayerRole fromId(int id) {
return plugin.getRoles().getPlayerRoleFromId(id);
}
public static PlayerRole of(String name) {
return plugin.getRoles().getPlayerRole(name);
}
public static String getValuesString() {
StringBuilder stringBuilder = new StringBuilder();
plugin.getRoles().getRoles().forEach(playerRole -> stringBuilder.append(", ").append(playerRole.toString().toLowerCase(Locale.ENGLISH)));
return stringBuilder.substring(2);
}
@Override
public int getId() {
return id;
}
@Override
public String getName() {
return name;
}
@Override
public String getDisplayName() {
return displayName;
}
@Override
public int getWeight() {
return weight;
}
@Override
public boolean isHigherThan(PlayerRole role) {
Preconditions.checkNotNull(role, "playerRole parameter cannot be null.");
return getWeight() > role.getWeight();
}
@Override
public boolean isLessThan(PlayerRole role) {
Preconditions.checkNotNull(role, "playerRole parameter cannot be null.");
return getWeight() < role.getWeight();
}
@Override
public boolean isFirstRole() {
return getWeight() == 0;
}
@Override
public boolean isLastRole() {
return getWeight() == lastRole().getWeight();
}
@Override
public boolean isRoleLadder() {
return getWeight() >= 0 && (getPreviousRole() != null || getNextRole() != null);
}
@Override
public PlayerRole getNextRole() {
return getWeight() < 0 ? null : plugin.getRoles().getPlayerRole(getWeight() + 1);
}
@Override
public PlayerRole getPreviousRole() {
return getWeight() <= 0 ? null : plugin.getRoles().getPlayerRole(getWeight() - 1);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SPlayerRole that = (SPlayerRole) o;
return id == that.id;
}
@Override
public String toString() {
return name;
}
public RolePermissionNode getDefaultPermissions() {
return defaultPermissions;
}
}