This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
generated from MinestomPlugins/minestom-library-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Window5000/master
why is dev out of date lol
- Loading branch information
Showing
8 changed files
with
254 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package xyz.citywide.next.extension; | ||
|
||
import xyz.citywide.next.permission.PermissionProvider; | ||
|
||
/** | ||
* Normal CityStom extension that Automatically creates a PermissionProvider. | ||
* @see xyz.citywide.citystom.Extension | ||
*/ | ||
public abstract class Extension extends xyz.citywide.citystom.Extension { | ||
|
||
/** | ||
* The extension PermissionProvider. | ||
* | ||
* @see PermissionProvider | ||
*/ | ||
|
||
public static PermissionProvider provider; | ||
|
||
/** | ||
* Create a new Extension and create PermissionProvider with custom op level requirement. | ||
* | ||
* @param permission The permission that this extension will use. | ||
* @param oplevel The custom op level requirement. | ||
* | ||
* @see Extension#Extension() | ||
* @see Extension#Extension(String) | ||
*/ | ||
public Extension(String permission, int oplevel) { | ||
provider = new PermissionProvider(oplevel, permission); | ||
} | ||
|
||
/** | ||
* Create a new Extension and create PermissionProvider. | ||
* | ||
* @param permission The permission that this extension will use. | ||
* | ||
* @see Extension#Extension() | ||
* @see Extension#Extension(String) | ||
*/ | ||
public Extension(String permission) { | ||
provider = new PermissionProvider(permission); | ||
} | ||
|
||
/** | ||
* Create a new Extension and create PermissionProvider with the extension name from extension.json. | ||
* | ||
* @see Extension#Extension(String) | ||
* @see Extension#Extension(String, int) | ||
*/ | ||
public Extension() { | ||
provider = new PermissionProvider(getOrigin().getName()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/xyz/citywide/next/permission/GlobalPermissionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package xyz.citywide.next.permission; | ||
|
||
import net.minestom.server.entity.Player; | ||
|
||
|
||
/** | ||
* | ||
*Static methods for handling permissions. | ||
* <pre></pre> | ||
*See also: {@link PermissionProvider PermissionProvider} | ||
* | ||
*/ | ||
public class GlobalPermissionProvider { | ||
/** | ||
* Check if the player has a root permission. Uses op level 4 by default. | ||
* @param player the player | ||
* @param permission The permission to check | ||
* @return if the player has the permission | ||
*/ | ||
public static boolean hasPermission(Player player, String permission) { | ||
return player.hasPermission("*") || player.hasPermission(permission) || player.getPermissionLevel() >= 4; | ||
} | ||
|
||
/** | ||
* Check if the player has a root permission. | ||
* @param player the player | ||
* @param permission The permission to check | ||
* @param opLevel the minimum permission level where the player bypasses permission check. | ||
* @return if the player has the permission | ||
*/ | ||
public static boolean hasPermission(Player player, String permission, int opLevel) { | ||
return player.hasPermission("*") || player.hasPermission(permission) || player.getPermissionLevel() >= opLevel; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/xyz/citywide/next/permission/PermissionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package xyz.citywide.next.permission; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import net.minestom.server.entity.Player; | ||
|
||
/** | ||
* | ||
*Easy methods for handling permissions. | ||
* <pre></pre> | ||
*See also: {@link GlobalPermissionProvider GlobalPermissionProvider} | ||
* | ||
*/ | ||
public class PermissionProvider { | ||
|
||
/** | ||
* Permission | ||
*/ | ||
@Getter protected String extensionPermission; | ||
/** | ||
*<pre> | ||
*The minimum permission level where the player bypasses permission check. | ||
*1-4, 5 to disable. | ||
*</pre> | ||
*/ | ||
@Getter @Setter public int opLevel; | ||
|
||
/** | ||
* Creates a new PermissionProvider | ||
* @param extensionPermission The permission that this that will be checked | ||
*/ | ||
public PermissionProvider(String extensionPermission) { | ||
this.extensionPermission = extensionPermission; | ||
opLevel = 4; | ||
} | ||
/** | ||
* Creates a new PermissionProvider | ||
* @param opLevel The permission level where a player will be able to bypass permissions | ||
* @param extensionPermission The permission that this that will be checked | ||
*/ | ||
public PermissionProvider(int opLevel, String extensionPermission) { | ||
this.extensionPermission = extensionPermission; | ||
this.opLevel = opLevel; | ||
} | ||
|
||
/** | ||
* Replaces permission with new permissions | ||
* @param extensionPermission The new permissions | ||
*/ | ||
public void updatePermissions(String extensionPermission) { | ||
this.extensionPermission = extensionPermission; | ||
opLevel = 4; | ||
} | ||
/** | ||
* Replaces permission with new permissions | ||
* @param opLevel the new op level | ||
* @param extensionPermission The new permissions | ||
*/ | ||
public void updatePermissions(int opLevel, String extensionPermission) { | ||
this.extensionPermission = extensionPermission; | ||
this.opLevel = opLevel; | ||
} | ||
|
||
/** | ||
* Checks if the player has a subpermission | ||
* @param player the player to check | ||
* @param permission the permission | ||
* @return if the player has the permission | ||
*/ | ||
public boolean hasPermission(Player player, String permission) { | ||
if (player.hasPermission("*")|| player.getPermissionLevel() >= opLevel) | ||
return true; | ||
return player.hasPermission(extensionPermission + ".*") || player.hasPermission(extensionPermission + "." + permission); | ||
} | ||
|
||
/** | ||
* Checks if the player has one of the provider permissions | ||
* @param player the player to check | ||
* @return if the player has permission | ||
*/ | ||
public boolean hasPermission(Player player) { | ||
if(player.hasPermission("*") || player.getPermissionLevel() >= opLevel) return true; | ||
return player.hasPermission(extensionPermission); | ||
} | ||
|
||
/** | ||
* Checks if the player has a subpermission | ||
* @param player the player to check | ||
* @param permission the permission | ||
* @param opLevel override the default provider op level | ||
* @return if the player has the permission | ||
*/ | ||
public boolean hasPermission(Player player, String permission, int opLevel) { | ||
if (player.hasPermission("*")|| player.getPermissionLevel() >= opLevel) | ||
return true; | ||
return player.hasPermission(extensionPermission + ".*") || player.hasPermission(extensionPermission + "." + permission); | ||
} | ||
|
||
/** | ||
* Checks if the player has one of the provider permissions | ||
* @param player the player to check | ||
* @param opLevel override the default provider op level | ||
* @return if the player has permission | ||
*/ | ||
public boolean hasPermission(Player player, int opLevel) { | ||
if(player.hasPermission("*") || player.getPermissionLevel() >= opLevel) return true; | ||
return player.hasPermission(extensionPermission); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/xyz/citywide/next/permission/SubPermission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package xyz.citywide.next.permission; | ||
|
||
/** | ||
*Subpermission of a {@link PermissionProvider PermissionProvider} | ||
*/ | ||
public class SubPermission extends PermissionProvider { | ||
|
||
/** | ||
* THe parent permission. | ||
*/ | ||
public PermissionProvider parent; | ||
|
||
/** | ||
* Creates a new subpermission | ||
* @param parent The parent permission can be either a permission provider or a SubPermission | ||
* @param extensionPermission The permission that this that will be checked | ||
*/ | ||
public SubPermission(PermissionProvider parent, String extensionPermission) { | ||
super(parent.extensionPermission + "." + extensionPermission); | ||
this.parent = parent; | ||
} | ||
|
||
/** | ||
* Creates a new subpermission | ||
* @param parent The parent permission can be either a permission provider or a SubPermission | ||
* @param opLevel The permission level where a player will be able to bypass permissions | ||
* @param extensionPermission The permission that will be checked | ||
*/ | ||
public SubPermission(PermissionProvider parent, int opLevel, String extensionPermission) { | ||
super(opLevel, parent.extensionPermission + "." + extensionPermission); | ||
this.parent = parent; | ||
} | ||
|
||
/** | ||
* Replaces permission with new permissions | ||
* @param extensionPermission The new permission | ||
*/ | ||
@Override | ||
public void updatePermissions(String extensionPermission) { | ||
super.updatePermissions(parent.extensionPermission + "." + extensionPermission); | ||
} | ||
/** | ||
* Replaces permission with new permissions | ||
* @param opLevel the new op level | ||
* @param extensionPermission The new permission | ||
*/ | ||
public void updatePermissions(int opLevel, String extensionPermission) { | ||
super.updatePermissions(opLevel, parent.extensionPermission + "." + extensionPermission); | ||
} | ||
} |