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

Standardize invokers #30

Merged
merged 11 commits into from
Aug 9, 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 @@ -8,7 +8,7 @@
* <p>The {@link CommandInvoker} will be provided by the {@link CommandContext} when a command gets executed.</p>
* @since 1.0.0
*/
public interface CommandInvoker {
public interface CommandInvoker<S> {

/**
* <p>Gets the type of the {@link CommandInvoker}</p>
Expand All @@ -23,4 +23,16 @@ public interface CommandInvoker {
*/
boolean hasPermission(@NotNull String permission);

/**
* <p>Gets the "command sender" version of the invoker.</p>
* @return the "command sender" as S.
*/
S getSender();

/**
* <p>Sends a message to the invoker.</p>
* @param message the message as a {@link String}
*/
void sendMessage(String message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.zffu.hardened.api.invoker;

import org.jetbrains.annotations.NotNull;

/**
* <p>A factory to create command invokers.</p>
* @param <S> the type of invoker to create
* @param <T> the type of the "command sender" of the invoker
*/
public interface InvokerFactory<S extends CommandInvoker<T>, T> {

/**
* <p>Creates an {@link CommandInvoker} of type S for the provided sender with the provided type.</p>
* @param sender the sender.
* @param type the type.
* @return the {@link CommandInvoker} as S.
*/
@NotNull S createInvoker(@NotNull T sender, @NotNull InvokerType type);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.zffu.hardened.shared;

import net.zffu.hardened.api.invoker.CommandInvoker;
import net.zffu.hardened.api.invoker.InvokerFactory;
import net.zffu.hardened.api.invoker.InvokerType;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

/**
* <p>Invoker factory for Spigot, Paper</p>
* @since 1.0.0
*/
public class SharedInvokerFactory implements InvokerFactory<CommandInvoker<CommandSender>, CommandSender> {

@Override
public CommandInvoker<CommandSender> createInvoker(CommandSender sender, InvokerType type) {
return new CommandInvoker<CommandSender>() {

@Override
public @NotNull InvokerType getType() {
return type;
}

@Override
public boolean hasPermission(@NotNull String permission) {
return sender.hasPermission(permission);
}

@Override
public CommandSender getSender() {
return sender;
}

@Override
public void sendMessage(String message) {
sender.sendMessage(message);
}

};
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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

import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.CommandInvoker;
import net.zffu.hardened.spigot.invokers.ConsoleInvoker;
import net.zffu.hardened.spigot.invokers.OtherInvoker;
import net.zffu.hardened.spigot.invokers.PlayerInvoker;
import net.zffu.hardened.api.invoker.InvokerType;
import net.zffu.hardened.spigot.registrar.SpigotCommandRegistrar;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -17,10 +16,11 @@
*/
public class SpigotCommandHandler implements CommandExecutor {

private SpigotCommandRegistrar registrar;
private net.zffu.hardened.api.commands.Command<?> command;
private boolean parseArgs;

public SpigotCommandHandler(net.zffu.hardened.api.commands.Command<?> command) {
public SpigotCommandHandler(net.zffu.hardened.api.commands.Command<?> command, SpigotCommandRegistrar registrar) {
this.command = command;
this.parseArgs = !this.command.getArguments().getArguments().isEmpty();
}
Expand All @@ -36,10 +36,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
* @param sender the {@link CommandSender}
* @return the {@link CommandInvoker}
*/
public static CommandInvoker fromCommandSender(CommandSender sender) {
if(sender instanceof Player) return new PlayerInvoker((Player) sender);
if(sender instanceof ConsoleCommandSender) return new ConsoleInvoker(sender);
return new OtherInvoker(sender);
public CommandInvoker<CommandSender> fromCommandSender(CommandSender sender) {
InvokerType type = (sender instanceof Player ? InvokerType.PLAYER : (sender instanceof ConsoleCommandSender ? InvokerType.CONSOLE : InvokerType.OTHER));
return this.registrar.factory.createInvoker(sender, type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import net.zffu.hardened.api.commands.Command;
import net.zffu.hardened.api.registrar.CommandRegistrar;
import net.zffu.hardened.shared.SharedInvokerFactory;
import net.zffu.hardened.spigot.handler.SpigotCommandHandler;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;

/**
* A reflection-less {@link net.zffu.hardened.api.registrar.CommandRegistrar} for Spigot platforms.
* <p>Since spigot api is bad, aliases aren't supported by the reflection-less version.</p>
* @since 1.0.0
*/
public class SpigotCommandRegistrar implements CommandRegistrar {

public final SharedInvokerFactory factory;
protected JavaPlugin plugin;

/**
Expand All @@ -23,9 +22,10 @@ public class SpigotCommandRegistrar implements CommandRegistrar {
*/
public SpigotCommandRegistrar(JavaPlugin plugin) {
this.plugin = plugin;
this.factory = new SharedInvokerFactory();
}

public void register(Command<?> command) {
this.plugin.getCommand(command.getPrimaryName()).setExecutor(new SpigotCommandHandler(command));
this.plugin.getCommand(command.getPrimaryName()).setExecutor(new SpigotCommandHandler(command, this));
}
}
Loading