Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.event.modsetting;

import com.lunarclient.apollo.event.Event;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.player.ApolloPlayer;
import lombok.Value;
import org.jetbrains.annotations.Nullable;

/**
* Represents an event that is fired when an option is updated.
*
* @since 1.2.1
*/
@Value
public class ApolloUpdateModOptionEvent implements Event {

/**
* The {@link ApolloPlayer} that the option was updated for.
*
* @return the player
* @since 1.2.1
*/
ApolloPlayer player;

/**
* The {@link Option} that was updated.
*
* @return the option
* @since 1.2.1
*/
Option<?, ?, ?> option;

/**
* The new value of the option.
*
* @return the new value
* @since 1.2.1
*/
@Nullable Object value;

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,17 @@ protected ApolloModule() {
* @since 1.0.0
*/
protected void registerOptions(Option<?, ?, ?>... options) {
this.optionKeys.addAll(Arrays.asList(options));
this.registerOptions(Arrays.asList(options));
}

/**
* Registers {@link Option}s for this module.
*
* @param options the option keys
* @since 1.2.1
*/
protected void registerOptions(Collection<Option<?, ?, ?>> options) {
this.optionKeys.addAll(options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,25 @@
package com.lunarclient.apollo.module.modsetting;

import com.lunarclient.apollo.ApolloPlatform;
import com.lunarclient.apollo.mods.Mods;
import com.lunarclient.apollo.module.ApolloModule;
import com.lunarclient.apollo.module.ModuleDefinition;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.player.ApolloPlayer;
import com.lunarclient.apollo.util.ConfigTarget;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import lombok.NonNull;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Represents the mod settings module.
*
* @since 1.0.0
*/
@ApiStatus.NonExtendable
@ModuleDefinition(id = "mod_setting", name = "Mod Setting", configTarget = ConfigTarget.MOD_SETTINGS)
public final class ModSettingModule extends ApolloModule {

ModSettingModule() {
this.registerModOptions();
}
public abstract class ModSettingModule extends ApolloModule {

@Override
public Collection<ApolloPlatform.Kind> getSupportedPlatforms() {
Expand All @@ -55,20 +54,16 @@ public boolean isClientNotify() {
return true;
}

private void registerModOptions() {
for (Class<?> mod : Mods.ALL_MODS) {
Field[] fields = mod.getDeclaredFields();

for (Field field : fields) {
try {
field.setAccessible(true);
Option<?, ?, ?> option = (Option<?, ?, ?>) field.get(Option.class);
this.registerOptions(option);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
/**
* Gets the value of the specified {@link Option} for the {@link ApolloPlayer}.
*
* @param player the apollo player
* @param option the option
* @param <T> the value type
* @param <C> the option type
* @return the value of the option
* @since 1.2.1
*/
public abstract <T, C extends Option<T, ?, ?>> T getStatus(@NotNull ApolloPlayer player, @NonNull C option);

}
11 changes: 11 additions & 0 deletions api/src/main/java/com/lunarclient/apollo/option/EmptyOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.lunarclient.apollo.player.ApolloPlayer;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import lombok.AccessLevel;
Expand Down Expand Up @@ -97,6 +98,16 @@ public <T> void replace(ApolloPlayer player, Option<?, ?, ?> option, BiFunction<

}

@Override
public <T, C extends Option<T, ?, ?>> void register(C option) {

}

@Override
public Map<String, Option<?, ?, ?>> getRegistry() {
return Collections.emptyMap();
}

@Override
public @NonNull Iterator<Option<?, ?, ?>> iterator() {
return Collections.emptyIterator();
Expand Down
12 changes: 11 additions & 1 deletion api/src/main/java/com/lunarclient/apollo/option/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@Getter
@EqualsAndHashCode
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public abstract class Option<V, M extends OptionBuilder<V, M, I>, I extends Option<V, M, I>> {
public abstract class Option<V, M extends OptionBuilder<V, M, I>, I extends Option<V, M, I>> implements Cloneable {

/**
* Returns a new {@link SimpleOption.SimpleOptionBuilder}.
Expand Down Expand Up @@ -149,4 +149,14 @@ public String getKey() {
return String.join(".", this.getPath());
}

@Override
@SuppressWarnings("unchecked")
public Option<V, M, I> clone() {
try {
return (Option<V, M, I>) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}

}
19 changes: 19 additions & 0 deletions api/src/main/java/com/lunarclient/apollo/option/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo.option;

import com.lunarclient.apollo.player.ApolloPlayer;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -188,4 +189,22 @@ static Options empty() {
*/
<T> void replace(ApolloPlayer player, Option<?, ?, ?> option, BiFunction<Option<?, ?, ?>, T, T> remappingFunction);

/**
* Registers the provided {@code C} option for the {@link Options} implementation.
*
* @param option the option
* @param <T> the value type
* @param <C> the option type
* @since 1.2.1
*/
<T, C extends Option<T, ?, ?>> void register(C option);

/**
* Returns the internal registry containing all registered {@link Option} instances, mapped by their keys.
*
* @return the map of option keys to their corresponding {@link Option} instances
* @since 1.2.1
*/
Map<String, Option<?, ?, ?>> getRegistry();

}
21 changes: 13 additions & 8 deletions common/src/main/java/com/lunarclient/apollo/ApolloManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lunarclient.apollo.api.ApolloHttpManager;
import com.lunarclient.apollo.mods.ApolloModsManager;
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
import com.lunarclient.apollo.network.ApolloNetworkManager;
import com.lunarclient.apollo.option.ConfigOptions;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.option.Options;
import com.lunarclient.apollo.option.config.CommonSerializers;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.roundtrip.ApolloRoundtripManager;
Expand All @@ -39,9 +41,7 @@
import com.lunarclient.apollo.version.ApolloVersionManager;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Collection;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -67,15 +67,14 @@ public final class ApolloManager {
*/
public static final Gson GSON = new GsonBuilder().create();

private static final List<Option<?, ?, ?>> optionKeys = new LinkedList<>();

private static ApolloPlatform platform;

@Getter private static ApolloRoundtripManager roundtripManager;
@Getter private static ApolloHttpManager httpManager;
@Getter private static ApolloNetworkManager networkManager;
@Getter private static ApolloVersionManager versionManager;
@Getter private static ApolloStatsManager statsManager;
@Getter private static ApolloModsManager modsManager;
@Getter @Setter private static ApolloMetadataManager metadataManager;

@Getter private static Path configPath;
Expand Down Expand Up @@ -106,6 +105,7 @@ public static void bootstrap(ApolloPlatform platform) {
ApolloManager.networkManager = new ApolloNetworkManager();
ApolloManager.versionManager = new ApolloVersionManager();
ApolloManager.statsManager = new ApolloStatsManager();
ApolloManager.modsManager = new ApolloModsManager();

new CommonSerializers();

Expand All @@ -124,7 +124,10 @@ public static void bootstrap(ApolloPlatform platform) {
* @since 1.0.0
*/
public static void registerOptions(Option<?, ?, ?>... options) {
ApolloManager.optionKeys.addAll(Arrays.asList(options));
Options platformOptions = Apollo.getPlatform().getOptions();
for (Option<?, ?, ?> option : options) {
platformOptions.register(option);
}
}

/**
Expand All @@ -147,8 +150,9 @@ public static void loadConfiguration() throws Throwable {
config.reset();
}

Collection<Option<?, ?, ?>> platformOptions = Apollo.getPlatform().getOptions().getRegistry().values();
ApolloConfig generalSettings = ApolloConfig.compute(ApolloManager.configPath, ConfigTarget.GENERAL_SETTINGS);
ConfigOptions.loadOptions(ApolloManager.platform.getOptions(), generalSettings.node(), ApolloManager.optionKeys);
ConfigOptions.loadOptions(ApolloManager.platform.getOptions(), generalSettings.node(), platformOptions);
}

/**
Expand All @@ -157,8 +161,9 @@ public static void loadConfiguration() throws Throwable {
* @since 1.0.0
*/
public static void saveConfiguration() throws Throwable {
Collection<Option<?, ?, ?>> platformOptions = Apollo.getPlatform().getOptions().getRegistry().values();
ApolloConfig generalSettings = ApolloConfig.compute(ApolloManager.configPath, ConfigTarget.GENERAL_SETTINGS);
ConfigOptions.saveOptions(ApolloManager.platform.getOptions(), generalSettings.node(), ApolloManager.optionKeys);
ConfigOptions.saveOptions(ApolloManager.platform.getOptions(), generalSettings.node(), platformOptions);

((ApolloModuleManagerImpl) Apollo.getModuleManager()).saveConfiguration();

Expand Down
Loading