Skip to content

Commit

Permalink
Merge pull request #1 from TheNextLvl-net/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
NonSwag authored Sep 16, 2023
2 parents c212aa9 + c77e6d9 commit bc2b8fb
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 295 deletions.
7 changes: 3 additions & 4 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ java {
}

dependencies {
compileOnly("org.projectlombok:lombok:1.18.28")
compileOnly("com.google.code.gson:gson:2.10")
compileOnly("net.kyori:adventure-api:4.13.1")
compileOnly("org.projectlombok:lombok:1.18.26")
compileOnly("net.thenextlvl.core:annotations:1.0.0")

implementation("net.thenextlvl.core:api:3.1.12")
implementation("net.thenextlvl.core:api:3.2.1")

annotationProcessor("org.projectlombok:lombok:1.18.26")
annotationProcessor("org.projectlombok:lombok:1.18.28")
}
41 changes: 0 additions & 41 deletions api/src/main/java/net/thenextlvl/commander/api/CommandManager.java

This file was deleted.

32 changes: 24 additions & 8 deletions api/src/main/java/net/thenextlvl/commander/api/Commander.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package net.thenextlvl.commander.api;

import net.thenextlvl.commander.i18n.Placeholders;
import net.thenextlvl.commander.api.command.CommandRegistry;
import net.thenextlvl.commander.api.command.PlatformCommandRegistry;
import net.thenextlvl.commander.api.permission.PermissionRegistry;
import net.thenextlvl.commander.api.permission.PlatformPermissionRegistry;

public abstract class Commander {
public interface Commander {
/**
* @return The command manager
* @return the command registry
*/
public abstract CommandManager commandManager();
CommandRegistry commandRegistry();

/**
* @return The permission manager
* @return the permission registry
*/
public abstract PermissionManager permissionManager();
PermissionRegistry permissionRegistry();

static {
Placeholders.init();
/**
* @return the platform registries
*/
PlatformRegistry platform();

interface PlatformRegistry {
/**
* @return the platform command registry
*/
PlatformCommandRegistry<?> commandRegistry();

/**
* @return the platform permission registry
*/
PlatformPermissionRegistry permissionRegistry();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package net.thenextlvl.commander.api.command;

import com.google.gson.reflect.TypeToken;
import core.api.file.format.GsonFile;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.commander.api.Commander;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class CommandRegistry {
private final GsonFile<HashSet<String>> removedCommandsFile;
private final @Getter Commander commander;

protected CommandRegistry(Commander commander, File dataFolder) {
this(new GsonFile<>(
new File(dataFolder, "removed-commands.json"),
new HashSet<String>(),
new TypeToken<>() {
}
).saveIfAbsent(), commander);
}

/**
* Register a removed command query again
*
* @param query the command query
* @return whether the command query was registered
*/
public boolean registerCommand(String query) {
var remove = getRemovedCommands().remove(query);
if (remove) removedCommandsFile.save();
return remove;
}

/**
* Unregister a command query
*
* @param query the command query
* @return whether the pattern was not registered before
*/
public boolean unregisterCommands(String query) {
var added = getRemovedCommands().add(query);
if (added) removedCommandsFile.save();
return added;
}

/**
* Get whether a command query is removed
*
* @param query the command query
* @return whether a command is removed
*/
public boolean isCommandRemoved(String query) {
return getRemovedCommands().stream().anyMatch(s -> query.equals(s)
|| (s.contains("*") && query.matches(s.replaceAll("\\*", ".+"))));
}

/**
* Get all removed command patterns
*
* @return all removed command patterns
*/
public Set<String> getRemovedCommands() {
return removedCommandsFile.getRoot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.thenextlvl.commander.api.command;

import net.thenextlvl.commander.api.Commander;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

public interface PlatformCommandRegistry<C> {

/**
* Get the namespaces of all registered commands
*
* @return a stream of command namespaces
*/
Stream<String> getCommandNamespaces();

/**
* Get all registered command instanced
*
* @return a set of command instances
*/
Collection<C> getCommands();

/**
* Get a registered command instance by its literal
*
* @param literal the command literal
* @return the command instance
*/
Optional<C> getCommand(String literal);

/**
* Check whether a command is registered
*
* @param literal the command literal
* @return whether the command is registered
*/
default boolean isCommandRegistered(String literal) {
return getCommand(literal).isPresent();
}

/**
* Check whether both commands match
*
* @param first the first command
* @param second the second command
* @return whether the commands match
*/
boolean matches(C first, C second);

/**
* Update all commands
*/
void updateCommands();

Commander commander();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.thenextlvl.commander.api.permission;

import com.google.gson.reflect.TypeToken;
import core.api.file.format.GsonFile;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.commander.api.Commander;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class PermissionRegistry {
private final GsonFile<HashMap<String, String>> file;
private final @Getter Map<String, String> originalPermissions = new HashMap<>();
private final @Getter Commander commander;

protected PermissionRegistry(Commander commander, File dataFolder) {
this(new GsonFile<>(
new File(dataFolder, "permission-override.json"),
new HashMap<String, String>(),
new TypeToken<>() {
}
).saveIfAbsent(), commander);
}

/**
* Reset the permission of a certain command
*
* @param literal the command literal
* @return whether the permission was overridden in the first place
*/
public boolean resetPermission(String literal) {
if (!getPermissionOverride().containsKey(literal)) return false;
getPermissionOverride().remove(literal);
file.save();
return true;
}

public void overridePermission(String literal, @Nullable String permission) {
getPermissionOverride().put(literal, permission);
file.save();
}

/**
* Get a map of all overridden permissions<br/>
* The key being the command pattern and the value being the new permission
*
* @return all overridden permissions
*/
public Map<String, String> getPermissionOverride() {
return file.getRoot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.thenextlvl.commander.api.permission;

import net.thenextlvl.commander.api.Commander;
import org.jetbrains.annotations.Nullable;

public interface PlatformPermissionRegistry {
Commander commander();

default void overridePermissions() {
commander().permissionRegistry().getPermissionOverride().forEach(this::overridePermission);
}

boolean overridePermission(String literal, @Nullable String permission);

@Nullable String getOriginalPermission(String literal);

boolean hasOriginalPermission(String literal);
}
Loading

0 comments on commit bc2b8fb

Please sign in to comment.