Skip to content

Commit

Permalink
jitpack
Browse files Browse the repository at this point in the history
  • Loading branch information
hostadam committed Sep 29, 2024
1 parent 34dbafa commit 24744ba
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 243 deletions.
9 changes: 0 additions & 9 deletions src/main/java/com/github/hostadam/Ares.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.github.hostadam.command.ParameterConverter;
import com.github.hostadam.command.handler.CommandHandler;
import com.github.hostadam.command.impl.CommandImpl;
import com.github.hostadam.menu.Menu;
import com.github.hostadam.menu.MenuHandler;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -19,12 +17,10 @@ public class Ares {

private BoardHandler boardHandler;
private CommandHandler commandHandler;
private MenuHandler menuHandler;

public Ares(JavaPlugin parent) {
this.boardHandler = new BoardHandler(parent);
this.commandHandler = new CommandHandler();
this.menuHandler = new MenuHandler(parent);
}

public void setScoreboardAdapter(BoardAdapter adapter) {
Expand All @@ -41,11 +37,6 @@ public Board getScoreboard(UUID uniqueId) {
return this.boardHandler.getScoreboard(uniqueId);
}

/** Menus **/
public void openMenu(Player player, Menu menu) {
this.menuHandler.openMenu(player, menu);
}

/** Commands **/
public void registerCommand(Object object) {
this.commandHandler.register(object);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/github/hostadam/command/AresCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
String usage() default "";
String permission() default "";
int requiredArgs() default 0;
CommandTarget target() default CommandTarget.ALL;
}
29 changes: 0 additions & 29 deletions src/main/java/com/github/hostadam/command/CommandTarget.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class CommandHandler {

private CommandMap map;

private final Map<Class<?>, ParameterConverter<?>> parameters;
private final Map<String, CommandImpl> commands;

Expand Down Expand Up @@ -62,11 +63,6 @@ public void register(Object object) {
CommandImpl parentCommand = this.getCommandByLabel(split[0]);
if(parentCommand != null) parentCommand.addSubCommand(data);
} else {
List<String> aliases = new ArrayList<>();
if(command.labels().length > 1) {
aliases.addAll(List.of(Arrays.copyOfRange(command.labels(), 1, command.labels().length)));
}

CommandImpl impl = new CommandImpl(this, data);
this.map.register(name, impl);
this.commands.put(name, impl);
Expand Down
77 changes: 39 additions & 38 deletions src/main/java/com/github/hostadam/command/impl/CommandData.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,18 @@ public CommandData(AresCommand command, Method method, Object object) {
this.playerOnly = method.getParameters()[0].getType() == Player.class;
}

public boolean match(String name) {
for(String string : this.command.labels()) {
if(!string.contains(" ")) continue;
if(string.split(" ")[1].equalsIgnoreCase(name)) {
return true;
}
}

return false;
}

public void execute(CommandHandler handler, CommandSender sender, String[] args) {
int parameters = this.method.getParameterCount(); //2
Object[] objects = new Object[parameters]; //2

/**
* feed command (args 0 = heal self, args 1 = target)
* onCommand(CommandSender sender, String[] args)
*
* faction create (args 0 = usage, args 1 = name)
* onSubCommand(CommandSender sender, String name)
*/

if(this.playerOnly && !(sender instanceof Player)) {
sender.sendMessage("§cOnly players can run this command");
return;
Expand All @@ -54,43 +51,47 @@ public void execute(CommandHandler handler, CommandSender sender, String[] args)
return;
}

objects[0] = sender;
objects[0] = (this.playerOnly ? (Player) sender : sender);

int argCount = 0;

for(int i = 1; i < parameters; i++) {
Parameter parameter = this.method.getParameters()[i];

if(parameter.getType() == String[].class) {
if(i > args.length) {
objects[i] = new String[] {};
if(parameters > 1) {
for(int i = 1; i < parameters; i++) {
Parameter parameter = this.method.getParameters()[i];

if(parameter.getType() == String[].class) {
if(i > args.length) {
argCount++;
objects[i] = new String[] {};
} else {
String[] arrayCopy = Arrays.copyOfRange(args, i, args.length);
objects[i] = arrayCopy;
argCount += arrayCopy.length;
}
} else {
String[] arrayCopy = Arrays.copyOfRange(args, i, args.length);
objects[i] = arrayCopy;
argCount += arrayCopy.length;
}
} else {
if(i - 1 >= args.length) {
sender.sendMessage("§cUsage: /" + this.command.usage());
return;
if(i - 1 >= args.length) {
sender.sendMessage("§cUsage: /" + this.command.usage());
return;
}

argCount++;
}
}

argCount++;
ParameterConverter<?> converter = handler.getConverter(parameter.getType());
if(converter == null) continue;
ParameterConverter<?> converter = handler.getConverter(parameter.getType());
if(converter == null) continue;

try {
Object object = converter.convert(args[i - 1].trim());
if(object == null) {
converter.error(sender, args[i - 1]);
return;
}

try {
Object object = converter.convert(args[i - 1].trim());
if(object == null) {
objects[i] = object;
} catch(Exception exception) {
converter.error(sender, args[i - 1]);
return;
}

objects[i] = object;
} catch(Exception exception) {
converter.error(sender, args[i - 1]);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public void addSubCommand(CommandData data) {

public CommandData getSubCommand(String name) {
for(CommandData command : this.subcommands) {
if(command.match(name)) {
return command;
for(String string : command.getCommand().labels()) {
if(!string.contains(" ")) continue;
if(string.split(" ")[1].equalsIgnoreCase(name)) {
return command;
}
}
}

Expand Down
Loading

0 comments on commit 24744ba

Please sign in to comment.