generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
490 additions
and
3 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
45 changes: 45 additions & 0 deletions
45
...ommon/src/main/java/com/xpdustry/distributor/core/permission/ImmutablePermissionTree.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,45 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.permission; | ||
|
||
import java.util.Map; | ||
|
||
final class ImmutablePermissionTree implements PermissionTree { | ||
|
||
private final PermissionTree inner; | ||
|
||
ImmutablePermissionTree(final PermissionTree inner) { | ||
this.inner = inner; | ||
} | ||
|
||
@Override | ||
public TriState getPermission(final String permission) { | ||
return inner.getPermission(permission); | ||
} | ||
|
||
@Override | ||
public void setPermission(final String permission, final TriState state) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Map<String, Boolean> getPermissions() { | ||
return inner.getPermissions(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...utor-common/src/main/java/com/xpdustry/distributor/core/permission/PermissionManager.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,47 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.permission; | ||
|
||
import java.util.regex.Pattern; | ||
import mindustry.gen.Player; | ||
|
||
public interface PermissionManager { | ||
|
||
/** | ||
* Regex pattern used to validate permission strings. | ||
* <p> | ||
* <strong>Notes:</strong> | ||
* <blockquote> | ||
* Permission strings are composed of a series of nodes separated by dots with alphanumeric characters and minus | ||
* signs, such as {@code "plugin.command"}. | ||
* <br> | ||
* A parent node is always overridden by a child node, such as {@code "plugin.command"} overriding {@code "plugin"}. | ||
* <br> | ||
* Wildcards are also allowed, but they currently have the same effect as a normal node, such as {@code "plugin.command.*"} equals {@code "plugin.command"}. | ||
* <br> | ||
* The only relevant use of wildcards is the root permission {@code "*"} permission. It | ||
* allows you to set a default value for all permissions. | ||
* </blockquote> | ||
*/ | ||
String PERMISSION_REGEX = "^(\\*|[a-z\\d\\-]+)(\\.(\\*|[a-z\\d\\-]+))*$"; | ||
|
||
Pattern PERMISSION_PATTERN = Pattern.compile(PERMISSION_REGEX); | ||
|
||
TriState getPermission(final Player player, final String permission); | ||
} |
38 changes: 38 additions & 0 deletions
38
...ributor-common/src/main/java/com/xpdustry/distributor/core/permission/PermissionTree.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,38 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.permission; | ||
|
||
import java.util.Map; | ||
|
||
public interface PermissionTree { | ||
|
||
static PermissionTree simple() { | ||
return new SimplePermissionTree(); | ||
} | ||
|
||
static PermissionTree immutable(final PermissionTree tree) { | ||
return (tree instanceof ImmutablePermissionTree) ? tree : new ImmutablePermissionTree(tree); | ||
} | ||
|
||
TriState getPermission(final String permission); | ||
|
||
void setPermission(final String permission, final TriState state); | ||
|
||
Map<String, Boolean> getPermissions(); | ||
} |
121 changes: 121 additions & 0 deletions
121
...r-common/src/main/java/com/xpdustry/distributor/core/permission/SimplePermissionTree.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,121 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.permission; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
final class SimplePermissionTree implements PermissionTree { | ||
|
||
private final @Nullable SimplePermissionTree parent; | ||
private final Map<String, SimplePermissionTree> children = new HashMap<>(); | ||
private TriState value = TriState.UNDEFINED; | ||
|
||
SimplePermissionTree() { | ||
this.parent = null; | ||
} | ||
|
||
private SimplePermissionTree(final @Nullable SimplePermissionTree parent) { | ||
this.parent = parent; | ||
} | ||
|
||
public TriState getPermission(final String permission) { | ||
if (!PermissionManager.PERMISSION_PATTERN.matcher(permission).matches()) { | ||
throw new IllegalArgumentException("The permission doesn't match the regex: " + permission); | ||
} | ||
var state = TriState.UNDEFINED; | ||
var node = this; | ||
for (final var part : permission.split("\\.", -1)) { | ||
if (node.children.containsKey("*") && node.children.get("*").value != TriState.UNDEFINED) { | ||
state = node.children.get("*").value; | ||
} | ||
node = node.children.get(part); | ||
if (node == null) { | ||
return state; | ||
} else if (node.value != TriState.UNDEFINED) { | ||
state = node.value; | ||
} | ||
} | ||
return state; | ||
} | ||
|
||
public void setPermission(final String permission, final TriState state) { | ||
if (!PermissionManager.PERMISSION_PATTERN.matcher(permission).matches()) { | ||
throw new IllegalArgumentException("The permission doesn't match the regex: " + permission); | ||
} | ||
final var parts = permission.split("\\.", -1); | ||
var node = this; | ||
if (state != TriState.UNDEFINED) { | ||
for (final var part : parts) { | ||
final var parent = node; | ||
node = node.children.computeIfAbsent(part, k -> new SimplePermissionTree(parent)); | ||
} | ||
node.value = state; | ||
} else { | ||
for (final var part : parts) { | ||
node = node.children.get(part); | ||
if (node == null) { | ||
return; | ||
} | ||
} | ||
node.value = state; | ||
var index = parts.length - 1; | ||
while (node.parent != null && node.children.isEmpty()) { | ||
node = node.parent; | ||
node.children.remove(parts[index--]); | ||
} | ||
} | ||
} | ||
|
||
public Map<String, Boolean> getPermissions() { | ||
final Map<String, Boolean> permissions = new HashMap<>(); | ||
for (final var child : this.children.entrySet()) { | ||
if (child.getValue().value != TriState.UNDEFINED) { | ||
permissions.put(child.getKey(), child.getValue().value.asBoolean()); | ||
} | ||
for (final var entry : child.getValue().getPermissions().entrySet()) { | ||
permissions.put(child.getKey() + "." + entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
return Collections.unmodifiableMap(permissions); | ||
} | ||
|
||
@Override | ||
public boolean equals(final @Nullable Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof final SimplePermissionTree that)) { | ||
return false; | ||
} | ||
if (!this.children.equals(that.children)) { | ||
return false; | ||
} | ||
return this.value == that.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = this.children.hashCode(); | ||
result = 31 * result + this.value.hashCode(); | ||
return result; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
distributor-common/src/main/java/com/xpdustry/distributor/core/permission/TriState.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,44 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.core.permission; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
/** | ||
* A ternary boolean type, used by the permission system. | ||
*/ | ||
public enum TriState { | ||
FALSE(false), | ||
TRUE(true), | ||
UNDEFINED(false); | ||
|
||
private final boolean value; | ||
|
||
TriState(final boolean value) { | ||
this.value = value; | ||
} | ||
|
||
public static TriState of(final @Nullable Boolean state) { | ||
return state == null ? UNDEFINED : state ? TRUE : FALSE; | ||
} | ||
|
||
public boolean asBoolean() { | ||
return this.value; | ||
} | ||
} |
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,15 @@ | ||
plugins { | ||
id("distributor4.base-conventions") | ||
id("distributor4.mindustry-conventions") | ||
} | ||
|
||
module { | ||
identifier = "distributor-permission" | ||
display = "DistributorLoggerSimple" | ||
main = "com.xpdustry.distributor.logger.simple.DistributorLoggerPlugin" | ||
description = "Simple permission system based on linear ranks." | ||
} | ||
|
||
dependencies { | ||
compileOnly(project(":distributor-common")) | ||
} |
46 changes: 46 additions & 0 deletions
46
...permission-rank/src/main/java/com/xpdustry/distributor/permission/LinearEnumRankNode.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,46 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.permission; | ||
|
||
import java.util.function.Function; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
record LinearEnumRankNode<E extends Enum<E>>(E value, Function<E, String> nameProvider, boolean ascending) | ||
implements RankNode { | ||
|
||
@Override | ||
public String getName() { | ||
return nameProvider.apply(value); | ||
} | ||
|
||
@Override | ||
public @Nullable RankNode getPrevious() { | ||
@SuppressWarnings("unchecked") | ||
final var constants = (E[]) value.getClass().getEnumConstants(); | ||
if (ascending) { | ||
return (value.ordinal() > 0) | ||
? new LinearEnumRankNode<>(constants[value.ordinal() - 1], nameProvider, true) | ||
: null; | ||
} else { | ||
return (value.ordinal() + 1 < constants.length) | ||
? new LinearEnumRankNode<>(constants[value.ordinal() + 1], nameProvider, false) | ||
: null; | ||
} | ||
} | ||
} |
Oops, something went wrong.