Permissions System

Discussion in 'Plugin Development' started by Blackveil, Apr 7, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Blackveil

    Alright, so I'm working on my own permissions system. I haven't tested the script yet, but I want to see if there's anything I'm missing out of these two basic classes. Please be aware that I have it in my other scripts, but my Permissions Manager does exist too, I just have not put the code up.

    Group.java
    Show Spoiler

    Code:java
    1. /*
    2. * This plugin is licensed and owned by (myself) Blackveil also as known as Blackveiled
    3. * It is free to use, however no support nor database schemas are available
    4. * from (myself) Blackveil, you are responsible for maintaining your server's database
    5. * and keeping it up to date as new releases are given, and just to warn you again
    6. * I do NOT offer support for this plugin, you must modify everything yourself to
    7. * use it for your own experiments or Minecraft servers.
    8. */
    9.  
    10. package com.blackveiled.roc.permissions;
    11. import com.blackveiled.roc.RealmOfConquest;
    12. import com.blackveiled.roc.player.Character;
    13. import java.util.UUID;
    14. import java.util.HashMap;
    15. import java.util.Map;
    16. import java.util.List;
    17. import java.util.ArrayList;
    18. import org.bukkit.ChatColor;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.command.CommandSender;
    21.  
    22. public class Group {
    23. public String name = null;
    24. public String prefix = null;
    25. public String suffix = null;
    26. public String error = null;
    27. public Map<String, Permission> permissions = new HashMap<String, Permission>();
    28. public List<String> groups = new ArrayList<String>();
    29. public List<UUID> players = new ArrayList<UUID>();
    30. /**s
    31. * @param Name String value is required to set a name for the Group object.
    32. */
    33. public Group(String Name) {
    34. this.name = Name;
    35. }
    36.  
    37. /**
    38. * @return The name of the group you have selected. If the group you selected does not exist, you will receive a NullPointerException.
    39. */
    40. public String getName() {
    41. return this.name;
    42. }
    43. /**
    44. * @return The plugin will verify if the player who executed the command is a part of this group and if the permission exists as well as if all the permissions requirements are fulfilled.
    45. * @param s - CommandSender required
    46. * @param node - Permission node required
    47. */
    48. public boolean hasPermission(Player p, String node) {
    49. // If player is an operator, they will automatically have permission for whatever they do.
    50. if(p.isOp()) {
    51. return true;
    52. }// To avoid NullPointerException anyways, if no players are in the group then this will return false.
    53. if(!this.players.isEmpty()) {
    54. return false;
    55. }
    56. UUID uuid = p.getUniqueId();// Check if the player exists in the group. If they do, carry on.
    57. if(!this.players.contains(uuid)) {
    58. return false;
    59. }
    60. if(RealmOfConquest.Characters.containsKey(p.getName())) {
    61. Character character = RealmOfConquest.Characters.get(p.getName());
    62. if(character.permissions.containsKey(node)) {
    63. Permission permission = character.permissions.get(node);
    64. if(permission.getPermissionLevel()==true) {// If this permission is not set to any particular world, then return true.
    65. if(permission.getPermissionWorld()==null) {
    66. return true;
    67. }// If the command sender is in the required world, then return true.
    68. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    69. return true;
    70. }
    71. }
    72. }
    73. }
    74. // Check if the group even has permissions to avoid NullPointerException
    75. if(this.permissions.isEmpty()) {
    76. return false;
    77. }// Check if the permission node exists inside of the group's permissions.
    78. if(this.permissions.containsKey(node)) {
    79. Permission permission = this.permissions.get(node);
    80. if(permission.getPermissionLevel()==true) {
    81. // If this permission is not set to any particular world, then return true.
    82. if(permission.getPermissionWorld()==null) {
    83. return true;
    84. }// If the command sender is in the required world, then return true.
    85. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    86. return true;
    87. }
    88. }
    89. }
    90. // Check if the group even has groups to inherit permissions from.
    91. if(!this.groups.isEmpty()) {
    92. for(int i = 0; i < this.groups.size(); i++) {
    93. String groupname = this.groups.get(i);
    94. // Check if the server's global groups even contains the group inside.
    95. if(PermissionManager.Groups.containsKey(groupname)) {
    96. Group group = PermissionManager.Groups.get(groupname);
    97. if(group.permissions.containsKey(node)) {
    98. Permission permission = this.permissions.get(node);
    99. if(permission.getPermissionLevel()==true) {
    100. // If this permission is not set to any particular world, then return true.
    101. if(permission.getPermissionWorld()==null) {
    102. return true;
    103. }// If the command sender is in the required world, then return true.
    104. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    105. return true;
    106. }
    107. }
    108. }
    109. }
    110. }
    111. }
    112. return false;
    113. }
    114. /**
    115. * @return The plugin will verify if this whoever executed the command is a part of this group and if the permission exists as well as if all the permissions requirements are fulfilled.
    116. * @param s - CommandSender required
    117. * @param node - Permission node required
    118. */
    119. public boolean hasPermission(CommandSender s, String node) {
    120. // If player is an operator, they will automatically have permission for whatever they do.
    121. if(s.isOp()) {
    122. return true;
    123. }// To avoid NullPointerException anyways, if no players are in the group then this will return false.
    124. if(!this.players.isEmpty()) {
    125. return false;
    126. }
    127. if(s instanceof Player) {
    128. Player p = (Player) s;
    129. UUID uuid = p.getUniqueId();// Check if the player exists in the group. If they do, carry on.
    130. if(!this.players.contains(uuid)) {
    131. return false;
    132. }
    133. if(RealmOfConquest.Characters.containsKey(p.getName())) {
    134. Character character = RealmOfConquest.Characters.get(p.getName());
    135. if(character.permissions.containsKey(node)) {
    136. Permission permission = character.permissions.get(node);
    137. if(permission.getPermissionLevel()==true) {
    138. if(s instanceof Player) {// If this permission is not set to any particular world, then return true.
    139. if(permission.getPermissionWorld()==null) {
    140. return true;
    141. }// If the command sender is in the required world, then return true.
    142. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    143. return true;
    144. }
    145. }
    146. }
    147. }
    148. }
    149. }// Check if the group even has permissions to avoid NullPointerException
    150. if(this.permissions.isEmpty()) {
    151. return false;
    152. }// Check if the permission node exists inside of the group's permissions.
    153. if(this.permissions.containsKey(node)) {
    154. Permission permission = this.permissions.get(node);
    155. if(permission.getPermissionLevel()==true) {
    156. if(s instanceof Player) {
    157. Player p = (Player) s;// If this permission is not set to any particular world, then return true.
    158. if(permission.getPermissionWorld()==null) {
    159. return true;
    160. }// If the command sender is in the required world, then return true.
    161. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    162. return true;
    163. }
    164. }
    165. }
    166. }// Check if the group even has groups to inherit permissions from.
    167. if(!this.groups.isEmpty()) {
    168. for(int i = 0; i < this.groups.size(); i++) {
    169. String groupname = this.groups.get(i);
    170. // Check if the server's global groups even contains the group inside.
    171. if(PermissionManager.Groups.containsKey(groupname)) {
    172. Group group = PermissionManager.Groups.get(groupname);
    173. if(group.permissions.containsKey(node)) {
    174. Permission permission = this.permissions.get(node);
    175. if(permission.getPermissionLevel()==true) {
    176. if(s instanceof Player) {
    177. Player p = (Player) s;// If this permission is not set to any particular world, then return true.
    178. if(permission.getPermissionWorld()==null) {
    179. return true;
    180. }// If the command sender is in the required world, then return true.
    181. if(permission.getPermissionWorld().equalsIgnoreCase(p.getWorld().getName())) {
    182. return true;
    183. }
    184. }
    185. }
    186. }
    187. }
    188. }
    189. }
    190. return false;
    191. }
    192. /**
    193. * @return An attempt will be made to add a permission to the group. A boolean value is returned if it was successfully added to the group.
    194. * @param Permission Permission object is required to be added to the Group object.
    195. */
    196. public boolean addPermission(Permission Permission) {
    197. if(this.permissions.containsKey(Permission.getPermission())) {
    198. this.error = "This group already has that permission!";
    199. return false;
    200. }
    201. this.permissions.put(Permission.getPermission(), Permission);
    202. this.error = "Successfully added permission to group!";
    203. return true;
    204. }
    205. /**
    206. * @return An attempt will be made to remove a permission to the group. A boolean value is returned if it was successfully removed from the group.
    207. * @param Permission Permission object is required to be added to the Group object.
    208. */
    209. public boolean removePermission(Permission Permission) {
    210. if(this.permissions.containsKey(Permission.getPermission())) {
    211. this.permissions.remove(Permission.getPermission());
    212. this.error = "Successfully removed permission from group!";
    213. return true;
    214. }
    215. this.error = "This group does not have those permissions!";
    216. return false;
    217. }
    218. /**
    219. * @return An attempt will be made to add a group to inherit permissions from. A boolean value is returned if it was successfully added to the group.
    220. * @param Group Group object is required to add an inheritance.
    221. */
    222. public boolean addGroup(String Group) {
    223. if(this.groups.contains(Group)) {
    224. this.error = "This group already inherits the selected group!";
    225. return false;
    226. }
    227. if(PermissionManager.Groups.containsKey(Group)) {
    228. this.groups.add(Group);
    229. this.error = "Successfully added "+Group+" to "+this.getName()+"!";
    230. return true;
    231. }
    232. this.error = "That group does not exist!";
    233. return false;
    234. }
    235. /**
    236. * @return An attempt will be made to remove a group inheritance. A boolean value is returned if it was successfully removed from the group.
    237. * @param Group Group object is required to remove an inheritance.
    238. */
    239. public boolean removeGroup(String Group) {
    240. if(this.groups.contains(Group)) {
    241. this.groups.add(Group);
    242. this.error = "Successfully removed the group "+Group+" from group "+this.getName()+"!";
    243. return true;
    244. }
    245. this.error = "That group does not exist inside of this group!";
    246. return false;
    247. }
    248. /**
    249. * @return Returns a string with the chat colors stripped from the original string. Null if no prefix has been set.
    250. */
    251. public String getPrefix() {
    252. String output = ChatColor.stripColor(this.prefix);
    253. return output;
    254. }
    255. /**
    256. * @return Returns a string with the chat colors stripped from the original string. Null if no suffix has been set.
    257. */
    258. public String getSuffix() {
    259. String output = ChatColor.stripColor(this.suffix);
    260. return output;
    261. }
    262. /**
    263. * @return Returns a string with the chat colors if the prefix has them. Null if no prefix has been set.
    264. * [USER=13316]error[/USER] NullPointerException if a prefix has not been set.
    265. */
    266. public String getFullPrefix() {
    267. String output = ChatColor.translateAlternateColorCodes('&', this.prefix);
    268. return output;
    269. }
    270. /**
    271. * @return Returns a string with the chat colors if the suffix has them. Null if no suffix has been set.
    272. */
    273. public String getFullSuffix() {
    274. String output = ChatColor.translateAlternateColorCodes('&', this.suffix);
    275. return output;
    276. }
    277. /**
    278. * @return Returns a string of the last error message sent from the group class selected.
    279. */
    280. public String getLastError() {
    281. return this.error;
    282. }
    283.  
    284. }
    285.  



    Permission.java
    Show Spoiler

    Code:java
    1. /*
    2. * This plugin is licensed and owned by (myself) Blackveil also as known as Blackveiled
    3. * It is free to use, however no support nor database schemas are available
    4. * from (myself) Blackveil, you are responsible for maintaining your server's database
    5. * and keeping it up to date as new releases are given, and just to warn you again
    6. * I do NOT offer support for this plugin, you must modify everything yourself to
    7. * use it for your own experiments or Minecraft servers.
    8. */
    9.  
    10. package com.blackveiled.roc.permissions;
    11.  
    12. public class Permission {
    13. public String name = null;
    14. public String node = null;
    15. public String world = null;
    16. public boolean level = false;
    17.  
    18.  
    19.  
    20. public Permission(String node, boolean level) {
    21. this.node = node;
    22. this.level = level;
    23. }
    24. public Permission(String node, boolean level, String world) {
    25. this.node = node;
    26. this.level = level;
    27. this.world = world;
    28. }
    29. public Permission(String name, String node, boolean level) {
    30. this.name = name;
    31. this.node = node;
    32. this.level = level;
    33. }
    34. public Permission(String name, String node, boolean level, String world) {
    35. this.name = name;
    36. this.node = node;
    37. this.level = level;
    38. this.world = world;
    39. }
    40. /**
    41. * @return The permission node of the permission you selected. Returns a string value, null if no node exists.
    42. */
    43. public String getPermission() {
    44. return this.node;
    45. }
    46.  
    47. /**
    48. * @return The name of the permission you have selected. Returns a string value, null if no name exists.
    49. */
    50. public String getPermissionName() {
    51. return this.name;
    52. }
    53.  
    54. /**
    55. * @return The permission level of the permission you selected. Returns a boolean value, true or false.
    56. */
    57. public boolean getPermissionLevel() {
    58. return this.level;
    59. }
    60.  
    61. /**
    62. * @return The world which the group has this permission in. Returns a string value, if null then this permission applies to all worlds.
    63. */
    64. public String getPermissionWorld() {
    65. return this.world;
    66. }
    67.  
    68. public void setPermissionName(String name) {
    69. this.name = name;
    70. }
    71.  
    72. public void setPermissionNode(String node) {
    73. this.node = node;
    74. }
    75.  
    76. public void setPermissionLevel(boolean level) {
    77. this.level = level;
    78. }
    79.  
    80. public void setPermissionWorld(String world) {
    81. this.world = world;
    82. }
    83.  
    84. }



    No comments? Lol.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    RawCode

    making method for every field and making fields public makes no sense
     
  3. Offline

    Blackveil

    Well then. Thank you for no elaboration or providing anything constructive to this thread.
     
  4. Offline

    RawCode

    Blackveil
    looks like you too smart and dont need any help.

    but in such case, what reason of this thread?
     
  5. Offline

    Blackveil

    There was no implied meaning underneath my response to what you said. I'm looking for advice to help better myself and if you're going to say there's no reason to do something, explain why.
     
  6. Offline

    RawCode

    you can explain here and now why you added set\get methods for public fields?

    what reason for it?
     
Thread Status:
Not open for further replies.

Share This Page