use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class Scroll method fromArguments.
public static Scroll fromArguments(BrushTool tool, Player player, LocalSession session, Action mode, List<String> arguments, boolean message) throws InputParseException {
ParserContext parserContext = new ParserContext();
parserContext.setActor(player);
parserContext.setWorld(player.getWorld());
parserContext.setSession(session);
switch(mode) {
case CLIPBOARD:
if (arguments.size() != 2) {
if (message) {
player.print(Caption.of("fawe.error.command.syntax", "clipboard [file]"));
}
return null;
}
String filename = arguments.get(1);
try {
MultiClipboardHolder multi = ClipboardFormats.loadAllFromInput(player, filename, null, message);
if (multi == null) {
return null;
}
return (new ScrollClipboard(tool, session, multi.getHolders()));
} catch (IOException e) {
throw new RuntimeException(e);
}
case MASK:
if (arguments.size() < 2) {
if (message) {
player.print(Caption.of("fawe.error.command.syntax", "mask [mask 1] [mask 2] [mask 3]..."));
}
return null;
}
Mask[] masks = new Mask[arguments.size() - 1];
for (int i = 1; i < arguments.size(); i++) {
String arg = arguments.get(i);
masks[i - 1] = WorldEdit.getInstance().getMaskFactory().parseFromInput(arg, parserContext);
}
return (new ScrollMask(tool, masks));
case PATTERN:
if (arguments.size() < 2) {
if (message) {
player.print(Caption.of("fawe.error.command.syntax", "pattern [pattern 1] [pattern 2] [pattern 3]..."));
}
return null;
}
Pattern[] patterns = new Pattern[arguments.size() - 1];
for (int i = 1; i < arguments.size(); i++) {
String arg = arguments.get(i);
patterns[i - 1] = WorldEdit.getInstance().getPatternFactory().parseFromInput(arg, parserContext);
}
return (new ScrollPattern(tool, patterns));
case TARGET_OFFSET:
return (new ScrollTargetOffset(tool));
case RANGE:
return (new ScrollRange(tool));
case SIZE:
return (new ScrollSize(tool));
case TARGET:
return (new ScrollTarget(tool));
default:
return null;
}
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class CraftScriptContext method getBlocks.
/**
* Get a list of blocks as a set.
*
* @param list a list
* @param allBlocksAllowed true if all blocks are allowed
* @return set
* @throws NoMatchException if the blocks couldn't be found
* @throws DisallowedUsageException if the block is disallowed
*/
public Set<BaseBlock> getBlocks(String list, boolean allBlocksAllowed) throws WorldEditException {
ParserContext context = new ParserContext();
context.setActor(player);
context.setWorld(player.getWorld());
context.setSession(session);
context.setRestricted(!allBlocksAllowed);
return controller.getBlockFactory().parseFromListInput(list, context);
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class CraftScriptContext method getBlockPattern.
/**
* Get a list of blocks as a set. This returns a Pattern.
*
* @param list the input
* @return pattern
* @throws NoMatchException if the pattern was invalid
* @throws DisallowedUsageException if the block is disallowed
*/
public Pattern getBlockPattern(String list) throws WorldEditException {
ParserContext context = new ParserContext();
context.setActor(player);
context.setWorld(player.getWorld());
context.setSession(session);
return controller.getPatternFactory().parseFromInput(list, context);
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class BlockCategoryPatternParser method parseFromInput.
@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if (!input.startsWith("##")) {
return null;
}
String tag = input.substring(2).toLowerCase(Locale.ROOT);
boolean anyState = false;
if (tag.startsWith("*")) {
tag = tag.substring(1);
anyState = true;
}
BlockCategory category = BlockCategory.REGISTRY.get(tag);
if (category == null) {
throw new InputParseException(Caption.of("worldedit.error.unknown-tag", TextComponent.of(tag)));
}
RandomPattern randomPattern = new RandomPattern();
Set<BlockType> blocks = category.getAll();
if (blocks.isEmpty()) {
throw new InputParseException(Caption.of("worldedit.error.empty-tag", TextComponent.of(category.getId())));
}
if (anyState) {
blocks.stream().flatMap(blockType -> blockType.getAllStates().stream()).forEach(state -> randomPattern.add(state, 1.0));
} else {
for (BlockType blockType : blocks) {
randomPattern.add(blockType.getDefaultState(), 1.0);
}
}
return randomPattern;
}
use of com.sk89q.worldedit.extension.input.ParserContext in project FastAsyncWorldEdit by IntellectualSites.
the class DefaultBlockParser method parseProperties.
// FAWE start - make public
public static Map<Property<?>, Object> parseProperties(// FAWE end
BlockType type, String[] stateProperties, ParserContext context, // FAWE start - if null should be returned instead of throwing an error
boolean nullNotError) throws // FAWE end
NoMatchException {
Map<Property<?>, Object> blockStates = new HashMap<>();
// FAWE start - disallowed states
if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
for (String input : context.getActor().getLimit().DISALLOWED_BLOCKS) {
if (input.indexOf('[') == -1 && input.indexOf(']') == -1) {
continue;
}
if (!type.getId().equalsIgnoreCase(input.substring(0, input.indexOf('[')))) {
continue;
}
String[] properties = input.substring(input.indexOf('[') + 1, input.indexOf(']')).split(",");
Set<String> blocked = Arrays.stream(properties).filter(s -> {
for (String in : stateProperties) {
if (in.equalsIgnoreCase(s)) {
return true;
}
}
return false;
}).collect(Collectors.toSet());
if (!blocked.isEmpty()) {
throw new DisallowedUsageException(Caption.of("fawe.error.limit.disallowed-block", TextComponent.of(input)));
}
}
}
if (stateProperties.length > 0) {
// Parse the block data (optional)
for (String parseableData : stateProperties) {
try {
String[] parts = parseableData.split("=");
if (parts.length != 2) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
}
@SuppressWarnings("unchecked") Property<Object> propertyKey = (Property<Object>) type.getPropertyMap().get(parts[0]);
if (propertyKey == null) {
// FAWE start - nullable context
if (context != null && context.getActor() != null) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-property", TextComponent.of(parts[0]), TextComponent.of(type.getId())));
} else {
WorldEdit.logger.debug("Unknown property " + parts[0] + " for block " + type.getId());
}
return Maps.newHashMap();
}
if (blockStates.containsKey(propertyKey)) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.duplicate-property", TextComponent.of(parts[0])));
}
Object value;
try {
value = propertyKey.getValueFor(parts[1]);
} catch (IllegalArgumentException e) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-value", TextComponent.of(parts[1]), TextComponent.of(propertyKey.getName())));
}
// FAWE start - blocked states
if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
if (context.getActor().getLimit().REMAP_PROPERTIES != null && !context.getActor().getLimit().REMAP_PROPERTIES.isEmpty()) {
for (PropertyRemap remap : context.getActor().getLimit().REMAP_PROPERTIES) {
Object newValue = remap.apply(type, value);
if (newValue != value) {
value = newValue;
break;
}
}
}
}
// FAWE end
blockStates.put(propertyKey, value);
} catch (NoMatchException | DisallowedUsageException e) {
// Pass-through
throw e;
} catch (Exception e) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
}
}
}
return blockStates;
}
Aggregations