Answer the question
In order to leave comments, you need to log in
How to implement a condition check in a loop?
Greetings, there was a difficulty in writing the code, the point is that the for loop contains the str variable, which can contain an unlimited number of values, but for example, let's take that we have 2 values, when the loop runs the first value and the value matches the condition, then the loop ends its work, but I need both values from str to be checked for this condition and only then the command nextItemLore.add(ChatColor.GREEN + "Click to improve!"); , also if one of these values is not suitable, the nextItemLore.add(ChatColor.RED + "Something is missing to increase!") command was displayed; . I tried to remove break, but still only one value is checked and the loop works correctly only if the second value from str matches the condition or both conditions do not match
for (String str : main.instance.itemsConfig.getConfigurationSection(String.valueOf(newPath) + ".requirements.blocks").getKeys(false)) {
if (Blocks.getBlockBrokeAmount(p, str) >= main.instance.itemsConfig.getInt(String.valueOf(newPath) + ".requirements.blocks." + str) && main.econ.getBalance((OfflinePlayer) p) >= main.instance.itemsConfig.getConfigurationSection(newPath).getInt("price")) {
nextItemLore.add(ChatColor.GREEN + "Нажмите для улучшения!");
glass.setDurability((short) 5);
break;
} else {
glass.setDurability((short) 14);
nextItemLore.add(ChatColor.RED + "Чего-то не хватает для повышения!");
break;
}
}
Answer the question
In order to leave comments, you need to log in
var booleans = List.of(true, false, true, false);
var result = booleans.stream()
.reduce((a, b) -> (a & b))
.orElse(false);
main.instance.itemsConfig.getConfigurationSection(String.valueOf(newPath) + ".requirements.blocks").getKeys(false)
leaf booleans ?
Good afternoon
In order not to write a bunch of if... else or switch ... cases, you can try to use the Chain Of Responsibility pattern
https://refactoring.guru/en/design-patterns/chain-...
Firstly, the code will turn out more concise, and secondly, you can implement what you need.
Relatively speaking, if the code does not pass for verification through a certain filter, then the condition is not met, and if it is, then the request goes further along the chain.
I advise you, as Dmitry Roo correctly noted, to use Streams (from jdk 8 and higher), since both Sonar and Kiuwan, for example, will be very happy about this
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question