Skip to content

Commit 5a71210

Browse files
committed
Implement Minestom user metadata
1 parent 4961543 commit 5a71210

File tree

6 files changed

+280
-2
lines changed

6 files changed

+280
-2
lines changed

minestom/src/main/java/com/lunarclient/apollo/ApolloMinestomPlatform.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
import com.lunarclient.apollo.command.MinestomApolloCommand;
2727
import com.lunarclient.apollo.command.MinestomLunarClientCommand;
28+
import com.lunarclient.apollo.listener.ApolloMetadataListener;
2829
import com.lunarclient.apollo.listener.ApolloPlayerListener;
2930
import com.lunarclient.apollo.listener.ApolloWorldListener;
31+
import com.lunarclient.apollo.metadata.MinestomMetadataManager;
3032
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
3133
import com.lunarclient.apollo.module.autotexthotkey.AutoTextHotkeyModule;
3234
import com.lunarclient.apollo.module.beam.BeamModule;
@@ -132,9 +134,11 @@ public static void init() {
132134
instance = new ApolloMinestomPlatform();
133135

134136
ApolloManager.bootstrap(instance);
137+
ApolloManager.setMetadataManager(new MinestomMetadataManager());
135138

136139
EventNode<Event> node = EventNode.all("apollo");
137140

141+
new ApolloMetadataListener(node);
138142
new ApolloPlayerListener(node);
139143
new ApolloWorldListener(node);
140144

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.listener;
25+
26+
import com.lunarclient.apollo.ApolloManager;
27+
import com.lunarclient.apollo.metadata.MinestomMetadataManager;
28+
import com.lunarclient.apollo.util.ByteBufUtil;
29+
import java.nio.ByteBuffer;
30+
import java.util.Map;
31+
import net.minestom.server.event.Event;
32+
import net.minestom.server.event.EventNode;
33+
import net.minestom.server.event.player.PlayerPluginMessageEvent;
34+
import net.minestom.server.event.player.PlayerResourcePackStatusEvent;
35+
36+
/**
37+
* Handles Apollo metadata listeners.
38+
*
39+
* @since 1.2.0
40+
*/
41+
public final class ApolloMetadataListener {
42+
43+
/**
44+
* Constructs the {@link ApolloMetadataListener}.
45+
*
46+
* @param node the node
47+
* @since 1.2.0
48+
*/
49+
public ApolloMetadataListener(EventNode<Event> node) {
50+
node.addListener(PlayerResourcePackStatusEvent.class, this::onResourcePackStatus);
51+
node.addListener(PlayerPluginMessageEvent.class, this::onPlayerPluginMessage);
52+
}
53+
54+
private void onResourcePackStatus(PlayerResourcePackStatusEvent event) {
55+
String status = event.getStatus().name();
56+
MinestomMetadataManager manager = (MinestomMetadataManager) ApolloManager.getMetadataManager();
57+
Map<String, Integer> statuses = manager.getResourcePackStatuses();
58+
59+
statuses.put(status, statuses.getOrDefault(status, 0) + 1);
60+
}
61+
62+
private void onPlayerPluginMessage(PlayerPluginMessageEvent event) {
63+
String identifier = event.getIdentifier();
64+
65+
if (!identifier.equals("minecraft:brand")) {
66+
return;
67+
}
68+
69+
ByteBuffer buffer = ByteBuffer.wrap(event.getMessage());
70+
MinestomMetadataManager manager = (MinestomMetadataManager) ApolloManager.getMetadataManager();
71+
manager.getClientBrands().add(ByteBufUtil.readString(buffer));
72+
}
73+
74+
}

minestom/src/main/java/com/lunarclient/apollo/listener/ApolloPlayerListener.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ private void onPlayerPluginMessage(PlayerPluginMessageEvent event) {
6969
Player player = event.getPlayer();
7070
String identifier = event.getIdentifier();
7171

72-
System.out.println(event.getIdentifier());
73-
7472
switch (identifier) {
7573
case "minecraft:register": {
7674
if (this.getChannels(event.getMessageString()).contains(ApolloManager.PLUGIN_MESSAGE_CHANNEL)) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.metadata;
25+
26+
import com.lunarclient.apollo.stats.metadata.PlatformMetadata;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import lombok.Builder;
30+
import lombok.ToString;
31+
32+
/**
33+
* Represents the minestom metadata implementation.
34+
*
35+
* @since 1.2.0
36+
*/
37+
@ToString
38+
@Builder(toBuilder = true)
39+
public class MinestomMetadata extends PlatformMetadata {
40+
41+
/**
42+
* Tracks client brands sent by the players.
43+
*
44+
* <p>A {@link Set} of {@link String} client brands.</p>
45+
*
46+
* @since 1.2.0
47+
*/
48+
private final Set<String> clientBrands;
49+
50+
/**
51+
* Tracks the total number of resource pack status events received.
52+
*
53+
* <p>Represents a {@link Map} of {@link String} status enum name as a key
54+
* and {@link Integer} count of how many times that status has been reported.</p>
55+
*
56+
* @since 1.2.0
57+
*/
58+
private final Map<String, Integer> resourcePackStatuses;
59+
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.metadata;
25+
26+
import com.lunarclient.apollo.stats.metadata.ApolloMetadataManager;
27+
import com.lunarclient.apollo.stats.metadata.PlatformMetadata;
28+
import java.util.HashMap;
29+
import java.util.HashSet;
30+
import java.util.Map;
31+
import java.util.Set;
32+
import lombok.Getter;
33+
34+
/**
35+
* The Minestom implementation of {@link ApolloMetadataManager}.
36+
*
37+
* @since 1.2.0
38+
*/
39+
@Getter
40+
public class MinestomMetadataManager implements ApolloMetadataManager {
41+
42+
private final Set<String> clientBrands = new HashSet<>();
43+
private final Map<String, Integer> resourcePackStatuses = new HashMap<>();
44+
45+
@Override
46+
public PlatformMetadata extract() {
47+
return MinestomMetadata.builder()
48+
.clientBrands(new HashSet<>(this.clientBrands))
49+
.resourcePackStatuses(new HashMap<>(this.resourcePackStatuses))
50+
.build();
51+
}
52+
53+
@Override
54+
public void clear() {
55+
this.clientBrands.clear();
56+
this.resourcePackStatuses.clear();
57+
}
58+
59+
}
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 java.nio.ByteBuffer;
27+
import java.nio.charset.StandardCharsets;
28+
29+
/**
30+
* Represents a util for reading byte buffs.
31+
*
32+
* @since 1.2.0
33+
*/
34+
public final class ByteBufUtil {
35+
36+
/**
37+
* Reads an int from the given input stream.
38+
*
39+
* @param in in the {@link ByteBuffer} to read from
40+
* @return the read int
41+
* @throws IllegalArgumentException if the value length is invalid or too large
42+
* @since 1.2.0
43+
*/
44+
public static int readVarInt(ByteBuffer in) {
45+
int i = 0;
46+
int j = 0;
47+
48+
while (true) {
49+
byte b = in.get();
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 ByteBuffer} to read from
68+
* @return the decoded string
69+
* @throws IllegalArgumentException if the value length is invalid or too large
70+
* @since 1.2.0
71+
*/
72+
public static String readString(ByteBuffer in) {
73+
int length = ByteBufUtil.readVarInt(in);
74+
byte[] bytes = new byte[length];
75+
in.get(bytes);
76+
77+
return new String(bytes, StandardCharsets.UTF_8);
78+
}
79+
80+
private ByteBufUtil() {
81+
}
82+
83+
}

0 commit comments

Comments
 (0)