Skip to content

Commit

Permalink
Support extra options (#31)
Browse files Browse the repository at this point in the history
* add an options container to ApolloPlatform

* Add registerOptions method

* give saveOptions the list of option keys

---------

Co-authored-by: ItsNature <[email protected]>
  • Loading branch information
vectrixdevelops and ItsNature authored Sep 21, 2023
1 parent 4b6d9ec commit a86119b
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 48 deletions.
9 changes: 9 additions & 0 deletions api/src/main/java/com/lunarclient/apollo/ApolloPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.lunarclient.apollo;

import com.lunarclient.apollo.option.Options;
import org.jetbrains.annotations.ApiStatus;

/**
Expand All @@ -41,6 +42,14 @@ public interface ApolloPlatform {
*/
Kind getKind();

/**
* Returns the platform options that don't belong to a specific module.
*
* @return the platform options
* @since 1.0.0
*/
Options getOptions();

/**
* Represents the kind of server a platform is.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import com.lunarclient.apollo.module.vignette.VignetteModuleImpl;
import com.lunarclient.apollo.module.waypoint.WaypointModule;
import com.lunarclient.apollo.module.waypoint.WaypointModuleImpl;
import com.lunarclient.apollo.option.Options;
import com.lunarclient.apollo.option.OptionsImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import com.lunarclient.apollo.wrapper.BukkitApolloPlayer;
Expand All @@ -85,6 +87,8 @@ public final class ApolloBukkitPlatform extends JavaPlugin implements ApolloPlat

@Getter private static ApolloBukkitPlatform instance;

@Getter private final Options options = new OptionsImpl(null);

@Override
public void onEnable() {
ApolloBukkitPlatform.instance = this;
Expand Down Expand Up @@ -113,16 +117,14 @@ public void onEnable() {
.addModule(WaypointModule.class, new WaypointModuleImpl());

ApolloManager.loadConfiguration(this.getDataFolder().toPath());

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

Messenger messenger = this.getServer().getMessenger();
messenger.registerOutgoingPluginChannel(this, ApolloManager.PLUGIN_MESSAGE_CHANNEL);
messenger.registerIncomingPluginChannel(this, ApolloManager.PLUGIN_MESSAGE_CHANNEL,
(channel, player, bytes) -> this.handlePacket(player, bytes)
);

ApolloManager.saveConfiguration();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.google.common.base.Charsets;
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
import com.lunarclient.apollo.option.Options;
import com.lunarclient.apollo.option.OptionsImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.wrapper.BungeeApolloPlayer;
import lombok.Getter;
Expand All @@ -45,20 +47,21 @@ public final class ApolloBungeePlatform extends Plugin implements ApolloPlatform

@Getter private static ApolloBungeePlatform instance;

@Getter private final Options options = new OptionsImpl(null);

@Override
public void onEnable() {
ApolloBungeePlatform.instance = this;

this.getProxy().getPluginManager().registerListener(this, this);

ApolloManager.bootstrap(this);
ApolloManager.loadConfiguration(this.getDataFolder().toPath());

ApolloManager.loadConfiguration(this.getDataFolder().toPath());
((ApolloModuleManagerImpl) Apollo.getModuleManager()).enableModules();
ApolloManager.saveConfiguration();

this.getProxy().registerChannel(ApolloManager.PLUGIN_MESSAGE_CHANNEL);

ApolloManager.saveConfiguration();
}

@Override
Expand Down
30 changes: 30 additions & 0 deletions common/src/main/java/com/lunarclient/apollo/ApolloManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@

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.config.Serializers;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.roundtrip.ApolloRoundtripManager;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import lombok.Getter;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.yaml.NodeStyle;
Expand All @@ -47,6 +52,14 @@ public final class ApolloManager {
*/
public static final String PLUGIN_MESSAGE_CHANNEL = "lunar:apollo";

/**
* The plugin root module identifier for Apollos general options.
*/
public static final String PLUGIN_ROOT_MODULE = "apollo";

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

@Getter private static ApolloPlatform platform;
@Getter private static ApolloNetworkManager networkManager;
@Getter private static CommentedConfigurationNode configurationNode;

Expand Down Expand Up @@ -74,12 +87,25 @@ public static void bootstrap(ApolloPlatform platform) {
);

ApolloManager.networkManager = new ApolloNetworkManager();

ApolloManager.platform = platform;
} catch (Throwable throwable) {
throw new RuntimeException("Unable to bootstrap Apollo!", throwable);
}

ApolloManager.bootstrapped = true;
}

/**
* Registers {@link Option}s for Apollo.
*
* @param options the option keys
* @since 1.0.0
*/
public static void registerOptions(Option<?, ?, ?>... options) {
ApolloManager.optionKeys.addAll(Arrays.asList(options));
}

