some bindings

This commit is contained in:
Jesse Boyd
2019-07-22 16:02:51 +10:00
parent f5c202af6d
commit 07283af614
37 changed files with 1080 additions and 2239 deletions

View File

@ -21,6 +21,9 @@ package com.boydti.fawe.util;
import com.boydti.fawe.command.AnvilCommands;
import com.boydti.fawe.command.CFICommands;
import com.sk89q.minecraft.util.commands.Step;
import com.sk89q.worldedit.command.ToolUtilCommands;
import com.sk89q.worldedit.internal.annotation.Range;
import org.enginehub.piston.annotation.Command;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.minecraft.util.commands.NestedCommand;
@ -33,7 +36,6 @@ import com.sk89q.worldedit.command.GenerationCommands;
import com.sk89q.worldedit.command.HistoryCommands;
import com.sk89q.worldedit.command.MaskCommands;
import com.sk89q.worldedit.command.NavigationCommands;
import com.sk89q.worldedit.command.OptionsCommands;
import com.sk89q.worldedit.command.PatternCommands;
import com.sk89q.worldedit.command.RegionCommands;
import com.sk89q.worldedit.command.SchematicCommands;
@ -46,10 +48,19 @@ import com.sk89q.worldedit.command.ToolCommands;
import com.sk89q.worldedit.command.TransformCommands;
import com.sk89q.worldedit.command.UtilityCommands;
import com.sk89q.worldedit.command.WorldEditCommands;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.ArgFlag;
import org.enginehub.piston.annotation.param.Switch;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public final class DocumentationPrinter {
@ -113,7 +124,7 @@ public final class DocumentationPrinter {
writePermissionsWikiTable(stream, builder, "/", SnapshotUtilCommands.class);
writePermissionsWikiTable(stream, builder, "/", ScriptingCommands.class);
writePermissionsWikiTable(stream, builder, "/", ChunkCommands.class);
writePermissionsWikiTable(stream, builder, "/", OptionsCommands.class);
writePermissionsWikiTable(stream, builder, "/", ToolUtilCommands.class);
writePermissionsWikiTable(stream, builder, "/", BrushOptionsCommands.class);
writePermissionsWikiTable(stream, builder, "/tool ", ToolCommands.class);
writePermissionsWikiTable(stream, builder, "/brush ", BrushCommands.class);
@ -153,7 +164,6 @@ public final class DocumentationPrinter {
}
private static void writePermissionsWikiTable(StringBuilder stream, String prefix, Class<?> cls, String name, boolean title) {
// //setbiome || worldedit.biome.set || //setbiome || p || Sets the biome of the player's current block or region.
if (title) {
String path = "https://github.com/boy0001/FastAsyncWorldedit/edit/master/core/src/main/java/" + cls.getName().replaceAll("\\.", "/") + ".java";
stream.append("### **" + name + "** `[`[`edit`](" + path + ")`|`[`top`](#overview)`]`");
@ -163,8 +173,8 @@ public final class DocumentationPrinter {
if (!cmd.desc().isEmpty()) {
stream.append("> (" + (cmd.desc()) + ") \n");
}
if (!cmd.help().isEmpty()) {
stream.append("" + (cmd.help()) + " \n");
if (!cmd.descFooter().isEmpty()) {
stream.append("" + (cmd.descFooter()) + " \n");
}
}
stream.append("\n");
@ -176,22 +186,16 @@ public final class DocumentationPrinter {
if (!method.isAnnotationPresent(Command.class)) {
continue;
}
Command cmd = method.getAnnotation(Command.class);
String[] aliases = cmd.aliases();
String usage = prefix + aliases[0] + " " + cmd.usage();
if (!cmd.flags().isEmpty()) {
for (char c : cmd.flags().toCharArray()) {
usage += " [-" + c + "]";
}
}
// stream.append("#### [`" + usage + "`](" + "https://github.com/boy0001/FastAsyncWorldedit/wiki/" + aliases[0] + ")\n");
String usage = prefix + aliases[0] + " " + getUsage(cmd, method);
stream.append("#### `" + usage + "`\n");
if (method.isAnnotationPresent(CommandPermissions.class)) {
CommandPermissions perms = method.getAnnotation(CommandPermissions.class);
stream.append("**Perm**: `" + StringMan.join(perms.value(), "`, `") + "` \n");
}
String help = cmd.help() == null || cmd.help().isEmpty() ? cmd.desc() : cmd.help();
String help = getDesc(cmd, method);
stream.append("**Desc**: " + help.trim().replaceAll("\n", "<br />") + " \n");
if (method.isAnnotationPresent(NestedCommand.class)) {
@ -209,4 +213,78 @@ public final class DocumentationPrinter {
stream.append("\n");
stream.append("\n");
}
public static String getDesc(Command command, Method method) {
Parameter[] params = method.getParameters();
List<String> desc = new ArrayList<>();
for (Parameter param : params) {
String[] info = getParamInfo(param);
if (info != null) {
desc.add(info[0].replace("%s0", info[1]) + " - " + info[2] + ": " + info[3]);
}
}
String footer = command.descFooter();
if (!footer.isEmpty()) footer += "\n";
return footer + StringMan.join(desc, "\n");
}
public static String getUsage(Command command, Method method) {
Parameter[] params = method.getParameters();
List<String> usage = new ArrayList<>();
for (Parameter param : params) {
String[] info = getParamInfo(param);
if (info != null) {
usage.add(info[0].replace("%s0", info[1]));
}
}
return StringMan.join(usage, " ");
}
/*
Return format, name, type, description
*/
public static String[] getParamInfo(Parameter param) {
Switch switchAnn = param.getAnnotation(Switch.class);
Arg argAnn = param.getAnnotation(Arg.class);
Range rangeAnn = param.getAnnotation(Range.class);
Step stepAnn = param.getAnnotation(Step.class);
if (switchAnn != null || argAnn != null || rangeAnn != null || stepAnn != null) {
String[] result = new String[] { "[%s0]", param.getName(), param.getType().getSimpleName(), ""};
boolean optional = argAnn != null && argAnn.def().length != 0;
if (optional) {
result[0] = "<%s0>";
}
if (argAnn != null) result[1] = argAnn.name();
if (argAnn != null) {
if (argAnn.def().length != 0) {
result[0] = result[0].replace("%s0", "%s0=" + argAnn.def());
}
result[3] = argAnn.desc();
} else if (switchAnn != null) {
result[0] = result[0].replace("%s0", "-" + switchAnn.name() + " %s0");
}
if (switchAnn != null) result[3] = switchAnn.desc();
if (rangeAnn != null) {
String step;
String min = rangeAnn.min() == Double.MIN_VALUE ? "(-∞" : ("[" + rangeAnn.min());
String max = rangeAnn.max() == Double.MAX_VALUE ? "∞)" : (rangeAnn.max() + "]");
result[0] += min + "," + max;
}
if (stepAnn != null) {
result[0] += "" + stepAnn.value();
}
return result;
}
return null;
}
public static Collection<ArgFlag> getFlags(Command command, Method method) {
Parameter[] params = method.getParameters();
List<ArgFlag> flags = new ArrayList<>();
for (Parameter param : params) {
ArgFlag flagAnn = param.getAnnotation(ArgFlag.class);
if (flagAnn != null) flags.add(flagAnn);
}
return flags;
}
}

View File

@ -1,6 +1,7 @@
package com.boydti.fawe.util;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.beta.implementation.QueueHandler;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.beta.IQueueExtent;
import com.boydti.fawe.object.RunnableVal;
@ -10,7 +11,9 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
@ -138,7 +141,8 @@ public abstract class TaskManager {
* @param queue
* @param run
*/
public void runUnsafe(IQueueExtent queue, Runnable run) {
public void runUnsafe(Runnable run) {
QueueHandler queue = Fawe.get().getQueueHandler();
queue.startSet(true);
try {
run.run();
@ -254,23 +258,6 @@ public abstract class TaskManager {
});
}
/**
* Quickly run a task on the main thread, and wait for execution to finish:<br>
* - Useful if you need to access something from the Bukkit API from another thread<br>
* - Usualy wait time is around 25ms<br>
*
* @param function
* @param <T>
* @return
*/
public <T> T sync(final RunnableVal<T> function) {
return sync(function, Integer.MAX_VALUE);
}
public <T> T sync(final Supplier<T> function) {
return sync(function, Integer.MAX_VALUE);
}
public void wait(AtomicBoolean running, int timout) {
try {
long start = System.currentTimeMillis();
@ -295,15 +282,11 @@ public abstract class TaskManager {
}
}
public <T> T syncWhenFree(@NotNull final RunnableVal<T> function) {
return syncWhenFree(function, Integer.MAX_VALUE);
}
public void taskWhenFree(@NotNull Runnable run) {
if (Fawe.isMainThread()) {
run.run();
} else {
SetQueue.IMP.addTask(run);
Fawe.get().getQueueHandler().sync(run);
}
}
@ -317,43 +300,16 @@ public abstract class TaskManager {
* @param <T>
* @return
*/
public <T> T syncWhenFree(@NotNull final RunnableVal<T> function, int timeout) {
public <T> T syncWhenFree(@NotNull final RunnableVal<T> function) {
if (Fawe.isMainThread()) {
function.run();
return function.value;
}
final AtomicBoolean running = new AtomicBoolean(true);
RunnableVal<RuntimeException> run = new RunnableVal<RuntimeException>() {
@Override
public void run(RuntimeException value) {
try {
function.run();
} catch (RuntimeException e) {
this.value = e;
} catch (Throwable neverHappens) {
neverHappens.printStackTrace();
} finally {
running.set(false);
}
synchronized (function) {
function.notifyAll();
}
}
};
SetQueue.IMP.addTask(run);
try {
synchronized (function) {
while (running.get()) {
function.wait(timeout);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
return Fawe.get().getQueueHandler().sync((Supplier<T>) function).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
if (run.value != null) {
throw run.value;
}
return function.value;
}
/**
@ -366,45 +322,27 @@ public abstract class TaskManager {
* @param <T>
* @return
*/
public <T> T sync(@NotNull final RunnableVal<T> function, int timeout) {
return sync((Supplier<T>) function, timeout);
public <T> T sync(@NotNull final RunnableVal<T> function) {
return sync((Supplier<T>) function);
}
public <T> T sync(final Supplier<T> function, int timeout) {
/**
* Quickly run a task on the main thread, and wait for execution to finish:<br>
* - Useful if you need to access something from the Bukkit API from another thread<br>
* - Usually wait time is around 25ms<br>
*
* @param function
* @param <T>
* @return
*/
public <T> T sync(final Supplier<T> function) {
if (Fawe.isMainThread()) {
return function.get();
}
final AtomicBoolean running = new AtomicBoolean(true);
RunnableVal<Object> run = new RunnableVal<Object>() {
@Override
public void run(Object value) {
try {
this.value = function.get();
} catch (RuntimeException e) {
this.value = e;
} catch (Throwable neverHappens) {
neverHappens.printStackTrace();
} finally {
running.set(false);
synchronized (function) {
function.notifyAll();
}
}
}
};
SetQueue.IMP.addTask(run);
try {
synchronized (function) {
while (running.get()) {
function.wait(timeout);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
return Fawe.get().getQueueHandler().sync(function).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
if (run.value instanceof RuntimeException) {
throw (RuntimeException) run.value;
}
return (T) run.value;
}
}

View File

@ -1,156 +0,0 @@
package com.boydti.fawe.util.chat;
import com.boydti.fawe.config.BBC;
import com.boydti.fawe.config.Commands;
import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.StringMan;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.minecraft.util.commands.Link;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.util.command.CommandCallable;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.util.command.Parameter;
import com.sk89q.worldedit.util.command.PrimaryAliasComparator;
import com.sk89q.worldedit.internal.annotation.Range;
import com.sk89q.worldedit.util.command.parametric.ParameterData;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
public class UsageMessage extends Message {
/**
* Create a new usage box.
*
* @param command the command to describe
* @param commandString the command that was used, such as "/we" or "/brush sphere"
*/
public UsageMessage(CommandCallable command, String commandString) {
this(command, commandString, null);
}
/**
* Create a new usage box.
*
* @param command the command to describe
* @param commandString the command that was used, such as "/we" or "/brush sphere"
* @param locals list of locals to use
*/
public UsageMessage(CommandCallable command, String commandString, @Nullable CommandLocals locals) {
checkNotNull(command);
checkNotNull(commandString);
if (command instanceof Dispatcher) {
attachDispatcherUsage((Dispatcher) command, commandString, locals);
} else {
attachCommandUsage(command.getDescription(), commandString);
}
}
private void attachDispatcherUsage(Dispatcher dispatcher, String commandString, @Nullable CommandLocals locals) {
prefix();
text(BBC.HELP_HEADER_SUBCOMMANDS.f());
String prefix = !commandString.isEmpty() ? commandString + " " : "";
List<CommandMapping> list = new ArrayList<>(dispatcher.getCommands());
list.sort(new PrimaryAliasComparator(PlatformCommandManager.COMMAND_CLEAN_PATTERN));
for (CommandMapping mapping : list) {
boolean perm = locals == null || mapping.getCallable().testPermission(locals);
newline();
String cmd = prefix + mapping.getPrimaryAlias();
text((perm ? BBC.HELP_ITEM_ALLOWED : BBC.HELP_ITEM_DENIED).format(cmd, mapping.getDescription().getDescription()));
command(cmd);
}
}
protected String separateArg(String arg) {
return " " + arg;
}
private void attachCommandUsage(Description description, String commandString) {
List<Parameter> params = description.getParameters();
String[] usage;
if (description.getUsage() != null) {
usage = description.getUsage().split(" ", params.size());
} else {
usage = new String[params.size()];
for (int i = 0; i < usage.length; i++) {
Parameter param = params.get(i);
boolean optional = param.isValueFlag() || param.isOptional();
String arg;
if (param.getFlag() != null) {
arg = "-" + param.getFlag();
if (param.isValueFlag())
arg += param.getName();
} else {
arg = param.getName();
if (param.getDefaultValue() != null && param.getDefaultValue().length > 0)
arg += "=" + StringMan.join(param.getDefaultValue(), ",");
}
usage[i] = optional ? ("[" + arg + "]") : ("<" + arg + ">");
}
}
prefix();
text("&cUsage: ");
text("&7" + commandString);
suggestTip(commandString + " ");
for (int i = 0; i < usage.length; i++) {
String argStr = usage[i];
text(separateArg(argStr.replaceAll("[\\[|\\]|<|>]", "&0$0&7")));
if (params.isEmpty()) continue;
Parameter param = params.get(i);
StringBuilder tooltip = new StringBuilder();
String command = null;
tooltip.append("Name: " + param.getName());
if (param instanceof ParameterData) {
ParameterData pd = (ParameterData) param;
Type type = pd.getType();
if (type instanceof Class) {
tooltip.append("\nType: " + ((Class) type).getSimpleName());
}
Range range = MainUtil.getOf(pd.getModifiers(), Range.class);
if (range != null) {
String min = range.min() == Double.MIN_VALUE ? "(-∞" : ("[" + range.min());
String max = range.max() == Double.MAX_VALUE ? "∞)" : (range.max() + "]");
tooltip.append("\nRange: " + min + "," + max);
}
if (type instanceof Class) {
Link link = (Link) ((Class) type).getAnnotation(Link.class);
if (link != null) {
command = Commands.getAlias(link.clazz(), link.value());
}
}
}
tooltip.append("\nOptional: " + (param.isOptional() || param.isValueFlag()));
if (param.getDefaultValue() != null) {
tooltip.append("\nDefault: " + param.getDefaultValue()[0]);
} else if (argStr.contains("=")) {
tooltip.append("\nDefault: " + argStr.split("[=|\\]|>]")[1]);
}
if (command != null) {
tooltip.append("\nClick for more info");
}
tooltip(tooltip.toString());
if (command != null) command(command);
}
newline();
if (description.getHelp() != null) {
text("&cHelp: &7" + description.getHelp());
} else if (description.getDescription() != null) {
text("&cDescription: &7" + description.getDescription());
} else {
text("No further help is available.");
}
}
}

View File

@ -2,9 +2,9 @@ package com.boydti.fawe.util.task;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.beta.IQueueExtent;
import com.boydti.fawe.beta.implementation.QueueHandler;
import com.boydti.fawe.object.Metadatable;
import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.util.SetQueue;
import com.boydti.fawe.util.TaskManager;
import java.util.ArrayDeque;
import java.util.ArrayList;
@ -430,7 +430,7 @@ public class TaskBuilder extends Metadatable {
public static abstract class SplitTask extends RunnableTask {
private final long allocation;
private final IQueueExtent queue;
private final QueueHandler queue;
private long last;
private long start;
private Object asyncWaitLock = new Object();
@ -447,7 +447,7 @@ public class TaskBuilder extends Metadatable {
public SplitTask(long allocation) {
super(TaskType.SYNC_WHEN_FREE);
this.allocation = allocation;
this.queue = SetQueue.IMP.getNewQueue((String) null, true, false);
this.queue = Fawe.get().getQueueHandler();
}
public Object execSplit(final Object previous) {