Skip to content
Closed
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'maven-publish'

allprojects {
group = 'com.wizardlybump17.wlib'
version = '1.6.7'
version = '1.7.0'

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.wizardlybump17.wlib.bungee.command;

import com.wizardlybump17.wlib.bungee.command.sender.GenericSender;
import com.wizardlybump17.wlib.bungee.command.sender.ProxiedPlayerSender;
import com.wizardlybump17.wlib.bungee.command.sender.CommandSender;
import com.wizardlybump17.wlib.command.CommandManager;
import com.wizardlybump17.wlib.command.CommandSender;
import com.wizardlybump17.wlib.command.exception.CommandException;
import com.wizardlybump17.wlib.command.holder.CommandExecutor;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.util.logging.Level;

public class BungeeCommandExecutor extends Command implements CommandExecutor {

private final CommandManager manager;
Expand All @@ -18,17 +18,16 @@ public BungeeCommandExecutor(CommandManager manager, String name) {
}

@Override
public void execute(CommandSender<?> sender, String commandName, String[] args) {
public void execute(com.wizardlybump17.wlib.command.CommandSender<?> sender, String commandName, String[] args) throws CommandException {
manager.execute(sender, commandName + " " + String.join(" ", args));
}

@Override
public void execute(net.md_5.bungee.api.CommandSender sender, String[] args) {
CommandSender<?> realSender;
if (sender instanceof ProxiedPlayer)
realSender = new ProxiedPlayerSender((ProxiedPlayer) sender);
else
realSender = new GenericSender(sender);
execute(realSender, getName(), args);
try {
execute(new CommandSender(sender), getName(), args);
} catch (CommandException e) {
manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wizardlybump17.wlib.bungee.command;

import com.wizardlybump17.wlib.command.CommandManager;
import com.wizardlybump17.wlib.command.RegisteredCommand;
import com.wizardlybump17.wlib.command.registered.RegisteredCommand;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.wizardlybump17.wlib.bungee.command.sender;

import com.wizardlybump17.wlib.command.CommandSender;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ConnectedPlayer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;

@RequiredArgsConstructor
public class GenericSender implements CommandSender<net.md_5.bungee.api.CommandSender> {
public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender<net.md_5.bungee.api.CommandSender> {

private final net.md_5.bungee.api.CommandSender handle;

Expand Down Expand Up @@ -34,12 +36,11 @@ public boolean hasPermission(String permission) {
return handle.hasPermission(permission);
}

@Override
public GenericSender toGeneric() {
return this;
public @NotNull ProxiedPlayer asProxiedPlayer() {
return (ProxiedPlayer) handle;
}

public static boolean isGeneric() {
return true;
public @NotNull ConnectedPlayer asConnectedPlayer() {
return (ConnectedPlayer) handle;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,82 +1,64 @@
package com.wizardlybump17.wlib.command;

import com.wizardlybump17.wlib.command.data.CommandData;
import com.wizardlybump17.wlib.command.exception.CommandException;
import com.wizardlybump17.wlib.command.extractor.CommandExtractor;
import com.wizardlybump17.wlib.command.extractor.DirectCommandExtractor;
import com.wizardlybump17.wlib.command.extractor.MethodCommandExtractor;
import com.wizardlybump17.wlib.command.holder.CommandHolder;
import com.wizardlybump17.wlib.util.ReflectionUtil;
import com.wizardlybump17.wlib.command.registered.RegisteredCommand;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.*;

@Getter
@RequiredArgsConstructor
public class CommandManager {

private final List<RegisteredCommand> commands = new ArrayList<>();
protected final CommandHolder<?> holder;
private final @NonNull Map<Class<?>, Map<String, Field>> fieldCache = new HashMap<>();
private final @NotNull Set<CommandExtractor> commandExtractors = new HashSet<>();

public void registerCommands(Object... objects) {
for (Object object : objects) {
for (Method method : object.getClass().getDeclaredMethods()) {
if (!method.isAnnotationPresent(Command.class) || method.getParameterCount() == 0 || !CommandSender.class.isAssignableFrom(method.getParameterTypes()[0]))
continue;

MethodHandle handle;
try {
handle = MethodHandles.publicLookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes()));
} catch (NoSuchMethodException | IllegalAccessException e) {
holder.getLogger().log(Level.SEVERE, "Error while trying to get the MethodHandle for " + method.getName() + " at " + object.getClass().getName(), e);
continue;
}

RegisteredCommand command = new RegisteredCommand(
method.getAnnotation(Command.class),
object,
method,
handle
);

commands.add(command);
com.wizardlybump17.wlib.command.holder.Command holderCommand = holder.getCommand(command.getName());
if (holderCommand != null)
holderCommand.setExecutor(holderCommand.getDefaultExecutor(this, command.getName()));
}
}
public CommandManager(@NotNull CommandHolder<?> holder) {
this.holder = holder;
commandExtractors.add(new MethodCommandExtractor());
commandExtractors.add(new DirectCommandExtractor());
}

public void registerCommands(Object... objects) {
for (Object object : objects)
for (CommandExtractor extractor : commandExtractors)
commands.addAll(extractor.extract(this, holder, object));
commands.sort(null);
}

public void unregisterCommands() {
commands.clear();
}

public void execute(CommandSender<?> sender, String string) {
public void execute(CommandSender<?> sender, String string) throws CommandException {
if (commands.isEmpty())
return;

for (RegisteredCommand registeredCommand : commands) {
Command command = registeredCommand.getCommand();
CommandData command = registeredCommand.getCommand();
CommandResult result = registeredCommand.execute(sender, string);

switch (result) {
case PERMISSION_FAIL -> {
handleMessage(registeredCommand, sender, command.permissionMessage(), command.permissionMessageIsField());
String message = command.getPermissionMessage();
if (message != null)
sender.sendMessage(message);
return;
}

case INVALID_SENDER -> {
handleMessage(registeredCommand, sender, command.invalidSenderMessage(), command.invalidSenderMessageIsField());
String message = command.getInvalidSenderMessage();
if (message != null)
sender.sendMessage(message);
return;
}

Expand All @@ -87,40 +69,9 @@ public void execute(CommandSender<?> sender, String string) {
}
}

protected void handleMessage(@NonNull RegisteredCommand registeredCommand, @NonNull CommandSender<?> sender, @NonNull String message, boolean isField) {
if (!isField) {
if (!message.isEmpty())
sender.sendMessage(message);
return;
}

String fieldMessage = getFieldMessage(registeredCommand, message);
if (fieldMessage != null)
sender.sendMessage(fieldMessage);
}

protected @Nullable String getFieldMessage(@NonNull RegisteredCommand registeredCommand, @NonNull String fieldName) {
Map<String, Field> fields = fieldCache.computeIfAbsent(registeredCommand.getObject().getClass(), clazz -> {
Map<String, Field> map = new HashMap<>();
for (Field field : clazz.getDeclaredFields())
map.put(field.getName(), field);
return map;
});

Field field = fields.get(fieldName);
if (field == null)
return null;

Object fieldValue = ReflectionUtil.getFieldValue(field, registeredCommand.getObject());
return fieldValue == null ? null : fieldValue.toString();
}

@NonNull
public List<@NonNull String> autoComplete(@NonNull CommandSender<?> sender, @NonNull String string) {
List<String> result = new ArrayList<>();
for (RegisteredCommand command : commands)
result.addAll(command.autoComplete(sender, string));
return result;
return List.of();
}

public List<RegisteredCommand> getCommand(String name) {
Expand All @@ -131,10 +82,10 @@ public List<RegisteredCommand> getCommand(String name) {
return commands;
}

public List<RegisteredCommand> getCommands(Object object) {
public List<RegisteredCommand> getCommands(@NotNull Object object) {
List<RegisteredCommand> commands = new ArrayList<>(this.commands.size());
for (RegisteredCommand command : this.commands)
if (command.getObject() == object)
if (command.isOwnedBy(object))
commands.add(command);
return commands;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.wizardlybump17.wlib.command;

import org.jetbrains.annotations.Nullable;

/**
* Represents a command sender, someone that can trigger commands.
*
Expand All @@ -21,14 +19,4 @@ public interface CommandSender<S> {
String getName();

boolean hasPermission(String permission);

/**
* Used to convert this CommandSender to a generic sender, a command sender that can be anything
*
* @return the generic sender
*/
@Nullable
default CommandSender<?> toGeneric() {
return null;
}
}
Loading