Skip to content

Commit

Permalink
Move the CommandRegistry to the core
Browse files Browse the repository at this point in the history
For future usage
  • Loading branch information
mcmonkey4eva committed Nov 4, 2014
1 parent d558503 commit 18476aa
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 129 deletions.
9 changes: 5 additions & 4 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -13,6 +13,7 @@
import net.aufdemrand.denizen.events.bukkit.SavesReloadEvent;
import net.aufdemrand.denizen.events.bukkit.ScriptReloadEvent;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.scripts.commands.BukkitCommandRegistry;
import net.aufdemrand.denizen.utilities.*;
import net.aufdemrand.denizencore.interfaces.dExternal;
import net.aufdemrand.denizen.listeners.ListenerRegistry;
Expand All @@ -23,7 +24,6 @@
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.*;
import net.aufdemrand.denizen.scripts.commands.CommandRegistry;
import net.aufdemrand.denizen.scripts.containers.core.*;
import net.aufdemrand.denizen.scripts.queues.ScriptEngine;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
Expand Down Expand Up @@ -80,14 +80,14 @@ public ScriptEngine getScriptEngine() {
/*
* Denizen Registries
*/
private CommandRegistry commandRegistry = new CommandRegistry(this);
private BukkitCommandRegistry commandRegistry = new BukkitCommandRegistry();
private TriggerRegistry triggerRegistry = new TriggerRegistry();
private RequirementRegistry requirementRegistry = new RequirementRegistry(this);
private ListenerRegistry listenerRegistry = new ListenerRegistry();
private dNPCRegistry dNPCRegistry;


public CommandRegistry getCommandRegistry() {
public BukkitCommandRegistry getCommandRegistry() {
return commandRegistry;
}

Expand Down Expand Up @@ -226,6 +226,7 @@ public void onEnable() {
}

try {
DenizenCore.setCommandRegistry(getCommandRegistry());
getCommandRegistry().registerCoreMembers();
}
catch (Exception e) {
Expand Down Expand Up @@ -686,7 +687,7 @@ public void debugMessage(String message) {
}

@Override
public void debugException(Exception ex) {
public void debugException(Throwable ex) {
dB.echoError(ex);
}

Expand Down
Expand Up @@ -114,7 +114,7 @@ else if (command.charAt(0) == '~') {
dB.echoError(null, "The command '" + this.command + "' cannot be waited for!");
}
}
actualCommand = DenizenAPI.getCurrentInstance().getCommandRegistry().get(this.command);
actualCommand = (AbstractCommand)DenizenAPI.getCurrentInstance().getCommandRegistry().get(this.command);
}
else {
actualCommand = null;
Expand Down
Expand Up @@ -4,25 +4,10 @@
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;

import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.scripts.commands.BaseAbstractCommand;

public abstract class AbstractCommand extends BaseAbstractCommand {

@Override
public AbstractCommand activate() {
return this;
}

@Override
public AbstractCommand as(String commandName) {
// Register command with Registry with a Name
name = commandName.toUpperCase();
DenizenAPI.getCurrentInstance().getCommandRegistry().register(this.name, this);
onEnable();
return this;
}

public abstract void execute(ScriptEntry scriptEntry) throws CommandExecutionException;

/**
Expand Down
@@ -1,11 +1,5 @@
package net.aufdemrand.denizen.scripts.commands;

import java.util.HashMap;
import java.util.Map;

import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizencore.interfaces.RegistrationableInstance;
import net.aufdemrand.denizencore.interfaces.dRegistry;
import net.aufdemrand.denizen.scripts.commands.core.*;
import net.aufdemrand.denizen.scripts.commands.entity.*;
import net.aufdemrand.denizen.scripts.commands.item.*;
Expand All @@ -15,88 +9,15 @@
import net.aufdemrand.denizen.scripts.commands.world.*;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.scripts.commands.CommandRegistry;

public class CommandRegistry implements dRegistry {

public Denizen denizen;

public CommandRegistry(Denizen denizen) {
this.denizen = denizen;
}

private final Map<String, AbstractCommand> instances = new HashMap<String, AbstractCommand>();
private final Map<Class<? extends AbstractCommand>, String> classes = new HashMap<Class<? extends AbstractCommand>, String>();

@Override
public boolean register(String commandName, RegistrationableInstance commandInstance) {
this.instances.put(commandName.toUpperCase(), (AbstractCommand) commandInstance);
this.classes.put(((AbstractCommand) commandInstance).getClass(), commandName.toUpperCase());
return true;
}

@Override
public Map<String, AbstractCommand> list() {
return instances;
}

@Override
public AbstractCommand get(String commandName) {
return instances.get(commandName.toUpperCase());
}

@Override
public <T extends RegistrationableInstance> T get(Class<T> clazz) {
String command = classes.get(clazz);
if (command != null) return clazz.cast(instances.get(command));
else return null;
}

// <--[language]
// @Name Command Syntax
// @group Script Command System
// @Description
// Almost every Denizen command and requirement has arguments after the command itself.
// These arguments are just snippets of text showing what exactly the command should do,
// like what the chat command should say, or where the look command should point.
// But how do you know what to put in the arguments?
//
// You merely need to look at the command's usage/syntax info.
// Let's take for example:
// <code>
// - animatechest [<location>] ({open}/close) (sound:{true}/false)
// </code>
// Obviously, the command is 'animatechest'... but what does the rest of it mean?
//
// Anything in [brackets] is required... you MUST put it there.
// Anything in (parenthesis) is optional... you only need to put it there if you want to.
// Anything in {braces} is default... the command will just assume this if no argument is actually typed.
// Anything in <> is non-literal... you must change what is inside of it.
// Anything outside of <> is literal... you must put it exactly as-is.
// <#> represents a number without a decimal, and <#.#> represents a number with a decimal
// Lastly, input that ends with "|..." (EG, [<entity>|...] ) can take a list of the input indicated before it (In that example, a list of entities)
//
// A few examples:
// [<location>] is required and non-literal... you might fill it with 'l@1,2,3,world' which is a valid location object.
// (sound:{true}/false) is optional and has a default value of true... you can put sound:false to prevent sound, or leave it blank to allow sound.
// (repeats:<#>) is optional, has no clear default, and is a number. You can put repeats:3 to repeat three times, or leave it blank to not repeat.
// Note: Optional arguments without a default usually have a secret default... EG, the (repeats:<#>) above has a secret default of '0'.
//
// Also, you should never directly type in [], (), {}, or <> even though they are in the syntax info.
// The only exception is in a replaceable tag (EG: <npc.has_trait[<traitname>]> will take <npc.has_trait[mytrait]> as a valid actual usage)
//
// Highly specific note: <commands> means a block of commands wrapped in braces... EG:
// <code>
// - repeat 3 {
// - narrate "%value%"
// - narrate "everything between the {and} symbols (including them) are for the <commands> input!"
// }
// </code>
//
// -->
public class BukkitCommandRegistry extends CommandRegistry {

@Override
public void registerCoreMembers() {

registerCoreCommands();

// <--[command]
// @Name Action
// @Syntax Action [<action name>|...] (<npc>|...) (context:<name>|<object>|...)
Expand Down Expand Up @@ -2935,27 +2856,4 @@ public void registerCoreMembers() {

dB.echoApproval("Loaded core commands: " + instances.keySet().toString());
}

private <T extends AbstractCommand> void registerCoreMember(Class<T> cmd, String names, String hint, int args) {
for (String name : names.split(", ")) {

try {
cmd.newInstance().activate().as(name).withOptions(hint, args);
} catch(Throwable e) {
dB.echoError("Could not register command " + name + ": " + e.getMessage());
dB.echoError(e);
}
}
}

@Override
public void disableCoreMembers() {
for (RegistrationableInstance member : instances.values())
try {
member.onDisable();
} catch (Exception e) {
dB.echoError("Unable to disable '" + member.getClass().getName() + "'!");
dB.echoError(e);
}
}
}
Expand Up @@ -66,7 +66,7 @@ public boolean execute(ScriptEntry scriptEntry) {
// Get the command instance ready for the execution of the scriptEntry
AbstractCommand command = scriptEntry.getCommand();
if (command == null) {
command = DenizenAPI.getCurrentInstance().getCommandRegistry().get(scriptEntry.getCommandName());
command = (AbstractCommand)DenizenAPI.getCurrentInstance().getCommandRegistry().get(scriptEntry.getCommandName());
}

if (command == null) {
Expand Down
Expand Up @@ -3,8 +3,8 @@
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.npc.dNPCRegistry;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.scripts.commands.BukkitCommandRegistry;
import net.aufdemrand.denizen.scripts.queues.ScriptEngine;
import net.aufdemrand.denizen.scripts.commands.CommandRegistry;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static Denizen getCurrentInstance() {
return (Denizen) Bukkit.getPluginManager().getPlugin("Denizen");
}

public static CommandRegistry _commandRegistry() {
public static BukkitCommandRegistry _commandRegistry() {
return getCurrentInstance().getCommandRegistry();
}

Expand Down

0 comments on commit 18476aa

Please sign in to comment.