Plugin Message channal and global chat check
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.commands.GlobalChat;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.handler.ChatHandler;
|
||||
import com.alttd.chat.listeners.PlayerListener;
|
||||
import com.alttd.chat.listeners.PluginMessage;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@@ -14,6 +16,8 @@ public class ChatPlugin extends JavaPlugin {
|
||||
private ChatAPI chatAPI;
|
||||
private ChatHandler chatHandler;
|
||||
|
||||
private String messageChannel;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
@@ -21,6 +25,11 @@ public class ChatPlugin extends JavaPlugin {
|
||||
chatHandler = new ChatHandler();
|
||||
registerListener(new PlayerListener());
|
||||
registerCommand("globalchat", new GlobalChat());
|
||||
|
||||
messageChannel = Config.MESSAGECHANNEL;
|
||||
getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel);
|
||||
getServer().getMessenger().registerIncomingPluginChannel(this, messageChannel, new PluginMessage());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,11 +5,15 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class GlobalChat implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if(!(sender instanceof Player)) {
|
||||
return true;
|
||||
}
|
||||
String message = StringUtils.join(args, " ", 0, args.length);
|
||||
ChatPlugin.getInstance().getChatHandler().globalChat(sender, message);
|
||||
return false;
|
||||
|
||||
@@ -2,13 +2,20 @@ package com.alttd.chat.handler;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChatHandler {
|
||||
@@ -21,29 +28,71 @@ public class ChatHandler {
|
||||
|
||||
public void globalChat(CommandSender source, String message) {
|
||||
String senderName, prefix = "";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
if (source instanceof Player) {
|
||||
Player sender = (Player) source;
|
||||
senderName = sender.getDisplayName();
|
||||
prefix = plugin.getChatAPI().getPrefix(sender.getUniqueId());
|
||||
} else {
|
||||
senderName = Config.CONSOLENAME;
|
||||
}
|
||||
Player sender = (Player) source;
|
||||
senderName = sender.getDisplayName(); // TODO this can be a component
|
||||
prefix = plugin.getChatAPI().getPrefix(sender.getUniqueId());
|
||||
|
||||
MiniMessage miniMessage = MiniMessage.get();
|
||||
if(!source.hasPermission("chat.format"))
|
||||
message = miniMessage.stripTokens(message);
|
||||
if(message.contains("[i]"))
|
||||
message = message.replace("[i]", "<[i]>");
|
||||
|
||||
map.put("sender", senderName);
|
||||
map.put("message", message);
|
||||
map.put("server", Bukkit.getServerName());
|
||||
map.put("prefix", prefix);
|
||||
List<Template> templates = new ArrayList<>(List.of(
|
||||
Template.of("sender", senderName),
|
||||
Template.of("prefix", prefix),
|
||||
Template.of("message", message),
|
||||
Template.of("server", Bukkit.getServerName()),
|
||||
Template.of("[i]", itemComponent(sender.getInventory().getItemInMainHand()))));
|
||||
|
||||
Component component = miniMessage.parse(Config.GCFORMAT, map);
|
||||
Component component = miniMessage.parse(Config.GCFORMAT, templates);
|
||||
|
||||
Bukkit.broadcast(component, Config.GCPERMISSION);
|
||||
// TODO this should be a plugin message, so proxy can handle the forwarding, we only do this on server level for [i] support
|
||||
Bukkit.broadcast(miniMessage.serialize(component), Config.GCPERMISSION);
|
||||
|
||||
//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());
|
||||
|
||||
}
|
||||
|
||||
public Component itemComponent(ItemStack item) {
|
||||
Component component = Component.text("[i]"); // todo config stuff
|
||||
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;
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.alttd.chat.listeners;
|
||||
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
|
||||
public class PluginMessage implements PluginMessageListener {
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
|
||||
if(!channel.equals(Config.MESSAGECHANNEL)) {
|
||||
return;
|
||||
}
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
|
||||
String subChannel = in.readUTF();
|
||||
switch(subChannel) {
|
||||
case "globalchat":
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user