Skip to content

Commit d5d9834

Browse files
committed
Now using our own util to paste text
1 parent f97e9a4 commit d5d9834

File tree

8 files changed

+194
-79
lines changed

8 files changed

+194
-79
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,17 @@
9191
<scope>compile</scope>
9292
</dependency>
9393
<dependency>
94-
<groupId>com.github.kennedyoliveira</groupId>
95-
<artifactId>pastebin4j</artifactId>
96-
<version>1.2.0</version>
94+
<groupId>org.apache.httpcomponents</groupId>
95+
<artifactId>httpclient</artifactId>
96+
<version>4.5.2</version>
9797
<scope>compile</scope>
9898
</dependency>
99-
99+
<dependency>
100+
<groupId>commons-io</groupId>
101+
<artifactId>commons-io</artifactId>
102+
<version>LATEST</version>
103+
<scope>provided</scope>
104+
</dependency>
100105

101106
<!-- TODO Ditch this dependency, move to ConfigMe -->
102107
<dependency>

src/main/java/com/jaimemartz/playerbalancer/commands/FallbackCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public void execute(CommandSender sender, String[] args) {
8585
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
8686
}
8787
} else {
88+
//TODO instead of checking if the sections are the same, we have to check if the section the player is
89+
//TODO currently going to contains the server the player is connected to
90+
//TODO this should be done in ConnectionIntent instead of here
8891
ServerSection current = plugin.getSectionManager().getByPlayer(player);
8992
if (current != target) {
9093
ConnectionIntent.simple(plugin, player, target);

src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public void execute(final CommandSender sender, String[] args) {
2222
switch (args[0].toLowerCase()) {
2323
case "paste": {
2424
if (sender.hasPermission("lobbybalancer.admin")) {
25-
PasteHelper.PLUGIN.send(plugin, sender, "Plugin config paste link: {link}");
26-
PasteHelper.BUNGEE.send(plugin, sender, "Bungee config paste link (sensitive): {link}");
25+
PasteHelper.PLUGIN.send(plugin, sender, "Plugin config paste link: {response}");
26+
PasteHelper.BUNGEE.send(plugin, sender, "Bungee config paste link (IPs stripped): {response}");
2727
} else {
2828
sender.sendMessage(new ComponentBuilder("You do not have permission to execute this command!").color(ChatColor.RED).create());
2929
}
@@ -45,7 +45,7 @@ public void execute(final CommandSender sender, String[] args) {
4545
}
4646

4747
default: {
48-
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command!").color(ChatColor.RED).create());
48+
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /balancer for help").color(ChatColor.RED).create());
4949
}
5050
}
5151
} else {

src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.commons.lang3.StringUtils;
1818

1919
import java.util.Iterator;
20+
import java.util.Map;
2021
import java.util.Set;
2122

2223
public class ManageCommand extends Command {
@@ -106,7 +107,7 @@ public void execute(CommandSender sender, String[] args) {
106107
.color(ChatColor.GRAY)
107108
.append(section.getProvider().name())
108109
.color(ChatColor.AQUA)
109-
.append(String.format("(%s)", section.isInherited() ? "Inherited" : "Specified"))
110+
.append(String.format(" (%s)", section.isInherited() ? "Inherited" : "Specified"))
110111
.color(ChatColor.GRAY)
111112
.create()
112113
);
@@ -164,15 +165,16 @@ public void execute(CommandSender sender, String[] args) {
164165
//TODO show status when hovering over server
165166
section.getServers().forEach(server -> {
166167
ServerStatus status = plugin.getStatusManager().getStatus(server);
167-
sender.sendMessage(new ComponentBuilder("|> Server: ")
168+
sender.sendMessage(new ComponentBuilder("\u2022 Server: ")
168169
.color(ChatColor.GRAY)
169170
.append(server.getName())
170171
.color(ChatColor.AQUA)
172+
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
173+
new ComponentBuilder("This is a test\nThis is a test").create()))
171174
.append(String.format(" (%d/%d) ",
172175
status.getOnline(),
173176
status.getMaximum()))
174177
.color(ChatColor.RED)
175-
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("This is a test\nThis is a test").create()))
176178
.create()
177179
);
178180
});
@@ -196,41 +198,29 @@ public void execute(CommandSender sender, String[] args) {
196198
}
197199

198200
case "list": {
199-
Set<String> keys = plugin.getSectionManager().getSections().keySet();
200-
Iterator<String> iterator = keys.iterator();
201-
TextComponent message = new TextComponent("There are ");
202-
message.addExtra(new TextComponent(new ComponentBuilder(String.valueOf(keys.size())).color(ChatColor.AQUA).create()));
203-
message.addExtra(" configured sections:\n");
204-
message.setColor(ChatColor.GRAY);
205-
206-
if (iterator.hasNext()) {
207-
while (iterator.hasNext()) {
208-
String name = iterator.next();
209-
TextComponent extra = new TextComponent(name);
210-
extra.setColor(ChatColor.GREEN);
211-
extra.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/section info %s", name)));
212-
extra.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click me for info").color(ChatColor.RED).create()));
213-
214-
if (iterator.hasNext()) {
215-
TextComponent sep = new TextComponent(", ");
216-
sep.setColor(ChatColor.GRAY);
217-
extra.addExtra(sep);
218-
}
201+
Map<String, ServerSection> sections = plugin.getSectionManager().getSections();
219202

220-
message.addExtra(extra);
221-
}
203+
if (!sections.isEmpty()) {
204+
sender.sendMessage(new ComponentBuilder("These are the registered sections: ").color(ChatColor.GRAY).create());
205+
206+
sections.forEach((name, section) -> {
207+
sender.sendMessage(new ComponentBuilder("\u2022 Section: ")
208+
.color(ChatColor.GRAY)
209+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/section info %s", name)))
210+
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click me for info").color(ChatColor.RED).create()))
211+
.append(name)
212+
.color(ChatColor.AQUA)
213+
.create()
214+
);
215+
});
222216
} else {
223-
TextComponent extra = new TextComponent("There are no sections to list");
224-
extra.setColor(ChatColor.RED);
225-
message.addExtra(extra);
217+
sender.sendMessage(new ComponentBuilder("There are no sections to list").color(ChatColor.GRAY).create());
226218
}
227-
228-
sender.sendMessage(message);
229219
break;
230220
}
231221

232222
default: {
233-
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command!").color(ChatColor.RED).create());
223+
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /section for help").color(ChatColor.RED).create());
234224
}
235225
}
236226
} else {
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.jaimemartz.playerbalancer.manager;
22

3-
import com.github.kennedyoliveira.pastebin4j.*;
43
import com.google.common.io.CharStreams;
54
import com.jaimemartz.playerbalancer.PlayerBalancer;
5+
import com.jaimemartz.playerbalancer.utils.GuestPaste;
6+
import net.md_5.bungee.api.Callback;
67
import net.md_5.bungee.api.ChatColor;
78
import net.md_5.bungee.api.CommandSender;
89
import net.md_5.bungee.api.chat.ComponentBuilder;
910
import net.md_5.bungee.api.scheduler.ScheduledTask;
11+
import org.apache.http.client.HttpClient;
12+
import org.apache.http.client.methods.HttpPost;
13+
import org.apache.http.impl.client.DefaultHttpClient;
14+
import org.apache.http.impl.client.HttpClients;
1015

1116
import java.io.File;
1217
import java.io.FileInputStream;
@@ -22,25 +27,24 @@ public String paste(PlayerBalancer plugin) throws Exception {
2227
return "File does not exist";
2328
}
2429

25-
GuestPaste paste = new GuestPaste();
26-
paste.setTitle("{name} ({version} on {bungee_version}) Configuration"
27-
.replace("{name}", plugin.getDescription().getName())
28-
.replace("{version}", plugin.getDescription().getVersion())
29-
.replace("{bungee_version}", plugin.getProxy().getVersion())
30-
);
31-
32-
paste.setExpiration(PasteExpiration.ONE_MONTH);
33-
paste.setVisibility(PasteVisibility.UNLISTED);
34-
paste.setHighLight(PasteHighLight.YAML);
35-
3630
try (FileInputStream stream = new FileInputStream(file)) {
3731
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
3832
String content = CharStreams.toString(reader);
39-
paste.setContent(content);
33+
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd", content);
34+
35+
paste.setName("{name} ({version} on {bungee_version}) Configuration"
36+
.replace("{name}", plugin.getDescription().getName())
37+
.replace("{version}", plugin.getDescription().getVersion())
38+
.replace("{bungee_version}", plugin.getProxy().getVersion())
39+
);
40+
41+
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
42+
paste.setExposure(GuestPaste.Exposure.UNLISTED);
43+
paste.setFormat("yaml");
44+
45+
return paste.paste();
4046
}
4147
}
42-
43-
return paste.paste(credentials);
4448
}
4549
},
4650
BUNGEE {
@@ -51,48 +55,45 @@ public String paste(PlayerBalancer plugin) throws Exception {
5155
return "File does not exist";
5256
}
5357

54-
GuestPaste paste = new GuestPaste();
55-
paste.setTitle("{name} ({version}) Configuration"
56-
.replace("{name}", plugin.getProxy().getName())
57-
.replace("{version}", plugin.getProxy().getVersion())
58-
);
59-
60-
paste.setExpiration(PasteExpiration.ONE_MONTH);
61-
paste.setVisibility(PasteVisibility.UNLISTED);
62-
paste.setHighLight(PasteHighLight.YAML);
63-
6458
try (FileInputStream stream = new FileInputStream(file)) {
6559
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
6660
String content = CharStreams.toString(reader);
6761
content = content.replaceAll("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", "X.X.X.X");
68-
paste.setContent(content);
62+
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd", content);
63+
64+
paste.setName("{name} ({version}) Configuration"
65+
.replace("{name}", plugin.getProxy().getName())
66+
.replace("{version}", plugin.getProxy().getVersion())
67+
);
68+
69+
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
70+
paste.setExposure(GuestPaste.Exposure.UNLISTED);
71+
paste.setFormat("yaml");
72+
73+
return paste.paste();
6974
}
7075
}
71-
72-
return paste.paste(credentials);
7376
}
7477
};
7578

