Skip to content

Commit 4c3cb86

Browse files
committed
fix brand collecting on bukkit & bungee
1 parent 8fcd31a commit 4c3cb86

File tree

3 files changed

+128
-18
lines changed

3 files changed

+128
-18
lines changed

bukkit/src/main/java/com/lunarclient/apollo/listener/ApolloMetadataListener.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
*/
2424
package com.lunarclient.apollo.listener;
2525

26+
import com.google.common.io.ByteArrayDataInput;
27+
import com.google.common.io.ByteStreams;
2628
import com.lunarclient.apollo.ApolloManager;
2729
import com.lunarclient.apollo.metadata.BukkitMetadataManager;
28-
import java.nio.charset.StandardCharsets;
30+
import com.lunarclient.apollo.util.ByteBufUtil;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.DataInputStream;
33+
import java.io.IOException;
2934
import java.util.Map;
3035
import org.bukkit.Bukkit;
3136
import org.bukkit.event.EventHandler;
@@ -67,21 +72,29 @@ private void onResourcePackStatus(PlayerResourcePackStatusEvent event) {
6772
}
6873

6974
private void registerBrandListener() {
70-
PluginMessageListener listener = (channel, player, bytes) -> {
71-
String brand = new String(bytes, StandardCharsets.UTF_8);
72-
73-
BukkitMetadataManager manager = (BukkitMetadataManager) ApolloManager.getMetadataManager();
74-
manager.getClientBrands().add(brand);
75-
};
76-
7775
Messenger messenger = this.plugin.getServer().getMessenger();
7876

7977
try {
78+
PluginMessageListener listener = (channel, player, bytes) -> {
79+
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
80+
this.collectBrand(in.readUTF());
81+
} catch (IOException ignored) {
82+
}
83+
};
84+
8085
messenger.registerIncomingPluginChannel(this.plugin, "MC|Brand", listener);
8186
} catch (IllegalArgumentException ignored) {
8287
}
8388

84-
messenger.registerIncomingPluginChannel(this.plugin, "minecraft:brand", listener);
89+
messenger.registerIncomingPluginChannel(this.plugin, "minecraft:brand", (channel, player, bytes) -> {
90+
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
91+
this.collectBrand(ByteBufUtil.readString(in));
92+
});
93+
}
94+
95+
private void collectBrand(String brand) {
96+
BukkitMetadataManager manager = (BukkitMetadataManager) ApolloManager.getMetadataManager();
97+
manager.getClientBrands().add(brand);
8598
}
8699

87100
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of Apollo, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2023 Moonsworth
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.lunarclient.apollo.util;
25+
26+
import com.google.common.io.ByteArrayDataInput;
27+
import java.nio.charset.StandardCharsets;
28+
29+
/**
30+
* Represents a util for reading byte buffs.
31+
*
32+
* @since 1.1.9
33+
*/
34+
public final class ByteBufUtil {
35+
36+
/**
37+
* Reads an int from the given input stream.
38+
*
39+
* @param in in the {@link ByteArrayDataInput} to read from
40+
* @return the read int
41+
* @throws IllegalArgumentException if the value length is invalid or too large
42+
* @since 1.1.9
43+
*/
44+
public static int readVarInt(ByteArrayDataInput in) {
45+
int i = 0;
46+
int j = 0;
47+
48+
while (true) {
49+
byte b = in.readByte();
50+
i |= (b & 0x7F) << j++ * 7;
51+
52+
if (j > 5) {
53+
throw new RuntimeException("VarInt too big");
54+
}
55+
56+
if ((b & 0x80) != 128) {
57+
break;
58+
}
59+
}
60+
61+
return i;
62+
}
63+
64+
/**
65+
* Reads a UTF-8 encoded string from the given input stream.
66+
*
67+
* @param in the {@link ByteArrayDataInput} to read from
68+
* @return the decoded string
69+
* @throws IllegalArgumentException if the value length is invalid or too large
70+
* @since 1.1.9
71+
*/
72+
public static String readString(ByteArrayDataInput in) {
73+
int length = ByteBufUtil.readVarInt(in);
74+
byte[] bytes = new byte[length];
75+
in.readFully(bytes);
76+
77+
return new String(bytes, StandardCharsets.UTF_8);
78+
}
79+
80+
private ByteBufUtil() {
81+
}
82+
83+
}

bungee/src/main/java/com/lunarclient/apollo/listener/ApolloMetadataListener.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import com.lunarclient.apollo.ApolloManager;
2929
import com.lunarclient.apollo.metadata.BungeeMetadataManager;
3030
import com.lunarclient.apollo.util.ByteBufUtil;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.DataInputStream;
33+
import java.io.IOException;
3134
import java.net.InetSocketAddress;
32-
import java.nio.charset.StandardCharsets;
3335
import net.md_5.bungee.api.ProxyServer;
3436
import net.md_5.bungee.api.connection.PendingConnection;
3537
import net.md_5.bungee.api.connection.ProxiedPlayer;
@@ -75,14 +77,27 @@ public void onPluginMessage(PluginMessageEvent event) {
7577
}
7678

7779
String tag = event.getTag();
80+
byte[] data = event.getData();
7881

79-
if (tag.equals("minecraft:brand") || tag.equals("MC|Brand")) {
80-
this.handleBrand(event.getData());
81-
return;
82-
}
82+
switch (tag) {
83+
case "MC|Brand": {
84+
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(data))) {
85+
this.collectBrand(in.readUTF());
86+
} catch (IOException ignored) {
87+
}
8388

84-
if (tag.equals("fml:handshake") || tag.equals("FML|HS")) {
85-
this.handleFml(event.getData());
89+
break;
90+
}
91+
case "minecraft:brand": {
92+
ByteArrayDataInput in = ByteStreams.newDataInput(data);
93+
this.collectBrand(ByteBufUtil.readString(in));
94+
break;
95+
}
96+
case "fml:handshake":
97+
case "FML|HS": {
98+
this.handleFml(data);
99+
break;
100+
}
86101
}
87102
}
88103

@@ -112,8 +127,7 @@ public void onPreLogin(PreLoginEvent event) {
112127
manager.getServerAddress().add(hostString + ":" + host.getPort());
113128
}
114129

115-
private void handleBrand(byte[] data) {
116-
String brand = new String(data, StandardCharsets.UTF_8);
130+
private void collectBrand(String brand) {
117131
BungeeMetadataManager manager = (BungeeMetadataManager) ApolloManager.getMetadataManager();
118132
manager.getClientBrands().add(brand);
119133
}

0 commit comments

Comments
 (0)