Skip to content

Commit

Permalink
Fixed top-down resolution of send-messages setting
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfirecodes committed Jan 3, 2019
1 parent 0e29cba commit a47e5ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ConfigManager {

public void load(ConfigurationSection config) {
bypassAllowed = config.getBoolean("allow-bypass", true);
messagesEnabled = config.getBoolean("send-messages");
messagesEnabled = config.getBoolean("send-messages", true);
messageCooldown = config.getInt("message-cooldown");
preventOptions = PreventOptions.load(config.getConfigurationSection("prevent"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import java.util.Optional;
import java.util.stream.Collectors;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class PermissionItem {
private String name;
private String permission;
private PreventOptions preventOverrides;
private List<ItemFilter> filterList;
private boolean messagesEnabled;
private Optional<Boolean> messagesEnabled;

public PermissionItem(String name, String permission, PreventOptions preventOverrides, List<ItemFilter> filterList, boolean messagesEnabled) {
public PermissionItem(String name, String permission, PreventOptions preventOverrides, List<ItemFilter> filterList, Optional<Boolean> messagesEnabled) {
this.name = name;
this.permission = permission;
this.preventOverrides = preventOverrides;
Expand All @@ -42,15 +43,15 @@ public List<ItemFilter> getFilterList() {
return filterList;
}

public boolean isMessagesEnabled() {
public Optional<Boolean> isMessagesEnabled() {
return messagesEnabled;
}

@SuppressWarnings("unchecked")
public static PermissionItem load(String name, ConfigurationSection config) {
String permission = config.getString("permission");
List<Map<?, ?>> filters = config.getMapList("filters");
boolean messagesEnabled = config.getBoolean("send-messages", false);
Optional<Boolean> messagesEnabled = config.isBoolean("send-messages") ? Optional.of(config.getBoolean("send-messages")) : Optional.empty();
PreventOptions preventOverrides = Optional.ofNullable(config.getConfigurationSection("prevent"))
.map(PreventOptions::load)
.orElse(null);
Expand All @@ -77,7 +78,8 @@ public static PermissionItem load(String name, ConfigurationSection config) {
})
.filter(Objects::nonNull)
.collect(Collectors.toList()),
messagesEnabled);
messagesEnabled
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ private boolean isPreventedFor(Player player, ItemStack item, Function<PreventOp

Optional<PermissionItem> result = plugin.getItemManager().getPermissionItemList().stream()
// Resolve prevention settings from top to bottom, starting with permission item overrides and ending with the config.yml settings
// TODO: very messy top-down setting resolution
.filter(permissionItem -> option.apply(permissionItem.getPreventOverrides().orElse(defaults)).orElse(option.apply(defaults).orElse(false)))
.filter(permissionItem -> !player.hasPermission(permissionItem.getPermission()))
.filter(permissionItem -> permissionItem.getFilterList().stream().allMatch(filter -> filter.isValidMatch(item)))
.findFirst();

if (result.isPresent() && (plugin.getConfigManager().isMessagesEnabled() || result.get().isMessagesEnabled())) {
// TODO: also very messy top-down setting resolution
if (result.isPresent() && (result.get().isMessagesEnabled().orElse(plugin.getConfigManager().isMessagesEnabled()))) {
sendPreventionMessage(player, messageType);
}

Expand Down

0 comments on commit a47e5ab

Please sign in to comment.