Skip to content

Commit e938745

Browse files
committed
API: Primary group support
1 parent 91bdba5 commit e938745

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

API/src/main/java/me/innectic/permissify/api/database/DatabaseHandler.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ protected void displayError(ConnectionError error, Exception e) {
148148
/**
149149
* Remove a player from a group.
150150
*
151-
* @param uuid the player to remove
151+
* @param uuid the player to remove
152152
* @param group the group to remove from
153-
* @return if the player was removed
153+
* @return if the player was removed
154154
*/
155155
public abstract boolean removePlayerFromGroup(UUID uuid, PermissionGroup group);
156156

@@ -165,10 +165,26 @@ protected void displayError(ConnectionError error, Exception e) {
165165
* Get all permission groups a player is in.
166166
*
167167
* @param uuid the uuid of the player
168-
* @return the groups the player is in
168+
* @return the groups the player is in
169169
*/
170170
public abstract List<PermissionGroup> getGroups(UUID uuid);
171171

172+
/**
173+
* Set the primary group of the player
174+
*
175+
* @param group the group to set as the primary
176+
* @param uuid the uuid of the player to set the primary of
177+
*/
178+
public abstract boolean setPrimaryGroup(PermissionGroup group, UUID uuid);
179+
180+
/**
181+
* Get the primary group of a player.
182+
*
183+
* @param uuid the uuid of the player to get
184+
* @return the primary group of the player
185+
*/
186+
public abstract Optional<PermissionGroup> getPrimaryGroup(UUID uuid);
187+
172188
/**
173189
* Update a player's cache.
174190
*

API/src/main/java/me/innectic/permissify/api/database/handlers/MySQLHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,35 @@ public List<PermissionGroup> getGroups(UUID uuid) {
335335
return cachedGroups.stream().filter(group -> group.hasPlayer(uuid)).collect(Collectors.toList());
336336
}
337337

338+
@Override
339+
public boolean setPrimaryGroup(PermissionGroup group, UUID uuid) {
340+
// @Return: Should primary group put the player in the group if they're not already in it?
341+
if (!group.hasPlayer(uuid)) return false;
342+
if (group.isPrimaryGroup(uuid)) return false;
343+
group.setPrimaryGroup(uuid, true);
344+
Optional<Connection> connection = getConnection();
345+
if (!connection.isPresent()) {
346+
displayError(ConnectionError.REJECTED);
347+
return false;
348+
}
349+
350+
try {
351+
PreparedStatement statement = connection.get().prepareStatement("UPDATE groupMembers SET `primary`=? WHERE uuid=? AND `group`=?");
352+
statement.setBoolean(1, true);
353+
statement.setString(2, uuid.toString());
354+
statement.setString(3, group.getName());
355+
statement.execute();
356+
} catch (SQLException e) {
357+
e.printStackTrace();
358+
}
359+
return true;
360+
}
361+
362+
@Override
363+
public Optional<PermissionGroup> getPrimaryGroup(UUID uuid) {
364+
return getGroups(uuid).stream().filter(group -> group.isPrimaryGroup(uuid)).findFirst();
365+
}
366+
338367
@Override
339368
public void updateCache(UUID uuid) {
340369
// TODO: Make this method not exist. It shouldn't be needed.

API/src/main/java/me/innectic/permissify/api/permission/PermissionGroup.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626

2727
import lombok.Getter;
2828
import lombok.RequiredArgsConstructor;
29+
import lombok.Setter;
2930

30-
import java.util.ArrayList;
31-
import java.util.List;
32-
import java.util.Optional;
33-
import java.util.UUID;
31+
import java.util.*;
3432

3533
/**
3634
* @author Innectic
@@ -43,7 +41,7 @@ public class PermissionGroup {
4341
@Getter private final String prefix;
4442
@Getter private final String suffix;
4543
@Getter private List<Permission> permissions = new ArrayList<>();
46-
@Getter private List<UUID> players = new ArrayList<>();
44+
@Getter private Map<UUID, Boolean> players = new HashMap<>();
4745

4846
/**
4947
* Remove a permission from the group
@@ -74,14 +72,22 @@ public boolean hasPermission(String permission) {
7472
}
7573

7674
public void addPlayer(UUID uuid) {
77-
if (!players.contains(uuid)) players.add(uuid);
75+
if (!hasPlayer(uuid)) players.put(uuid, false);
7876
}
7977

8078
public void removePlayer(UUID uuid) {
81-
players.removeIf(entry -> entry.equals(uuid));
79+
players.remove(uuid);
8280
}
8381

8482
public boolean hasPlayer(UUID uuid) {
85-
return players.contains(uuid);
83+
return players.containsKey(uuid);
84+
}
85+
86+
public boolean isPrimaryGroup(UUID uuid) {
87+
return players.containsKey(uuid) && players.get(uuid).equals(true);
88+
}
89+
90+
public void setPrimaryGroup(UUID uuid, boolean isPrimary) {
91+
players.put(uuid, isPrimary);
8692
}
8793
}

0 commit comments

Comments
 (0)