Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Swapped .getAllowedInvokerTypes() to be a list to avoid Arrays.asList operations #20

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

}
Loading