Bat/src/main/java/cc/fascinated/bat/service/FeatureService.java

82 lines
2.4 KiB
Java
Raw Normal View History

2024-06-25 14:43:36 +00:00
package cc.fascinated.bat.service;
2024-06-25 16:20:19 +00:00
import cc.fascinated.bat.command.BatCommand;
2024-06-25 14:43:36 +00:00
import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.base.commands.server.feature.FeatureCommand;
2024-06-30 04:15:37 +00:00
import jakarta.annotation.PostConstruct;
2024-06-25 14:43:36 +00:00
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
2024-06-30 04:15:37 +00:00
import java.util.HashMap;
import java.util.Map;
2024-06-25 14:43:36 +00:00
/**
* @author Fascinated (fascinated7)
*/
2024-06-28 02:01:21 +00:00
@Service
@Getter
@Log4j2
2024-06-25 14:43:36 +00:00
@DependsOn("commandService")
public class FeatureService {
2024-06-30 04:15:37 +00:00
public static FeatureService INSTANCE;
private final ApplicationContext context;
private final CommandService commandService;
2024-06-25 14:43:36 +00:00
/**
* The registered features
*/
2024-06-30 04:15:37 +00:00
private final Map<String, Feature> features = new HashMap<>();
2024-06-25 14:43:36 +00:00
@Autowired
public FeatureService(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
2024-06-30 04:15:37 +00:00
this.context = context;
this.commandService = commandService;
INSTANCE = this;
}
@PostConstruct
public void init() {
2024-06-25 14:43:36 +00:00
context.getBeansOfType(Feature.class)
.values()
.forEach((feature) -> {
2024-06-30 04:15:37 +00:00
features.put(feature.getName().toLowerCase(), feature);
2024-06-25 14:43:36 +00:00
});
2024-06-25 16:20:19 +00:00
context.getBeansOfType(BatCommand.class)
.values()
.forEach((command) -> {
if (command.getFeature() == null) {
log.error("Command \"{}\" does not belong to a feature, not registering it...", command.getCommandInfo().name());
}
2024-06-25 16:20:19 +00:00
});
2024-06-30 04:15:37 +00:00
commandService.registerCommand(context.getBean(FeatureCommand.class));
2024-06-25 14:43:36 +00:00
commandService.registerSlashCommands(); // Register all slash commands
}
2024-06-30 04:15:37 +00:00
/**
* Gets a feature by name
*
* @param name The name of the feature
* @return The feature
*/
public Feature getFeature(@NonNull String name) {
return features.get(name.toLowerCase());
}
/**
* Checks if a feature is registered
*
* @param name The name of the feature
* @return Whether the feature is registered
*/
public boolean isFeature(@NonNull String name) {
return features.containsKey(name.toLowerCase());
}
2024-06-25 14:43:36 +00:00
}