Begin API abstraction!

This commit is contained in:
Draycia committed 2020-09-25 01:43:16 -07:00
1 parent 59511c037f
commit 7299ff64a5
86 files changed
+399 -436

No files matched your search

@@ -0,0 +1,74 @@
package net.draycia.carbon.managers;
import de.themoep.minedown.adventure.MineDown;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.CarbonChat;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class AdventureManager {
@NonNull
private final CarbonChat carbonChat;
@NonNull
private final BukkitAudiences audiences;
public AdventureManager(@NonNull final CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.audiences = BukkitAudiences.create(carbonChat);
}
@NonNull
public Component processMessageWithPapi(@NonNull final Player player, @Nullable final String input,
@NonNull final String @NonNull ... placeholders) {
if (input == null || input.trim().isEmpty()) {
return TextComponent.empty();
}
return this.processMessage(PlaceholderAPI.setPlaceholders(player, input), placeholders);
}
@NonNull
public Component processMessage(@Nullable final String input, @NonNull final String @NonNull ... placeholders) {
if (input == null || input.trim().isEmpty()) {
return TextComponent.empty();
}
switch (this.carbonChat.language().getString("formatting.type", "minimessage").toLowerCase()) {
case "minedown":
return MineDown.parse(input, placeholders);
case "mojang":
case "mojangson":
case "json":
return this.processMojang(input, placeholders);
case "minimessage-markdown":
return MiniMessage.markdown().parse(input, placeholders);
case "minimessage":
default:
return MiniMessage.get().parse(input, placeholders);
}
}
@NonNull
private Component processMojang(@NonNull String input, @NonNull final String @NonNull ... placeholders) {
for (int i = 0; i < placeholders.length; i += 2) {
final String placeholder = placeholders[i];
final String replacement = placeholders[i + 1];
input = input.replace("<" + placeholder + ">", replacement);
}
return this.audiences().gsonSerializer().deserialize(input);
}
@NonNull
public BukkitAudiences audiences() {
return this.audiences;
}
}
@@ -0,0 +1,115 @@
package net.draycia.carbon.managers;
import net.draycia.carbon.channels.impls.CarbonChatChannel;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.ChannelRegisterEvent;
import org.bukkit.configuration.ConfigurationSection;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collections;
public class ChannelManager {
@NonNull
private final CarbonChat carbonChat;
@NonNull
private final ChannelRegistry registry;
@Nullable
@MonotonicNonNull
private String defaultChannelKey = null;
public ChannelManager(@NonNull final CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.registry = new ChannelRegistry(carbonChat);
this.reload();
}
public @Nullable ChatChannel loadChannel(@NonNull final String key, @NonNull final ConfigurationSection section) {
final ChatChannel channel = new CarbonChatChannel(key, this.carbonChat, section);
final String name = section.getString("name");
if (name != null && name.length() > 16) {
this.carbonChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16.");
this.carbonChat.getLogger().warning("Skipping channel, please check your settings!");
return null;
}
return channel;
}
public boolean registerChannel(@NonNull final ChatChannel channel) {
final boolean success = this.registry().register(channel.key(), channel);
if (success) {
if (channel.isDefault() && this.defaultChannelKey == null) {
this.carbonChat.getLogger().info("Default channel registered: " + channel.name());
this.defaultChannelKey = channel.key();
}
CarbonEvents.post(new ChannelRegisterEvent(Collections.singletonList(channel), this.registry()));
}
return success;
}
public @NonNull Registry<ChatChannel> registry() {
return this.registry;
}
public @Nullable ChatChannel defaultChannel() {
if (this.defaultChannelKey != null) {
return this.registry.channel(this.defaultChannelKey);
}
return null;
}
public @Nullable ChatChannel channelOrDefault(@Nullable final String key) {
if (key == null) {
return this.defaultChannel();
}
final ChatChannel channel = this.registry.channel(key);
if (channel == null) {
return this.defaultChannel();
}
return channel;
}
private void reload() {
this.registry.clearAll();
for (final String key : this.carbonChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
final ConfigurationSection section =
this.carbonChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
final ChatChannel channel = this.loadChannel(key, section);
if (channel != null) {
if (this.registerChannel(channel)) {
String prefix = channel.messagePrefix();
if (prefix != null && !prefix.trim().isEmpty()) {
prefix = "(" + prefix + ") ";
} else {
prefix = "";
}
this.carbonChat.getLogger().info("Registering channel: " + prefix + channel.name());
}
}
}
}
}
@@ -0,0 +1,60 @@
package net.draycia.carbon.managers;
import net.draycia.carbon.commands.ChannelCommand;
import net.draycia.carbon.commands.ChannelListCommand;
import net.draycia.carbon.commands.ChatReloadCommand;
import net.draycia.carbon.commands.ClearChatCommand;
import net.draycia.carbon.commands.IgnoreCommand;
import net.draycia.carbon.commands.MeCommand;
import net.draycia.carbon.commands.MessageCommand;
import net.draycia.carbon.commands.MuteCommand;
import net.draycia.carbon.commands.NicknameCommand;
import net.draycia.carbon.commands.ReplyCommand;
import net.draycia.carbon.commands.SetColorCommand;
import net.draycia.carbon.commands.SpyChannelCommand;
import net.draycia.carbon.commands.SudoChannelCommand;
import net.draycia.carbon.commands.ToggleCommand;
import net.draycia.carbon.api.commands.CommandSettings;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.commands.ShadowMuteCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.checkerframework.checker.nullness.qual.NonNull;
public class CommandManager {
@NonNull
private final CarbonChat carbonChat;
public CommandManager(@NonNull final CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.reloadCommands();
}
private void reloadCommands() {
new ChannelCommand(this.carbonChat, this.commandSettings("channel"));
new ChannelListCommand(this.carbonChat, this.commandSettings("channellist"));
new ChatReloadCommand(this.carbonChat, this.commandSettings("chatreload"));
new ClearChatCommand(this.carbonChat, this.commandSettings("clearchat"));
new IgnoreCommand(this.carbonChat, this.commandSettings("ignore"));
new MeCommand(this.carbonChat, this.commandSettings("me"));
new MessageCommand(this.carbonChat, this.commandSettings("message"));
new MuteCommand(this.carbonChat, this.commandSettings("mute"));
new NicknameCommand(this.carbonChat, this.commandSettings("nickname"));
new ReplyCommand(this.carbonChat, this.commandSettings("reply"));
new SetColorCommand(this.carbonChat, this.commandSettings("setcolor"));
new ShadowMuteCommand(this.carbonChat, this.commandSettings("shadowmute"));
new SpyChannelCommand(this.carbonChat, this.commandSettings("spy"));
new SudoChannelCommand(this.carbonChat, this.commandSettings("sudochannel"));
new ToggleCommand(this.carbonChat, this.commandSettings("toggle"));
}
@NonNull
private CommandSettings commandSettings(@NonNull final String command) {
final ConfigurationSection section = this.carbonChat.commandsConfig().getConfigurationSection(command);
return new CommandSettings(section.getBoolean("enabled"), section.getString("name"),
section.getStringList("aliases"));
}
}