Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/command/ListFilters.java

128 lines
4.0 KiB
Java
Raw Normal View History

2019-07-20 05:32:15 +00:00
package com.sk89q.worldedit.command;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.util.StringMan;
import com.sk89q.worldedit.extension.platform.Actor;
2019-07-21 11:39:36 +00:00
import com.sk89q.worldedit.util.formatting.text.TextComponent;
2019-07-20 05:32:15 +00:00
import java.io.File;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
2019-07-23 20:26:18 +00:00
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.exception.StopExecutionException;
2019-07-20 05:32:15 +00:00
2019-07-23 20:26:18 +00:00
//TODO This class breaks compilation
2019-08-06 15:29:49 +00:00
//@CommandContainer
2019-07-21 11:39:36 +00:00
public class ListFilters {
2019-07-20 05:32:15 +00:00
public class Filter {
public boolean listPrivate() {
return true;
}
public boolean listPublic() {
return false;
}
public File getPath(File root) {
2019-07-21 11:39:36 +00:00
return null;
2019-07-20 05:32:15 +00:00
}
public boolean applies(File file) {
return true;
}
}
@Command(
name = "all",
desc = "List both public and private schematics"
)
public Filter all() {
return new Filter() {
@Override
public boolean listPublic() {
return true;
}
};
}
@Command(
2019-07-29 21:39:18 +00:00
name = "local",
aliases = {"me", "mine", "private"},
2019-07-20 05:32:15 +00:00
desc = "List your personal schematics"
)
public Filter local() {
return new Filter();
}
@Command(
2019-07-29 21:39:18 +00:00
name = "global",
aliases = {"public"},
2019-07-20 05:32:15 +00:00
desc = "List public schematics"
)
public Filter global() {
return new Filter() {
@Override
public boolean listPrivate() {
return false;
}
@Override
public boolean listPublic() {
return true;
}
};
}
@Command(
2019-07-29 21:39:18 +00:00
name = "*", //TODO NOT IMPLEMENTED originally this was left blank but doing so causes a major compilation error
2019-07-20 05:32:15 +00:00
desc = "wildcard"
)
2019-07-21 11:39:36 +00:00
public Filter wildcard(Actor actor, File root, String arg) {
2019-07-20 05:32:15 +00:00
arg = arg.replace("/", File.separator);
String argLower = arg.toLowerCase(Locale.ROOT);
2019-07-21 11:39:36 +00:00
if (arg.endsWith(File.separator)) {
String finalArg = arg;
return new Filter() {
@Override
public File getPath(File root) {
File newRoot = new File(root, finalArg);
if (newRoot.exists()) return newRoot;
String firstArg = finalArg.substring(0, finalArg.length() - File.separator.length());
if (firstArg.length() > 3 && firstArg.length() <= 16) {
UUID fromName = Fawe.imp().getUUID(finalArg);
if (fromName != null) {
newRoot = new File(root, finalArg);
if (newRoot.exists()) return newRoot;
}
}
throw new StopExecutionException(TextComponent.of("Cannot find path: " + finalArg));
}
};
2019-07-20 05:32:15 +00:00
} else {
if (StringMan.containsAny(arg, "\\^$.|?+(){}<>~$!%^&*+-/")) {
Pattern pattern;
try {
pattern = Pattern.compile(argLower);
} catch (PatternSyntaxException ignore) {
pattern = Pattern.compile(Pattern.quote(argLower));
}
Pattern finalPattern = pattern;
return new Filter() {
@Override
public boolean applies(File file) {
String path = file.getPath().toLowerCase(Locale.ROOT);
return finalPattern.matcher(path).find();
}
};
}
return new Filter() {
@Override
public boolean applies(File file) {
return StringMan.containsIgnoreCase(file.getPath(), argLower);
}
};
}
}
}