2019-09-30 08:14:23 +02:00
# Ability Extensions
You have around 100 abilities in your bot and you're looking for a way to refactor that mess into more modular classes. `AbillityExtension` is here to support just that! It's not a secret that AbilityBot uses refactoring backstage to be able to construct all of your abilities and map them accordingly. However, AbilityBot searches initially for all methods that return an `AbilityExtension` type. Then, those extensions will be used to search for declared abilities. Here's an example.
```java
public class MrGoodGuy implements AbilityExtension {
2021-12-09 06:23:35 +01:00
private AbilityBot extensionUser;
public MrGoodGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
public Ability nice() {
return Ability.builder()
.name("nice")
.privacy(PUBLIC)
.locality(ALL)
.action(ctx -> extensionUser.silent().send("You're awesome!", ctx.chatId())
);
}
2019-09-30 08:14:23 +02:00
}
public class MrBadGuy implements AbilityExtension {
2021-12-09 06:23:35 +01:00
private AbilityBot extensionUser;
public MrBadGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
public Ability notnice() {
return Ability.builder()
.name("notnice")
.privacy(PUBLIC)
.locality(ALL)
.action(ctx -> extensionUser.silent().send("You're horrible!", ctx.chatId())
);
}
}
public class YourAwesomeBot implements AbilityBot {
2019-09-30 08:14:23 +02:00
// Constructor for your bot
2021-12-09 06:23:35 +01:00
2019-09-30 08:14:23 +02:00
public AbilityExtension goodGuy() {
2021-12-09 06:23:35 +01:00
return new MrGoodGuy(this);
2019-09-30 08:14:23 +02:00
}
2021-12-09 06:23:35 +01:00
2019-09-30 08:14:23 +02:00
public AbilityExtension badGuy() {
2021-12-09 06:23:35 +01:00
return new MrBadGuy(this);
2019-09-30 08:14:23 +02:00
}
2021-12-09 06:23:35 +01:00
2020-10-08 16:11:45 +02:00
// Override creatorId
2021-12-09 06:23:35 +01:00
}
2020-10-08 16:11:45 +02:00
```
It's also possible to add extensions in the constructor by using the `addExtension()` or `addExtensions()` method:
```java
public class YourAwesomeBot implements AbilityBot {
public YourAwesomeBot() {
2021-12-09 06:23:35 +01:00
super(/* pass required args ... */);
addExtensions(new MrGoodGuy(this), new MrBadGuy(this));
2020-10-08 16:11:45 +02:00
}
2021-12-09 06:23:35 +01:00
2019-09-30 08:14:23 +02:00
// Override creatorId
2021-12-09 06:23:35 +01:00
}
2020-12-09 11:01:51 +01:00
```