Add channel defaults handling

This commit is contained in:
Draycia committed 2020-09-25 01:45:36 -07:00
1 parent 6c9810ea72
commit 50e359a51f
4 files changed
+137 -49

No files matched your search

@@ -108,7 +108,7 @@ public class CarbonSettings {
return this.showTips; return this.showTips;
} }
public @NonNull List<@NonNull ChannelOptions> channelSettings() { public @NonNull List<@NonNull ChannelOptions> channelOptions() {
return this.channelSettings; return this.channelSettings;
} }
@@ -1,5 +1,6 @@
package net.draycia.carbon.api.config; package net.draycia.carbon.api.config;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.Context; import net.draycia.carbon.api.Context;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@@ -20,17 +21,17 @@ public class ChannelOptions {
@Setting private Map<String, Context> contexts = new HashMap<>(); // TODO: set defaults @Setting private Map<String, Context> contexts = new HashMap<>(); // TODO: set defaults
@Setting private Map<String, String> formats = new HashMap<>(); // TODO: set defaults @Setting private Map<String, String> formats = new HashMap<>(); // TODO: set defaults
@Setting private String defaultFormatName = "default"; @Setting private String defaultFormatName = "default";
@Setting private boolean isDefault = false; @Setting private boolean isDefault = false; // primitive because missing = false
@Setting private boolean ignorable = true; @Setting private Boolean ignorable = true; // boxed because missing = use defaults
@Setting private boolean crossServer = true; @Setting private Boolean crossServer = true;
@Setting private boolean honorsRecipientList = false; @Setting private Boolean honorsRecipientList = false;
@Setting private boolean permissionGroupMatching = false; @Setting private Boolean permissionGroupMatching = false;
@Setting private List<String> groupOverrides = new ArrayList<>(); @Setting private List<String> groupOverrides = new ArrayList<>();
@Setting private String name = ""; @Setting private String name = "";
@Setting private String messagePrefix = ""; @Setting private String messagePrefix = "";
@Setting private List<String> aliases = new ArrayList<>(); @Setting private List<String> aliases = new ArrayList<>();
@Setting private boolean shouldCancelChatEvent = false; @Setting private Boolean shouldCancelChatEvent = false;
@Setting private boolean primaryGroupOnly = false; @Setting private Boolean primaryGroupOnly = false;
@Setting private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!"; @Setting private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
@Setting private String switchOtherMessage = "<gray><player> <reset><gray>is now in <color><channel> <gray>chat!"; @Setting private String switchOtherMessage = "<gray><player> <reset><gray>is now in <color><channel> <gray>chat!";
@Setting private String switchFailureMessage = "<red>You cannot use channel <channel>!"; @Setting private String switchFailureMessage = "<red>You cannot use channel <channel>!";
@@ -41,6 +42,9 @@ public class ChannelOptions {
@Setting private String cannotUseMessage = "You cannot use that channel!"; @Setting private String cannotUseMessage = "You cannot use that channel!";
@Setting private String cannotIgnoreMessage = "<red>You cannot ignore that channel!"; @Setting private String cannotIgnoreMessage = "<red>You cannot ignore that channel!";
private final SharedChannelOptions defaultOptions =
CarbonChatProvider.carbonChat().channelSettings().defaultChannelOptions();
public static ChannelOptions defaultChannel() { public static ChannelOptions defaultChannel() {
final ChannelOptions settings = new ChannelOptions(); final ChannelOptions settings = new ChannelOptions();
settings.key = "global"; settings.key = "global";
@@ -58,21 +62,41 @@ public class ChannelOptions {
@Nullable @Nullable
public String color() { public String color() {
if (this.color == null) {
return this.defaultOptions.color();
}
return this.color; return this.color;
} }
@Nullable @Nullable
public Map<String, Context> contexts() { public Map<String, Context> contexts() {
// TODO: properly implement default context inheritence
// TODO: perhaps add an option to make it so overriding any context overrides all?
if (this.contexts == null) {
return this.defaultOptions.contexts();
}
return this.contexts; return this.contexts;
} }
@Nullable @Nullable
public Map<String, String> formats() { public Map<String, String> formats() {
// TODO: properly implement default format inheritence
// TODO: perhaps add an option to make it so overriding any format overrides all?
if (this.formats == null) {
return this.defaultOptions.formats();
}
return this.formats; return this.formats;
} }
@Nullable @Nullable
public String defaultFormatName() { public String defaultFormatName() {
if (this.defaultFormatName == null || this.defaultFormatName.isEmpty()) {
return this.defaultOptions.defaultFormatName();
}
return this.defaultFormatName; return this.defaultFormatName;
} }
@@ -80,24 +104,44 @@ public class ChannelOptions {
return this.isDefault; return this.isDefault;
} }
public boolean ignorable() { public Boolean ignorable() {
if (this.ignorable == null) {
return this.defaultOptions.ignorable();
}
return this.ignorable; return this.ignorable;
} }
public boolean crossServer() { public Boolean crossServer() {
if (this.crossServer == null) {
return this.defaultOptions.crossServer();
}
return this.crossServer; return this.crossServer;
} }
public boolean honorsRecipientList() { public Boolean honorsRecipientList() {
if (this.honorsRecipientList == null) {
return this.defaultOptions.honorsRecipientList();
}
return this.honorsRecipientList; return this.honorsRecipientList;
} }
public boolean permissionGroupMatching() { public Boolean permissionGroupMatching() {
if (this.permissionGroupMatching == null) {
return this.defaultOptions.permissionGroupMatching();
}
return this.permissionGroupMatching; return this.permissionGroupMatching;
} }
@NonNull @NonNull
public List<@NonNull String> groupOverrides() { public List<@NonNull String> groupOverrides() {
if (this.groupOverrides == null) {
return this.defaultOptions.groupOverrides();
}
return this.groupOverrides; return this.groupOverrides;
} }
@@ -117,54 +161,98 @@ public class ChannelOptions {
@Nullable @Nullable
public String switchMessage() { public String switchMessage() {
if (this.switchMessage == null) {
return this.defaultOptions.switchMessage();
}
return this.switchMessage; return this.switchMessage;
} }
@Nullable @Nullable
public String switchOtherMessage() { public String switchOtherMessage() {
if (this.switchOtherMessage == null) {
return this.defaultOptions.switchOtherMessage();
}
return this.switchOtherMessage; return this.switchOtherMessage;
} }
@Nullable @Nullable
public String switchFailureMessage() { public String switchFailureMessage() {
if (this.switchFailureMessage == null) {
return this.defaultOptions.switchFailureMessage();
}
return this.switchFailureMessage; return this.switchFailureMessage;
} }
@Nullable @Nullable
public String cannotIgnoreMessage() { public String cannotIgnoreMessage() {
if (this.cannotIgnoreMessage == null) {
return this.defaultOptions.cannotIgnoreMessage();
}
return this.cannotIgnoreMessage; return this.cannotIgnoreMessage;
} }
@Nullable @Nullable
public String toggleOffMessage() { public String toggleOffMessage() {
if (this.toggleOffMessage == null) {
return this.defaultOptions.toggleOffMessage();
}
return this.toggleOffMessage; return this.toggleOffMessage;
} }
@Nullable @Nullable
public String toggleOnMessage() { public String toggleOnMessage() {
if (this.toggleOnMessage == null) {
return this.defaultOptions.toggleOnMessage();
}
return this.toggleOnMessage; return this.toggleOnMessage;
} }
@Nullable @Nullable
public String toggleOtherOnMessage() { public String toggleOtherOnMessage() {
if (this.toggleOtherOnMessage == null) {
return this.defaultOptions.toggleOtherOnMessage();
}
return this.toggleOtherOnMessage; return this.toggleOtherOnMessage;
} }
@Nullable @Nullable
public String toggleOtherOffMessage() { public String toggleOtherOffMessage() {
if (this.toggleOtherOffMessage == null) {
return this.defaultOptions.toggleOtherOffMessage();
}
return this.toggleOtherOffMessage; return this.toggleOtherOffMessage;
} }
@Nullable @Nullable
public String cannotUseMessage() { public String cannotUseMessage() {
if (this.cannotUseMessage == null) {
return this.defaultOptions.cannotUseMessage();
}
return this.cannotUseMessage; return this.cannotUseMessage;
} }
public boolean primaryGroupOnly() { public Boolean primaryGroupOnly() {
if (this.primaryGroupOnly == null) {
return this.defaultOptions.primaryGroupOnly();
}
return this.primaryGroupOnly; return this.primaryGroupOnly;
} }
public boolean shouldCancelChatEvent() { public Boolean shouldCancelChatEvent() {
if (this.shouldCancelChatEvent == null) {
return this.defaultOptions.shouldCancelChatEvent();
}
return this.shouldCancelChatEvent; return this.shouldCancelChatEvent;
} }
@@ -1,7 +1,7 @@
package net.draycia.carbon.common.channels; package net.draycia.carbon.common.channels;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.config.ChannelSettings; import net.draycia.carbon.api.config.ChannelOptions;
import net.draycia.carbon.common.utils.ColorUtils; import net.draycia.carbon.common.utils.ColorUtils;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents; import net.draycia.carbon.api.events.misc.CarbonEvents;
@@ -33,15 +33,15 @@ public class CarbonChatChannel implements ChatChannel {
private final CarbonChat carbonChat; private final CarbonChat carbonChat;
@NonNull @NonNull
private final ChannelSettings settings; private final ChannelOptions options;
public CarbonChatChannel(final @NonNull CarbonChat carbonChat, @Nullable final ChannelSettings settings) { public CarbonChatChannel(final @NonNull CarbonChat carbonChat, @Nullable final ChannelOptions options) {
this.carbonChat = carbonChat; this.carbonChat = carbonChat;
this.settings = settings; this.options = options;
} }
public @NonNull ChannelSettings settings() { public @NonNull ChannelOptions options() {
return this.settings; return this.options;
} }
@NonNull @NonNull
@@ -271,7 +271,7 @@ public class CarbonChatChannel implements ChatChannel {
@Override @Override
@Nullable @Nullable
public String format(final @NonNull String group) { public String format(final @NonNull String group) {
return this.settings().formats().get("formats." + group); return this.options().formats().get("formats." + group);
} }
private boolean isUserSpying(final @NonNull ChatUser sender, final @NonNull ChatUser target) { private boolean isUserSpying(final @NonNull ChatUser sender, final @NonNull ChatUser target) {
@@ -339,7 +339,7 @@ public class CarbonChatChannel implements ChatChannel {
return userColor; return userColor;
} }
final String input = this.settings().color(); final String input = this.options().color();
final TextColor color = ColorUtils.parseColor(user, input); final TextColor color = ColorUtils.parseColor(user, input);
@@ -357,114 +357,114 @@ public class CarbonChatChannel implements ChatChannel {
@Nullable @Nullable
public String defaultFormatName() { public String defaultFormatName() {
return this.settings().defaultFormatName(); return this.options().defaultFormatName();
} }
@Override @Override
public boolean isDefault() { public boolean isDefault() {
return this.settings().isDefault(); return this.options().isDefault();
} }
@Override @Override
public boolean ignorable() { public boolean ignorable() {
return this.settings().ignorable(); return this.options().ignorable();
} }
@Override @Override
public boolean crossServer() { public boolean crossServer() {
return this.settings().crossServer(); return this.options().crossServer();
} }
@Override @Override
public boolean honorsRecipientList() { public boolean honorsRecipientList() {
return this.settings().honorsRecipientList(); return this.options().honorsRecipientList();
} }
@Override @Override
public boolean permissionGroupMatching() { public boolean permissionGroupMatching() {
return this.settings().permissionGroupMatching(); return this.options().permissionGroupMatching();
} }
@Override @Override
@NonNull @NonNull
public List<@NonNull String> groupOverrides() { public List<@NonNull String> groupOverrides() {
return this.settings().groupOverrides(); return this.options().groupOverrides();
} }
@Override @Override
@NonNull @NonNull
public String name() { public String name() {
return this.settings().name(); return this.options().name();
} }
@Override @Override
@Nullable @Nullable
public String messagePrefix() { public String messagePrefix() {
return this.settings().messagePrefix(); return this.options().messagePrefix();
} }
@Override @Override
@Nullable @Nullable
public String switchMessage() { public String switchMessage() {
return this.settings().switchMessage(); return this.options().switchMessage();
} }
@Override @Override
@Nullable @Nullable
public String switchOtherMessage() { public String switchOtherMessage() {
return this.settings().switchOtherMessage(); return this.options().switchOtherMessage();
} }
@Override @Override
@Nullable @Nullable
public String switchFailureMessage() { public String switchFailureMessage() {
return this.settings().switchFailureMessage(); return this.options().switchFailureMessage();
} }
@Override @Override
@Nullable @Nullable
public String cannotIgnoreMessage() { public String cannotIgnoreMessage() {
return this.settings().cannotIgnoreMessage(); return this.options().cannotIgnoreMessage();
} }
@Override @Override
@Nullable @Nullable
public String toggleOffMessage() { public String toggleOffMessage() {
return this.settings().toggleOffMessage(); return this.options().toggleOffMessage();
} }
@Override @Override
@Nullable @Nullable
public String toggleOnMessage() { public String toggleOnMessage() {
return this.settings().toggleOnMessage(); return this.options().toggleOnMessage();
} }
@Override @Override
@Nullable @Nullable
public String toggleOtherOnMessage() { public String toggleOtherOnMessage() {
return this.settings().toggleOtherOnMessage(); return this.options().toggleOtherOnMessage();
} }
@Override @Override
@Nullable @Nullable
public String toggleOtherOffMessage() { public String toggleOtherOffMessage() {
return this.settings().toggleOtherOffMessage(); return this.options().toggleOtherOffMessage();
} }
@Override @Override
@Nullable @Nullable
public String cannotUseMessage() { public String cannotUseMessage() {
return this.settings().cannotUseMessage(); return this.options().cannotUseMessage();
} }
@Override @Override
public boolean primaryGroupOnly() { public boolean primaryGroupOnly() {
return this.settings().primaryGroupOnly(); return this.options().primaryGroupOnly();
} }
@Override @Override
public boolean shouldCancelChatEvent() { public boolean shouldCancelChatEvent() {
return this.settings().shouldCancelChatEvent(); return this.options().shouldCancelChatEvent();
} }
@Override @Override
@@ -476,19 +476,19 @@ public class CarbonChatChannel implements ChatChannel {
@Override @Override
@Nullable @Nullable
public List<String> aliases() { public List<String> aliases() {
return this.settings().aliases(); return this.options().aliases();
} }
@Override @Override
@Nullable @Nullable
public Context context(final @NonNull String key) { public Context context(final @NonNull String key) {
return this.settings().contexts().get(key); return this.options().contexts().get(key);
} }
@Override @Override
@NonNull @NonNull
public String key() { public String key() {
return this.settings().key(); return this.options().key();
} }
} }
@@ -1,7 +1,7 @@
package net.draycia.carbon.common.channels; package net.draycia.carbon.common.channels;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.config.ChannelSettings; import net.draycia.carbon.api.config.ChannelOptions;
import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents; import net.draycia.carbon.api.events.misc.CarbonEvents;
@@ -32,7 +32,7 @@ public class ChannelManager {
this.reload(); this.reload();
} }
public @Nullable ChatChannel loadChannel(final @NonNull ChannelSettings settings) { public @Nullable ChatChannel loadChannel(final @NonNull ChannelOptions settings) {
final ChatChannel channel = new CarbonChatChannel(this.carbonChat, settings); final ChatChannel channel = new CarbonChatChannel(this.carbonChat, settings);
final String name = settings.name(); final String name = settings.name();
@@ -84,8 +84,8 @@ public class ChannelManager {
} }
private void reload() { private void reload() {
for (final ChannelSettings settings : this.carbonChat.carbonSettings().channelSettings()) { for (final ChannelOptions options : this.carbonChat.carbonSettings().channelOptions()) {
final ChatChannel channel = this.loadChannel(settings); final ChatChannel channel = this.loadChannel(options);
if (channel != null) { if (channel != null) {
this.registerChannel(channel); this.registerChannel(channel);