Skip to content

Commit 83d7ccb

Browse files
authored
Feature - User Metadata (#225)
* Bukkit and bungee impl * finish bungee impl * Velocity & Folia impl * Remove debug * fix brand collecting on bukkit & bungee
1 parent 92a7aa1 commit 83d7ccb

File tree

29 files changed

+1400
-14
lines changed

29 files changed

+1400
-14
lines changed

api/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ setupPlatforms()
77

88
setupPlatformDependency("bukkit", "bukkitJar")
99
setupPlatformDependency("bungee", "bungeeJar")
10-
setupPlatformDependency("velocity", "velocityJar")
10+
setupPlatformDependency("velocity", "velocityJar", 17)
1111
setupPlatformDependency("folia", "foliaJar", 21)
1212

1313
val main by sourceSets

build-logic/src/main/kotlin/apollo.base-conventions.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import com.diffplug.gradle.spotless.FormatExtension
2+
import org.gradle.jvm.toolchain.JavaLanguageVersion
3+
import org.gradle.jvm.toolchain.JavaToolchainService
24
import java.nio.charset.StandardCharsets
35
import java.nio.file.Files
46
import java.util.regex.Pattern
@@ -83,6 +85,11 @@ checkstyle {
8385

8486
tasks {
8587
javadoc {
88+
val javaToolchains = project.extensions.getByType(JavaToolchainService::class.java)
89+
javadocTool.set(javaToolchains.javadocToolFor {
90+
languageVersion.set(JavaLanguageVersion.of(21))
91+
})
92+
8693
val minimalOptions: MinimalJavadocOptions = options
8794
options.encoding("UTF-8")
8895

bukkit/src/main/java/com/lunarclient/apollo/ApolloBukkitPlatform.java

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

2626
import com.lunarclient.apollo.command.impl.ApolloCommand;
2727
import com.lunarclient.apollo.command.impl.LunarClientCommand;
28+
import com.lunarclient.apollo.listener.ApolloMetadataListener;
2829
import com.lunarclient.apollo.listener.ApolloPlayerListener;
2930
import com.lunarclient.apollo.listener.ApolloWorldListener;
3031
import com.lunarclient.apollo.loader.PlatformPlugin;
32+
import com.lunarclient.apollo.metadata.BukkitMetadataManager;
3133
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
3234
import com.lunarclient.apollo.module.autotexthotkey.AutoTextHotkeyModule;
3335
import com.lunarclient.apollo.module.beam.BeamModule;
@@ -119,7 +121,9 @@ public void onEnable() {
119121
this.stats = new BukkitApolloStats();
120122

121123
ApolloManager.bootstrap(this);
124+
ApolloManager.setMetadataManager(new BukkitMetadataManager());
122125

126+
new ApolloMetadataListener(this.plugin);
123127
new ApolloPlayerListener(this.plugin);
124128
new ApolloWorldListener(this.plugin);
125129

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.google.common.io.ByteArrayDataInput;
27+
import com.google.common.io.ByteStreams;
28+
import com.lunarclient.apollo.ApolloManager;
29+
import com.lunarclient.apollo.metadata.BukkitMetadataManager;
30+
import com.lunarclient.apollo.util.ByteBufUtil;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.DataInputStream;
33+
import java.io.IOException;
34+
import java.util.Map;
35+
import org.bukkit.Bukkit;
36+
import org.bukkit.event.EventHandler;
37+
import org.bukkit.event.Listener;
38+
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
39+
import org.bukkit.plugin.java.JavaPlugin;
40+
import org.bukkit.plugin.messaging.Messenger;
41+
import org.bukkit.plugin.messaging.PluginMessageListener;
42+
43+
/**
44+
* Handles Apollo metadata listeners.
45+
*
46+
* @since 1.1.9
47+
*/
48+
public final class ApolloMetadataListener implements Listener {
49+
50+
private final JavaPlugin plugin;
51+
52+
/**
53+
* Constructs the {@link ApolloMetadataListener}.
54+
*
55+
* @param plugin the plugin
56+
* @since 1.1.9
57+
*/
58+
public ApolloMetadataListener(JavaPlugin plugin) {
59+
this.plugin = plugin;
60+
61+
this.registerBrandListener();
62+
Bukkit.getPluginManager().registerEvents(this, plugin);
63+
}
64+
65+
@EventHandler
66+
private void onResourcePackStatus(PlayerResourcePackStatusEvent event) {
67+
String status = event.getStatus().name();
68+
BukkitMetadataManager manager = (BukkitMetadataManager) ApolloManager.getMetadataManager();
69+
Map<String, Integer> statuses = manager.getResourcePackStatuses();
70+
71+
statuses.put(status, statuses.getOrDefault(status, 0) + 1);
72+
}
73+
74+
private void registerBrandListener() {
75+
Messenger messenger = this.plugin.getServer().getMessenger();
76+
77+
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+
85+
messenger.registerIncomingPluginChannel(this.plugin, "MC|Brand", listener);
86+
} catch (IllegalArgumentException ignored) {
87+
}
88+
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);
98+
}
99+
100+
}
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 bukkit metadata implementation.
34+
*
35+
* @since 1.1.9
36+
*/
37+
@ToString
38+
@Builder(toBuilder = true)
39+
public class BukkitMetadata 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.1.9
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.1.9
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 Bukkit implementation of {@link ApolloMetadataManager}.
36+
*
37+
* @since 1.1.9
38+
*/
39+
@Getter
40+
public class BukkitMetadataManager 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 BukkitMetadata.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 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/ApolloBungeePlatform.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.impl.ApolloCommand;
2727
import com.lunarclient.apollo.command.impl.LunarClientCommand;
28+
import com.lunarclient.apollo.listener.ApolloMetadataListener;
2829
import com.lunarclient.apollo.listener.ApolloPlayerListener;
2930
import com.lunarclient.apollo.loader.PlatformPlugin;
31+
import com.lunarclient.apollo.metadata.BungeeMetadataManager;
3032
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
3133
import com.lunarclient.apollo.module.autotexthotkey.AutoTextHotkeyModule;
3234
import com.lunarclient.apollo.module.beam.BeamModule;
@@ -107,6 +109,7 @@ public void onEnable() {
107109
this.stats = new BungeeApolloStats();
108110

109111
ApolloManager.bootstrap(this);
112+
ApolloManager.setMetadataManager(new BungeeMetadataManager());
110113

111114
((ApolloModuleManagerImpl) Apollo.getModuleManager())
112115
.addModule(AutoTextHotkeyModule.class)
@@ -146,6 +149,7 @@ public void onEnable() {
146149
server.registerChannel(ApolloManager.PLUGIN_MESSAGE_CHANNEL);
147150

148151
PluginManager pluginManager = server.getPluginManager();
152+
pluginManager.registerListener(this.plugin, new ApolloMetadataListener(this.plugin));
149153
pluginManager.registerListener(this.plugin, new ApolloPlayerListener());
150154
pluginManager.registerCommand(this.plugin, ApolloCommand.create());
151155
pluginManager.registerCommand(this.plugin, LunarClientCommand.create());

0 commit comments

Comments
 (0)