update to built-in modules style

This commit is contained in:
Larry committed 2020-08-22 13:17:28 -06:00
commit 9d3e893ca9
85 files changed
+455 -702

No files matched your search

@@ -0,0 +1,59 @@
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.minimessage.MiniMessage;
import org.bukkit.entity.Player;
public class AdventureManager {
private final CarbonChat carbonChat;
private final BukkitAudiences audiences;
public AdventureManager(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.audiences = BukkitAudiences.create(carbonChat);
}
public Component processMessageWithPapi(Player player, String input, String... placeholders) {
return processMessage(PlaceholderAPI.setPlaceholders(player, input), placeholders);
}
public Component processMessage(String input, String... placeholders) {
switch (carbonChat.getLanguage().getString("formatting.type", "minimessage").toLowerCase()) {
case "minedown":
return processMineDown(input, placeholders);
case "mojang":
case "mojangson":
case "json":
return processMojang(input, placeholders);
case "minimessage-markdown":
return MiniMessage.markdown().parse(input, placeholders);
case "minimessage":
default:
return MiniMessage.get().parse(input, placeholders);
}
}
private Component processMojang(String input, String... placeholders) {
for (int i = 0; i < placeholders.length; i += 2) {
String placeholder = placeholders[i];
String replacement = placeholders[i+1];
input = input.replace("<" + placeholder + ">", replacement);
}
return getAudiences().gsonSerializer().deserialize(input);
}
private Component processMineDown(String input, String... placeholders) {
return MineDown.parse(input, placeholders);
}
public BukkitAudiences getAudiences() {
return audiences;
}
}
@@ -0,0 +1,106 @@
package net.draycia.carbon.managers;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChannelRegistry;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.channels.impls.*;
import net.draycia.carbon.events.ChannelRegisterEvent;
import net.draycia.carbon.util.Registry;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Collections;
public class ChannelManager {
private final CarbonChat carbonChat;
private final ChannelRegistry registry;
private String defaultChannelKey = null;
public ChannelManager(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.registry = new ChannelRegistry(carbonChat);
reload();
}
public ChatChannel loadChannel(String key, ConfigurationSection section) {
ChatChannel channel = new CarbonChatChannel(key, carbonChat, section);
String name = section.getString("name");
if (name != null && name.length() > 16) {
carbonChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16.");
carbonChat.getLogger().warning("Skipping channel, please check your settings!");
return null;
}
return channel;
}
public boolean registerChannel(ChatChannel channel) {
boolean success = getRegistry().register(channel.getKey(), channel);
if (success) {
if (channel.isDefault() && defaultChannelKey == null) {
carbonChat.getLogger().info("Default channel registered: " + channel.getName());
defaultChannelKey = channel.getKey();
}
Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(Collections.singletonList(channel), getRegistry()));
}
return success;
}
public Registry<ChatChannel> getRegistry() {
return registry;
}
public ChatChannel getDefaultChannel() {
if (defaultChannelKey != null) {
return registry.get(defaultChannelKey);
}
return null;
}
public ChatChannel getChannelOrDefault(String key) {
if (key == null) {
return getDefaultChannel();
}
ChatChannel channel = registry.get(key);
if (channel == null) {
return getDefaultChannel();
}
return channel;
}
public void reload() {
registry.clearAll();
for (String key : carbonChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
ConfigurationSection section = carbonChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
ChatChannel channel = loadChannel(key, section);
if (channel != null) {
if (registerChannel(channel)) {
String prefix = channel.getMessagePrefix();
if (prefix != null && !prefix.trim().isEmpty()) {
prefix = ("(" + prefix + ") ");
} else {
prefix = "";
}
carbonChat.getLogger().info("Registering channel: " + prefix + channel.getName());
}
}
}
}
}
@@ -0,0 +1,105 @@
package net.draycia.carbon.managers;
import co.aikar.commands.*;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.commands.*;
import net.draycia.carbon.storage.ChatUser;
import java.util.ArrayList;
import java.util.List;
public class CommandManager {
private final BukkitCommandManager commandManager;
public CommandManager(CarbonChat carbonChat) {
commandManager = new BukkitCommandManager(carbonChat);
commandManager.enableUnstableAPI("help");
commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
ChatUser user = carbonChat.getUserService().wrap(context.getPlayer());
List<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
if (chatChannel.canPlayerUse(user)) {
completions.add(chatChannel.getKey());
}
}
return completions;
});
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
List<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
completions.add(chatChannel.getKey());
}
return completions;
});
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
String name = context.popFirstArg();
for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) {
if (chatChannel.getKey().equalsIgnoreCase(name)) {
return chatChannel;
}
}
return null;
});
commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> {
return carbonChat.getUserService().wrap(context.popFirstArg());
});
commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
if (value == null) {
throw new ConditionFailedException(carbonChat.getLanguage().getString("cannot-use-channel"));
}
ChatUser user = carbonChat.getUserService().wrap(context.getIssuer().getPlayer());
if (!value.canPlayerUse(user)) {
throw new ConditionFailedException(value.getCannotUseMessage());
}
});
commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> {
if (value == null) {
throw new ConditionFailedException(carbonChat.getLanguage().getString("cannot-use-channel"));
}
});
reloadCommands();
}
public void reloadCommands() {
this.commandManager.registerCommand(new ToggleCommand());
this.commandManager.registerCommand(new ChannelCommand());
this.commandManager.registerCommand(new ChannelListCommand());
this.commandManager.registerCommand(new IgnoreCommand());
this.commandManager.registerCommand(new ChatReloadCommand());
this.commandManager.registerCommand(new MeCommand());
this.commandManager.registerCommand(new MessageCommand());
this.commandManager.registerCommand(new NicknameCommand());
this.commandManager.registerCommand(new ReplyCommand());
this.commandManager.registerCommand(new SetColorCommand());
this.commandManager.registerCommand(new SpyChannelCommand());
this.commandManager.registerCommand(new ClearChatCommand());
this.commandManager.registerCommand(new MuteCommand());
this.commandManager.registerCommand(new ShadowMuteCommand());
}
public BukkitCommandManager getCommandManager() {
return commandManager;
}
}
@@ -0,0 +1,43 @@
package net.draycia.carbon.managers;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.channels.contexts.MessageContext;
import net.draycia.carbon.channels.contexts.impl.SimpleMessageContext;
import net.draycia.carbon.storage.ChatUser;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ContextManager {
private final Map<String, Function<MessageContext, Boolean>> handlers = new HashMap<>();
public boolean register(String key, Function<MessageContext, Boolean> handler) {
if (this.handlers.containsKey(key)) {
return false;
}
this.handlers.put(key, handler);
return true;
}
public boolean testContext(ChatUser user, ChatUser target, ChatChannel channel) {
for (Map.Entry<String, Function<MessageContext, Boolean>> handler : handlers.entrySet()) {
String key = handler.getKey();
Object value = channel.getContext(key);
if (value == null) {
continue;
}
MessageContext context = new SimpleMessageContext(user, target, channel, value);
return handler.getValue().apply(context);
}
return true;
}
}