Skip to content

Commit f752eb2

Browse files
committed
add mod settings module tests
1 parent 8a148a9 commit f752eb2

File tree

10 files changed

+213
-27
lines changed

10 files changed

+213
-27
lines changed

common/src/main/java/com/lunarclient/apollo/module/modsettings/ModSettingsModuleImpl.java renamed to common/src/main/java/com/lunarclient/apollo/module/modsettings/ModSettingModuleImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
*
4949
* @since 1.2.1
5050
*/
51-
public final class ModSettingsModuleImpl extends ModSettingModule {
51+
public final class ModSettingModuleImpl extends ModSettingModule {
5252

5353
/**
54-
* Creates a new instance of {@link ModSettingsModuleImpl}.
54+
* Creates a new instance of {@link ModSettingModuleImpl}.
5555
*
5656
* @since 1.2.1
5757
*/
58-
public ModSettingsModuleImpl() {
58+
public ModSettingModuleImpl() {
5959
super();
6060
this.registerOptions(ApolloManager.getModsManager().getContainer().getModSettingsOptions());
6161
this.handle(ApolloReceivePacketEvent.class, this::onReceivePacket);

common/src/main/java/com/lunarclient/apollo/player/ApolloPlayerManagerImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.lunarclient.apollo.event.player.ApolloRegisterPlayerEvent;
3535
import com.lunarclient.apollo.event.player.ApolloUnregisterPlayerEvent;
3636
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
37-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
37+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
3838
import com.lunarclient.apollo.module.tebex.TebexEmbeddedCheckoutSupport;
3939
import com.lunarclient.apollo.network.NetworkOptions;
4040
import com.lunarclient.apollo.player.v1.PlayerHandshakeMessage;
@@ -147,13 +147,6 @@ public void handlePlayerHandshake(@NotNull ApolloPlayer player, @NotNull PlayerH
147147
checkoutSupportType = TebexEmbeddedCheckoutSupport.UNSUPPORTED;
148148
}
149149

150-
EventBus.EventResult<ApolloPlayerHandshakeEvent> result = EventBus.getBus()
151-
.post(new ApolloPlayerHandshakeEvent(player, minecraftVersion, lunarClientVersion, mods, checkoutSupportType));
152-
153-
for (Throwable throwable : result.getThrowing()) {
154-
throwable.printStackTrace();
155-
}
156-
157150
AbstractApolloPlayer apolloPlayer = ((AbstractApolloPlayer) player);
158151
apolloPlayer.setMinecraftVersion(minecraftVersion);
159152
apolloPlayer.setLunarClientVersion(lunarClientVersion);
@@ -162,12 +155,19 @@ public void handlePlayerHandshake(@NotNull ApolloPlayer player, @NotNull PlayerH
162155

163156
Map<String, Value> modStatus = message.getModStatusMap();
164157
if (!modStatus.isEmpty()) {
165-
ModSettingsModuleImpl modSettingModule = (ModSettingsModuleImpl) Apollo.getModuleManager().getModule(ModSettingModule.class);
158+
ModSettingModuleImpl modSettingModule = (ModSettingModuleImpl) Apollo.getModuleManager().getModule(ModSettingModule.class);
166159

167160
if (modSettingModule.isEnabled()) {
168161
modSettingModule.updateOptions(apolloPlayer, modStatus, false);
169162
}
170163
}
164+
165+
EventBus.EventResult<ApolloPlayerHandshakeEvent> result = EventBus.getBus()
166+
.post(new ApolloPlayerHandshakeEvent(player, minecraftVersion, lunarClientVersion, mods, checkoutSupportType));
167+
168+
for (Throwable throwable : result.getThrowing()) {
169+
throwable.printStackTrace();
170+
}
171171
}
172172

173173
}

example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/debug/DebugManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
package com.lunarclient.apollo.example.api.debug;
2525

2626
import com.lunarclient.apollo.example.ApolloExamplePlugin;
27-
import com.lunarclient.apollo.example.api.debug.payload.PayloadListener;
27+
import com.lunarclient.apollo.example.api.debug.impl.ModSettingsTest;
28+
import com.lunarclient.apollo.example.api.debug.impl.PayloadTest;
2829
import java.util.HashMap;
2930
import java.util.Map;
3031
import java.util.UUID;
@@ -45,7 +46,8 @@ public DebugManager() {
4546

4647
this.players = new HashMap<>();
4748

48-
new PayloadListener();
49+
new ModSettingsTest();
50+
new PayloadTest();
4951

5052
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
5153
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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.debug.impl;
25+
26+
import com.lunarclient.apollo.Apollo;
27+
import com.lunarclient.apollo.event.ApolloListener;
28+
import com.lunarclient.apollo.event.EventBus;
29+
import com.lunarclient.apollo.event.Listen;
30+
import com.lunarclient.apollo.event.modsetting.ApolloUpdateModOptionEvent;
31+
import com.lunarclient.apollo.event.player.ApolloPlayerHandshakeEvent;
32+
import com.lunarclient.apollo.example.ApolloExamplePlugin;
33+
import com.lunarclient.apollo.mods.impl.ModWaypoints;
34+
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
35+
import com.lunarclient.apollo.option.Option;
36+
import com.lunarclient.apollo.player.ApolloPlayer;
37+
import java.util.ArrayList;
38+
import java.util.HashMap;
39+
import java.util.Iterator;
40+
import java.util.List;
41+
import java.util.Map;
42+
import java.util.Objects;
43+
import java.util.UUID;
44+
import java.util.function.Predicate;
45+
import lombok.Getter;
46+
import lombok.RequiredArgsConstructor;
47+
import lombok.ToString;
48+
import net.kyori.adventure.text.Component;
49+
import net.kyori.adventure.text.format.NamedTextColor;
50+
import org.bukkit.Bukkit;
51+
import org.bukkit.event.EventHandler;
52+
import org.bukkit.event.Listener;
53+
import org.bukkit.event.player.PlayerQuitEvent;
54+
import org.bukkit.scheduler.BukkitTask;
55+
56+
public class ModSettingsTest implements Listener, ApolloListener {
57+
58+
private static final Component PREFIX = Component.text("[Apollo] [ModSettingsTest] ", NamedTextColor.YELLOW);
59+
private static final Component FAILURE = Component.text(" ❌ ", NamedTextColor.RED);
60+
private static final Component SUCCESS = Component.text(" ✔ ", NamedTextColor.GREEN);
61+
62+
private final ModSettingModule modSettingModule = Apollo.getModuleManager().getModule(ModSettingModule.class);
63+
private final Map<UUID, List<OptionChange>> changes = new HashMap<>();
64+
private final Map<UUID, BukkitTask> tasks = new HashMap<>();
65+
66+
public ModSettingsTest() {
67+
EventBus.getBus().register(this);
68+
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
69+
}
70+
71+
@EventHandler
72+
private void onPlayerQuit(PlayerQuitEvent event) {
73+
UUID uuid = event.getPlayer().getUniqueId();
74+
BukkitTask task = this.tasks.remove(uuid);
75+
76+
if (task != null) {
77+
task.cancel();
78+
}
79+
80+
this.changes.remove(uuid);
81+
}
82+
83+
@Listen
84+
private void onApolloPlayerHandshake(ApolloPlayerHandshakeEvent event) {
85+
ApolloPlayer player = event.getPlayer();
86+
BukkitTask task = Bukkit.getScheduler().runTaskLater(ApolloExamplePlugin.getInstance(), () -> {
87+
this.changeOptions(player);
88+
this.checkOptions(player);
89+
}, 20 * 3);
90+
91+
this.tasks.put(player.getUniqueId(), task);
92+
}
93+
94+
private void checkOptions(ApolloPlayer player) {
95+
BukkitTask task = Bukkit.getScheduler().runTaskLater(ApolloExamplePlugin.getInstance(), () -> {
96+
List<OptionChange> changes = this.changes.getOrDefault(player.getUniqueId(), new ArrayList<>());
97+
if (!changes.isEmpty()) {
98+
Component message = PREFIX
99+
.append(Component.text("Failed to receive "))
100+
.append(Component.text(changes.size(), NamedTextColor.RED))
101+
.append(Component.text(" option changes!", NamedTextColor.YELLOW))
102+
.append(FAILURE);
103+
104+
player.sendMessage(message);
105+
this.changes.remove(player.getUniqueId());
106+
}
107+
}, 20 * 10);
108+
109+
this.tasks.put(player.getUniqueId(), task);
110+
}
111+
112+
@Listen
113+
private void onApolloUpdateModOption(ApolloUpdateModOptionEvent event) {
114+
ApolloPlayer player = event.getPlayer();
115+
Option<?, ?, ?> option = event.getOption();
116+
Object value = event.getValue();
117+
118+
List<OptionChange> changes = this.changes.getOrDefault(player.getUniqueId(), new ArrayList<>());
119+
boolean removed = false;
120+
121+
Predicate<OptionChange> isEventOption = change -> change.getKey().equals(option.getKey())
122+
&& (change.getValue() == null || Objects.equals(change.getValue(), value));
123+
124+
Iterator<OptionChange> iterator = changes.iterator();
125+
while (iterator.hasNext()) {
126+
OptionChange change = iterator.next();
127+
if (isEventOption.test(change)) {
128+
Object statusApiValue = this.modSettingModule.getStatus(player, option);
129+
130+
if (!Objects.equals(statusApiValue, value)) {
131+
Component message = PREFIX
132+
.append(Component.text("Status API returned wrong value \""))
133+
.append(Component.text(statusApiValue.toString()))
134+
.append(Component.text("\", expected \""))
135+
.append(Component.text(value == null ? "null" : value.toString()))
136+
.append(Component.text("\" for option \""))
137+
.append(Component.text(option.getKey()))
138+
.append(Component.text("\"!"))
139+
.append(FAILURE);
140+
141+
player.sendMessage(message);
142+
}
143+
144+
iterator.remove();
145+
removed = true;
146+
}
147+
}
148+
149+
if (removed && changes.isEmpty()) {
150+
Component message = PREFIX
151+
.append(Component.text("Mod Setting Module tests passed!"))
152+
.append(SUCCESS);
153+
154+
player.sendMessage(message);
155+
}
156+
157+
}
158+
159+
private void changeOptions(ApolloPlayer player) {
160+
this.changeOption(player, ModWaypoints.ENABLED, false);
161+
this.changeOption(player, ModWaypoints.DEATH_WAYPOINT, false);
162+
163+
this.changeOption(player, ModWaypoints.ENABLED, null);
164+
this.changeOption(player, ModWaypoints.DEATH_WAYPOINT, null);
165+
}
166+
167+
private void changeOption(ApolloPlayer player, Option<?, ?, ?> option, Object value) {
168+
this.modSettingModule.getOptions().set(option, value);
169+
170+
this.changes.computeIfAbsent(player.getUniqueId(), k -> new ArrayList<>())
171+
.add(new OptionChange(option.getKey(), value));
172+
}
173+
174+
@Getter
175+
@ToString
176+
@RequiredArgsConstructor
177+
private static class OptionChange {
178+
179+
private final String key;
180+
private final Object value;
181+
182+
}
183+
184+
}

example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/debug/payload/PayloadListener.java renamed to example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/debug/impl/PayloadTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
package com.lunarclient.apollo.example.api.debug.payload;
24+
package com.lunarclient.apollo.example.api.debug.impl;
2525

2626
import com.lunarclient.apollo.Apollo;
2727
import com.lunarclient.apollo.event.ApolloListener;
@@ -43,12 +43,12 @@
4343
import org.bukkit.event.player.PlayerQuitEvent;
4444
import org.bukkit.scheduler.BukkitTask;
4545

46-
public class PayloadListener implements Listener, ApolloListener {
46+
public class PayloadTest implements Listener, ApolloListener {
4747

4848
private final Set<UUID> handshakeUuids;
4949
private final Map<UUID, BukkitTask> tasks;
5050

51-
public PayloadListener() {
51+
public PayloadTest() {
5252
this.handshakeUuids = new HashSet<>();
5353
this.tasks = new HashMap<>();
5454

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import com.lunarclient.apollo.module.limb.LimbModule;
5555
import com.lunarclient.apollo.module.limb.LimbModuleImpl;
5656
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
57-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
57+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
5858
import com.lunarclient.apollo.module.nametag.NametagModule;
5959
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
6060
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -143,7 +143,7 @@ public void onEnable() {
143143
.addModule(HologramModule.class, new HologramModuleImpl())
144144
.addModule(InventoryModule.class)
145145
.addModule(LimbModule.class, new LimbModuleImpl())
146-
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
146+
.addModule(ModSettingModule.class, new ModSettingModuleImpl())
147147
.addModule(NametagModule.class, new NametagModuleImpl())
148148
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
149149
.addModule(NotificationModule.class, new NotificationModuleImpl())

platform/bungee/src/main/java/com/lunarclient/apollo/ApolloBungeePlatform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.lunarclient.apollo.module.limb.LimbModule;
5050
import com.lunarclient.apollo.module.limb.LimbModuleImpl;
5151
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
52-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
52+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
5353
import com.lunarclient.apollo.module.nametag.NametagModule;
5454
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5555
import com.lunarclient.apollo.module.notification.NotificationModule;
@@ -124,7 +124,7 @@ public void onEnable() {
124124
.addModule(EntityModule.class, new EntityModuleImpl())
125125
.addModule(HologramModule.class, new HologramModuleImpl())
126126
.addModule(LimbModule.class, new LimbModuleImpl())
127-
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
127+
.addModule(ModSettingModule.class, new ModSettingModuleImpl())
128128
.addModule(NametagModule.class, new NametagModuleImpl())
129129
.addModule(NotificationModule.class, new NotificationModuleImpl())
130130
.addModule(RichPresenceModule.class, new RichPresenceModuleImpl())

platform/folia/src/main/java/com/lunarclient/apollo/ApolloFoliaPlatform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import com.lunarclient.apollo.module.limb.LimbModule;
5252
import com.lunarclient.apollo.module.limb.LimbModuleImpl;
5353
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
54-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
54+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
5555
import com.lunarclient.apollo.module.nametag.NametagModule;
5656
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5757
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -131,7 +131,7 @@ public void onEnable() {
131131
.addModule(GlowModule.class, new GlowModuleImpl())
132132
.addModule(HologramModule.class, new HologramModuleImpl())
133133
.addModule(LimbModule.class, new LimbModuleImpl())
134-
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
134+
.addModule(ModSettingModule.class, new ModSettingModuleImpl())
135135
.addModule(NametagModule.class, new NametagModuleImpl())
136136
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
137137
.addModule(NotificationModule.class, new NotificationModuleImpl())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import com.lunarclient.apollo.module.limb.LimbModule;
5454
import com.lunarclient.apollo.module.limb.LimbModuleImpl;
5555
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
56-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
56+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
5757
import com.lunarclient.apollo.module.nametag.NametagModule;
5858
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5959
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -165,7 +165,7 @@ public static void init(ApolloMinestomProperties properties) {
165165
.addModule(HologramModule.class, new HologramModuleImpl())
166166
.addModule(InventoryModule.class)
167167
.addModule(LimbModule.class, new LimbModuleImpl())
168-
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
168+
.addModule(ModSettingModule.class, new ModSettingModuleImpl())
169169
.addModule(NametagModule.class, new NametagModuleImpl())
170170
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
171171
.addModule(NotificationModule.class, new NotificationModuleImpl())

platform/velocity/src/main/java/com/lunarclient/apollo/ApolloVelocityPlatform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.lunarclient.apollo.module.limb.LimbModule;
5050
import com.lunarclient.apollo.module.limb.LimbModuleImpl;
5151
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
52-
import com.lunarclient.apollo.module.modsettings.ModSettingsModuleImpl;
52+
import com.lunarclient.apollo.module.modsettings.ModSettingModuleImpl;
5353
import com.lunarclient.apollo.module.nametag.NametagModule;
5454
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5555
import com.lunarclient.apollo.module.notification.NotificationModule;
@@ -189,7 +189,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
189189
.addModule(EntityModule.class, new EntityModuleImpl())
190190
.addModule(HologramModule.class, new HologramModuleImpl())
191191
.addModule(LimbModule.class, new LimbModuleImpl())
192-
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
192+
.addModule(ModSettingModule.class, new ModSettingModuleImpl())
193193
.addModule(NametagModule.class, new NametagModuleImpl())
194194
.addModule(NotificationModule.class, new NotificationModuleImpl())
195195
.addModule(RichPresenceModule.class, new RichPresenceModuleImpl())

0 commit comments

Comments
 (0)