Add entity selector arguments (@p, @e, @r, etc.) for Bukkit and register with the PaperBrigadierListener.
This commit is contained in:
parent
b94075833d
commit
9a35a873c0
16 changed files with 1123 additions and 14 deletions
|
|
@ -23,13 +23,17 @@
|
|||
//
|
||||
package cloud.commandframework.paper;
|
||||
|
||||
import cloud.commandframework.brigadier.CloudBrigadierManager;
|
||||
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
||||
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.brigadier.CloudBrigadierManager;
|
||||
import cloud.commandframework.bukkit.arguments.selector.MultipleEntitySelector;
|
||||
import cloud.commandframework.bukkit.arguments.selector.MultiplePlayerSelector;
|
||||
import cloud.commandframework.bukkit.arguments.selector.SingleEntitySelector;
|
||||
import cloud.commandframework.bukkit.arguments.selector.SinglePlayerSelector;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import cloud.commandframework.permission.CommandPermission;
|
||||
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
||||
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
|
|
@ -43,6 +47,7 @@ import java.lang.reflect.Constructor;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
|
||||
class PaperBrigadierListener<C> implements Listener {
|
||||
|
|
@ -75,6 +80,11 @@ class PaperBrigadierListener<C> implements Listener {
|
|||
this.mapSimpleNMS(EntityType.class, this.getNMSArgument("EntitySummon").getConstructor());
|
||||
/* Map Material */
|
||||
this.mapSimpleNMS(Material.class, this.getNMSArgument("ItemStack").getConstructor());
|
||||
/* Map Entity Selectors */
|
||||
this.mapComplexNMS(SingleEntitySelector.class, this.getEntitySelectorArgument(true, false));
|
||||
this.mapComplexNMS(SinglePlayerSelector.class, this.getEntitySelectorArgument(true, true));
|
||||
this.mapComplexNMS(MultipleEntitySelector.class, this.getEntitySelectorArgument(false, false));
|
||||
this.mapComplexNMS(MultiplePlayerSelector.class, this.getEntitySelectorArgument(false, true));
|
||||
} catch (final Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin()
|
||||
.getLogger()
|
||||
|
|
@ -82,6 +92,24 @@ class PaperBrigadierListener<C> implements Listener {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param single Whether the selector is for a single entity only (true), or for multiple entities (false)
|
||||
* @param playersOnly Whether the selector is for players only (true), or for all entities (false)
|
||||
* @return The NMS ArgumentType
|
||||
*/
|
||||
private Supplier<ArgumentType<?>> getEntitySelectorArgument(final boolean single, final boolean playersOnly) {
|
||||
return () -> {
|
||||
try {
|
||||
Constructor<?> constructor = this.getNMSArgument("Entity").getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
return (ArgumentType<?>) constructor.newInstance(single, playersOnly);
|
||||
} catch (Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin().getLogger().log(Level.INFO, "Failed to retrieve Selector Argument", e);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to retrieve an NMS argument type
|
||||
*
|
||||
|
|
@ -119,6 +147,24 @@ class PaperBrigadierListener<C> implements Listener {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to register a mapping between a type and a NMS argument type
|
||||
*
|
||||
* @param type Type to map
|
||||
* @param argumentTypeSupplier Supplier of the NMS argument type
|
||||
*/
|
||||
public void mapComplexNMS(@Nonnull final Class<?> type,
|
||||
@Nonnull final Supplier<ArgumentType<?>> argumentTypeSupplier) {
|
||||
try {
|
||||
this.brigadierManager.registerDefaultArgumentTypeSupplier(type, argumentTypeSupplier);
|
||||
} catch (final Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin()
|
||||
.getLogger()
|
||||
.warning(String.format("Failed to map '%s' to a Mojang serializable argument type",
|
||||
type.getCanonicalName()));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onCommandRegister(@Nonnull final CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
|
||||
final CommandTree<C> commandTree = this.paperCommandManager.getCommandTree();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue