3131import org .bukkit .entity .Player ;
3232import org .bukkit .permissions .PermissionAttachment ;
3333
34+ import java .util .Objects ;
3435import java .util .Optional ;
3536
3637/**
@@ -49,18 +50,25 @@ public static boolean hasPermissionOrSuperAdmin(CommandSender sender, String per
4950 return sender instanceof CommandBlock || sender instanceof ConsoleCommandSender ;
5051 }
5152
53+ private static void removeAllPermissions (Player player ) {
54+ player .getEffectivePermissions ().stream ().map (info -> {
55+ if (info .getAttachment () == null ) {
56+ // When the attachment is null, this is something coming from default Spigot
57+ return null ;
58+ }
59+ return info .getAttachment ();
60+ }).filter (Objects ::nonNull ).forEach (PermissionAttachment ::remove );
61+ }
62+
5263 public static void applyPermissions (Player player ) {
5364 PermissifyMain plugin = PermissifyMain .getInstance ();
5465
55- System .out .println ("FIRST" );
56- plugin .getAttachmentManager ().getAllAttachmentsForPlayer (player .getUniqueId ()).forEach (entry -> entry .getPermissions ().keySet ().forEach (System .out ::println ));
57- // plugin.getAttachmentManager().removeAttachment(player.getUniqueId());
66+ removeAllPermissions (player );
5867 plugin .getAttachmentManager ().resetAllPermissibles (player .getUniqueId ());
68+
5969 plugin .getAttachmentManager ().setAttachment (player .getUniqueId (), player .addAttachment (plugin ), Optional .empty ());
6070 player .recalculatePermissions ();
6171
62- plugin .getAttachmentManager ().getAllAttachmentsForPlayer (player .getUniqueId ()).forEach (attachment -> player .getEffectivePermissions ().forEach (info -> attachment .unsetPermission (info .getPermission ())));
63-
6472 plugin .getPermissifyAPI ().getDatabaseHandler ().ifPresent (handler -> {
6573 // Check if the player should be in a default group.
6674 if (handler .getDefaultGroup ().isPresent () && !handler .getDefaultGroup ().get ().hasPlayer (player .getUniqueId ())) {
@@ -71,24 +79,15 @@ public static void applyPermissions(Player player) {
7179
7280 // Add the player's "self" permissions
7381 plugin .getAttachmentManager ().getAttachment (player .getUniqueId (), Optional .empty ()).ifPresent (self ->
74- handler .getPermissions (player .getUniqueId ()).forEach (permission -> {
75- System .out .println (permission .getPermission () + " -> " + permission .isGranted ());
76- self .setPermission (permission .getPermission (), permission .isGranted ());
77- }));
82+ handler .getPermissions (player .getUniqueId ()).forEach (permission -> self .setPermission (permission .getPermission (), permission .isGranted ())));
7883
7984 // Add the player's group permissions
8085 handler .getGroups (player .getUniqueId ()).forEach (group -> {
81- PermissionAttachment attachment = plugin .getAttachmentManager ().getAttachment (player .getUniqueId (), Optional .of (group .getName ())).orElse (player .addAttachment (plugin ));
82- attachment .getPermissions ().keySet ().forEach (attachment ::unsetPermission );
86+ PermissionAttachment attachment = player .addAttachment (plugin );
8387
84- group .getPermissions ().forEach (permission -> {
85- System .out .println (group .getName () + ": " + permission .getPermission () + " -> " + permission .isGranted ());
86- attachment .setPermission (permission .getPermission (), permission .isGranted ());
87- });
88+ group .getPermissions ().forEach (permission -> attachment .setPermission (permission .getPermission (), permission .isGranted ()));
8889 plugin .getAttachmentManager ().setAttachment (player .getUniqueId (), attachment , Optional .of (group .getName ()));
8990 });
9091 });
91- System .out .println ("SECOND" );
92- plugin .getAttachmentManager ().getAllAttachmentsForPlayer (player .getUniqueId ()).forEach (entry -> entry .getPermissions ().keySet ().forEach (System .out ::println ));
9392 }
9493}
0 commit comments