Skip to content

Commit c1900bf

Browse files
committed
Performance improvement
1 parent 772975a commit c1900bf

File tree

7 files changed

+71
-41
lines changed

7 files changed

+71
-41
lines changed

build.gradle

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,34 @@ tasks.register("sendDiscord") {
9999
mustRunAfter(tasks.buildAndGather, tasks.publishAll)
100100
doLast {
101101
Map<String, String> env = System.getenv()
102-
String msg =
103-
"# Announcement\nNew version of project [${env.REPO}](${env.REPO_URL}) has been released, check it on [Github Release](${env.REPO_URL}releases/tag/${env.TAG})\n## What's new:\n${file("changelog.md").text}\n"
104-
msg = msg.replace("\n", "\\n")
105-
println("Sending message to " + env.DISCORD_WEBHOOK + "...")
102+
StringBuilder msgBuilder = new StringBuilder(
103+
"# Announcement\nNew version of project [${env.REPO}](${env.REPO_URL}) has been released, check it on [Github Release](${env.REPO_URL}/releases/tag/${env.TAG})\n## What's new:\n${file("changelog.md").text}\n"
104+
)
105+
Map<String, Object> properties = rootProject.getProperties()
106+
boolean bl1 = properties.containsKey("modrinth_id"), bl2 = properties.containsKey("curseforge_id")
107+
if (bl1 || bl2) {
108+
msgBuilder.append("\nTry out on ")
109+
}
110+
if (bl1) {
111+
msgBuilder.append("[Modrinth](https://www.modrinth.com/mod/${properties.get("modrinth_id")})")
112+
if (bl2) {
113+
msgBuilder.append(" and [CurseForge](https://cflookup.com/${properties.get("curseforge_id")})")
114+
}
115+
} else if (bl2) {
116+
msgBuilder.append("[CurseForge](https://cflookup.com/${properties.get("curseforge_id")})")
117+
}
118+
String msg = msgBuilder.toString().replace("\n", "\\n")
119+
println("Sending json content to " + env.DISCORD_WEBHOOK + "...")
120+
String content = "{\"content\" : \"${msg}\"}"
121+
println(content)
106122

107123
HttpURLConnection connection = (HttpURLConnection) new URI(env.DISCORD_WEBHOOK).toURL().openConnection()
108124
connection.setRequestMethod("POST")
109125
connection.setRequestProperty("Content-Type", "application/json")
110126
connection.setDoInput(true)
111127
connection.setDoOutput(true)
112128
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())
113-
writer.write("{\"content\" : \"${msg}\"}")
129+
writer.write(content)
114130
writer.flush()
115131
writer.close()
116132
connection.connect()

changelog.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
### General
22

3-
- Support for Minecraft 1.21.8
4-
5-
### Bugfix
6-
7-
- Incorrect average ping when some but not all packages are lost
3+
- Performance improvement

common.gradle

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ loom {
9696
runConfigs.configureEach {
9797
runDir "../../run"
9898
vmArgs commonVmArgs
99-
ideConfigGenerated true
99+
}
100+
runConfigs.server {
101+
ideConfigGenerated = true
100102
}
101103

102104
runs {
@@ -106,11 +108,8 @@ loom {
106108
vmArgs auditVmArgs
107109
ideConfigGenerated false
108110
}
109-
clientMixinAudit {
110-
client()
111-
vmArgs auditVmArgs
112-
ideConfigGenerated false
113-
}
111+
112+
remove client
114113
}
115114
}
116115

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ loader_version=0.16.14
66
# Mod Properties
77
mod_id=authlib-proxy-for-server
88
mod_name=AuthlibProxyForServer
9-
mod_version=0.2.1
9+
mod_version=0.2.2
1010
mod_version_type=release
1111
group=com.ayakacraft
1212
archives_base_name=authlib-proxy-for-server

src/main/java/com/ayakacraft/authlibproxyforserver/commands/AuthProxyCommand.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
import com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer;
2424
import com.ayakacraft.authlibproxyforserver.ProxyConfig;
25-
import com.ayakacraft.authlibproxyforserver.mixin.YggdrasilAuthenticationServiceAccessor;
2625
import com.ayakacraft.authlibproxyforserver.utils.NetworkUtils;
2726
import com.ayakacraft.authlibproxyforserver.utils.preprocess.PreprocessPattern;
27+
import com.google.common.collect.Maps;
28+
import com.google.common.collect.Sets;
2829
import com.mojang.brigadier.CommandDispatcher;
2930
import com.mojang.brigadier.arguments.IntegerArgumentType;
3031
import com.mojang.brigadier.arguments.StringArgumentType;
@@ -36,8 +37,8 @@
3637

3738
import java.io.IOException;
3839
import java.net.URI;
39-
import java.util.LinkedList;
40-
import java.util.List;
40+
import java.util.Map;
41+
import java.util.Set;
4142

4243
import static com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer.LOGGER;
4344
import static com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer.config;
@@ -163,13 +164,13 @@ private static int type(CommandContext<ServerCommandSource> context, String type
163164
}
164165

165166
private static int ping(CommandContext<ServerCommandSource> context) {
166-
List<String> hosts = new LinkedList<>();
167+
Set<String> hosts = Sets.newHashSet();
167168
//#if MC>=12006
168-
com.mojang.authlib.Environment env = YggdrasilAuthenticationServiceAccessor.determineEnvironment();
169+
com.mojang.authlib.Environment env = com.ayakacraft.authlibproxyforserver.mixin.YggdrasilAuthenticationServiceAccessor.determineEnvironment();
169170
hosts.add(env.sessionHost());
170171
hosts.add(env.servicesHost());
171172
//#elseif MC>=11600
172-
//$$ com.mojang.authlib.Environment env = YggdrasilAuthenticationServiceAccessor.determineEnvironment();
173+
//$$ com.mojang.authlib.Environment env = com.ayakacraft.authlibproxyforserver.mixin.YggdrasilAuthenticationServiceAccessor.determineEnvironment();
173174
//$$ hosts.add(env.getAuthHost());
174175
//$$ hosts.add(env.getAccountsHost());
175176
//$$ hosts.add(env.getSessionHost());
@@ -248,11 +249,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
248249
private static class TcpingThread extends Thread {
249250

250251
private static Text packetLossStatusText(int packetsReceived) {
251-
double packetLoss = 1d - (double) packetsReceived / AuthProxyCommand.TCPING_TIMES;
252+
double packetLoss = 1D - (double) packetsReceived / AuthProxyCommand.TCPING_TIMES;
252253
Formatting colour;
253-
if (packetLoss >= 0.8d) {
254+
if (packetLoss > 0.8D) {
254255
colour = Formatting.RED;
255-
} else if (packetLoss >= 0.3d) {
256+
} else if (packetLoss > 0.2D) {
256257
colour = Formatting.YELLOW;
257258
} else {
258259
colour = Formatting.GREEN;
@@ -264,9 +265,11 @@ private static Text packetLossStatusText(int packetsReceived) {
264265

265266
private static Text averagePingText(long ping) {
266267
Formatting colour;
267-
if (ping >= 800) {
268+
if (ping > 1000) {
269+
colour = Formatting.LIGHT_PURPLE;
270+
} else if (ping >= 800) {
268271
colour = Formatting.RED;
269-
} else if (ping >= 400) {
272+
} else if (ping >= 300) {
270273
colour = Formatting.YELLOW;
271274
} else {
272275
colour = Formatting.GREEN;
@@ -275,40 +278,56 @@ private static Text averagePingText(long ping) {
275278
.append(Text.literal(ping + "ms").formatted(colour));
276279
}
277280

278-
private final List<String> hosts;
281+
private final Set<String> hosts;
279282

280283
private final ServerCommandSource source;
281284

282-
public TcpingThread(List<String> hosts, ServerCommandSource source) {
285+
private final Map<String, Pair<Long, Integer>> results;
286+
287+
private long startTimeMillis;
288+
289+
public TcpingThread(Set<String> hosts, ServerCommandSource source) {
290+
super("TCPing Thread");
283291
this.hosts = hosts;
284292
this.source = source;
293+
this.results = Maps.newHashMap();
294+
}
295+
296+
private synchronized void saveResult(String host, Pair<Long, Integer> res) {
297+
results.put(host, res);
298+
if (results.size() >= hosts.size()) {
299+
results.forEach(this::sendResult);
300+
sendFeedback(source, Text.literal("Ping finished after " + (System.currentTimeMillis() - startTimeMillis) + "ms"), false);
301+
}
285302
}
286303

287-
private void tcping(String host) {
288-
Pair<Long, Integer> res = NetworkUtils.tcpingMultiple(URI.create(host), proxy, TCPING_TIMES);
304+
private void sendResult(String h, Pair<Long, Integer> r) {
289305
sendFeedback(
290306
source,
291-
Text.literal(String.format("Ping for '%s':", host)),
307+
Text.literal(String.format("Ping for '%s':", h)),
292308
false
293309
);
294310
sendFeedback(
295311
source,
296-
packetLossStatusText(res.getRight()),
312+
packetLossStatusText(r.getRight()),
297313
false
298314
);
299-
if (res.getRight() > 0) {
315+
if (r.getRight() > 0) {
300316
sendFeedback(
301317
source,
302-
averagePingText(res.getLeft() / res.getRight()),
318+
averagePingText(r.getLeft() / r.getRight()),
303319
false
304320
);
305321
}
306322
}
307323

324+
@Override
308325
public void run() {
309326
sendFeedback(source, Text.literal("Ping started"), false);
310-
hosts.forEach(this::tcping);
311-
sendFeedback(source, Text.literal("Ping finished"), false);
327+
startTimeMillis = System.currentTimeMillis();
328+
hosts.forEach(host -> new Thread(() ->
329+
saveResult(host, NetworkUtils.tcpingMultiple(URI.create(host), proxy, TCPING_TIMES))).start()
330+
);
312331
}
313332

314333
}

src/main/java/com/ayakacraft/authlibproxyforserver/utils/NetworkUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public static long tcping(URI uri, Proxy proxy) {
4444

4545
public static Pair<Long, Integer> tcpingMultiple(URI uri, Proxy proxy, int times) {
4646
if (times <= 0) {
47-
throw new IllegalArgumentException("Not positive as expected: " + times);
47+
throw new IllegalArgumentException("TCPing times not positive as expected: " + times);
4848
}
4949
long sum = 0L;
50-
int i = 0;
50+
int i = 0;
5151
for (int j = 0; j < times; j++) {
5252
long l = tcping(uri, proxy);
5353
if (l >= 0) {

versions/1.21.8/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ minecraft_dependency=1.21.8
88
# The target mc versions for the mod during mod publishing, separated with \n
99
game_versions=1.21.8
1010
# Dependencies
11-
# fabric_api_version=0.129.0+1.21.8
11+
# fabric_api_version=0.130.0+1.21.8
1212
# Mod Dependency
1313
# check available versions on maven for the given minecraft version you are using

0 commit comments

Comments
 (0)