Refactor
This commit is contained in:
@@ -10,6 +10,7 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
// Todo this is only needed to get some special usecases eg [i] -> get and return the item the player is holding over a SYNCRONIZED connection:/
|
||||
public class ChatPlugin extends JavaPlugin {
|
||||
|
||||
private static ChatPlugin instance;
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.alttd.chat.commands;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
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;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class GlobalChat implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if(!(sender instanceof Player)) { // must be a player
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
if(args.length == 0) return false;
|
||||
if(args[0].equalsIgnoreCase("togglegc")) {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Objects.requireNonNull(ChatUserManager.getChatUser(((Player) sender).getUniqueId())).toggleGc();
|
||||
}
|
||||
}.runTask(ChatPlugin.getInstance());
|
||||
}
|
||||
|
||||
String message = StringUtils.join(args, " ", 0, args.length);
|
||||
ChatPlugin.getInstance().getChatHandler().globalChat(player, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,80 +27,7 @@ public class ChatHandler {
|
||||
plugin = ChatPlugin.getInstance();
|
||||
}
|
||||
|
||||
public void globalChat(Player player, String message) {
|
||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
if(user == null) return;
|
||||
if(!user.isGcOn()) {
|
||||
player.sendMessage();// GC IS OFF INFORM THEM ABOUT THIS and cancel
|
||||
return;
|
||||
}
|
||||
// Check if the player has global chat enabled, if not warn them
|
||||
String senderName, prefix = "";
|
||||
|
||||
senderName = player.getDisplayName(); // TODO this can be a component
|
||||
// can also be cached in the chatuser object?
|
||||
prefix = plugin.getChatAPI().getPrefix(player.getUniqueId());
|
||||
|
||||
MiniMessage miniMessage = MiniMessage.get();
|
||||
message = Utility.parseColors(message);
|
||||
if(!player.hasPermission("chat.format"))
|
||||
message = miniMessage.stripTokens(message);
|
||||
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),
|
||||
Template.of("server", Bukkit.getServerName())/*,
|
||||
Template.of("[i]", itemComponent(sender.getInventory().getItemInMainHand()))*/));
|
||||
|
||||
Component component = miniMessage.parse(Config.GCFORMAT, templates);
|
||||
|
||||
//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));
|
||||
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||
}
|
||||
|
||||
// Start - move these to util
|
||||
public Component itemComponent(ItemStack item) {
|
||||
Component component = Component.text("[i]");
|
||||
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();
|
||||
}
|
||||
// end - move these to util
|
||||
}
|
||||
|
||||
@@ -22,4 +22,45 @@ public class PluginMessage implements PluginMessageListener {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* // todo implement AdvancedChatFilter for this like this
|
||||
// send pluginmessage to backend server and return a shatteredcomponent to be reparsed by proxy
|
||||
// Start - move these to util
|
||||
public Component itemComponent(ItemStack item) {
|
||||
Component component = Component.text("[i]");
|
||||
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();
|
||||
}
|
||||
// end - move these to util
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user