Now using our own util to paste text

This commit is contained in:
Jaime Martinez Rincon 2017-08-09 01:23:21 +02:00
parent f97e9a46eb
commit d5d9834215
8 changed files with 193 additions and 78 deletions

13
pom.xml
View File

@ -91,12 +91,17 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.kennedyoliveira</groupId>
<artifactId>pastebin4j</artifactId>
<version>1.2.0</version>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<!-- TODO Ditch this dependency, move to ConfigMe -->
<dependency>

View File

@ -85,6 +85,9 @@ public class FallbackCommand extends Command {
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
}
} else {
//TODO instead of checking if the sections are the same, we have to check if the section the player is
//TODO currently going to contains the server the player is connected to
//TODO this should be done in ConnectionIntent instead of here
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (current != target) {
ConnectionIntent.simple(plugin, player, target);

View File

@ -22,8 +22,8 @@ public class MainCommand extends Command {
switch (args[0].toLowerCase()) {
case "paste": {
if (sender.hasPermission("lobbybalancer.admin")) {
PasteHelper.PLUGIN.send(plugin, sender, "Plugin config paste link: {link}");
PasteHelper.BUNGEE.send(plugin, sender, "Bungee config paste link (sensitive): {link}");
PasteHelper.PLUGIN.send(plugin, sender, "Plugin config paste link: {response}");
PasteHelper.BUNGEE.send(plugin, sender, "Bungee config paste link (IPs stripped): {response}");
} else {
sender.sendMessage(new ComponentBuilder("You do not have permission to execute this command!").color(ChatColor.RED).create());
}
@ -45,7 +45,7 @@ public class MainCommand extends Command {
}
default: {
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command!").color(ChatColor.RED).create());
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /balancer for help").color(ChatColor.RED).create());
}
}
} else {

View File

@ -17,6 +17,7 @@ import net.md_5.bungee.api.plugin.Command;
import org.apache.commons.lang3.StringUtils;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ManageCommand extends Command {
@ -164,15 +165,16 @@ public class ManageCommand extends Command {
//TODO show status when hovering over server
section.getServers().forEach(server -> {
ServerStatus status = plugin.getStatusManager().getStatus(server);
sender.sendMessage(new ComponentBuilder("|> Server: ")
sender.sendMessage(new ComponentBuilder("\u2022 Server: ")
.color(ChatColor.GRAY)
.append(server.getName())
.color(ChatColor.AQUA)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder("This is a test\nThis is a test").create()))
.append(String.format(" (%d/%d) ",
status.getOnline(),
status.getMaximum()))
.color(ChatColor.RED)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("This is a test\nThis is a test").create()))
.create()
);
});
@ -196,41 +198,29 @@ public class ManageCommand extends Command {
}
case "list": {
Set<String> keys = plugin.getSectionManager().getSections().keySet();
Iterator<String> iterator = keys.iterator();
TextComponent message = new TextComponent("There are ");
message.addExtra(new TextComponent(new ComponentBuilder(String.valueOf(keys.size())).color(ChatColor.AQUA).create()));
message.addExtra(" configured sections:\n");
message.setColor(ChatColor.GRAY);
Map<String, ServerSection> sections = plugin.getSectionManager().getSections();
if (iterator.hasNext()) {
while (iterator.hasNext()) {
String name = iterator.next();
TextComponent extra = new TextComponent(name);
extra.setColor(ChatColor.GREEN);
extra.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/section info %s", name)));
extra.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click me for info").color(ChatColor.RED).create()));
if (!sections.isEmpty()) {
sender.sendMessage(new ComponentBuilder("These are the registered sections: ").color(ChatColor.GRAY).create());
if (iterator.hasNext()) {
TextComponent sep = new TextComponent(", ");
sep.setColor(ChatColor.GRAY);
extra.addExtra(sep);
}
message.addExtra(extra);
}
sections.forEach((name, section) -> {
sender.sendMessage(new ComponentBuilder("\u2022 Section: ")
.color(ChatColor.GRAY)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/section info %s", name)))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click me for info").color(ChatColor.RED).create()))
.append(name)
.color(ChatColor.AQUA)
.create()
);
});
} else {
TextComponent extra = new TextComponent("There are no sections to list");
extra.setColor(ChatColor.RED);
message.addExtra(extra);
sender.sendMessage(new ComponentBuilder("There are no sections to list").color(ChatColor.GRAY).create());
}
sender.sendMessage(message);
break;
}
default: {
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command!").color(ChatColor.RED).create());
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /section for help").color(ChatColor.RED).create());
}
}
} else {

View File

@ -1,12 +1,17 @@
package com.jaimemartz.playerbalancer.manager;
import com.github.kennedyoliveira.pastebin4j.*;
import com.google.common.io.CharStreams;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.utils.GuestPaste;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.File;
import java.io.FileInputStream;
@ -22,25 +27,24 @@ public enum PasteHelper {
return "File does not exist";
}
GuestPaste paste = new GuestPaste();
paste.setTitle("{name} ({version} on {bungee_version}) Configuration"
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
String content = CharStreams.toString(reader);
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd", content);
paste.setName("{name} ({version} on {bungee_version}) Configuration"
.replace("{name}", plugin.getDescription().getName())
.replace("{version}", plugin.getDescription().getVersion())
.replace("{bungee_version}", plugin.getProxy().getVersion())
);
paste.setExpiration(PasteExpiration.ONE_MONTH);
paste.setVisibility(PasteVisibility.UNLISTED);
paste.setHighLight(PasteHighLight.YAML);
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
paste.setExposure(GuestPaste.Exposure.UNLISTED);
paste.setFormat("yaml");
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
String content = CharStreams.toString(reader);
paste.setContent(content);
return paste.paste();
}
}
return paste.paste(credentials);
}
},
BUNGEE {
@ -51,41 +55,40 @@ public enum PasteHelper {
return "File does not exist";
}
GuestPaste paste = new GuestPaste();
paste.setTitle("{name} ({version}) Configuration"
.replace("{name}", plugin.getProxy().getName())
.replace("{version}", plugin.getProxy().getVersion())
);
paste.setExpiration(PasteExpiration.ONE_MONTH);
paste.setVisibility(PasteVisibility.UNLISTED);
paste.setHighLight(PasteHighLight.YAML);
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
String content = CharStreams.toString(reader);
content = content.replaceAll("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", "X.X.X.X");
paste.setContent(content);
}
}
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd", content);
return paste.paste(credentials);
paste.setName("{name} ({version}) Configuration"
.replace("{name}", plugin.getProxy().getName())
.replace("{version}", plugin.getProxy().getVersion())
);
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
paste.setExposure(GuestPaste.Exposure.UNLISTED);
paste.setFormat("yaml");
return paste.paste();
}
}
}
};
//Cached link of the paste
private String link;
private String response;
private ScheduledTask task = null;
public void send(PlayerBalancer plugin, CommandSender sender, String message) {
try {
sender.sendMessage(new ComponentBuilder(message.replace("{link}", link == null ? link = paste(plugin) : link)).color(ChatColor.GREEN).create());
sender.sendMessage(new ComponentBuilder(message.replace("{response}", response == null ? response = paste(plugin) : response)).color(ChatColor.GREEN).create());
if (task != null) {
plugin.getProxy().getScheduler().cancel(task);
}
task = plugin.getProxy().getScheduler().schedule(plugin, () -> link = null, 5, TimeUnit.MINUTES);
task = plugin.getProxy().getScheduler().schedule(plugin, () -> response = null, 5, TimeUnit.MINUTES);
} catch (Exception e) {
sender.sendMessage(new ComponentBuilder("An internal error occurred while attempting to perform this command").color(ChatColor.RED).create());
e.printStackTrace();
@ -93,6 +96,4 @@ public enum PasteHelper {
}
public abstract String paste(PlayerBalancer plugin) throws Exception;
private static final AccountCredentials credentials = new AccountCredentials("e3ff18d8fb001a3ece08ae0d7d4a87bd");
}

View File

@ -0,0 +1,115 @@
package com.jaimemartz.playerbalancer.utils;
import lombok.Data;
import lombok.Getter;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@Data
public class GuestPaste {
private final String key;
private final String code;
private String name;
private String format;
private Expiration expiration;
private Exposure exposure;
public GuestPaste(String key, String code) {
this.key = key;
this.code = code;
}
public String paste() throws Exception {
HttpPost request = new HttpPost("https://pastebin.com/api/api_post.php");
List<BasicNameValuePair> params = new LinkedList<>();
params.add(new BasicNameValuePair("api_dev_key", key));
params.add(new BasicNameValuePair("api_option", "paste"));
params.add(new BasicNameValuePair("api_paste_code", code));
if (name != null) {
params.add(new BasicNameValuePair("api_paste_name", name));
}
if (format != null) {
params.add(new BasicNameValuePair("api_paste_format", format));
}
if (expiration != null) {
params.add(new BasicNameValuePair("api_paste_expire_date", expiration.getValue()));
}
if (exposure != null) {
params.add(new BasicNameValuePair("api_paste_private", String.valueOf(exposure.getValue())));
}
HttpEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
request.setEntity(entity);
Response response = new Response();
return client.execute(request, response);
}
public enum Expiration {
NEVER("N"),
TEN_MINUTES("10M"),
ONE_HOUR("1H"),
ONE_DAY("1D"),
ONE_WEEK("1W"),
TWO_WEEKS("2W"),
ONE_MONTH("1M"),
SIX_MONTHS("6M"),
ONE_YEAR("1Y");
@Getter
private final String value;
Expiration(String value) {
this.value = value;
}
}
public enum Exposure {
PUBLIC(0),
UNLISTED(1),
PRIVATE(2);
@Getter
private final int value;
Exposure(int value) {
this.value = value;
}
}
public static class Response implements ResponseHandler<String> {
@Override
public String handleResponse(HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
try {
return entity != null ? EntityUtils.toString(entity) : "";
} catch (ParseException | IOException e) {
return "Error : " + e.getMessage();
}
} else {
return "Unexpected response status: " + status;
}
}
}
private static final HttpClient client = HttpClients.createDefault();
}

View File

@ -0,0 +1,9 @@
import com.jaimemartz.playerbalancer.utils.GuestPaste;
import org.junit.Test;
public class GeneralTest {
@Test
public void test() throws Exception {
//Nothing to test
}
}

View File

@ -1,8 +0,0 @@
import org.junit.Test;
public class GeneralTests {
@Test
public void test() {
}
}