Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 528b609

Browse files
committed
1.4.8
1 parent 1b29b19 commit 528b609

File tree

7 files changed

+185
-97
lines changed

7 files changed

+185
-97
lines changed

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<groupId>lol.hyper</groupId>
2525
<artifactId>bungeeblockversion</artifactId>
26-
<version>1.4.7</version>
26+
<version>1.4.8</version>
2727
<packaging>jar</packaging>
2828

2929
<name>BungeeBlockVersion</name>
@@ -76,6 +76,10 @@
7676
<pattern>lol.hyper.githubreleaseapi</pattern>
7777
<shadedPattern>lol.hyper.bungeeblockversion.updater</shadedPattern>
7878
</relocation>
79+
<relocation>
80+
<pattern>org.json</pattern>
81+
<shadedPattern>lol.hyper.bungeeblockversion.json</shadedPattern>
82+
</relocation>
7983
</relocations>
8084
</configuration>
8185
<executions>
@@ -107,7 +111,7 @@
107111
<dependency>
108112
<groupId>net.md-5</groupId>
109113
<artifactId>bungeecord-api</artifactId>
110-
<version>1.20-R0.1</version>
114+
<version>1.20-R0.2-SNAPSHOT</version>
111115
<scope>provided</scope>
112116
</dependency>
113117
<dependency>
@@ -134,5 +138,11 @@
134138
<version>4.3.0</version>
135139
<scope>compile</scope>
136140
</dependency>
141+
<dependency>
142+
<groupId>org.json</groupId>
143+
<artifactId>json</artifactId>
144+
<version>20231013</version>
145+
<scope>compile</scope>
146+
</dependency>
137147
</dependencies>
138148
</project>

src/main/java/lol/hyper/bungeeblockversion/BungeeBlockVersion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.bstats.bungeecord.Metrics;
3131

3232
import java.io.IOException;
33+
import java.util.HashMap;
34+
import java.util.Map;
3335
import java.util.logging.Logger;
3436

