Skip to content

Commit fa4f866

Browse files
committed
example: add mod status command
1 parent 5e06fa1 commit fa4f866

File tree

8 files changed

+192
-33
lines changed

8 files changed

+192
-33
lines changed

api/src/main/java/com/lunarclient/apollo/event/mods/ApolloUpdateModOptionEvent.java

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

2626
import com.lunarclient.apollo.event.Event;
2727
import com.lunarclient.apollo.option.Option;
28-
import com.lunarclient.apollo.option.Options;
2928
import com.lunarclient.apollo.player.ApolloPlayer;
3029
import lombok.Value;
3130
import org.jetbrains.annotations.Nullable;
@@ -38,15 +37,6 @@
3837
@Value
3938
public final class ApolloUpdateModOptionEvent implements Event {
4039

41-
// TODO: not needed?
42-
/**
43-
* The {@link Options} container that the option is in.
44-
*
45-
* @return the options container
46-
* @since 1.2.1
47-
*/
48-
Options container;
49-
5040
/**
5141
* The {@link ApolloPlayer} that the option was updated for.
5242
*

api/src/main/java/com/lunarclient/apollo/mods/ModStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public interface ModStatus {
4444
* @return the value of the option
4545
* @since 1.2.1
4646
*/
47-
public <T, C extends Option<T, ?, ?>> T get(@NonNull C option);
47+
<T, C extends Option<T, ?, ?>> T get(@NonNull C option);
4848

4949
}

common/src/main/java/com/lunarclient/apollo/mods/ApolloModsManager.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@
2323
*/
2424
package com.lunarclient.apollo.mods;
2525

