Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from Window5000/master
Browse files Browse the repository at this point in the history
why is dev out of date lol
  • Loading branch information
Window5000 authored Jan 10, 2023
2 parents 5085abe + da2e124 commit ca3a055
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 47 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

# NextLib



This branch contains code that is unstable, has not been tested and is not ready for production.
Basic but useful utilities for creating Minestom extension (and other Minestom related stuff).
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'java'
id "com.github.johnrengelman.shadow" version "7.1.2"
id 'maven-publish'
id "io.freefair.lombok" version "6.6.1"
}

group 'com.github.MinestomPlugins'
Expand All @@ -16,7 +17,6 @@ repositories {

dependencies {
compileOnly 'com.github.Minestom:Minestom:-SNAPSHOT'
implementation 'com.github.MinestomPlugins:NextPermissions:v1.1.3'
implementation 'com.github.CityWideMC:CityStom:1.4.0'
}

Expand Down
25 changes: 0 additions & 25 deletions src/main/java/me/window/next/extension/Extension.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.window.next.command;
package xyz.citywide.next.command;

import me.window.permissions.PermissionProvider;
import me.window.permissions.SubPermission;
import xyz.citywide.next.permission.PermissionProvider;
import xyz.citywide.next.permission.SubPermission;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.adventure.audience.Audiences;
Expand All @@ -21,11 +21,6 @@
*/
public class Command extends net.minestom.server.command.builder.Command {

/**
* The base permissions of this command
*/
protected String permission;

/**
* The permission provider for this command
* @see SubPermission
Expand Down Expand Up @@ -56,23 +51,15 @@ public Command(@NotNull PermissionProvider provider, @NotNull String name, @Null
* @param permission The permission to be used
*/
public void setPermission(String permission) {
this.permission = permission;
updatePermissions();
provider.updatePermissions(permission);
}

/**
* Set the required permissions level required for bypassing permissions.
* @param level The new op level
*/
public void setPermissionLevel(int level) {
provider.updatePermissions(level, permission);
}

/**
* Updates the subpermission permissions with the new command permissions.
*/
public void updatePermissions() {
provider.updatePermissions(permission);
provider.setOpLevel(level);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/xyz/citywide/next/extension/Extension.java
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());
}
}
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 src/main/java/xyz/citywide/next/permission/PermissionProvider.java
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 src/main/java/xyz/citywide/next/permission/SubPermission.java
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);
}
}

0 comments on commit ca3a055

Please sign in to comment.