Skip to content

Commit 8f7b48f

Browse files
committed
Remove mod options loading logic from auto gen
1 parent 4e246da commit 8f7b48f

File tree

11 files changed

+158
-64
lines changed

11 files changed

+158
-64
lines changed

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

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,8 @@
110110
import com.lunarclient.apollo.mods.impl.ModWeatherChanger;
111111
import com.lunarclient.apollo.mods.impl.ModWorldeditCui;
112112
import com.lunarclient.apollo.mods.impl.ModZoom;
113-
import com.lunarclient.apollo.option.Option;
114-
import java.lang.reflect.Field;
115113
import java.util.Arrays;
116-
import java.util.LinkedHashMap;
117114
import java.util.List;
118-
import java.util.Map;
119115

120116
/**
121117
* Mod container.
@@ -124,8 +120,6 @@
124120
*/
125121
public final class Mods {
126122

127-
private static final Map<String, Option<?, ?, ?>> OPTIONS = Mods.collectModOptions();
128-
129123
/**
130124
* List of all current mod classes.
131125
*
@@ -221,36 +215,6 @@ public final class Mods {
221215
ModRadio.class
222216
);
223217

224-
/**
225-
* Returns a new map containing all mod options.
226-
*
227-
* @return a linked hash map of mod option keys to options
228-
* @since 1.2.1
229-
*/
230-
public static Map<String, Option<?, ?, ?>> getOptions() {
231-
return new LinkedHashMap<>(OPTIONS);
232-
}
233-
234-
private static Map<String, Option<?, ?, ?>> collectModOptions() {
235-
Map<String, Option<?, ?, ?>> options = new LinkedHashMap<>();
236-
237-
for (Class<?> mod : Mods.ALL_MODS) {
238-
Field[] fields = mod.getDeclaredFields();
239-
240-
for (Field field : fields) {
241-
try {
242-
field.setAccessible(true);
243-
Option<?, ?, ?> option = (Option<?, ?, ?>) field.get(Option.class);
244-
options.put(option.getKey(), option);
245-
} catch (IllegalAccessException e) {
246-
e.printStackTrace();
247-
}
248-
}
249-
}
250-
251-
return options;
252-
}
253-
254218
private Mods() {
255219
}
256220

api/src/main/java/com/lunarclient/apollo/module/modsetting/ModSettingModule.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@
2424
package com.lunarclient.apollo.module.modsetting;
2525

2626
import com.lunarclient.apollo.ApolloPlatform;
27-
import com.lunarclient.apollo.mods.Mods;
2827
import com.lunarclient.apollo.module.ApolloModule;
2928
import com.lunarclient.apollo.module.ModuleDefinition;
3029
import com.lunarclient.apollo.util.ConfigTarget;
3130
import java.util.Arrays;
3231
import java.util.Collection;
32+
import org.jetbrains.annotations.ApiStatus;
3333

3434
/**
3535
* Represents the mod settings module.
3636
*
3737
* @since 1.0.0
3838
*/
39+
@ApiStatus.NonExtendable
3940
@ModuleDefinition(id = "mod_setting", name = "Mod Setting", configTarget = ConfigTarget.MOD_SETTINGS)
40-
public final class ModSettingModule extends ApolloModule {
41-
42-
ModSettingModule() {
43-
this.registerOptions(Mods.getOptions().values());
44-
}
41+
public abstract class ModSettingModule extends ApolloModule {
4542

4643
@Override
4744
public Collection<ApolloPlatform.Kind> getSupportedPlatforms() {

api/src/main/java/com/lunarclient/apollo/option/Option.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@Getter
4444
@EqualsAndHashCode
4545
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
46-
public abstract class Option<V, M extends OptionBuilder<V, M, I>, I extends Option<V, M, I>> {
46+
public abstract class Option<V, M extends OptionBuilder<V, M, I>, I extends Option<V, M, I>> implements Cloneable {
4747

4848
/**
4949
* Returns a new {@link SimpleOption.SimpleOptionBuilder}.
@@ -149,4 +149,14 @@ public String getKey() {
149149
return String.join(".", this.getPath());
150150
}
151151

152+
@Override
153+
@SuppressWarnings("unchecked")
154+
public Option<V, M, I> clone() {
155+
try {
156+
return (Option<V, M, I>) super.clone();
157+
} catch (CloneNotSupportedException e) {
158+
throw new AssertionError(e);
159+
}
160+
}
161+
152162
}

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@
2929
import com.lunarclient.apollo.event.Listen;
3030
import com.lunarclient.apollo.option.Option;
3131
import com.lunarclient.apollo.option.Options;
32-
import com.lunarclient.apollo.option.OptionsImpl;
3332
import com.lunarclient.apollo.option.StatusOptionsImpl;
33+
import java.lang.reflect.Field;
34+
import java.util.ArrayList;
35+
import java.util.LinkedHashMap;
36+
import java.util.List;
3437
import java.util.Map;
3538
import lombok.Getter;
39+
import lombok.RequiredArgsConstructor;
3640

3741
/**
3842
* Manages Apollo mods for players.
3943
*
4044
* @since 1.2.1
4145
*/
46+
@Getter
4247
public final class ApolloModsManager implements ApolloListener {
4348

44-
@Getter private final Options playerOptions;
49+
private final Container container;
50+
private final Options playerOptions;
4551

4652
/**
4753
* Constructs the {@link ApolloModsManager}.
@@ -51,16 +57,78 @@ public final class ApolloModsManager implements ApolloListener {
5157
public ApolloModsManager() {
5258
EventBus.getBus().register(this);
5359

60+
this.container = ApolloModsManager.loadModOptions();
5461
this.playerOptions = new StatusOptionsImpl();
5562

56-
for (Option<?, ?, ?> option : Mods.getOptions().values()) {
63+
for (Option<?, ?, ?> option : this.container.getModStatusOptions().values()) {
5764
this.playerOptions.set(option, option.getDefaultValue());
5865
}
66+
}
67+
68+
/**
69+
* Load all options from auto-generated mod classes into a {@link Container} object.
70+
*
71+
* @return the container
72+
* @since 1.2.1
73+
*/
74+
public static Container loadModOptions() {
75+
Map<String, Option<?, ?, ?>> modStatusOptions = new LinkedHashMap<>();
76+
List<Option<?, ?, ?>> modSettingsOptions = new ArrayList<>();
77+
78+
try {
79+
Field defaultValueField = Option.class.getDeclaredField("defaultValue");
80+
defaultValueField.setAccessible(true);
81+
82+
for (Class<?> mod : Mods.ALL_MODS) {
83+
Field[] fields = mod.getDeclaredFields();
84+
85+
for (Field field : fields) {
86+
field.setAccessible(true);
87+
Option<?, ?, ?> option = (Option<?, ?, ?>) field.get(null);
88+
89+
if (option == null) {
90+
continue;
91+
}
92+
93+
modStatusOptions.put(option.getKey(), option);
5994

60-
OptionsImpl impl = ((OptionsImpl) this.playerOptions);
61-
for (Map.Entry<Option<?, ?, ?>, Object> entry : impl.getOptions().entrySet()) {
62-
System.out.println(entry.getKey().getKey() + ": " + entry.getValue());
95+
Option<?, ?, ?> copy = option.clone();
96+
defaultValueField.set(copy, null);
97+
modSettingsOptions.add(copy);
98+
}
99+
}
100+
} catch (Exception e) {
101+
e.printStackTrace();
63102
}
103+
104+
return new Container(modStatusOptions, modSettingsOptions);
105+
}
106+
107+
/**
108+
* Holds the loaded options.
109+
*
110+
* @since 1.2.1
111+
*/
112+
@Getter
113+
@RequiredArgsConstructor
114+
public static class Container {
115+
116+
/**
117+
* Returns all registered mod options with default values.
118+
*
119+
* @return the map of option key to option
120+
* @since 1.2.1
121+
*/
122+
private final Map<String, Option<?, ?, ?>> modStatusOptions;
123+
124+
/**
125+
* Returns all registered mod options with default values removed.
126+
*
127+
* @return the list of options without default values
128+
* @since 1.2.1
129+
*/
130+
private final List<Option<?, ?, ?>> modSettingsOptions;
131+
64132
}
65133

66134
@Listen
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.module.modsettings;
25+
26+
import com.lunarclient.apollo.ApolloManager;
27+
import com.lunarclient.apollo.module.modsetting.ModSettingModule;
28+
29+
/**
30+
* Provides the mod settings module.
31+
*
32+
* @since 1.2.1
33+
*/
34+
public final class ModSettingsModuleImpl extends ModSettingModule {
35+
36+
/**
37+
* Creates a new instance of {@link ModSettingsModuleImpl}.
38+
*
39+
* @since 1.2.1
40+
*/
41+
public ModSettingsModuleImpl() {
42+
super();
43+
this.registerOptions(ApolloManager.getModsManager().getContainer().getModSettingsOptions());
44+
}
45+
46+
}

common/src/main/java/com/lunarclient/apollo/option/config/CommonSerializers.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,31 @@ public Color deserialize(Type type, ConfigurationNode node) throws Serialization
5959
stringValue = stringValue.substring(1);
6060
}
6161

62-
if (stringValue.length() != 8) {
63-
throw new NumberFormatException("Invalid color string length: " + stringValue);
62+
if (stringValue.length() == 6) {
63+
return new Color(Integer.parseInt(stringValue, 16));
6464
}
6565

66-
long rgba = Long.parseLong(stringValue, 16);
67-
int alpha = (int) ((rgba >> 24) & 0xFF);
68-
int red = (int) ((rgba >> 16) & 0xFF);
69-
int green = (int) ((rgba >> 8) & 0xFF);
70-
int blue = (int) (rgba & 0xFF);
66+
if (stringValue.length() == 8) {
67+
long rgba = Long.parseLong(stringValue, 16);
68+
int alpha = (int) ((rgba >> 24) & 0xFF);
69+
int red = (int) ((rgba >> 16) & 0xFF);
70+
int green = (int) ((rgba >> 8) & 0xFF);
71+
int blue = (int) (rgba & 0xFF);
7172

72-
return new Color(red, green, blue, alpha);
73+
return new Color(red, green, blue, alpha);
74+
}
75+
76+
throw new NumberFormatException("Invalid color string length: " + stringValue);
7377
}
7478

7579
@Override
7680
public void serialize(Type type, @Nullable Color color, ConfigurationNode node) throws SerializationException {
7781
if (color == null) {
78-
node.set("#FFFFFF");
82+
node.set(null);
7983
return;
8084
}
8185

82-
node.set(String.format("#%06X", (0xFFFFFF & color.getRGB())));
86+
node.set(String.format("#%08X", color.getRGB()));
8387
}
8488
}
8589

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +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;
5758
import com.lunarclient.apollo.module.nametag.NametagModule;
5859
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5960
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -141,7 +142,7 @@ public void onEnable() {
141142
.addModule(HologramModule.class, new HologramModuleImpl())
142143
.addModule(InventoryModule.class)
143144
.addModule(LimbModule.class, new LimbModuleImpl())
144-
.addModule(ModSettingModule.class)
145+
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
145146
.addModule(NametagModule.class, new NametagModuleImpl())
146147
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
147148
.addModule(NotificationModule.class, new NotificationModuleImpl())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +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;
5253
import com.lunarclient.apollo.module.nametag.NametagModule;
5354
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5455
import com.lunarclient.apollo.module.notification.NotificationModule;
@@ -122,7 +123,7 @@ public void onEnable() {
122123
.addModule(EntityModule.class, new EntityModuleImpl())
123124
.addModule(HologramModule.class, new HologramModuleImpl())
124125
.addModule(LimbModule.class, new LimbModuleImpl())
125-
.addModule(ModSettingModule.class)
126+
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
126127
.addModule(NametagModule.class, new NametagModuleImpl())
127128
.addModule(NotificationModule.class, new NotificationModuleImpl())
128129
.addModule(RichPresenceModule.class, new RichPresenceModuleImpl())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +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;
5455
import com.lunarclient.apollo.module.nametag.NametagModule;
5556
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5657
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -129,7 +130,7 @@ public void onEnable() {
129130
.addModule(GlowModule.class, new GlowModuleImpl())
130131
.addModule(HologramModule.class, new HologramModuleImpl())
131132
.addModule(LimbModule.class, new LimbModuleImpl())
132-
.addModule(ModSettingModule.class)
133+
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
133134
.addModule(NametagModule.class, new NametagModuleImpl())
134135
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
135136
.addModule(NotificationModule.class, new NotificationModuleImpl())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +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;
5657
import com.lunarclient.apollo.module.nametag.NametagModule;
5758
import com.lunarclient.apollo.module.nametag.NametagModuleImpl;
5859
import com.lunarclient.apollo.module.nickhider.NickHiderModule;
@@ -163,7 +164,7 @@ public static void init(ApolloMinestomProperties properties) {
163164
.addModule(HologramModule.class, new HologramModuleImpl())
164165
.addModule(InventoryModule.class)
165166
.addModule(LimbModule.class, new LimbModuleImpl())
166-
.addModule(ModSettingModule.class)
167+
.addModule(ModSettingModule.class, new ModSettingsModuleImpl())
167168
.addModule(NametagModule.class, new NametagModuleImpl())
168169
.addModule(NickHiderModule.class, new NickHiderModuleImpl())
169170
.addModule(NotificationModule.class, new NotificationModuleImpl())

0 commit comments

Comments
 (0)