Add mask/command registration to FaweAPI
This commit is contained in:
parent
ec455189dc
commit
338d2094ce
@ -18,6 +18,7 @@ import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.extension.factory.DefaultMaskParser;
|
||||
import com.sk89q.worldedit.extension.platform.CommandManager;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
|
||||
@ -66,18 +67,19 @@ public class FaweAPI {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom mask for use in //mask [values...]
|
||||
* @param mask
|
||||
* Add a custom mask for use in e.g //mask #id:<input>
|
||||
* @param name The mask id
|
||||
* @param mask The mask class
|
||||
* @return true if the mask was registered
|
||||
*/
|
||||
public static boolean registerMask(CustomMask mask) {
|
||||
public static boolean registerMask(String name, Class<? extends CustomMask> mask) {
|
||||
try {
|
||||
Field field = AbstractFactory.class.getDeclaredField("parsers");
|
||||
field.setAccessible(true);
|
||||
List<InputParser> parsers = (List<InputParser>) field.get(WorldEdit.getInstance().getMaskFactory());
|
||||
for (InputParser parser : parsers) {
|
||||
if (parser instanceof DefaultMaskParser) {
|
||||
((DefaultMaskParser) parser).addMask(mask);
|
||||
((DefaultMaskParser) parser).addMask(name, mask);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -87,6 +89,17 @@ public class FaweAPI {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a command with the provided aliases and register all methods of the class as sub commands.<br>
|
||||
* - You should try to register commands during startup
|
||||
* - If no aliases are specified, all methods become root commands
|
||||
* @param clazz The class containing all the sub command methods
|
||||
* @param aliases The aliases to give the command (or none)
|
||||
*/
|
||||
public static void registerCommands(Object clazz, String... aliases) {
|
||||
CommandManager.getInstance().registerCommands(clazz, aliases);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap some object into a FawePlayer<br>
|
||||
* - org.bukkit.entity.Player
|
||||
|
@ -157,6 +157,6 @@ public class DefaultTransformParser extends InputParser<ResettableExtent> {
|
||||
}
|
||||
|
||||
public static Class<?> inject() {
|
||||
return DefaultMaskParser.class;
|
||||
return DefaultTransformParser.class;
|
||||
}
|
||||
}
|
@ -8,11 +8,12 @@ public abstract class CustomMask implements Mask {
|
||||
|
||||
/**
|
||||
* Constructor for custom mask
|
||||
* When used in commands, use #custom:<input>
|
||||
* @param masks Any previous masks set (usually from //mask [previous] [thismask]
|
||||
* @param component The input to parse
|
||||
* @param input The input to parse
|
||||
* @param context The context (for extent, player etc)
|
||||
*/
|
||||
public CustomMask(List<Mask> masks, String component, ParserContext context) {
|
||||
public CustomMask(List<Mask> masks, String input, ParserContext context) {
|
||||
try {
|
||||
this.getClass(). getConstructor ( List.class, String.class, ParserContext.class ) ;
|
||||
} catch (NoSuchMethodException e) {
|
||||
@ -21,6 +22,4 @@ public abstract class CustomMask implements Mask {
|
||||
}
|
||||
|
||||
public abstract String description();
|
||||
|
||||
public abstract boolean accepts(String input);
|
||||
}
|
||||
|
@ -568,7 +568,7 @@ public class BrushCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "scatter" },
|
||||
aliases = { "scatter", "scat" },
|
||||
usage = "<pattern> [radius=5] [points=5] [distance=1]",
|
||||
desc = "Scatter blocks on a surface",
|
||||
help =
|
||||
@ -594,7 +594,7 @@ public class BrushCommands {
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "splatter" },
|
||||
aliases = { "splatter", "splat" },
|
||||
usage = "<pattern> [radius=5] [seeds=1] [recursion=5] [solid=true]",
|
||||
desc = "Splatter blocks on a surface",
|
||||
help =
|
||||
|
@ -2,7 +2,23 @@ package com.sk89q.worldedit.extension.factory;
|
||||
|
||||
import com.boydti.fawe.command.FaweParser;
|
||||
import com.boydti.fawe.command.SuggestInputParseException;
|
||||
import com.boydti.fawe.object.mask.*;
|
||||
import com.boydti.fawe.object.mask.AdjacentAnyMask;
|
||||
import com.boydti.fawe.object.mask.AdjacentMask;
|
||||
import com.boydti.fawe.object.mask.AngleMask;
|
||||
import com.boydti.fawe.object.mask.BlockLightMask;
|
||||
import com.boydti.fawe.object.mask.BrightnessMask;
|
||||
import com.boydti.fawe.object.mask.CustomMask;
|
||||
import com.boydti.fawe.object.mask.DataMask;
|
||||
import com.boydti.fawe.object.mask.IdDataMask;
|
||||
import com.boydti.fawe.object.mask.IdMask;
|
||||
import com.boydti.fawe.object.mask.LightMask;
|
||||
import com.boydti.fawe.object.mask.OpacityMask;
|
||||
import com.boydti.fawe.object.mask.RadiusMask;
|
||||
import com.boydti.fawe.object.mask.SkyLightMask;
|
||||
import com.boydti.fawe.object.mask.WallMask;
|
||||
import com.boydti.fawe.object.mask.XAxisMask;
|
||||
import com.boydti.fawe.object.mask.YAxisMask;
|
||||
import com.boydti.fawe.object.mask.ZAxisMask;
|
||||
import com.boydti.fawe.util.MainUtil;
|
||||
import com.boydti.fawe.util.StringMan;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
@ -39,7 +55,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
@ -73,17 +91,21 @@ public class DefaultMaskParser extends FaweParser<Mask> {
|
||||
super(worldEdit);
|
||||
}
|
||||
|
||||
private static CustomMask[] customMasks = new CustomMask[0];
|
||||
private static Map<String, Class<? extends CustomMask>> customMasks = new ConcurrentHashMap<>();
|
||||
|
||||
public void addMask(CustomMask mask) {
|
||||
checkNotNull(mask);
|
||||
List<CustomMask> list = new ArrayList<>(Arrays.asList(customMasks));
|
||||
list.add(mask);
|
||||
customMasks = list.toArray(new CustomMask[list.size()]);
|
||||
/**
|
||||
* Register a mask!
|
||||
* @param id
|
||||
* @param clazz
|
||||
*/
|
||||
public void addMask(String id, Class<? extends CustomMask> clazz) {
|
||||
checkNotNull(clazz);
|
||||
checkNotNull(id);
|
||||
customMasks.put(id, clazz);
|
||||
}
|
||||
|
||||
public List<CustomMask> getCustomMasks() {
|
||||
return Arrays.asList(customMasks);
|
||||
public static Map<String, Class<? extends CustomMask>> getCustomMasks() {
|
||||
return customMasks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -148,9 +170,18 @@ public class DefaultMaskParser extends FaweParser<Mask> {
|
||||
int colon = input.indexOf(':');
|
||||
String component = input;
|
||||
if (colon != -1) {
|
||||
component = component.substring(0, colon);
|
||||
component = component.substring(0, colon).toLowerCase();
|
||||
String rest = input.substring(colon + 1);
|
||||
switch (component.toLowerCase()) {
|
||||
Class<? extends CustomMask> customMask = customMasks.get(component);
|
||||
if (customMask != null) {
|
||||
try {
|
||||
Constructor<? extends CustomMask> constructor = customMask.getDeclaredConstructor(List.class, String.class, ParserContext.class);
|
||||
return constructor.newInstance(masks, rest, context);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
switch (component) {
|
||||
case "#light":
|
||||
case "#skylight":
|
||||
case "#blocklight":
|
||||
@ -166,7 +197,7 @@ public class DefaultMaskParser extends FaweParser<Mask> {
|
||||
try {
|
||||
int y1 = (int) Math.abs(Expression.compile(split[0]).evaluate());
|
||||
int y2 = (int) Math.abs(Expression.compile(split[1]).evaluate());
|
||||
switch (component.toLowerCase()) {
|
||||
switch (component) {
|
||||
case "#light":
|
||||
return new LightMask(extent, y1, y2);
|
||||
case "#skylight":
|
||||
@ -201,6 +232,16 @@ public class DefaultMaskParser extends FaweParser<Mask> {
|
||||
}
|
||||
Mask mask = catchSuggestion(input, masks, rest, context);
|
||||
masks.add(mask);
|
||||
} else {
|
||||
Class<? extends CustomMask> customMask = customMasks.get(component);
|
||||
if (customMask != null) {
|
||||
try {
|
||||
Constructor<? extends CustomMask> constructor = customMask.getDeclaredConstructor(List.class, String.class, ParserContext.class);
|
||||
return constructor.newInstance(masks, "", context);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (component.toLowerCase()) {
|
||||
case "#haslight":
|
||||
@ -357,16 +398,6 @@ public class DefaultMaskParser extends FaweParser<Mask> {
|
||||
}
|
||||
throw new SuggestInputParseException(input, "!<mask>");
|
||||
default:
|
||||
for (CustomMask mask : customMasks) {
|
||||
if (mask.accepts(input)) {
|
||||
try {
|
||||
Constructor<? extends CustomMask> constructor = mask.getClass().getDeclaredConstructor(List.class, String.class, ParserContext.class);
|
||||
return constructor.newInstance(masks, input, context);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
ParserContext tempContext = new ParserContext(context);
|
||||
tempContext.setRestricted(false);
|
||||
tempContext.setPreferringWildcard(true);
|
||||
|
Loading…
Reference in New Issue
Block a user