More configuration progress

This commit is contained in:
Draycia committed 2020-09-25 01:45:35 -07:00
1 parent 3878be3563
commit f588f3b24b
13 files changed
+318 -256

No files matched your search

@@ -9,12 +9,15 @@ import net.draycia.carbon.api.adventure.MessageProcessor;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.commands.settings.CommandSettingsRegistry;
import net.draycia.carbon.api.config.CarbonSettings;
import net.draycia.carbon.api.config.MessagingType;
import net.draycia.carbon.api.config.ModerationSettings;
import net.draycia.carbon.api.config.StorageType;
import net.draycia.carbon.api.messaging.MessageService;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.common.adventure.FormatType;
import net.draycia.carbon.common.messaging.EmptyMessageService;
import net.draycia.carbon.api.config.SQLCredentials;
import net.draycia.carbon.common.messaging.RedisMessageService;
import net.draycia.carbon.listeners.contexts.DistanceContext;
import net.draycia.carbon.listeners.contexts.EconomyContext;
import net.draycia.carbon.listeners.contexts.FilterContext;
@@ -42,6 +45,7 @@ import net.draycia.carbon.common.messaging.MessageManager;
import net.draycia.carbon.api.users.UserService;
import net.draycia.carbon.common.users.JSONUserService;
import net.draycia.carbon.common.users.MySQLUserService;
import net.draycia.carbon.messaging.BungeeMessageService;
import net.draycia.carbon.storage.BukkitChatUser;
import net.draycia.carbon.util.CarbonPlaceholders;
import net.draycia.carbon.util.FunctionalityConstants;
@@ -81,8 +85,6 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
private UserService<BukkitChatUser> userService;
private MessageManager messageManager;
private FilterContext filterContext;
private CarbonTranslations translations;
private Logger logger;
@@ -104,17 +106,9 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
public void onEnable() {
this.logger = LoggerFactory.getLogger(this.getName());
// Setup metrics
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID);
// Ensure config is present to be modified by the user
if (!(new File(this.getDataFolder(), "config.yml").exists())) {
this.saveDefaultConfig();
}
if (!(new File(this.getDataFolder(), "moderation.yml").exists())) {
this.saveResource("moderation.yml", false);
}
this.reloadConfig();
// Setup Adventure
@@ -122,9 +116,23 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
// Initialize managers
this.channelRegistry = new ChannelRegistry();
this.messageManager = new MessageManager(this, new EmptyMessageService()); // TODO: get message service from config
final CarbonSettings.StorageType storageType = this.carbonSettings().storageType();
// Handle messaging service
final MessagingType messagingType = this.carbonSettings().messagingType();
final MessageService messageService;
if (messagingType == MessagingType.REDIS) {
messageService = new RedisMessageService(this, this.carbonSettings.redisCredentials());
} else if (messagingType == MessagingType.BUNGEECORD) {
messageService = new BungeeMessageService(this); // TODO: only support this if bukkit
} else {
messageService = new EmptyMessageService();
}
this.messageManager = new MessageManager(this, messageService);
// Handle storage service
final StorageType storageType = this.carbonSettings().storageType();
final Supplier<Iterable<BukkitChatUser>> supplier = () -> {
final List<BukkitChatUser> users = new ArrayList<>();
@@ -136,11 +144,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
return users;
};
if (storageType == CarbonSettings.StorageType.MYSQL) {
if (storageType == StorageType.MYSQL) {
this.logger().info("Enabling MySQL storage!");
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
this.userService = new MySQLUserService<>(this, credentials, supplier, BukkitChatUser::new);
} else if (storageType == CarbonSettings.StorageType.JSON) {
} else if (storageType == StorageType.JSON) {
this.logger().info("Enabling JSON storage!");
this.userService = new JSONUserService<>(BukkitChatUser.class, this, supplier, BukkitChatUser::new);
} else {
@@ -153,9 +161,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
this.setupListeners();
this.registerContexts();
// Setup PlaceholderAPI placeholders
new CarbonPlaceholders(this).register();
if (!FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
// Log missing functionality
if (this.carbonSettings().showTips() && !FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
this.logger().error("Item linking disabled! Please use Paper 1.16.2 #172 or newer.");
}
}
@@ -208,13 +218,24 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
@Override
public void reloadConfig() {
super.reloadConfig();
this.loadCarbonSettingS();
this.loadLanguage();
this.loadModerationSettings();
this.loadCommandSettings();
}
private void loadCarbonSettingS() {
final File settingsFile = new File(this.getDataFolder(), "config.yml");
final YamlConfigurationLoader settingsLoader = YamlConfigurationLoader.builder().setFile(settingsFile).build();
try {
final BasicConfigurationNode settingsNode = settingsLoader.load();
this.carbonSettings = CarbonSettings.loadFrom(settingsNode);
settingsLoader.save(settingsNode);
} catch (final IOException | ObjectMappingException ignored) {
}
}
private void loadLanguage() {
final File languageFile = new File(this.getDataFolder(), "language.yml");
final YamlConfigurationLoader languageLoader = YamlConfigurationLoader.builder().setFile(languageFile).build();
@@ -227,6 +248,18 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
}
}
private void loadModerationSettings() {
final File moderationFile = new File(this.getDataFolder(), "moderation.yml");
final YamlConfigurationLoader moderationLoader = YamlConfigurationLoader.builder().setFile(moderationFile).build();
try {
final BasicConfigurationNode moderationNode = moderationLoader.load();
this.moderationSettings = ModerationSettings.loadFrom(moderationNode);
moderationLoader.save(moderationNode);
} catch (final IOException | ObjectMappingException ignored) {
}
}
private void loadCommandSettings() {
// TODO: probably redo this from scratch
final File carbonCommands = new File(this.getDataFolder(), "carbon-commands.yml");
@@ -246,17 +279,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
}
}
private void loadModerationSettings() {
}
public void reloadFilters() {
this.filterContext.reloadFilters();
}
private void registerContexts() {
this.filterContext = new FilterContext(this);
if (Bukkit.getPluginManager().isPluginEnabled("Towny")) {
this.getServer().getPluginManager().registerEvents(new TownyContext(this), this);
}
@@ -274,6 +297,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
}
new DistanceContext();
new FilterContext(this);
}
@NonNull
@@ -8,8 +8,6 @@ import net.draycia.carbon.api.Context;
import net.kyori.event.PostOrders;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
@@ -20,20 +18,13 @@ public class FilterContext {
@NonNull
private final CarbonChat carbonChat;
@NonNull
private final Map<String, List<@NonNull Pattern>> patternReplacements = new HashMap<>();
@NonNull
private final List<@NonNull Pattern> blockedWords = new ArrayList<>();
public FilterContext(final @NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.reloadFilters();
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
// if (!carbonChat.moderationSettings().filters().enabled()) {
// return;
// }
if (!carbonChat.moderationSettings().filters().enabled()) {
return;
}
if (event.user().permissible() && event.user().hasPermission("carbonchat.filter.exempt")) {
return;
@@ -45,7 +36,9 @@ public class FilterContext {
String message = event.message();
for (final Map.Entry<String, List<Pattern>> entry : this.patternReplacements.entrySet()) {
for (final Map.Entry<String, List<Pattern>> entry :
this.carbonChat.moderationSettings().filters().replacements().entrySet()) {
for (final Pattern pattern : entry.getValue()) {
final Matcher matcher = pattern.matcher(message);
@@ -59,7 +52,7 @@ public class FilterContext {
event.message(message);
for (final Pattern blockedWord : this.blockedWords) {
for (final Pattern blockedWord : this.carbonChat.moderationSettings().filters().blockedPatterns()) {
if (blockedWord.matcher(event.message()).find()) {
event.cancelled(true);
break;
@@ -68,38 +61,6 @@ public class FilterContext {
});
}
public void reloadFilters() {
this.patternReplacements.clear();
this.blockedWords.clear();
// final FileConfiguration config = this.carbonChat.moderationConfig();
// final ConfigurationSection filters = config.getConfigurationSection("filters.filters");
//
// if (filters != null) {
// for (final String replacement : filters.getKeys(false)) {
// final List<Pattern> patterns = new ArrayList<>();
//
// for (final String word : filters.getStringList(replacement)) {
// if (this.carbonChat.moderationConfig().getBoolean("filters.case-sensitive")) {
// patterns.add(Pattern.compile(word));
// } else {
// patterns.add(Pattern.compile(word, Pattern.CASE_INSENSITIVE));
// }
// }
//
// this.patternReplacements.put(replacement, patterns);
// }
// }
//
// for (final String replacement : config.getStringList("filters.blocked-words")) {
// if (this.carbonChat.moderationConfig().getBoolean("filters.case-sensitive")) {
// this.blockedWords.add(Pattern.compile(replacement));
// } else {
// this.blockedWords.add(Pattern.compile(replacement, Pattern.CASE_INSENSITIVE));
// }
// }
}
private boolean channelUsesFilter(final @NonNull ChatChannel chatChannel) {
final Context context = chatChannel.context("filter");
@@ -1,9 +1,6 @@
# CarbonChat Docs:
# https://github.com/Draycia/Chat/wiki
# Set this to false to disable warnings and tips when the plugin thinks there may be configuration issues.
show-tips: true
# All of the settings under this 'default' section can be applied to each channel individually to override these.
# These are provided as a way to configure default channel settings.
default:
@@ -93,19 +90,6 @@ channels:
is-cross-server: false
message-prefix: '!'
# Used for message formats - custom tags that are replaced in descending order
# For example, in the default config, <group> wil be replaced with <green>%vault_group%
placeholders:
prefix: "<gray>[<group><gray>]"
group: "<green>%vault_group%"
# Sets the player's channel to the specified channel when they join
# Set to "" or remove to disable
# Set to DEFAULT to set the player's channel to the default channel on join
# Otherwise, set to a channel to set the player's channel to it on join
# For example, channel-on-join: "global" sets their channel to global on join
channel-on-join: ""
# Global context options
contexts:
Towny:
@@ -113,61 +97,9 @@ contexts:
mcMMO:
cancellation-message: '<red>You do not have a party!'
# Used for <server> placeholder in channel formats
server-name: 'Server'
item-link-placeholders:
- '[item]'
- '%item%'
# The prefix shown when spying on a user's message
spy-prefix: '<color>[SPY] '
# Determines if the player's tab menu name is updated with /nickname
nicknames-set-tab-name: true
# Plays a sound and highlights the message when someone types your name
pings:
enabled: true
case-sensitive: false
source: 'PLAYER'
sound: 'minecraft:entity.experience_orb.pickup'
prefix: '@'
volume: 10.0
pitch: 1.0
# highlight: true # currently disabled and nonfunctional
# Plays a sound when receiving private messages with /whisper /msg
whisper:
pings:
enabled: true
source: 'PLAYER'
sound: 'minecraft:entity.experience_orb.pickup'
volume: 10.0
pitch: 1.0
# Options:
# 'JSON' - Uses JSON text files to store player data
# 'MySQL' - Uses MySQL to store player data
storage:
type: JSON
username: 'username'
password: 'password'
database: 'database'
hostname: 'localhost'
port: 3306
# Options:
# 'bungee' - Uses bungee plugin messaging, requires BungeeCord or another proxy which supports it (Velocity!)
# 'redis' - Uses redis for cross server syncing, does not require a server proxy
# 'none' - Do not sync anything cross server, this is the default
#
# Note: In order for channels to sync cross server, you'll need to enable is-cross-server for the
# channel as well as this setting.
message-system: 'none'
redis:
host: 'localhost'
port: 6379
#password: 'password'
database: 0
@@ -1,18 +0,0 @@
filters:
enabled: true
case-sensitive: false
# The keys ("****" for example) are what the text is replaced with
# The strings in the lists ("lag" etc) are what's replaced
# Set to filters: {} if you want to disable the filter feature.
filters:
# In this example, any instance of "lag" or "laggggg" will be replaced with "****"
"****":
- "la[g]+"
- "lag"
# Underscore = blank, empty. The text will basically be removed.
"_":
- "broccoli sucks"
# Anything in blocked-words will prevent the message from being sent at all.
# Set to blocked-words: [] if you want to disable the blocked words feature.
blocked-words:
- "pineapple doesn't belong on pizza"