Skip to content

Commit 0b9c5f5

Browse files
committed
Some changes to the TODOs
Removed the matcher thingy, I will just support regex
1 parent 5747b9f commit 0b9c5f5

File tree

6 files changed

+238
-17
lines changed

6 files changed

+238
-17
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
[![Build Status](https://travis-ci.com/jaime29010/LobbyBalancer.svg?token=2yUi9WpA9QzSbJx9eTmy&branch=master)](https://travis-ci.com/jaime29010/LobbyBalancer)
55

6-
### Things to do:
6+
### Things- to do:
77
- [x] Get dummy sections able to have already registered servers on other sections
8-
- [ ] Make `marker-descs` work per section
8+
- [x] Add a new message for when a player gets connected to a server and repurpose the connecting one
9+
- [ ] Make the feature `marker-descs` work per section
910
- [ ] Unify the code that loads server into a section (duplicated at SectionManager and ServerSection)
1011
- [ ] Unify some of the code used in the FallbackCommand and SectionCommand
11-
- [ ] Make the way of matching a string configurable (wildcard, contains, similar, regex)
12-
- [ ] Make the section initialization work in stages instead of being hardcoded
13-
- [ ] Ditch the faucet dependency and use [ConfigMe](https://github.com/AuthMe/ConfigMe) and [DependencyInjector](https://github.com/ljacqu/DependencyInjector) instead
12+
- [ ] Use https://github.com/kennedyoliveira/pastebin4j instead of jpaste
13+
- [ ] (!) Make the section initialization work in stages instead of being hardcoded
14+
- [ ] (!) Ditch the faucet dependency and use [ConfigMe](https://github.com/AuthMe/ConfigMe) and [DependencyInjector](https://github.com/ljacqu/DependencyInjector) instead
1415
- [ ] Use a separate file for configuring the sections, must be done alongside the forth item
1516
- [ ] Separate the types of connections in classes instead of being in ConnectionIntent
1617
- [ ] Make the plugin API be not so dependent on a instance of LobbyBalancer

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.jaimemartz</groupId>
88
<artifactId>lobbybalancer</artifactId>
9-
<version>2.0.9.8</version>
9+
<version>2.0.9.9</version>
1010
<name>LobbyBalancer</name>
1111

1212
<properties>

src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ConfigEntries implements ConfigEntryHolder {
4242
public static final ConfigEntry<Boolean> SERVERS_UPDATE = new ConfigEntry<>(0, "settings.servers-update", false);
4343

4444
public static final ConfigEntry<String> CONNECTING_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting-server", null);
45+
public static final ConfigEntry<String> CONNECTED_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting-server", null);
4546
public static final ConfigEntry<String> FAILURE_MESSAGE = new ConfigEntry<>(0, "settings.messages.misc-failure", null);
4647
public static final ConfigEntry<String> UNKNOWN_SECTION_MESSAGE = new ConfigEntry<>(0, "settings.messages.unknown-section", null);
4748
public static final ConfigEntry<String> INVALID_INPUT_MESSAGE = new ConfigEntry<>(0, "settings.messages.invalid-input", null);

src/main/java/me/jaimemartz/lobbybalancer/connection/ConnectionIntent.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ public ConnectionIntent(LobbyBalancer plugin, ProxiedPlayer player, ProviderType
2424
this.player = player;
2525
this.section = section;
2626

27+
new Messager(player).send(ConfigEntries.CONNECTING_MESSAGE.get(),
28+
new Replacement("{section}", section.getName())
29+
);
30+
2731
if (servers == section.getServers()) {
2832
throw new IllegalStateException("The servers list parameter is the same object as the section servers list, this cannot happen");
2933
}
3034

3135
if (section.getProvider() != ProviderType.NONE) {
3236
ServerInfo target = this.fetchServer(plugin, player, section, provider, servers);
3337
if (target != null) {
34-
new Messager(player).send(ConfigEntries.CONNECTING_MESSAGE.get(),
38+
this.connect(target);
39+
new Messager(player).send(ConfigEntries.CONNECTED_MESSAGE.get(),
3540
new Replacement("{server}", target.getName())
3641
);
37-
this.connect(target);
3842
} else {
3943
new Messager(player).send(ConfigEntries.FAILURE_MESSAGE.get());
4044
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package me.jaimemartz.lobbybalancer.utils;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import net.md_5.bungee.api.chat.BaseComponent;
6+
import net.md_5.bungee.api.chat.TextComponent;
7+
import net.md_5.bungee.chat.ComponentSerializer;
8+
import net.md_5.bungee.chat.TextComponentSerializer;
9+
import net.md_5.bungee.chat.TranslatableComponentSerializer;
10+
11+
import java.io.ByteArrayOutputStream;
12+
import java.io.DataInputStream;
13+
import java.io.DataOutputStream;
14+
import java.io.IOException;
15+
import java.net.InetSocketAddress;
16+
import java.net.Socket;
17+
import java.util.List;
18+
19+
public class ServerListPing {
20+
private int timeout = 7000;
21+
private static Gson gson = new GsonBuilder()
22+
.registerTypeAdapter(BaseComponent.class, new ComponentSerializer())
23+
.registerTypeAdapter(TextComponent.class, new TextComponentSerializer())
24+
.registerTypeAdapter(TranslatableComponentSerializer.class, new TranslatableComponentSerializer())
25+
.create();
26+
27+
private static int readVarInt(DataInputStream in) throws IOException {
28+
int i = 0;
29+
int j = 0;
30+
while (true) {
31+
int k = in.readByte();
32+
i |= (k & 0x7F) << j++ * 7;
33+
if (j > 5) throw new RuntimeException("VarInt too big");
34+
if ((k & 0x80) != 128) break;
35+
}
36+
return i;
37+
}
38+
39+
private static void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
40+
while (true) {
41+
if ((paramInt & 0xFFFFFF80) == 0) {
42+
out.writeByte(paramInt);
43+
return;
44+
}
45+
46+
out.writeByte(paramInt & 0x7F | 0x80);
47+
paramInt >>>= 7;
48+
}
49+
}
50+
51+
public StatusResponse ping(InetSocketAddress host) throws IOException {
52+
try (Socket socket = new Socket()) {
53+
socket.setSoTimeout(timeout);
54+
socket.connect(host, timeout);
55+
56+
try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
57+
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())) {
58+
ByteArrayOutputStream b = new ByteArrayOutputStream();
59+
DataOutputStream handshake = new DataOutputStream(b);
60+
handshake.writeByte(0x00); //packet id for handshake
61+
writeVarInt(handshake, 4); //protocol version
62+
writeVarInt(handshake, host.getHostString().length()); //host length
63+
handshake.writeBytes(host.getHostString()); //host string
64+
handshake.writeShort(host.getPort()); //port
65+
writeVarInt(handshake, 1); //state (1 for handshake)
66+
67+
writeVarInt(dataOutputStream, b.size()); //prepend size
68+
dataOutputStream.write(b.toByteArray()); //write handshake packet
69+
70+
71+
dataOutputStream.writeByte(0x01); //size is only 1
72+
dataOutputStream.writeByte(0x00); //packet id for ping
73+
int size = readVarInt(dataInputStream); //size of packet
74+
int id = readVarInt(dataInputStream); //packet id
75+
76+
if (id == -1) {
77+
throw new IOException("Premature end of stream.");
78+
}
79+
80+
if (id != 0x00) { //we want a status response
81+
throw new IOException("Invalid packetID");
82+
}
83+
84+
int length = readVarInt(dataInputStream); //length of json string
85+
if (length == -1) {
86+
throw new IOException("Premature end of stream.");
87+
}
88+
89+
if (length == 0) {
90+
throw new IOException("Invalid string length.");
91+
}
92+
93+
byte[] in = new byte[length];
94+
dataInputStream.readFully(in); //read json string
95+
String json = new String(in);
96+
97+
98+
long now = System.currentTimeMillis();
99+
dataOutputStream.writeByte(0x09); //size of packet
100+
dataOutputStream.writeByte(0x01); //0x01 for ping
101+
dataOutputStream.writeLong(now); //time!?
102+
103+
readVarInt(dataInputStream);
104+
id = readVarInt(dataInputStream);
105+
if (id == -1) {
106+
throw new IOException("Premature end of stream.");
107+
}
108+
109+
if (id != 0x01) {
110+
throw new IOException("Invalid packetID");
111+
}
112+
long pingtime = dataInputStream.readLong(); //read response
113+
114+
StatusResponse response = gson.fromJson(json, StatusResponse.class);
115+
response.time = (int) (now - pingtime);
116+
return response;
117+
}
118+
}
119+
}
120+
121+
public void setTimeout(int timeout) {
122+
this.timeout = timeout;
123+
}
124+
125+
public int getTimeout() {
126+
return timeout;
127+
}
128+
129+
public static class StatusResponse {
130+
private BaseComponent description;
131+
private Players players;
132+
private Version version;
133+
private String favicon;
134+
private int time;
135+
136+
public BaseComponent getDescription() {
137+
return description;
138+
}
139+
140+
public Players getPlayers() {
141+
return players;
142+
}
143+
144+
public Version getVersion() {
145+
return version;
146+
}
147+
148+
public String getFavicon() {
149+
return favicon;
150+
}
151+
152+
public int getTime() {
153+
return time;
154+
}
155+
156+
public static class Players {
157+
private int max;
158+
private int online;
159+
private List<Player> sample;
160+
161+
public int getMax() {
162+
return max;
163+
}
164+
165+
public int getOnline() {
166+
return online;
167+
}
168+
169+
public List<Player> getSample() {
170+
return sample;
171+
}
172+
}
173+
174+
public static class Player {
175+
private String name;
176+
private String id;
177+
178+
public String getName() {
179+
return name;
180+
}
181+
182+
public String getId() {
183+
return id;
184+
}
185+
}
186+
187+
public static class Version {
188+
private String name;
189+
private String protocol;
190+
191+
public String getName() {
192+
return name;
193+
}
194+
195+
public String getProtocol() {
196+
return protocol;
197+
}
198+
}
199+
}
200+
}

src/main/resources/config.yml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ settings:
9191
rules:
9292
'section-from': 'section-to'
9393

94+
# This will determine how the plugin if a string matches other string
95+
string-matcher:
96+
# Matches when the string is like the other string
97+
# ? for one character, * for any number of characters
98+
wildcard: true
99+
100+
# Matches when the string contains the other string
101+
contains: false
102+
103+
# Matches when the string matches the pattern of the other string
104+
regex: false
105+
106+
# Matches when both strings are very similar
107+
# See https://en.wikipedia.org/wiki/Levenshtein_distance
108+
# Set to 100 to disable it, or other value to match when the result is higher than it
109+
similar: 100
110+
94111
# This will reload the plugin every time you execute /greload
95112
auto-reload: true
96113

@@ -110,7 +127,8 @@ settings:
110127

111128
# Comment a message to disable it
112129
messages:
113-
connecting-server: '&aConnecting to {server}'
130+
# connecting-server: '&aConnecting to a {section} server'
131+
connected-server: '&aConnected to {server}'
114132
misc-failure: '&cCould not find a server to get connected'
115133
unknown-section: '&cCould not find a section with that name'
116134
invalid-input: '&cThis is an invalid input type for this command'
@@ -137,32 +155,29 @@ sections:
137155
auth-lobbies:
138156
provider: RANDOM
139157
servers: ["Auth1", "Auth2", "Auth3"]
140-
141158
general-lobbies:
142159
parent: 'auth-lobbies'
143-
principal: true
160+
principal: true # Cannot get above the principal section
144161
provider: RANDOM
145162
servers: ["Lobby1", "Lobby2", "Lobby3"]
146-
147163
skywars-lobbies:
148164
parent: 'general-lobbies'
149165
parent: LOWEST
150166
servers: ["SWLobby1", "SWLobby2", "SWLobby3"]
151-
152167
skywars-games:
153168
provider: FILLER
154169
parent: 'skywars-lobbies'
155170
servers: [
156-
"SW_A1", "SW_A2", "SW_A3", "SW_A4", "SW_A5",
157-
"SW_B1", "SW_B2", "SW_B3", "SW_B4", "SW_B5"
171+
"SW_A1", "SW_A2", "SW_A3", "SW_A4", "SW_A5",
172+
"SW_B1", "SW_B2", "SW_B3", "SW_B4", "SW_B5"
158173
]
159-
section-server: 'randomskywars'
174+
section-server: 'randomskywars' # Creates a fake server you can use, ex: @randomskywars
160175
section-command:
161176
name: 'playskywars'
162177
permission: ''
163178
aliases: ["skywars"]
164179
skywars-typea:
165-
dummy: true
180+
dummy: true # Use servers already registered on skywars-games
166181
parent: 'skywars-lobbies'
167182
servers: ["SW_A1", "SW_A2", "SW_A3", "SW_A4", "SW_A5"]
168183
section-server: 'skywarstypea'

0 commit comments

Comments
 (0)