/**
* Loads the configuration from the given {@link Path}.
*
Expand All @@ -97,6 +123,8 @@ public static void loadConfiguration(Path path) {
}

ApolloManager.configurationNode = ApolloManager.configurationLoader.load();

ConfigOptions.loadOptions(ApolloManager.platform.getOptions(), ApolloManager.configurationNode, ApolloManager.optionKeys);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Expand All @@ -113,6 +141,8 @@ public static void saveConfiguration() {
return;
}

ConfigOptions.saveOptions(ApolloManager.platform.getOptions(), ApolloManager.configurationNode, ApolloManager.optionKeys);

CommentedConfigurationNode modules = ApolloManager.configurationNode.node("modules");

((ApolloModuleManagerImpl) Apollo.getModuleManager()).saveConfiguration(modules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.event.EventBus;
import com.lunarclient.apollo.option.ConfigOptions;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.option.Options;
import com.lunarclient.apollo.option.OptionsImpl;
import io.leangen.geantyref.TypeToken;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -146,28 +146,12 @@ public <T extends ApolloModule> ApolloModuleManagerImpl addModule(Class<T> modul
* @param node the configuration node
* @since 1.0.0
*/
@SuppressWarnings("unchecked")
public void saveConfiguration(CommentedConfigurationNode node) {
for (ApolloModule module : this.modules.values()) {
CommentedConfigurationNode moduleNode = node.node(module.getId().toLowerCase(Locale.ENGLISH));

Options optionsContainer = module.getOptions();
for (Option<?, ?, ?> option : module.getOptionKeys()) {
CommentedConfigurationNode optionNode = moduleNode.node((Object[]) option.getPath());
if (optionNode == null) {
continue;
}

try {
if (option.getComment() != null) {
optionNode.comment(option.getComment());
}

optionNode.set((TypeToken<Object>) option.getTypeToken(), optionsContainer.get(option));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
ConfigOptions.saveOptions(optionsContainer, moduleNode, module.getOptionKeys());
}
}

Expand All @@ -179,19 +163,7 @@ private void loadConfiguration(ApolloModule module, CommentedConfigurationNode n
}

Options optionsContainer = module.getOptions();
for (Option<?, ?, ?> option : options) {
CommentedConfigurationNode optionNode = moduleNode.node((Object[]) option.getPath());
if (optionNode.virtual()) {
continue;
}

try {
Object value = optionNode.get(option.getTypeToken());
optionsContainer.set(option, value);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
ConfigOptions.loadOptions(optionsContainer, moduleNode, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.lunarclient.apollo.network;

import com.google.protobuf.Value;
import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.configurable.v1.ConfigurableSettings;
import com.lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage;
import com.lunarclient.apollo.module.ApolloModule;
Expand All @@ -32,6 +33,7 @@
import com.lunarclient.apollo.option.OptionsImpl;
import com.lunarclient.apollo.player.AbstractApolloPlayer;
import com.lunarclient.apollo.player.ApolloPlayer;
import org.jetbrains.annotations.Nullable;

/**
* Utility class for sending options to the client.
Expand All @@ -40,10 +42,6 @@
*/
public final class NetworkOptions {

private NetworkOptions() {

}

/**
* Send a single option to a single player.
*
Expand All @@ -53,7 +51,7 @@ private NetworkOptions() {
* @param players the players to send the option to
* @since 1.0.0
*/
public static void sendOption(ApolloModule module,
public static void sendOption(@Nullable ApolloModule module,
Option<?, ?, ?> key,
Value value,
Iterable<ApolloPlayer> players) {
Expand Down Expand Up @@ -112,9 +110,13 @@ private static ConfigurableSettings.Builder moduleWithOptions(ApolloModule modul
return builder;
}

private static ConfigurableSettings.Builder module(ApolloModule module) {
private static ConfigurableSettings.Builder module(@Nullable ApolloModule module) {
return ConfigurableSettings.newBuilder()
.setApolloModule(module.getId())
.setEnable(module.isEnabled());
.setApolloModule(module == null ? ApolloManager.PLUGIN_ROOT_MODULE : module.getId())
.setEnable(module == null || module.isEnabled());
}

private NetworkOptions() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.option;

import io.leangen.geantyref.TypeToken;
import java.util.List;
import org.spongepowered.configurate.CommentedConfigurationNode;

/**
* Utility class for storing options into configuration.
*
* @since 1.0.0
*/
public final class ConfigOptions {

/**
* Loads the list of options from the configuration node into the option
* container.
*
* @param options the options container
* @param node the configuration node
* @param optionKeys the option keys
* @since 1.0.0
*/
public static void loadOptions(Options options, CommentedConfigurationNode node, List<Option<?, ?, ?>> optionKeys) {
for (Option<?, ?, ?> option : optionKeys) {
CommentedConfigurationNode optionNode = node.node((Object[]) option.getPath());
if (optionNode.virtual()) {
continue;
}

try {
Object value = optionNode.get(option.getTypeToken());
options.set(option, value);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

/**
* Saves the options to the configuration.
*
* @param options the options container
* @param node the configuration node
* @param optionKeys the option keys
* @since 1.0.0
*/
@SuppressWarnings("unchecked")
public static void saveOptions(Options options, CommentedConfigurationNode node, List<Option<?, ?, ?>> optionKeys) {
for (Option<?, ?, ?> option : optionKeys) {
CommentedConfigurationNode optionNode = node.node((Object[]) option.getPath());
if (optionNode == null) {
continue;
}

try {
if (option.getComment() != null) {
optionNode.comment(option.getComment());
}

optionNode.set((TypeToken<Object>) option.getTypeToken(), options.get(option));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

private ConfigOptions() {
}

}
Loading

0 comments on commit a86119b

Please sign in to comment.