2021-05-13 12:11:29 +00:00
|
|
|
package com.alttd.chat.handler;
|
|
|
|
|
|
|
|
|
|
import com.alttd.chat.ChatPlugin;
|
|
|
|
|
import com.alttd.chat.config.Config;
|
2021-05-13 18:27:20 +00:00
|
|
|
import com.google.common.io.ByteArrayDataOutput;
|
|
|
|
|
import com.google.common.io.ByteStreams;
|
2021-05-13 12:11:29 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
2021-05-13 18:27:20 +00:00
|
|
|
import net.kyori.adventure.text.minimessage.Template;
|
2021-05-13 12:11:29 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2021-05-13 18:27:20 +00:00
|
|
|
import org.bukkit.Material;
|
2021-05-13 12:11:29 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
import org.bukkit.entity.Player;
|
2021-05-13 18:27:20 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2021-05-13 12:11:29 +00:00
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2021-05-13 12:11:29 +00:00
|
|
|
|
|
|
|
|
public class ChatHandler {
|
|
|
|
|
|
|
|
|
|
private ChatPlugin plugin;
|
|
|
|
|
|
|
|
|
|
public ChatHandler() {
|
|
|
|
|
plugin = ChatPlugin.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void globalChat(CommandSender source, String message) {
|
2021-05-15 11:52:02 +00:00
|
|
|
// Check if the player has global chat enabled, if not warn them
|
2021-05-13 12:11:29 +00:00
|
|
|
String senderName, prefix = "";
|
|
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
Player sender = (Player) source;
|
|
|
|
|
senderName = sender.getDisplayName(); // TODO this can be a component
|
|
|
|
|
prefix = plugin.getChatAPI().getPrefix(sender.getUniqueId());
|
2021-05-13 12:11:29 +00:00
|
|
|
|
|
|
|
|
MiniMessage miniMessage = MiniMessage.get();
|
|
|
|
|
if(!source.hasPermission("chat.format"))
|
|
|
|
|
message = miniMessage.stripTokens(message);
|
2021-05-13 18:27:20 +00:00
|
|
|
if(message.contains("[i]"))
|
|
|
|
|
message = message.replace("[i]", "<[i]>");
|
|
|
|
|
|
|
|
|
|
List<Template> templates = new ArrayList<>(List.of(
|
|
|
|
|
Template.of("sender", senderName),
|
|
|
|
|
Template.of("prefix", prefix),
|
|
|
|
|
Template.of("message", message),
|
2021-05-15 11:56:54 +00:00
|
|
|
Template.of("server", Bukkit.getServerName())/*,
|
|
|
|
|
Template.of("[i]", itemComponent(sender.getInventory().getItemInMainHand()))*/));
|
2021-05-13 18:27:20 +00:00
|
|
|
|
|
|
|
|
Component component = miniMessage.parse(Config.GCFORMAT, templates);
|
2021-05-13 12:11:29 +00:00
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
//todo make a method for this, it'll be used more then onc
|
|
|
|
|
|
|
|
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
|
|
|
out.writeUTF("globalchat");
|
|
|
|
|
out.writeUTF(miniMessage.serialize(component));
|
|
|
|
|
sender.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
|
|
|
|
}
|
2021-05-13 12:11:29 +00:00
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
public Component itemComponent(ItemStack item) {
|
2021-05-13 22:06:21 +00:00
|
|
|
Component component = Component.text("[i]");
|
2021-05-13 18:27:20 +00:00
|
|
|
if(item.getType().equals(Material.AIR))
|
|
|
|
|
return component;
|
|
|
|
|
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
|
|
|
|
if(dname) {
|
|
|
|
|
component = component.append(item.getItemMeta().displayName());
|
|
|
|
|
} else {
|
|
|
|
|
component = Component.text(materialToName(item.getType()));
|
|
|
|
|
}
|
|
|
|
|
component = component.hoverEvent(item.asHoverEvent());
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String materialToName(Material m) {
|
|
|
|
|
if (m.equals(Material.TNT)) {
|
|
|
|
|
return "TNT";
|
|
|
|
|
}
|
|
|
|
|
String orig = m.toString().toLowerCase();
|
|
|
|
|
String[] splits = orig.split("_");
|
|
|
|
|
StringBuilder sb = new StringBuilder(orig.length());
|
|
|
|
|
int pos = 0;
|
|
|
|
|
for (String split : splits) {
|
|
|
|
|
sb.append(split);
|
|
|
|
|
int loc = sb.lastIndexOf(split);
|
|
|
|
|
char charLoc = sb.charAt(loc);
|
|
|
|
|
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
|
|
|
|
|
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
|
|
|
|
|
sb.setCharAt(loc, Character.toUpperCase(charLoc));
|
|
|
|
|
if (pos != splits.length - 1)
|
|
|
|
|
sb.append(' ');
|
|
|
|
|
++pos;
|
|
|
|
|
}
|
2021-05-13 12:11:29 +00:00
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
return sb.toString();
|
2021-05-13 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
}
|