26+
import com.google.protobuf.Any;
27+
import com.google.protobuf.InvalidProtocolBufferException;
28+
import com.google.protobuf.Value;
29+
import com.lunarclient.apollo.configurable.v1.ConfigurableSettings;
30+
import com.lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage;
2631
import com.lunarclient.apollo.event.ApolloListener;
2732
import com.lunarclient.apollo.event.ApolloReceivePacketEvent;
2833
import com.lunarclient.apollo.event.EventBus;
2934
import com.lunarclient.apollo.event.Listen;
35+
import com.lunarclient.apollo.event.mods.ApolloUpdateModOptionEvent;
36+
import com.lunarclient.apollo.network.NetworkOptions;
3037
import com.lunarclient.apollo.option.Option;
31-
import com.lunarclient.apollo.option.Options;
3238
import com.lunarclient.apollo.option.StatusOptionsImpl;
39+
import com.lunarclient.apollo.player.ApolloPlayer;
3340
import java.lang.reflect.Field;
3441
import java.util.ArrayList;
42+
import java.util.Collections;
3543
import java.util.LinkedHashMap;
3644
import java.util.List;
3745
import java.util.Map;
@@ -47,7 +55,7 @@
4755
public final class ApolloModsManager implements ApolloListener {
4856

4957
private final Container container;
50-
private final Options playerOptions;
58+
private final StatusOptionsImpl playerOptions;
5159

5260
/**
5361
* Constructs the {@link ApolloModsManager}.
@@ -58,11 +66,7 @@ public ApolloModsManager() {
5866
EventBus.getBus().register(this);
5967

6068
this.container = ApolloModsManager.loadModOptions();
61-
this.playerOptions = new StatusOptionsImpl();
62-
63-
for (Option<?, ?, ?> option : this.container.getModStatusOptions().values()) {
64-
this.playerOptions.set(option, option.getDefaultValue());
65-
}
69+
this.playerOptions = new StatusOptionsImpl(this.container.getModStatusOptions());
6670
}
6771

6872
/**
@@ -133,7 +137,54 @@ public static class Container {
133137

134138
@Listen
135139
private void onApolloReceivePacket(ApolloReceivePacketEvent event) {
140+
ApolloPlayer player = event.getPlayer();
141+
Any packet = event.getPacket();
136142

143+
if(packet.is(OverrideConfigurableSettingsMessage.class) || packet.is(ConfigurableSettings.class)) {
144+
this.handleConfiguration(player, packet);
145+
}
137146
}
138147

148+
private void handleConfiguration(ApolloPlayer player, Any any) {
149+
// Unpack the settings first.
150+
List<ConfigurableSettings> settings;
151+
try {
152+
if (any.is(OverrideConfigurableSettingsMessage.class)) {
153+
OverrideConfigurableSettingsMessage message = any.unpack(OverrideConfigurableSettingsMessage.class);
154+
settings = message.getConfigurableSettingsList();
155+
} else {
156+
settings = Collections.singletonList(any.unpack(ConfigurableSettings.class));
157+
}
158+
} catch (InvalidProtocolBufferException exception) {
159+
throw new RuntimeException(exception);
160+
}
161+
162+
for (ConfigurableSettings setting : settings) {
163+
if (!setting.hasApolloModule()) {
164+
continue;
165+
}
166+
167+
if (!setting.getApolloModule().equals("mod_status")) {
168+
continue;
169+
}
170+
171+
for (Map.Entry<String, Value> entry : setting.getPropertiesMap().entrySet()) {
172+
Option<?, ?, ?> option = this.playerOptions.getOptionsByKey().get(entry.getKey());
173+
174+
Object unwrappedValue = NetworkOptions.unwrapValue(
175+
entry.getValue(),
176+
option.getTypeToken().getType()
177+
);
178+
179+
this.playerOptions.set(player, option, unwrappedValue);
180+
181+
EventBus.EventResult<ApolloUpdateModOptionEvent> eventResult = EventBus.getBus()
182+
.post(new ApolloUpdateModOptionEvent(player, option, unwrappedValue));
183+
184+
for (Throwable throwable : eventResult.getThrowing()) {
185+
throwable.printStackTrace();
186+
}
187+
}
188+
}
189+
}
139190
}

common/src/main/java/com/lunarclient/apollo/option/StatusOptionsImpl.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
*/
2424
package com.lunarclient.apollo.option;
2525

26-
import com.lunarclient.apollo.event.EventBus;
27-
import com.lunarclient.apollo.event.mods.ApolloUpdateModOptionEvent;
2826
import com.lunarclient.apollo.player.ApolloPlayer;
2927
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.Map;
3030
import java.util.Objects;
3131
import java.util.WeakHashMap;
32+
import lombok.Getter;
3233
import lombok.NonNull;
3334
import org.jetbrains.annotations.Nullable;
3435

@@ -39,13 +40,24 @@
3940
*/
4041
public class StatusOptionsImpl extends OptionsImpl {
4142

43+
@Getter
44+
private final Map<String, Option<?, ?, ?>> optionsByKey = new HashMap<>();
45+
4246
/**
4347
* Constructs a new {@link StatusOptionsImpl}.
4448
*
49+
* @param options the mod options with default values
4550
* @since 1.2.1
4651
*/
47-
public StatusOptionsImpl() {
52+
public StatusOptionsImpl(Map<String, Option<?, ?, ?>> options) {
4853
super(null);
54+
55+
for (Map.Entry<String, Option<?, ?, ?>> entry : options.entrySet()) {
56+
Option<?, ?, ?> option = entry.getValue();
57+
58+
this.set(option, option.getDefaultValue());
59+
this.optionsByKey.put(entry.getKey(), option);
60+
}
4961
}
5062

5163
@Override
@@ -69,13 +81,6 @@ public <T> void set(@NonNull ApolloPlayer player, @NonNull Option<?, ?, ?> optio
6981

7082
@Override
7183
protected boolean postEvent(Option<?, ?, ?> option, @Nullable ApolloPlayer player, @Nullable Object value) {
72-
EventBus.EventResult<ApolloUpdateModOptionEvent> eventResult = EventBus.getBus()
73-
.post(new ApolloUpdateModOptionEvent(this, player, option, value));
74-
75-
for (Throwable throwable : eventResult.getThrowing()) {
76-
throwable.printStackTrace();
77-
}
78-
7984
return false;
8085
}
8186

example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/ApolloApiExamplePlatform.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.lunarclient.apollo.example.api.debug.impl.SpamPacketDebug;
3131
import com.lunarclient.apollo.example.api.listener.ApolloPlayerApiListener;
3232
import com.lunarclient.apollo.example.api.mods.ApolloModStatusExample;
33+
import com.lunarclient.apollo.example.api.mods.ModStatusCommand;
3334
import com.lunarclient.apollo.example.api.module.AutoTextHotkeyApiExample;
3435
import com.lunarclient.apollo.example.api.module.BeamApiExample;
3536
import com.lunarclient.apollo.example.api.module.BorderApiExample;
@@ -74,6 +75,7 @@ public void enable() {
7475
@Override
7576
public void registerCommands() {
7677
this.getCommand("apollodebug").setExecutor(new ApolloDebugCommand());
78+
this.getCommand("modstatus").setExecutor(new ModStatusCommand());
7779
}
7880

7981
@Override

example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/mods/ApolloModStatusExample.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.lunarclient.apollo.mods.impl.ModFreelook;
3434
import com.lunarclient.apollo.mods.impl.ModMinimap;
3535
import com.lunarclient.apollo.mods.impl.ModWaypoints;
36+
import java.util.Objects;
37+
import net.kyori.adventure.text.Component;
3638
import org.bukkit.Bukkit;
3739
import org.bukkit.entity.Player;
3840
import org.bukkit.event.EventHandler;
@@ -48,7 +50,9 @@ public ApolloModStatusExample(ApolloExamplePlugin plugin) {
4850

4951
@Listen
5052
private void onApolloUpdateModOption(ApolloUpdateModOptionEvent event) {
51-
System.out.println(event.getOption().getKey() + " was updated to " + event.getValue());
53+
event.getPlayer().sendMessage(Component.text(event.getOption().getKey())
54+
.append(Component.text(" was updated to "))
55+
.append(Component.text(Objects.toString(event.getValue()))));
5256
}
5357

5458
@EventHandler(ignoreCancelled = true)
@@ -58,10 +62,17 @@ private void onBlockPlace(BlockPlaceEvent event) {
5862
Apollo.getPlayerManager().getPlayer(player.getUniqueId()).ifPresent(apolloPlayer -> {
5963
ModStatus status = apolloPlayer.getModStatus();
6064

61-
System.out.println("Waypoints Enabled: " + status.get(ModWaypoints.ENABLED));
62-
System.out.println("Freelook Invert Yaw: " + status.get(ModFreelook.INVERT_YAW));
63-
System.out.println("Freelook Invert Pitch: " + status.get(ModFreelook.INVERT_PITCH));
64-
System.out.println("Minimap scale: " + status.get(ModMinimap.SCALE));
65+
apolloPlayer.sendMessage(Component.text("Waypoints Enabled: ")
66+
.append(Component.text(status.get(ModWaypoints.ENABLED))));
67+
68+
apolloPlayer.sendMessage(Component.text("Freelook Invert Yaw: ")
69+
.append(Component.text(status.get(ModFreelook.INVERT_YAW))));
70+
71+
apolloPlayer.sendMessage(Component.text("Freelook Invert Pitch: ")
72+
.append(Component.text(status.get(ModFreelook.INVERT_PITCH))));
73+
74+
apolloPlayer.sendMessage(Component.text("Minimap Scale: ")
75+
.append(Component.text(status.get(ModMinimap.SCALE))));
6576
});
6677
}
6778

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.example.api.mods;
25+
26+
import com.lunarclient.apollo.Apollo;
27+
import com.lunarclient.apollo.ApolloManager;
28+
import com.lunarclient.apollo.option.Option;
29+
import com.lunarclient.apollo.player.ApolloPlayer;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
import org.bukkit.Bukkit;
33+
import org.bukkit.command.Command;
34+
import org.bukkit.command.CommandExecutor;
35+
import org.bukkit.command.CommandSender;
36+
import org.bukkit.entity.Player;
37+
import org.jetbrains.annotations.NotNull;
38+
39+
public class ModStatusCommand implements CommandExecutor {
40+
41+
@Override
42+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
43+
if (args.length == 0) {
44+
sender.sendMessage("/modstatus <player>");
45+
sender.sendMessage("/modstatus <player> <option>");
46+
return true;
47+
}
48+
49+
Player target = Bukkit.getPlayer(args[0]);
50+
51+
if (target == null) {
52+
sender.sendMessage("Player '" + args[0] + "' not found!");
53+
return true;
54+
}
55+
56+
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(target.getUniqueId());
57+
if (!apolloPlayerOpt.isPresent()) {
58+
sender.sendMessage("Player '" + args[0] + "' not running Lunar Client!");
59+
return true;
60+
}
61+
62+
if (args.length == 1) {
63+
this.sendCurrent(sender, apolloPlayerOpt.get());
64+
return true;
65+
}
66+
67+
if (args.length == 2) {
68+
Option<?, ?, ?> option = ApolloManager.getModsManager().getPlayerOptions().getOptionsByKey().get(args[1]);
69+
70+
if (option == null) {
71+
sender.sendMessage("Option not found!");
72+
return true;
73+
}
74+
75+
Object value = apolloPlayerOpt.get().getModStatus().get(option);
76+
sender.sendMessage("Current value for \"" + option.getKey() + "\" is \"" + value + "\"");
77+
return true;
78+
}
79+
80+
sender.sendMessage("/modstatus <player>");
81+
sender.sendMessage("/modstatus <player> <option>");
82+
return true;
83+
}
84+
85+
private void sendCurrent(CommandSender sender, ApolloPlayer target) {
86+
Map<Option<?, ?, ?>, Object> playerOptions = ApolloManager.getModsManager().getPlayerOptions().getPlayerOptions().get(target.getUniqueId());
87+
88+
sender.sendMessage("-------------------------------------");
89+
sender.sendMessage("Target: " + target.getName());
90+
sender.sendMessage("");
91+
92+
for (Map.Entry<Option<?, ?, ?>, Object> entry : playerOptions.entrySet()) {
93+
sender.sendMessage(" - " + entry.getKey().getKey() + "=" + entry.getValue());
94+
}
95+
96+
sender.sendMessage("-------------------------------------");
97+
}
98+
}

example/bukkit/api/src/main/resources/plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ folia-supported: true
99
commands:
1010
apollodebug:
1111
description: "Apollo Debug!"
12+
modstatus:
13+
description: "Mod Status Command!"
1214
autotexthotkey:
1315
description: "Auto Text Hotkey!"
1416
beam:

0 commit comments

Comments
 (0)