7679
//Cached link of the paste
77-
private String link;
80+
private String response;
7881
private ScheduledTask task = null;
7982

8083
public void send(PlayerBalancer plugin, CommandSender sender, String message) {
8184
try {
82-
sender.sendMessage(new ComponentBuilder(message.replace("{link}", link == null ? link = paste(plugin) : link)).color(ChatColor.GREEN).create());
85+
sender.sendMessage(new ComponentBuilder(message.replace("{response}", response == null ? response = paste(plugin) : response)).color(ChatColor.GREEN).create());
8386

8487
if (task != null) {
8588
plugin.getProxy().getScheduler().cancel(task);
8689
}
8790

88-
task = plugin.getProxy().getScheduler().schedule(plugin, () -> link = null, 5, TimeUnit.MINUTES);
91+
task = plugin.getProxy().getScheduler().schedule(plugin, () -> response = null, 5, TimeUnit.MINUTES);
8992
} catch (Exception e) {
9093
sender.sendMessage(new ComponentBuilder("An internal error occurred while attempting to perform this command").color(ChatColor.RED).create());
9194
e.printStackTrace();
9295
}
9396
}
9497

9598
public abstract String paste(PlayerBalancer plugin) throws Exception;
96-
97-
private static final AccountCredentials credentials = new AccountCredentials("e3ff18d8fb001a3ece08ae0d7d4a87bd");
9899
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.jaimemartz.playerbalancer.utils;
2+
3+
import lombok.Data;
4+
import lombok.Getter;
5+
import org.apache.http.Consts;
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.HttpResponse;
8+
import org.apache.http.ParseException;
9+
import org.apache.http.client.HttpClient;
10+
import org.apache.http.client.ResponseHandler;
11+
import org.apache.http.client.entity.UrlEncodedFormEntity;
12+
import org.apache.http.client.methods.HttpPost;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.http.message.BasicNameValuePair;
15+
import org.apache.http.util.EntityUtils;
16+
17+
import java.io.IOException;
18+
import java.util.LinkedList;
19+
import java.util.List;
20+
21+
@Data
22+
public class GuestPaste {
23+
private final String key;
24+
private final String code;
25+
26+
private String name;
27+
private String format;
28+
private Expiration expiration;
29+
private Exposure exposure;
30+
31+
public GuestPaste(String key, String code) {
32+
this.key = key;
33+
this.code = code;
34+
}
35+
36+
public String paste() throws Exception {
37+
HttpPost request = new HttpPost("https://pastebin.com/api/api_post.php");
38+
39+
List<BasicNameValuePair> params = new LinkedList<>();
40+
params.add(new BasicNameValuePair("api_dev_key", key));
41+
params.add(new BasicNameValuePair("api_option", "paste"));
42+
params.add(new BasicNameValuePair("api_paste_code", code));
43+
44+
if (name != null) {
45+
params.add(new BasicNameValuePair("api_paste_name", name));
46+
}
47+
48+
if (format != null) {
49+
params.add(new BasicNameValuePair("api_paste_format", format));
50+
}
51+
52+
if (expiration != null) {
53+
params.add(new BasicNameValuePair("api_paste_expire_date", expiration.getValue()));
54+
}
55+
56+
if (exposure != null) {
57+
params.add(new BasicNameValuePair("api_paste_private", String.valueOf(exposure.getValue())));
58+
}
59+
60+
HttpEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
61+
request.setEntity(entity);
62+
63+
Response response = new Response();
64+
return client.execute(request, response);
65+
}
66+
67+
public enum Expiration {
68+
NEVER("N"),
69+
TEN_MINUTES("10M"),
70+
ONE_HOUR("1H"),
71+
ONE_DAY("1D"),
72+
ONE_WEEK("1W"),
73+
TWO_WEEKS("2W"),
74+
ONE_MONTH("1M"),
75+
SIX_MONTHS("6M"),
76+
ONE_YEAR("1Y");
77+
78+
@Getter
79+
private final String value;
80+
Expiration(String value) {
81+
this.value = value;
82+
}
83+
}
84+
85+
public enum Exposure {
86+
PUBLIC(0),
87+
UNLISTED(1),
88+
PRIVATE(2);
89+
90+
@Getter
91+
private final int value;
92+
Exposure(int value) {
93+
this.value = value;
94+
}
95+
}
96+
97+
public static class Response implements ResponseHandler<String> {
98+
@Override
99+
public String handleResponse(HttpResponse response) throws IOException {
100+
int status = response.getStatusLine().getStatusCode();
101+
if (status >= 200 && status < 300) {
102+
HttpEntity entity = response.getEntity();
103+
try {
104+
return entity != null ? EntityUtils.toString(entity) : "";
105+
} catch (ParseException | IOException e) {
106+
return "Error : " + e.getMessage();
107+
}
108+
} else {
109+
return "Unexpected response status: " + status;
110+
}
111+
}
112+
}
113+
114+
private static final HttpClient client = HttpClients.createDefault();
115+
}

src/test/java/GeneralTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import com.jaimemartz.playerbalancer.utils.GuestPaste;
2+
import org.junit.Test;
3+
4+
public class GeneralTest {
5+
@Test
6+
public void test() throws Exception {
7+
//Nothing to test
8+
}
9+
}

0 commit comments

Comments
 (0)