Merge pull request #918 from monkeyboiii/dev
This commit is contained in:
commit
db1b24cd09
@ -4,10 +4,11 @@ import org.telegram.abilitybots.api.objects.Ability;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* This custom toggle can be used to customize default abilities supplied by the library. Users can call {@link CustomToggle#toggle} to
|
||||
* rename the default abilites or {@link CustomToggle#turnOff} to simply turn off the said ability.
|
||||
* rename the default abilities or {@link CustomToggle#turnOff} to simply turn off the said ability.
|
||||
*/
|
||||
public class CustomToggle implements AbilityToggle {
|
||||
public static final String OFF = "turn_off_base_ability";
|
||||
@ -53,4 +54,76 @@ public class CustomToggle implements AbilityToggle {
|
||||
baseMapping.put(ability, OFF);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param properties the abilities toggle definition
|
||||
* @return the toggle instance
|
||||
*/
|
||||
public CustomToggle config(Properties properties) {
|
||||
for (String key : properties.stringPropertyNames()) {
|
||||
String value = properties.getProperty(key);
|
||||
key = key.toLowerCase();
|
||||
|
||||
// compare with legal configuration names
|
||||
for (Property p: Property.values()) {
|
||||
if (key.equals(p.key())) {
|
||||
String ability = key.split("\\.")[1];
|
||||
if (key.contains("enabled") && value.equalsIgnoreCase("false")) {
|
||||
this.turnOff(ability);
|
||||
}else if (key.contains("toggle")) {
|
||||
this.toggle(ability, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of all the properties recognized by {@link CustomToggle}.
|
||||
* Can be used to programmatically get, set or remove default values.
|
||||
*/
|
||||
public enum Property{
|
||||
CLAIM_ENABLED("ability.claim.enabled"),
|
||||
CLAIM_TOGGLE("ability.claim.toggle"),
|
||||
|
||||
BAN_ENABLED("ability.ban.enabled"),
|
||||
BAN_TOGGLE("ability.ban.toggle"),
|
||||
|
||||
PROMOTE_ENABLED("ability.promote.enabled"),
|
||||
PROMOTE_TOGGLE("ability.promote.toggle"),
|
||||
|
||||
DEMOTE_ENABLED("ability.demote.enabled"),
|
||||
DEMOTE_TOGGLE("ability.demote.toggle"),
|
||||
|
||||
UNBAN_ENABLED("ability.unban.enabled"),
|
||||
UNBAN_TOGGLE("ability.unban.toggle"),
|
||||
|
||||
BACKUP_ENABLED("ability.backup.enabled"),
|
||||
BACKUP_TOGGLE("ability.backup.toggle"),
|
||||
|
||||
RECOVER_ENABLED("ability.recover.enabled"),
|
||||
RECOVER_TOGGLE("ability.recover.toggle"),
|
||||
|
||||
COMMANDS_ENABLED("ability.commands.enabled"),
|
||||
COMMANDS_TOGGLE("ability.commands.toggle"),
|
||||
|
||||
REPORT_ENABLED("ability.report.enabled"),
|
||||
REPORT_TOGGLE("ability.report.toggle"),
|
||||
|
||||
STATS_ENABLED("ability.stats.enabled"),
|
||||
STATS_TOGGLE("ability.stats.toggle")
|
||||
;
|
||||
|
||||
private final String key;
|
||||
|
||||
Property (final String key){
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String key() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,9 @@ import org.telegram.abilitybots.api.bot.DefaultAbilities;
|
||||
import org.telegram.abilitybots.api.bot.DefaultBot;
|
||||
import org.telegram.abilitybots.api.db.DBContext;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.EMPTY;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@ -17,10 +19,12 @@ class CustomToggleTest {
|
||||
private DBContext db;
|
||||
private AbilityToggle toggle;
|
||||
private DefaultBot customBot;
|
||||
private String filename;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
db = offlineInstance("db");
|
||||
filename = "src/test/resources/toggle.properties";
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@ -47,4 +51,37 @@ class CustomToggleTest {
|
||||
assertTrue(customBot.abilities().containsKey(targetName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canTurnOffAbilitiesThroughProperties() {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(new FileInputStream(filename));
|
||||
toggle = new CustomToggle().config(properties);
|
||||
} catch (IOException e) {
|
||||
System.out.println("No such file");
|
||||
}
|
||||
|
||||
customBot = new DefaultBot(EMPTY, EMPTY, db, toggle);
|
||||
customBot.onRegister();
|
||||
|
||||
assertFalse(customBot.abilities().containsKey(DefaultAbilities.CLAIM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canProcessAbilitiesThroughProperties() {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(new FileInputStream(filename));
|
||||
toggle = new CustomToggle().config(properties);
|
||||
} catch (IOException e) {
|
||||
System.out.println("No such file");
|
||||
}
|
||||
|
||||
customBot = new DefaultBot(EMPTY, EMPTY, db, toggle);
|
||||
customBot.onRegister();
|
||||
|
||||
String targetName = "restrict";
|
||||
assertTrue(customBot.abilities().containsKey(targetName));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
ability.claim.enabled=false
|
||||
ability.ban.toggle=restrict
|
Loading…
x
Reference in New Issue
Block a user