3537
public final class BungeeBlockVersion extends Plugin implements Listener {
@@ -41,6 +43,8 @@ public final class BungeeBlockVersion extends Plugin implements Listener {
4143
public final MiniMessage miniMessage = MiniMessage.miniMessage();
4244
private BungeeAudiences adventure;
4345

46+
public Map<Integer, String> versions = new HashMap<>();
47+
4448
@Override
4549
public void onEnable() {
4650
this.adventure = BungeeAudiences.create(this);

src/main/java/lol/hyper/bungeeblockversion/events/JoinEvent.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
package lol.hyper.bungeeblockversion.events;
1919

2020
import lol.hyper.bungeeblockversion.BungeeBlockVersion;
21-
import lol.hyper.bungeeblockversion.tools.VersionToStrings;
2221
import net.kyori.adventure.text.Component;
2322
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
2423
import net.md_5.bungee.api.chat.BaseComponent;
2524
import net.md_5.bungee.api.chat.TextComponent;
2625
import net.md_5.bungee.api.event.LoginEvent;
2726
import net.md_5.bungee.api.plugin.Listener;
2827
import net.md_5.bungee.event.EventHandler;
28+
import net.md_5.bungee.protocol.ProtocolConstants;
29+
30+
import java.util.ArrayList;
31+
import java.util.Collections;
32+
import java.util.List;
2933

3034
public class JoinEvent implements Listener {
3135

@@ -41,6 +45,7 @@ public void onPreConnect(LoginEvent event) {
4145
return;
4246
}
4347
int version = event.getConnection().getVersion();
48+
String playerName = event.getConnection().getName();
4449
if (bungeeBlockVersion.configHandler.configuration.getBoolean("log-connection-versions")) {
4550
bungeeBlockVersion.logger.info("Player is connecting with protocol version: " + version);
4651
}
@@ -51,7 +56,7 @@ public void onPreConnect(LoginEvent event) {
5156

5257
event.setCancelled(true);
5358
String blockedMessage = bungeeBlockVersion.configHandler.configuration.getString("disconnect-message");
54-
String allowedVersions = VersionToStrings.allowedVersions(bungeeBlockVersion.configHandler.blockedVersions);
59+
String allowedVersions = allowedVersions(bungeeBlockVersion.configHandler.blockedVersions);
5560
if (allowedVersions == null) {
5661
blockedMessage = "<red>All versions are currently blocked from playing.";
5762
}
@@ -60,7 +65,25 @@ public void onPreConnect(LoginEvent event) {
6065
}
6166
Component blockedMessageComponent = bungeeBlockVersion.miniMessage.deserialize(blockedMessage);
6267
BaseComponent blockedMessageBaseComponent = new TextComponent(BungeeComponentSerializer.get().serialize(blockedMessageComponent));
63-
event.setCancelReason(blockedMessageBaseComponent);
64-
bungeeBlockVersion.logger.info("Blocking player " + event.getConnection().getName() + " because they are playing on version " + VersionToStrings.versionMap.get(event.getConnection().getVersion()) + " which is blocked!");
68+
event.setReason(blockedMessageBaseComponent);
69+
bungeeBlockVersion.logger.info("Blocking player " + playerName + " because they are playing on version " + bungeeBlockVersion.configHandler.versionMap.get(version) + " which is blocked!");
70+
}
71+
72+
/**
73+
* Builds a string that will show what versions the server supports. Example: 1.8 to 1.14.4
74+
*
75+
* @param deniedVersions Versions to deny.
76+
* @return Returns the string of versions.
77+
*/
78+
public String allowedVersions(List<Integer> deniedVersions) {
79+
List<Integer> allVersions = new ArrayList<>(ProtocolConstants.SUPPORTED_VERSION_IDS);
80+
allVersions.removeAll(deniedVersions);
81+
if (allVersions.isEmpty()) {
82+
return null;
83+
}
84+
int minVersion = Collections.min(allVersions);
85+
int maxVersion = Collections.max(allVersions);
86+
87+
return bungeeBlockVersion.configHandler.versionMap.get(minVersion) + " to " + bungeeBlockVersion.configHandler.versionMap.get(maxVersion);
6588
}
6689
}

src/main/java/lol/hyper/bungeeblockversion/tools/ConfigHandler.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
import net.md_5.bungee.config.ConfigurationProvider;
2323
import net.md_5.bungee.config.YamlConfiguration;
2424
import net.md_5.bungee.protocol.ProtocolConstants;
25+
import org.json.JSONObject;
2526

2627
import java.io.File;
2728
import java.io.IOException;
2829
import java.io.InputStream;
2930
import java.nio.file.Files;
31+
import java.util.HashMap;
3032
import java.util.Iterator;
3133
import java.util.List;
34+
import java.util.Map;
3235
import java.util.stream.Collectors;
3336

3437
public class ConfigHandler {
3538

3639
public Configuration configuration;
3740
public List<Integer> blockedVersions;
41+
public Map<Integer, String> versionMap = new HashMap<>();
3842
private final BungeeBlockVersion bungeeBlockVersion;
3943

4044
public ConfigHandler(BungeeBlockVersion bungeeBlockVersion) {
@@ -53,8 +57,8 @@ public void loadConfig() {
5357
} else {
5458
bungeeBlockVersion.logger.warning("Unable to create config folder!");
5559
}
56-
} catch (IOException e) {
57-
throw new RuntimeException(e);
60+
} catch (IOException exception) {
61+
throw new RuntimeException(exception);
5862
}
5963
}
6064
try {
@@ -76,14 +80,39 @@ public void loadConfig() {
7680
Iterator<Integer> iter = blockedVersions.iterator();
7781
while (iter.hasNext()) {
7882
int version = iter.next();
83+
// make sure the versions the user entered exist
7984
if (!ProtocolConstants.SUPPORTED_VERSION_IDS.contains(version)) {
8085
bungeeBlockVersion.logger.warning("Version " + version + " is NOT a valid version number! Ignoring this version.");
8186
iter.remove();
8287
}
8388
}
84-
} catch (IOException e) {
85-
e.printStackTrace();
89+
} catch (IOException exception) {
90+
exception.printStackTrace();
8691
bungeeBlockVersion.logger.severe("Unable to load configuration file!");
8792
}
93+
94+
fetchVersions();
95+
}
96+
97+
private void fetchVersions() {
98+
bungeeBlockVersion.logger.info("Loading versions from GitHub...");
99+
JSONUtils jsonUtils = new JSONUtils(bungeeBlockVersion);
100+
JSONObject versions = jsonUtils.requestJSON("https://raw.githubusercontent.com/hyperdefined/BungeeBlockVersion/master/versions.json");
101+
if (versions == null) {
102+
bungeeBlockVersion.logger.severe("Unable to fetch versions from GitHub!");
103+
bungeeBlockVersion.logger.severe("The plugin is unable to function normally.");
104+
return;
105+
}
106+
bungeeBlockVersion.logger.info("Loaded " + versions.length() + " version(s) from GitHub!");
107+
// key is the protocol version
108+
// value is the name of the version
109+
versions.keys().forEachRemaining(key -> {
110+
int protocolVersion = Integer.parseInt(key);
111+
String namedVersion = versions.getString(key);
112+
// make sure the version exists before saving it
113+
if (ProtocolConstants.SUPPORTED_VERSION_IDS.contains(protocolVersion)) {
114+
versionMap.put(protocolVersion, namedVersion);
115+
}
116+
});
88117
}
89118
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is part of BungeeBlockVersion.
3+
*
4+
* BungeeBlockVersion is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* BungeeBlockVersion is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with BungeeBlockVersion. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.bungeeblockversion.tools;
19+
20+
import lol.hyper.bungeeblockversion.BungeeBlockVersion;
21+
import org.json.JSONObject;
22+
23+
import java.io.BufferedReader;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.net.URL;
28+
import java.net.URLConnection;
29+
import java.util.stream.Collectors;
30+
31+
public class JSONUtils {
32+
33+
private final BungeeBlockVersion bungeeBlockVersion;
34+
35+
public JSONUtils(BungeeBlockVersion bungeeBlockVersion) {
36+
this.bungeeBlockVersion = bungeeBlockVersion;
37+
}
38+
39+
/**
40+
* Get a JSONObject from a URL.
41+
*
42+
* @param url The URL to get JSON from.
43+
* @return The response JSONObject. Returns null if there was some issue.
44+
*/
45+
public JSONObject requestJSON(String url) {
46+
String rawJSON;
47+
try {
48+
URLConnection conn = new URL(url).openConnection();
49+
conn.setRequestProperty("User-Agent", "BungeeBlockVersion " + bungeeBlockVersion.getDescription().getVersion() + " (+https://github.com/hyperdefined/BungeeBlockVersion)");
50+
conn.connect();
51+
52+
InputStream in = conn.getInputStream();
53+
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
54+
rawJSON = reader.lines().collect(Collectors.joining(System.lineSeparator()));
55+
reader.close();
56+
57+
} catch (IOException exception) {
58+
exception.printStackTrace();
59+
bungeeBlockVersion.logger.severe("Unable to read URL " + url);
60+
return null;
61+
}
62+
63+
if (rawJSON.isEmpty()) {
64+
bungeeBlockVersion.logger.severe("Read JSON from " + url + " returned an empty string!");
65+
return null;
66+
}
67+
return new JSONObject(rawJSON);
68+
}
69+
}

src/main/java/lol/hyper/bungeeblockversion/tools/VersionToStrings.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)