Skip to content

Commit

Permalink
plugin-command helper tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 29, 2021
1 parent d0e8ce6 commit 1911899
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Expand Up @@ -8,13 +8,17 @@
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import java.util.List;
import java.util.Map;

public class PluginTag implements ObjectTag, FlaggableObject {

// <--[ObjectType]
Expand Down Expand Up @@ -220,6 +224,49 @@ public static void registerTags() {
tagProcessor.registerTag(ListTag.class, "soft_depends", (attribute, object) -> {
return new ListTag(object.plugin.getDescription().getSoftDepend());
});

// <--[tag]
// @attribute <PluginTag.commands>
// @returns MapTag(MapTag)
// @description
// Gets a map of commands registered this plugin registers by default.
// Map key is command name, map value is a sub-mapping with keys:
// description (ElementTag), usage (ElementTag), permission (ElementTag), aliases (ListTag)
// Not all keys will be present.
// For example, <plugin[denizen].commands.get[ex]> will return a MapTag with:
// [description=Executes a Denizen script command.;usage=/ex (-q) <Denizen script command> (arguments);permission=denizen.ex]
// -->
tagProcessor.registerTag(MapTag.class, "commands", (attribute, object) -> {
Map<String, Map<String, Object>> commands = object.plugin.getDescription().getCommands();
MapTag output = new MapTag();
if (commands == null || commands.isEmpty()) {
return output;
}
for (Map.Entry<String, Map<String, Object>> command : commands.entrySet()) {
MapTag dataMap = new MapTag();
if (command.getValue().containsKey("description")) {
dataMap.putObject("description", new ElementTag(command.getValue().get("description").toString(), true));
}
if (command.getValue().containsKey("usage")) {
dataMap.putObject("usage", new ElementTag(command.getValue().get("usage").toString(), true));
}
if (command.getValue().containsKey("permission")) {
dataMap.putObject("permission", new ElementTag(command.getValue().get("permission").toString(), true));
}
if (command.getValue().containsKey("aliases")) {
Object obj = command.getValue().get("aliases");
if (obj instanceof List) {
ListTag aliases = new ListTag();
for (Object entry : (List) obj) {
aliases.addObject(new ElementTag(String.valueOf(entry), true));
}
dataMap.putObject("aliases", aliases);
}
}
output.putObject(command.getKey(), dataMap);
}
return output;
});
}

public static ObjectTagProcessor<PluginTag> tagProcessor = new ObjectTagProcessor<>();
Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.bukkit.block.Biome;
import org.bukkit.block.banner.PatternType;
import org.bukkit.boss.BossBar;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -601,6 +602,21 @@ else if (recipe instanceof CookingRecipe<?>) {
return;
}

// <--[tag]
// @attribute <server.command_source_plugin[<name>]>
// @returns PluginTag
// @description
// Returns the plugin that created a command (if known).
// For example, <server.command_plugin[ex]> should return a PluginTag of Denizen.
// -->
if (attribute.startsWith("command_plugin")) {
PluginCommand cmd = Bukkit.getPluginCommand(attribute.getParam());
if (cmd != null) {
event.setReplacedObject(new PluginTag(cmd.getPlugin()).getObjectAttribute(attribute.fulfill(1)));
}
return;
}

// <--[tag]
// @attribute <server.art_types>
// @returns ListTag
Expand Down

0 comments on commit 1911899

Please sign in to comment.