Skip to content

Commit

Permalink
Merge pull request #20 from Zffu/fix/swap-allowed-invokers-to-list
Browse files Browse the repository at this point in the history
[fix] Swapped .getAllowedInvokerTypes() to be a list to avoid Arrays.asList operations
  • Loading branch information
Zffu authored Jul 20, 2024
2 parents 32dadf1 + 52c52db commit 4fa4d76
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.InvokerType;

import java.util.Arrays;
import java.util.List;

/**
* An example on how to create your own {@link net.zffu.hardened.api.commands.Command} implementation with the custom features you want.
*/

// In this example we create a invoker gated command.
public class CustomCommandExample implements Command, TypeGatedCommand {

private String[] names = new String[]{"test"};
private InvokerType[] allowedInvokers = new InvokerType[] {InvokerType.PLAYER}; // Only allows players to use our command, this will not matter at all if the custom validator doesn't include a check for it
private List<InvokerType> allowedInvokers = Arrays.asList(new InvokerType[]{InvokerType.PLAYER}); // Only allows players to use our command, this will not matter at all if the custom validator doesn't include a check for it

@Override
public String getPrimaryName() {
return "test";
}

@Override
public String[] getNames() {
return this.names;
public String[] getAliases() {
return new String[0];
}

@Override
Expand All @@ -32,7 +39,7 @@ public void execute(CommandContext commandContext) {
}

@Override
public InvokerType[] getAllowedInvokers() {
public List<InvokerType> getAllowedInvokers() {
return this.allowedInvokers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CustomCommandValidator implements CommandValidator<CustomCommandExa

@Override
public boolean validate(CustomCommandExample command, CommandContext invoker) {
if(Arrays.asList(command.getAllowedInvokers()).contains(invoker.getInvoker().getType())) return true; // Could simplify that but its for the sake of the example
if(command.getAllowedInvokers().contains(invoker.getInvoker().getType())) return true; // Could simplify that but its for the sake of the example
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import net.zffu.hardened.api.commands.types.TypeGatedCommand;
import net.zffu.hardened.api.invoker.InvokerType;

import java.util.ArrayList;
import java.util.List;

/**
* <p>Variant of the {@link net.zffu.hardened.api.commands.Command} interface.</p>
* <p>This variant isn't the default one as it implements every single feature that commands can have in the Hardened for the builder.</p>
Expand All @@ -21,7 +24,7 @@ public abstract class BuilderCommand implements Command<BuilderCommandValidator>

protected String primaryName;

protected InvokerType[] allowedTypes;
protected List<InvokerType> allowedTypes;

protected String[] aliases;

Expand All @@ -47,7 +50,7 @@ public BuilderCommandValidator getValidator() {
}

@Override
public InvokerType[] getAllowedInvokers() {
public List<InvokerType> getAllowedInvokers() {
return this.allowedTypes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.InvokerType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -44,7 +46,7 @@ public CommandBuilder(String primaryName, String... aliases) {
* @return
*/
public CommandBuilder allowed(InvokerType... types) {
this.allowedTypes = types;
this.allowedTypes = Arrays.asList(types);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.zffu.hardened.api.invoker.InvokerType;

import java.util.List;

/**
* A {@link net.zffu.hardened.api.commands.Command} that can onlu run with specific {@link net.zffu.hardened.api.invoker.InvokerType}.
* @since 1.0.0
Expand All @@ -12,6 +14,6 @@ public interface TypeGatedCommand {
* Gets the list of the {@link InvokerType} that are allowed to run the command.
* @return {@link InvokerType}.
*/
InvokerType[] getAllowedInvokers();
List<InvokerType> getAllowedInvokers();

}

0 comments on commit 4fa4d76

Please sign in to comment.