Fix /setcolor and fix color parsing

This commit is contained in:
Draycia committed 2020-08-24 23:56:40 -07:00
1 parent e8040120d7
commit 92505974d7
8 files changed
+58 -58

No files matched your search

@@ -12,18 +12,16 @@ import net.draycia.carbon.storage.UserService;
import net.draycia.carbon.storage.impl.JSONUserService;
import net.draycia.carbon.storage.impl.MySQLUserService;
import net.draycia.carbon.util.CarbonPlaceholders;
import net.draycia.carbon.util.ItemStackUtils;
import net.draycia.carbon.util.CarbonUtils;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public final class CarbonChat extends JavaPlugin {
@@ -37,7 +35,7 @@ public final class CarbonChat extends JavaPlugin {
private UserService userService;
private MessageManager messageManager;
private ItemStackUtils itemStackUtils;
private CarbonUtils itemStackUtils;
private YamlConfiguration modConfig;
private YamlConfiguration languageConfig;
@@ -72,7 +70,7 @@ public final class CarbonChat extends JavaPlugin {
adventureManager = new AdventureManager(this);
try {
itemStackUtils = new ItemStackUtils();
itemStackUtils = new CarbonUtils();
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
@@ -197,7 +195,7 @@ public final class CarbonChat extends JavaPlugin {
return userService;
}
public ItemStackUtils getItemStackUtils() {
public CarbonUtils getItemStackUtils() {
return itemStackUtils;
}
@@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
public abstract class ChatChannel implements ForwardingAudience {
@@ -6,7 +6,6 @@ import net.milkbowl.vault.economy.Economy;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
public class EconomyContext implements Listener {
private final CarbonChat carbonChat;
@@ -12,7 +12,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
public final class mcMMOContext implements Listener {
private CarbonChat carbonChat;
private final CarbonChat carbonChat;
public mcMMOContext(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
@@ -1,19 +1,17 @@
package net.draycia.carbon.channels.impls;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.events.ChatComponentEvent;
import net.draycia.carbon.events.ChatFormatEvent;
import net.draycia.carbon.events.PreChatFormatEvent;
import net.draycia.carbon.storage.ChatUser;
import net.draycia.carbon.util.CarbonUtils;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@@ -77,16 +75,6 @@ public class CarbonChatChannel extends ChatChannel {
}
}
private TextColor getColor(ChatUser user) {
TextColor userColor = user.getChannelSettings(this).getColor();
if (userColor != null) {
return userColor;
} else {
return getChannelColor(user);
}
}
public Component sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromRemote) {
updateUserNickname(user);
@@ -126,7 +114,7 @@ public class CarbonChatChannel extends ChatChannel {
continue;
}
TextColor targetColor = getColor(target);
TextColor targetColor = getChannelColor(target);
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
"br", "\n",
@@ -159,7 +147,7 @@ public class CarbonChatChannel extends ChatChannel {
TextComponent consoleMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(consoleFormatEvent.getFormat(),
"br", "\n",
"displayname", displayName,
"color", "<" + getColor(user).asHexString() + ">",
"color", "<" + getChannelColor(user).asHexString() + ">",
"phase", Long.toString(System.currentTimeMillis() % 25),
"server", carbonChat.getConfig().getString("server-name", "Server"),
"message", consoleFormatEvent.getMessage());
@@ -328,40 +316,25 @@ public class CarbonChatChannel extends ChatChannel {
@Override
public TextColor getChannelColor(ChatUser user) {
String color = getString("color");
TextColor userColor = user.getChannelSettings(this).getColor();
if (color == null) {
color = "white";
if (userColor != null) {
System.out.println("user color found!");
return userColor;
}
if (user.isOnline()) {
color = PlaceholderAPI.setPlaceholders(user.asPlayer(), color);
}
String input = getString("color");
for (NamedTextColor namedColor : NamedTextColor.values()) {
if (namedColor.toString().equalsIgnoreCase(color)) {
return namedColor;
}
}
TextColor color = CarbonUtils.parseColor(user, input);
if (color.contains("&") || color.contains("§")) {
color = color.replace("&", "§");
return LegacyComponentSerializer.legacySection().deserialize(color).color();
}
TextColor textColor = TextColor.fromHexString(color);
if (textColor != null) {
return textColor;
}
if (carbonChat.getConfig().getBoolean("show-tips")) {
if (color == null && carbonChat.getConfig().getBoolean("show-tips")) {
carbonChat.getLogger().warning("Tip: Channel color found (" + color + ") is invalid!");
carbonChat.getLogger().warning("Falling back to #FFFFFF");
return NamedTextColor.WHITE;
}
return NamedTextColor.WHITE;
return color;
}
private String getDefaultFormatName() {
@@ -7,10 +7,11 @@ import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser;
import net.draycia.carbon.storage.UserChannelSettings;
import net.draycia.carbon.util.CarbonUtils;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.entity.Player;
@CommandAlias("setcolor")
@CommandAlias("setcolor|setcolour")
@CommandPermission("carbonchat.setcolor")
public class SetColorCommand extends BaseCommand {
@@ -20,8 +21,8 @@ public class SetColorCommand extends BaseCommand {
@Default
@CommandCompletion("@chatchannel")
@Syntax("<channel> <color>")
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, String color) {
if (channel == null || color == null) {
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, String input) {
if (channel == null || input == null) {
throw new ConditionFailedException("Channel or Color not supplied!");
}
@@ -32,11 +33,12 @@ public class SetColorCommand extends BaseCommand {
}
UserChannelSettings settings = user.getChannelSettings(channel);
TextColor color = CarbonUtils.parseColor(user, input);
settings.setColor(TextColor.fromHexString(color));
settings.setColor(color);
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, carbonChat.getLanguage().getString("channel-color-set"),
"br", "\n", "color", "<color:" + color + ">", "channel", channel.getName(), "hex", color));
"br", "\n", "color", "<color:" + color.asHexString() + ">", "channel", channel.getName(), "hex", color.asHexString()));
}
}
@@ -21,8 +21,8 @@ import java.util.function.Consumer;
public class RedisMessageService implements MessageService {
private Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
private Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
private final Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
private final Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
private final RedisPubSubCommands<String, String> subscribeSync;
private final RedisPubSubCommands<String, String> publishSync;
@@ -20,9 +20,14 @@
*/
package net.draycia.carbon.util;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.storage.ChatUser;
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -31,12 +36,12 @@ import org.bukkit.inventory.ItemStack;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public final class ItemStackUtils {
public final class CarbonUtils {
private final Method asNMSCopyMethod;
private final Method cMethod;
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
public CarbonUtils() throws NoSuchMethodException, ClassNotFoundException {
final String version = Bukkit.getServer().getClass().getPackage().getName()
.substring("org.bukkit.craftbukkit.".length());
@@ -64,4 +69,28 @@ public final class ItemStackUtils {
}
}
public static TextColor parseColor(ChatUser user, String input) {
if (input == null) {
input = "white";
}
if (user.isOnline()) {
input = PlaceholderAPI.setPlaceholders(user.asPlayer(), input);
}
for (NamedTextColor namedColor : NamedTextColor.values()) {
if (namedColor.toString().equalsIgnoreCase(input)) {
return namedColor;
}
}
if (input.contains("&") || input.contains("§")) {
input = input.replace("&", "§");
return LegacyComponentSerializer.legacySection().deserialize(input).color();
}
return TextColor.fromCSSHexString(input);
}
}