0

Async mod #1

Merged
Pepich merged 4 commits from AsyncMod into master 2017-05-09 12:15:10 +00:00
4 changed files with 65 additions and 4 deletions
Showing only changes of commit 67a6b13f64 - Show all commits

View File

@ -7,7 +7,14 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Command { public @interface Command
{
enum AsyncType
{
NEVER, ALWAYS;
}
String hook(); String hook();
AsyncType async() default AsyncType.NEVER;
} }

View File

@ -1,3 +1,4 @@
//@noformat
package com.nemez.cmdmgr; package com.nemez.cmdmgr;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -100,6 +101,10 @@ public class CommandManager {
public static String noPermissionFormatting = "&c"; public static String noPermissionFormatting = "&c";
public static String notAllowedFormatting = "&c"; public static String notAllowedFormatting = "&c";
/* List of all commands that can be invoked async */
public static ArrayList<Executable> asyncExecutables = new ArrayList<Executable>();
/* */
/** /**
* Registers a command from a String of source code * Registers a command from a String of source code
* *
@ -626,4 +631,4 @@ public class CommandManager {
return data; return data;
} }
} }//@format

View File

@ -0,0 +1,39 @@
package com.nemez.cmdmgr.util;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.component.ICommandComponent;
public class AsyncExecutableDefinition extends ExecutableDefinition
{
public AsyncExecutableDefinition(ArrayList<ICommandComponent> cmd, ArrayList<Integer> paramLinks, String perm,
Method method, Object methodContainer, Type type)
{
super(cmd, paramLinks, perm, method, methodContainer, type);
}
@Override
public boolean invoke(Object[] args, CommandSender sender, JavaPlugin plugin)
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
invokeFromThread(args, sender, plugin);
}
});
t.setDaemon(true);
t.start();
return true;
}
private final void invokeFromThread(Object[] args, CommandSender sender, JavaPlugin plugin)
{
super.invoke(args, sender, plugin);
}
}

View File

@ -1,3 +1,4 @@
//@noformat
package com.nemez.cmdmgr.util; package com.nemez.cmdmgr.util;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -14,6 +15,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.Command; import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager; import com.nemez.cmdmgr.CommandManager;
import com.nemez.cmdmgr.component.BooleanComponent; import com.nemez.cmdmgr.component.BooleanComponent;
import com.nemez.cmdmgr.component.ByteComponent; import com.nemez.cmdmgr.component.ByteComponent;
@ -263,8 +265,15 @@ public class Executable extends org.bukkit.command.Command {
if (etype == null) { if (etype == null) {
etype = Type.BOTH; etype = Type.BOTH;
} }
ExecutableDefinition def = new ExecutableDefinition(command, links, permission, target, methodContainer, etype); ExecutableDefinition def;
AsyncType type = target.getAnnotation(Command.class).async();
if (type == AsyncType.ALWAYS)
def = new AsyncExecutableDefinition(command, links, permission, target, methodContainer, etype);
else
def = new ExecutableDefinition(command, links, permission, target, methodContainer, etype);
commands.add(def); commands.add(def);
if (def instanceof AsyncExecutableDefinition)
CommandManager.asyncExecutables.add(this);
} }
@Override @Override
@ -426,3 +435,4 @@ public class Executable extends org.bukkit.command.Command {
} }
} }
} }
//@format