1.14 support, fixes #105
13 changed files with 392 additions and 313 deletions
|
@ -1,11 +1,19 @@
|
|||
package buttondevteam.chat;
|
||||
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
import buttondevteam.lib.ThorpeUtils;
|
||||
import lombok.var;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class ChatUtils {
|
||||
private ChatUtils() {}
|
||||
public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
|
||||
|
||||
private ChatUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a console command.
|
||||
|
@ -33,4 +41,20 @@ public final class ChatUtils {
|
|||
int b = str.indexOf(end, a);
|
||||
return a != -1 && b != -1 ? Optional.of(str.substring(a, b)) : Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a regular (non-Markdown) chat message. Used as a fallback if the chat processing fails.
|
||||
*
|
||||
* @param e The chat event
|
||||
* @param modifier A function that alters the message to be displayed to the player
|
||||
*/
|
||||
public static void sendChatMessage(TBMCChatEvent e, Function<String, String> modifier) {
|
||||
var str = "[" + e.getChannel().DisplayName().get() + "] <"
|
||||
+ ThorpeUtils.getDisplayName(e.getSender()) + "> " + e.getMessage();
|
||||
str = modifier.apply(str);
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
if (e.shouldSendTo(p))
|
||||
p.sendMessage(str);
|
||||
Bukkit.getConsoleSender().sendMessage(str);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package buttondevteam.chat.components.chatonly;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.formatting.TellrawPart;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
package buttondevteam.chat;
|
||||
package buttondevteam.chat.components.formatter;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.ChatUtils;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.VanillaUtils;
|
||||
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
|
||||
import buttondevteam.chat.components.chatonly.ChatOnlyComponent;
|
||||
import buttondevteam.chat.components.formatter.formatting.ChatFormatter;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawSerializer;
|
||||
import buttondevteam.chat.components.fun.FunComponent;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.chat.formatting.ChatFormatter;
|
||||
import buttondevteam.chat.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.formatting.TellrawPart;
|
||||
import buttondevteam.chat.formatting.TellrawSerializer;
|
||||
import buttondevteam.chat.listener.PlayerListener;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
|
@ -101,12 +105,11 @@ public class ChatProcessing {
|
|||
.registerTypeAdapter(Boolean.class, new TellrawSerializer.TwBool())
|
||||
.registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create();
|
||||
private static final String[] testPlayers = {"Koiiev", "iie", "Alisolarflare", "NorbiPeti", "Arsen_Derby_FTW", "carrot_lynx"};
|
||||
public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
|
||||
|
||||
private ChatProcessing() {
|
||||
}
|
||||
|
||||
public static boolean ProcessChat(TBMCChatEvent e) {
|
||||
public static boolean ProcessChat(TBMCChatEvent e, FormatterComponent component) {
|
||||
Channel channel = e.getChannel();
|
||||
CommandSender sender = e.getSender();
|
||||
String message = e.getMessage();
|
||||
|
@ -141,7 +144,9 @@ public class ChatProcessing {
|
|||
colormode = Color.Green;
|
||||
// If greentext, ignore channel or player colors
|
||||
|
||||
ArrayList<ChatFormatter> formatters = addFormatters(colormode, e::shouldSendTo);
|
||||
ArrayList<ChatFormatter> formatters;
|
||||
if (component.allowFormatting().get()) {
|
||||
formatters = addFormatters(colormode, e::shouldSendTo);
|
||||
if (colormode == channel.Color().get() && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color
|
||||
final AtomicInteger rpc = new AtomicInteger(0);
|
||||
formatters.add(ChatFormatter.builder().regex(WORD_PATTERN).color(colormode).onmatch((match, cf, s) -> {
|
||||
|
@ -150,10 +155,13 @@ public class ChatProcessing {
|
|||
}).build());
|
||||
}
|
||||
pingedconsole = false; // Will set it to true onmatch (static constructor)
|
||||
} else
|
||||
formatters = Lists.newArrayList(ChatFormatter.builder().regex(ENTIRE_MESSAGE_PATTERN)
|
||||
.color(Color.White).priority(Priority.Low).build()); //This formatter is necessary
|
||||
|
||||
TellrawPart json = createTellraw(sender, message, player, mp, e.getUser(), channelidentifier, e.getOrigin());
|
||||
long combinetime = System.nanoTime();
|
||||
ChatFormatter.Combine(formatters, message, json);
|
||||
ChatFormatter.Combine(formatters, message, json, component.getConfig());
|
||||
combinetime = System.nanoTime() - combinetime;
|
||||
String jsonstr = toJson(json);
|
||||
if (jsonstr.length() >= 32767) {
|
||||
|
@ -211,7 +219,7 @@ public class ChatProcessing {
|
|||
new TellrawPart(channelidentifier)
|
||||
.setHoverEvent(
|
||||
TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT,
|
||||
new TellrawPart((MCORIGIN.equals(origin) ? "" : "From " + origin + "n")
|
||||
new TellrawPart((ChatUtils.MCORIGIN.equals(origin) ? "" : "From " + origin + "n")
|
||||
+ "Copy message").setColor(Color.Blue)))
|
||||
.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAction.SUGGEST_COMMAND, message)));
|
||||
if (PluginMain.permission.has(sender, "tbmc.badge.diamond"))
|
||||
|
@ -237,7 +245,7 @@ public class ChatProcessing {
|
|||
}
|
||||
|
||||
static String getChannelID(Channel channel, String origin) {
|
||||
return ("[" + (MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get())
|
||||
return ("[" + (ChatUtils.MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get())
|
||||
+ "]";
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package buttondevteam.chat.components.formatter;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
|
||||
/**
|
||||
* This component handles the custom processing of chat messages. If this component is disabled channels won't be supported either in Minecraft.
|
||||
* If you only want to disable the formatting features, set allowFormatting to false.
|
||||
*/
|
||||
public class FormatterComponent extends Component<PluginMain> {
|
||||
ConfigData<Boolean> allowFormatting() {
|
||||
return getConfig().getData("allowFormatting", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void enable() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the chat if the component is enabled.
|
||||
*
|
||||
* @param event The chat event
|
||||
* @return Whether the chat message shouldn't be sent for some reason
|
||||
*/
|
||||
public static boolean handleChat(TBMCChatEvent event) {
|
||||
FormatterComponent component = ComponentManager.getIfEnabled(FormatterComponent.class);
|
||||
if (component == null) return false;
|
||||
return ChatProcessing.ProcessChat(event, component);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package buttondevteam.chat.formatting;
|
||||
package buttondevteam.chat.components.formatter.formatting;
|
||||
|
||||
import buttondevteam.chat.ChatProcessing;
|
||||
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
|
||||
import buttondevteam.chat.components.formatter.ChatProcessing;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import buttondevteam.lib.architecture.IHaveConfig;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.Priority;
|
||||
import lombok.Builder;
|
||||
|
@ -40,6 +42,11 @@ public final class ChatFormatter {
|
|||
@Builder.Default
|
||||
Type type = Type.Normal;
|
||||
String hoverText;
|
||||
String name;
|
||||
|
||||
private ConfigData<Boolean> enabled(IHaveConfig config) {
|
||||
return config.getData(name + ".enabled", true);
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
Normal,
|
||||
|
@ -58,13 +65,15 @@ public final class ChatFormatter {
|
|||
R apply(T1 x1, T2 x2, T3 x3);
|
||||
}
|
||||
|
||||
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
|
||||
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp, IHaveConfig config) {
|
||||
/*
|
||||
* This method assumes that there is always a global formatter
|
||||
*/
|
||||
header("ChatFormatter.Combine begin");
|
||||
ArrayList<FormattedSection> sections = new ArrayList<>();
|
||||
|
||||
if (config != null) //null if testing
|
||||
formatters.removeIf(cf -> !cf.enabled(config).get()); //Remove disabled formatters
|
||||
createSections(formatters, str, sections, true);
|
||||
|
||||
header("Section creation (excluders done)");
|
|
@ -1,4 +1,4 @@
|
|||
package buttondevteam.chat.formatting;
|
||||
package buttondevteam.chat.components.formatter.formatting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
|
@ -1,9 +1,9 @@
|
|||
package buttondevteam.chat.formatting;
|
||||
|
||||
import java.io.Serializable;
|
||||
package buttondevteam.chat.components.formatter.formatting;
|
||||
|
||||
import buttondevteam.lib.chat.TellrawSerializableEnum;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public final class TellrawEvent<T extends TellrawEvent.Action> implements Serializable {
|
||||
private static final long serialVersionUID = -1681364161210561505L;
|
||||
private transient boolean hoverEvent;
|
|
@ -1,11 +1,11 @@
|
|||
package buttondevteam.chat.formatting;
|
||||
package buttondevteam.chat.components.formatter.formatting;
|
||||
|
||||
import buttondevteam.lib.chat.Color;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import buttondevteam.lib.chat.*;
|
||||
|
||||
public final class TellrawPart implements Serializable {
|
||||
private static final long serialVersionUID = 4125357644462144024L;
|
||||
private Color color;
|
|
@ -1,14 +1,14 @@
|
|||
package buttondevteam.chat.formatting;
|
||||
package buttondevteam.chat.components.formatter.formatting;
|
||||
|
||||
import buttondevteam.lib.chat.TellrawSerializableEnum;
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.stream.*;
|
||||
|
||||
import buttondevteam.lib.chat.TellrawSerializableEnum;
|
||||
|
||||
public abstract class TellrawSerializer {
|
||||
public static class TwEnum extends TypeAdapter<TellrawSerializableEnum> {
|
||||
@Override
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.chat.components.towny;
|
||||
|
||||
import buttondevteam.chat.ChatProcessing;
|
||||
import buttondevteam.chat.ChatUtils;
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
|
@ -34,18 +34,18 @@ public class TownyAnnouncer {
|
|||
if (townChannel == null) return;
|
||||
TBMCChatAPI.SendSystemMessage(townChannel,
|
||||
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, false), groupID),
|
||||
message, target, ChatProcessing.MCORIGIN);
|
||||
message, target, ChatUtils.MCORIGIN);
|
||||
break;
|
||||
case "Nation":
|
||||
if (nationChannel == null) return;
|
||||
TBMCChatAPI.SendSystemMessage(nationChannel,
|
||||
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, true), groupID),
|
||||
message, target, ChatProcessing.MCORIGIN);
|
||||
message, target, ChatUtils.MCORIGIN);
|
||||
break;
|
||||
case "Global":
|
||||
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat,
|
||||
Channel.RecipientTestResult.ALL,
|
||||
message, target, ChatProcessing.MCORIGIN);
|
||||
message, target, ChatUtils.MCORIGIN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package buttondevteam.chat.components.towny;
|
|||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.VanillaUtils;
|
||||
import buttondevteam.chat.formatting.TellrawPart;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package buttondevteam.chat.listener;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.ChatProcessing;
|
||||
import buttondevteam.chat.ChatUtils;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||
import buttondevteam.chat.components.flair.FlairComponent;
|
||||
import buttondevteam.chat.components.formatter.FormatterComponent;
|
||||
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
|
@ -51,6 +52,7 @@ public class PlayerListener implements Listener {
|
|||
if (event.isCancelled())
|
||||
return;
|
||||
//The custom event is called in the core, but doesn't cancel the MC event
|
||||
if (ComponentManager.isEnabled(FormatterComponent.class)) //If not enabled, then let the other plugins deal with the message
|
||||
event.setCancelled(true); // The custom event should only be cancelled when muted or similar
|
||||
}
|
||||
|
||||
|
@ -165,14 +167,9 @@ public class PlayerListener implements Listener {
|
|||
if (e.isCancelled())
|
||||
return;
|
||||
HistoryCommand.addChatMessage(e.getCm(), e.getChannel());
|
||||
e.setCancelled(ChatProcessing.ProcessChat(e));
|
||||
e.setCancelled(FormatterComponent.handleChat(e));
|
||||
} catch (NoClassDefFoundError | Exception ex) { // Weird things can happen
|
||||
val str = "§c!§r[" + e.getChannel().DisplayName().get() + "] <"
|
||||
+ ThorpeUtils.getDisplayName(e.getSender()) + "> " + e.getMessage();
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
if (e.shouldSendTo(p))
|
||||
p.sendMessage(str);
|
||||
Bukkit.getConsoleSender().sendMessage(str);
|
||||
ChatUtils.sendChatMessage(e, s -> "§c!§r" + s);
|
||||
TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
package buttondevteam.chat;
|
||||
package buttondevteam.chat.components.formatter;
|
||||
|
||||
import buttondevteam.chat.ChatUtils;
|
||||
import buttondevteam.chat.ObjectTestRunner;
|
||||
import buttondevteam.chat.ObjectTestRunner.Objects;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
|
||||
import buttondevteam.chat.formatting.ChatFormatter;
|
||||
import buttondevteam.chat.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.formatting.TellrawEvent.ClickAction;
|
||||
import buttondevteam.chat.formatting.TellrawEvent.HoverAction;
|
||||
import buttondevteam.chat.formatting.TellrawPart;
|
||||
import buttondevteam.chat.components.formatter.formatting.ChatFormatter;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawEvent;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawEvent.ClickAction;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawEvent.HoverAction;
|
||||
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
|
||||
import buttondevteam.core.TestPrepare;
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
|
@ -19,8 +23,6 @@ import org.mockito.Mockito;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(ObjectTestRunner.class)
|
||||
public class ChatFormatIT {
|
||||
@Objects
|
||||
|
@ -92,15 +94,15 @@ public class ChatFormatIT {
|
|||
@Test
|
||||
public void testMessage() {
|
||||
ArrayList<ChatFormatter> cfs = ChatProcessing.addFormatters(Color.White, p -> true);
|
||||
final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, ChatProcessing.MCORIGIN);
|
||||
final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatProcessing.MCORIGIN);
|
||||
ChatFormatter.Combine(cfs, message, tp);
|
||||
final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, ChatUtils.MCORIGIN);
|
||||
final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatUtils.MCORIGIN);
|
||||
ChatFormatter.Combine(cfs, message, tp, null);
|
||||
System.out.println("Testing: " + message);
|
||||
// System.out.println(ChatProcessing.toJson(tp));
|
||||
final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatProcessing.MCORIGIN);
|
||||
final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatUtils.MCORIGIN);
|
||||
// System.out.println("Raw: " + ChatProcessing.toJson(expectedtp));
|
||||
for (TellrawPart extra : extras)
|
||||
expectedtp.addExtra(extra);
|
||||
assertEquals(ChatProcessing.toJson(expectedtp), ChatProcessing.toJson(tp));
|
||||
Assert.assertEquals(ChatProcessing.toJson(expectedtp), ChatProcessing.toJson(tp));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue