Add AsyncCommandBuilder as replacement for AsyncCommandHelper.

See full explanation at https://github.com/EngineHub/WorldGuard/pull/408
This commit is contained in:
wizjany
2019-05-11 21:06:23 -04:00
parent aed25fce7c
commit fab21c3eea
7 changed files with 217 additions and 282 deletions

View File

@ -19,22 +19,16 @@
package com.sk89q.worldedit.util.paste;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.sk89q.worldedit.command.util.AsyncCommandHelper;
import com.sk89q.worldedit.command.util.AsyncCommandBuilder;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.command.exception.ExceptionConverter;
import com.sk89q.worldedit.util.task.Supervisor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URL;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Callable;
public class ActorCallbackPaste {
public final class ActorCallbackPaste {
private static final Logger LOGGER = LoggerFactory.getLogger(ActorCallbackPaste.class);
private static final Paster paster = new EngineHubPaste();
private ActorCallbackPaste() {
}
@ -48,25 +42,15 @@ public class ActorCallbackPaste {
* @param content The content
* @param successMessage The message, formatted with {@link String#format(String, Object...)} on success
*/
public static void pastebin(Supervisor supervisor, final Actor sender, String content, final String successMessage, final ExceptionConverter exceptionConverter) {
ListenableFuture<URL> future = new EngineHubPaste().paste(content);
public static void pastebin(Supervisor supervisor, final Actor sender, String content, final String successMessage) {
Callable<URL> task = paster.paste(content);
AsyncCommandHelper.wrap(future, supervisor, sender, exceptionConverter)
.registerWithSupervisor("Submitting content to a pastebin service...")
.sendMessageAfterDelay("(Please wait... sending output to pastebin...)");
Futures.addCallback(future, new FutureCallback<URL>() {
@Override
public void onSuccess(URL url) {
sender.print(String.format(successMessage, url));
}
@Override
public void onFailure(Throwable throwable) {
LOGGER.warn("Failed to submit pastebin", throwable);
sender.printError("Failed to submit to a pastebin. Please see console for the error.");
}
}, ForkJoinPool.commonPool());
AsyncCommandBuilder.wrap(task, sender)
.registerWithSupervisor(supervisor, "Submitting content to a pastebin service.")
.sendMessageAfterDelay("(Please wait... sending output to pastebin...)")
.onSuccess((String) null, url -> sender.print(String.format(successMessage, url)))
.onFailure("Failed to submit paste", null)
.buildAndExec(Pasters.getExecutor());
}
}

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.util.paste;
import com.google.common.util.concurrent.ListenableFuture;
import com.sk89q.worldedit.util.net.HttpRequest;
import org.json.simple.JSONValue;
@ -35,11 +34,11 @@ public class EngineHubPaste implements Paster {
private static final Pattern URL_PATTERN = Pattern.compile("https?://.+$");
@Override
public ListenableFuture<URL> paste(String content) {
return Pasters.getExecutor().submit(new PasteTask(content));
public Callable<URL> paste(String content) {
return new PasteTask(content);
}
private final class PasteTask implements Callable<URL> {
private static final class PasteTask implements Callable<URL> {
private final String content;
private PasteTask(String content) {
@ -50,7 +49,7 @@ public class EngineHubPaste implements Paster {
public URL call() throws IOException, InterruptedException {
HttpRequest.Form form = HttpRequest.Form.create();
form.add("content", content);
form.add("from", "worldguard");
form.add("from", "enginehub");
URL url = HttpRequest.url("http://paste.enginehub.org/paste");
String result = HttpRequest.post(url)

View File

@ -1,93 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.paste;
import com.google.common.util.concurrent.ListenableFuture;
import com.sk89q.worldedit.util.net.HttpRequest;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pastebin implements Paster {
private static final Pattern URL_PATTERN = Pattern.compile("https?://pastebin.com/([^/]+)$");
private boolean mungingLinks = true;
public boolean isMungingLinks() {
return mungingLinks;
}
public void setMungingLinks(boolean mungingLinks) {
this.mungingLinks = mungingLinks;
}
@Override
public ListenableFuture<URL> paste(String content) {
if (mungingLinks) {
content = content.replaceAll("http://", "http_//");
}
return Pasters.getExecutor().submit(new PasteTask(content));
}
private final class PasteTask implements Callable<URL> {
private final String content;
private PasteTask(String content) {
this.content = content;
}
@Override
public URL call() throws IOException, InterruptedException {
HttpRequest.Form form = HttpRequest.Form.create();
form.add("api_option", "paste");
form.add("api_dev_key", "4867eae74c6990dbdef07c543cf8f805");
form.add("api_paste_code", content);
form.add("api_paste_private", "0");
form.add("api_paste_name", "");
form.add("api_paste_expire_date", "1W");
form.add("api_paste_format", "text");
form.add("api_user_key", "");
URL url = HttpRequest.url("http://pastebin.com/api/api_post.php");
String result = HttpRequest.post(url)
.bodyForm(form)
.execute()
.expectResponseCode(200)
.returnContent()
.asString("UTF-8").trim();
Matcher m = URL_PATTERN.matcher(result);
if (m.matches()) {
return new URL("http://pastebin.com/raw.php?i=" + m.group(1));
} else if (result.matches("^https?://.+")) {
return new URL(result);
} else {
throw new IOException("Failed to save paste; instead, got: " + result);
}
}
}
}

View File

@ -19,12 +19,11 @@
package com.sk89q.worldedit.util.paste;
import com.google.common.util.concurrent.ListenableFuture;
import java.net.URL;
import java.util.concurrent.Callable;
public interface Paster {
ListenableFuture<URL> paste(String content);
Callable<URL> paste(String content);
}