Skip to content

Commit

Permalink
fixed executor and condition parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Draww committed Mar 15, 2019
1 parent 7e82113 commit a4113b0
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 34 deletions.
11 changes: 11 additions & 0 deletions src/main/java/me/draww/superrup/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import me.draww.superrup.executor.Executor;
import org.bukkit.inventory.ItemStack;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Rank {

Expand Down Expand Up @@ -38,6 +41,14 @@ public Rank(String id, String group, Integer queue) {
this.queue = queue;
}

public List<Condition> getSortedConditions() {
return conditions.values().stream().sorted(Comparator.comparingInt(Condition::getQueue)).collect(Collectors.toList());
}

public List<Executor> getSortedExecutors() {
return executors.values().stream().sorted(Comparator.comparingInt(Executor::getQueue)).collect(Collectors.toList());
}

public String getId() {
return id;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/draww/superrup/RankMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public RankMenu(Player player) {
addLast(new BasicElement(rank.getIconJump(),
new BasicTarget(e -> {
e.cancel();
boolean controlConditions = ConditionProvider.testAllConditions(player, new ArrayList<>(rank.getConditions().values()));
boolean controlConditions = ConditionProvider.testAllConditions(player, rank);
if (controlConditions) {
Main.getInstance().getGroupManager().setPlayerPrimaryGroup(player, rank.getGroup());
ExecutorProvider.runAllExecutors(player, new ArrayList<>(rank.getExecutors().values()));
ExecutorProvider.runAllExecutors(player, rank);
}
e.closeView();
})));
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/me/draww/superrup/condition/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
public class Condition {

private final String id;
private final Integer queue;
private final Rank rank;
private Map<String, Object> requiredData;
private String message;
private ConditionType type;

public Condition(String id, Rank rank, Map<String, Object> requiredData, String message, ConditionType type) {
public Condition(String id, Integer queue, Rank rank, Map<String, Object> requiredData, String message, ConditionType type) {
this.id = id;
this.queue = queue;
this.rank = rank;
this.requiredData = requiredData;
this.message = message;
Expand All @@ -29,6 +31,10 @@ public String getId() {
return id;
}

public Integer getQueue() {
return queue;
}

public Rank getRank() {
return rank;
}
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/me/draww/superrup/condition/ConditionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,36 @@ public class ConditionProvider {

public static Map<String, Condition> deserializeConditions(ConfigurationSection section, Rank rank) {
if (section == null) return new HashMap<>();
if (section.contains("template") && section.isString("template")) {
ConfigurationSection templateSection = Main.getInstance().getTemplateConfig().getConfigurationSection("conditions." + section.getString("template"));
return deserializeConditions(templateSection, rank);
}
Map<String, Condition> conditionMap = new HashMap<>();
for (String condKey : section.getKeys(false)) {
if ((!section.contains(condKey + ".type") && ! section.isString(condKey + ".type"))
|| (!section.contains(condKey + ".message") && ! section.isString(condKey + ".message"))) continue;
conditionMap.put(condKey, new Condition(condKey,
rank,
section.getConfigurationSection(condKey + ".data").getValues(true),
section.getString(condKey + ".message"),
ConditionType.valueOf(section.getString(condKey + ".type").toUpperCase())));
if (section.contains(condKey + ".template") && section.isString(condKey + ".template")) {
ConfigurationSection templateSection = Main.getInstance().getTemplateConfig().getConfigurationSection("conditions." + section.getString(condKey + ".template"));
if ((!templateSection.contains("type") && !templateSection.isString("type"))
|| (!templateSection.contains("message") && !templateSection.isString("message"))
|| (!section.contains(condKey + ".queue") && !section.isInt(condKey + ".queue"))) continue;
conditionMap.put(condKey, new Condition(condKey,
section.getInt(condKey + ".queue"),
rank,
templateSection.getConfigurationSection("data").getValues(true),
templateSection.getString("message"),
ConditionType.valueOf(templateSection.getString("type").toUpperCase())));
} else {
if ((!section.contains(condKey + ".type") && !section.isString(condKey + ".type"))
|| (!section.contains(condKey + ".message") && !section.isString(condKey + ".message"))
|| (!section.contains(condKey + ".queue") && !section.isString(condKey + ".queue"))) continue;
conditionMap.put(condKey, new Condition(condKey,
section.getInt(condKey + ".queue"),
rank,
section.getConfigurationSection(condKey + ".data").getValues(true),
section.getString(condKey + ".message"),
ConditionType.valueOf(section.getString(condKey + ".type").toUpperCase())));
}
}
return conditionMap;
}

public static boolean testAllConditions(Player player, List<Condition> conditions) {
for (Condition condition : conditions) {
public static boolean testAllConditions(Player player, Rank rank) {
for (Condition condition : rank.getSortedConditions()) {
if (!condition.test(player)) {
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/me/draww/superrup/executor/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
public class Executor {

private final String id;
private final Integer queue;
private final Rank rank;
private Map<String, Object> data;
private ExecutorType type;

public Executor(String id, Rank rank, Map<String, Object> data, ExecutorType type) {
public Executor(String id, Integer queue, Rank rank, Map<String, Object> data, ExecutorType type) {
this.id = id;
this.queue = queue;
this.rank = rank;
this.data = data;
this.type = type;
Expand All @@ -27,6 +29,10 @@ public String getId() {
return id;
}

public Integer getQueue() {
return queue;
}

public Rank getRank() {
return rank;
}
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/me/draww/superrup/executor/ExecutorProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@ public class ExecutorProvider {

public static Map<String, Executor> deserializeExecutors(ConfigurationSection section, Rank rank) {
if (section == null) return new HashMap<>();
if (section.contains("template") && section.isString("template")) {
ConfigurationSection templateSection = Main.getInstance().getTemplateConfig().getConfigurationSection("executors." + section.getString("template"));
return deserializeExecutors(templateSection, rank);
}
Map<String, Executor> executorMap = new HashMap<>();
for (String executorKey : section.getKeys(false)) {
if ((!section.contains(executorKey + ".type") && ! section.isString(executorKey + ".type"))
|| (!section.contains(executorKey + ".message") && ! section.isString(executorKey + ".message"))) continue;
executorMap.put(executorKey, new Executor(executorKey,
rank,
section.getConfigurationSection(executorKey + ".data").getValues(true),
ExecutorType.valueOf(section.getString(executorKey + ".type").toUpperCase())));
if (section.contains(executorKey + ".template") && section.isString(executorKey + ".template")) {
ConfigurationSection templateSection = Main.getInstance().getTemplateConfig().getConfigurationSection("executors." + section.getString(executorKey + ".template"));
if ((!templateSection.contains("type") && !templateSection.isString("type"))
|| (!section.contains(executorKey + ".queue") && !section.isInt(executorKey + ".queue")))
continue;
executorMap.put(executorKey, new Executor(executorKey,
section.getInt(executorKey + ".queue"),
rank,
templateSection.getConfigurationSection("data").getValues(true),
ExecutorType.valueOf(templateSection.getString("type").toUpperCase())));
} else {
if ((!section.contains(executorKey + ".type") && !section.isString(executorKey + ".type"))
|| (!section.contains(executorKey + ".queue") && !section.isInt(executorKey + ".queue")))
continue;
executorMap.put(executorKey, new Executor(executorKey,
section.getInt(executorKey + ".queue"),
rank,
section.getConfigurationSection(executorKey + ".data").getValues(true),
ExecutorType.valueOf(section.getString(executorKey + ".type").toUpperCase())));
}
}
return executorMap;
}

public static void runAllExecutors(Player player, List<Executor> executors) {
for (Executor executor : executors) {
public static void runAllExecutors(Player player, Rank rank) {
for (Executor executor : rank.getSortedExecutors()) {
executor.run(player);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/ranks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ example_rank_1:
template: custom_equal
conditions:
cond_1:
queue: 1
type: MONEY
message: "&cYou don't have enough money [&e%data%&c]"
message: "&cYou don't have enough money [&e%value%&c]"
data:
value: 500.00
cond_2:
template: custom_cond
queue: 2
template: custom_cond
executors:
exec_1:
queue: 1
type: MESSAGE
data:
value: "&atest message &e%player_name% &aand &6%rank%"
exec_2:
queue: 2
template: custom_exec
10 changes: 7 additions & 3 deletions src/main/resources/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ icons:
conditions:
custom_cond:
type: MONEY
message: "&cYou don't have enough money [&e%data%&c]"
message: "&cYou don't have enough money [&e%value%&c]"
data:
value: 500.00
executors: {}
value: 1500.00
executors:
custom_exec:
type: MESSAGE
data:
value: "&atest message 2 &e%player% &aand &6%rank%"

0 comments on commit a4113b0

Please sign in to comment.