55import fr .gamecreep .basichomes .entities .permissions .DefaultPermissions ;
66import fr .gamecreep .basichomes .entities .permissions .PermissionFile ;
77import fr .gamecreep .basichomes .entities .permissions .PlayerPermissions ;
8+ import fr .gamecreep .basichomes .utils .LoggerUtils ;
89import lombok .NonNull ;
910import org .bukkit .entity .Player ;
1011import org .bukkit .permissions .PermissionAttachment ;
@@ -27,7 +28,11 @@ public void setPermission(@NonNull final UUID playerId, @NonNull final String pe
2728 final PlayerPermissions entry = getOrCreatePlayerEntry (playerId );
2829 entry .getPermissions ().put (permissionNode , value );
2930 this .dataStore .save ();
30- this .applyPermissions (playerId );
31+
32+ Player player = this .plugin .getServer ().getPlayer (playerId );
33+ if (player != null ) {
34+ this .refreshPermissions (player );
35+ }
3136 }
3237
3338 public void setDefaultPermission (@ NonNull final DefaultPermissions .GroupPermission group ,
@@ -43,7 +48,11 @@ public void removePermission(@NonNull final UUID playerId, @NonNull final String
4348 final PlayerPermissions entry = getOrCreatePlayerEntry (playerId );
4449 entry .getPermissions ().remove (permissionNode );
4550 this .dataStore .save ();
46- this .applyPermissions (playerId );
51+
52+ Player player = this .plugin .getServer ().getPlayer (playerId );
53+ if (player != null ) {
54+ this .refreshPermissions (player );
55+ }
4756 }
4857
4958 public void removeDefaultPermission (@ NonNull final DefaultPermissions .GroupPermission group ,
@@ -138,36 +147,52 @@ public List<DefaultPermissions.GroupPermission> getPlayerGroups(@NonNull final P
138147 return groups ;
139148 }
140149
141- public void applyPermissions (@ NonNull final Player player ) {
142- final UUID playerId = player .getUniqueId ();
143-
144- final PermissionAttachment oldAttachment = this .plugin .getPermissionAttachments ().remove (playerId );
145- if (oldAttachment != null ) {
146- player .removeAttachment (oldAttachment );
150+ private void applyDefaultPermissions () {
151+ for (final Player player : this .plugin .getServer ().getOnlinePlayers ()) {
152+ this .refreshPermissions (player );
147153 }
154+ }
155+
156+ public void refreshPermissions (@ NonNull Player player ) {
157+ UUID uuid = player .getUniqueId ();
158+ PermissionAttachment attachment = this .plugin .getPermissionAttachments ().get (uuid );
148159
149- final PermissionAttachment newAttachment = player .addAttachment (this .plugin );
160+ if (attachment == null ) {
161+ // Fallback: create a new attachment
162+ attachment = player .addAttachment (this .plugin );
163+ this .plugin .getPermissionAttachments ().put (uuid , attachment );
164+ LoggerUtils .logInfo ("Recreating permission attachment for player " + uuid );
165+ }
150166
151- final List <DefaultPermissions .GroupPermission > groups = this .getPlayerGroups (player );
152- for (final DefaultPermissions .GroupPermission group : groups ) {
153- this .getDefaultPermissions (group ).forEach (newAttachment ::setPermission );
167+ for (String key : new HashSet <>(attachment .getPermissions ().keySet ())) {
168+ attachment .unsetPermission (key );
154169 }
155170
156- this .getPlayerPermissions (playerId ).forEach (newAttachment ::setPermission );
171+ for (DefaultPermissions .GroupPermission group : getPlayerGroups (player )) {
172+ getDefaultPermissions (group ).forEach (attachment ::setPermission );
173+ }
157174
158- this . plugin . getPermissionAttachments (). put ( playerId , newAttachment );
175+ getPlayerPermissions ( uuid ). forEach ( attachment :: setPermission );
159176 }
160177
161- public void applyPermissions ( @ NonNull final UUID playerId ) {
162- final Player player = this . plugin . getServer (). getPlayer ( playerId );
163- if ( player != null && player . isOnline ()) {
164- this . applyPermissions ( player );
165- }
178+ public void handlePlayerJoin ( Player player ) {
179+ PermissionAttachment attachment = player . addAttachment ( this . plugin );
180+ this . plugin . getPermissionAttachments (). put ( player . getUniqueId (), attachment );
181+
182+ this . refreshPermissions ( player );
166183 }
167184
168- private void applyDefaultPermissions () {
169- for (final Player player : this .plugin .getServer ().getOnlinePlayers ()) {
170- this .applyPermissions (player );
185+ public void handlePlayerQuit (Player player ) {
186+ UUID uuid = player .getUniqueId ();
187+ PermissionAttachment att = this .plugin .getPermissionAttachments ().remove (uuid );
188+
189+ if (att != null ) {
190+ try {
191+ player .removeAttachment (att );
192+ } catch (IllegalArgumentException ignored ) {
193+ LoggerUtils .logWarning ("Unable to remove permission attachment for player " + uuid );
194+ }
171195 }
172196 }
197+
173198}
0 commit comments