refactor some bits

This commit is contained in:
len 2021-05-12 10:43:30 +02:00
parent 21a15c3817
commit b8ff6a1fb4
9 changed files with 94 additions and 256 deletions

View File

@ -2,11 +2,11 @@ package com.alttd.chat.api;
import com.velocitypowered.api.command.CommandSource;
public class GlobalStaffChatEvent {
public class GlobalAdminChatEvent {
private final CommandSource sender;
private final String message;
public GlobalStaffChatEvent(CommandSource sender, String message) {
public GlobalAdminChatEvent(CommandSource sender, String message) {
this.sender = sender;
this.message = message;
}

View File

@ -1,29 +0,0 @@
package com.alttd.chat.api;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
public class MessageEvent {
private final CommandSource sender;
private final Player recipient;
private final String message;
public MessageEvent(CommandSource sender, Player recipient, String message) {
this.sender = sender;
this.recipient = recipient;
this.message = message;
}
public CommandSource getSender() {
return sender;
}
public Player getRecipient() {
return recipient;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,44 @@
package com.alttd.chat.api;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.Player;
import java.util.Objects;
public class PrivateMessageEvent implements ResultedEvent<ResultedEvent.GenericResult> {
private final CommandSource sender;
private final Player recipient;
private final String message;
private GenericResult result = GenericResult.allowed(); // Allowed by default
public PrivateMessageEvent(CommandSource sender, Player recipient, String message) {
this.sender = sender;
this.recipient = recipient;
this.message = message;
}
public CommandSource getSender() {
return sender;
}
public Player getRecipient() {
return recipient;
}
public String getMessage() {
return message;
}
@Override
public GenericResult getResult() {
return result;
}
@Override
public void setResult(GenericResult result) {
this.result = Objects.requireNonNull(result);
}
}

View File

@ -1,6 +1,6 @@
package com.alttd.chat.commands;
import com.alttd.chat.api.GlobalStaffChatEvent;
import com.alttd.chat.api.GlobalAdminChatEvent;
import com.alttd.chat.config.Config;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
@ -20,7 +20,7 @@ public class GlobalAdminChat {
.then(RequiredArgumentBuilder
.<CommandSource, String>argument("message", StringArgumentType.greedyString())
.executes(context -> {
proxyServer.getEventManager().fire(new GlobalStaffChatEvent(context.getSource(), context.getArgument("message", String.class)));
proxyServer.getEventManager().fire(new GlobalAdminChatEvent(context.getSource(), context.getArgument("message", String.class)));
return 1;
}) // TODO call in the same way as gc?
)

View File

@ -13,6 +13,7 @@ import com.velocitypowered.api.proxy.ProxyServer;
public class GlobalChat {
// todo move to server implementation and send plugin event to allowed servers, this means we can implement [i] in here
public GlobalChat(ProxyServer proxyServer) {
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
.<CommandSource>literal("globalchat")

View File

@ -1,6 +1,7 @@
package com.alttd.chat.commands;
import com.alttd.chat.api.MessageEvent;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.api.PrivateMessageEvent;
import com.alttd.chat.config.Config;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
@ -9,6 +10,7 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
@ -35,8 +37,13 @@ public class Message {
if (playerOptional.isPresent()) {
Player receiver = playerOptional.get();
proxyServer.getEventManager().fire(new MessageEvent(context.getSource(), receiver, context.getArgument("message", String.class)));
proxyServer.getEventManager().fire(new PrivateMessageEvent(context.getSource(), receiver, context.getArgument("message", String.class))).thenAccept((event) -> {
if(event.getResult() == ResultedEvent.GenericResult.allowed()) {
ChatPlugin.getPlugin().getChatHandler().privateMessage(event);
}
// event has finished firing
// do some logic dependent on the result
});
return 1;
} else {
// TODO NOBODY TO REPLY TO

View File

@ -1,190 +0,0 @@
package com.alttd.chat.config;
import com.alttd.chat.ChatPlugin;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.yaml.snakeyaml.DumperOptions;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public final class Config {
private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
private static final String HEADER = "";
private static File CONFIG_FILE;
public static ConfigurationNode config;
public static YAMLConfigurationLoader configLoader;
static int version;
static boolean verbose;
public static void init(File path) {
CONFIG_FILE = new File(path, "config.yml");;
configLoader = YAMLConfigurationLoader.builder()
.setFile(CONFIG_FILE)
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
.build();
if (!CONFIG_FILE.getParentFile().exists()) {
if(!CONFIG_FILE.getParentFile().mkdirs()) {
ChatPlugin.getPlugin().getLogger().error("Could create config and/or directory");
return;
}
}
if (!CONFIG_FILE.exists()) {
try {
if(!CONFIG_FILE.createNewFile()) {
ChatPlugin.getPlugin().getLogger().error("Could create config and/or directory");
return;
}
} catch (IOException error) {
error.printStackTrace();
}
}
try {
config = configLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
configLoader.getDefaultOptions().setHeader(HEADER);
configLoader.getDefaultOptions().withShouldCopyDefaults(true);
verbose = getBoolean("verbose", true);
version = getInt("config-version", 1);
readConfig(Config.class, null);
try {
configLoader.save(config);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readConfig(Class<?> clazz, Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPrivate(method.getModifiers())) {
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException | IllegalAccessException ex) {
throw Throwables.propagate(ex.getCause());
}
}
}
}
try {
configLoader.save(config);
} catch (IOException ex) {
throw Throwables.propagate(ex.getCause());
}
}
public static void saveConfig() {
try {
configLoader.save(config);
} catch (IOException ex) {
throw Throwables.propagate(ex.getCause());
}
}
private static Object[] splitPath(String key) {
return PATH_PATTERN.split(key);
}
private static void set(String path, Object def) {
if(config.getNode(splitPath(path)).isVirtual())
config.getNode(splitPath(path)).setValue(def);
}
private static void setString(String path, String def) {
try {
if(config.getNode(splitPath(path)).isVirtual())
config.getNode(splitPath(path)).setValue(TypeToken.of(String.class), def);
} catch(ObjectMappingException ex) {
}
}
private static boolean getBoolean(String path, boolean def) {
set(path, def);
return config.getNode(splitPath(path)).getBoolean(def);
}
private static double getDouble(String path, double def) {
set(path, def);
return config.getNode(splitPath(path)).getDouble(def);
}
private static int getInt(String path, int def) {
set(path, def);
return config.getNode(splitPath(path)).getInt(def);
}
private static String getString(String path, String def) {
setString(path, def);
return config.getNode(splitPath(path)).getString(def);
}
private static Long getLong(String path, Long def) {
set(path, def);
return config.getNode(splitPath(path)).getLong(def);
}
private static <T> List<String> getList(String path, T def) {
try {
set(path, def);
return config.getNode(splitPath(path)).getList(TypeToken.of(String.class));
} catch(ObjectMappingException ex) {
}
return new ArrayList<>();
}
/** ONLY EDIT ANYTHING BELOW THIS LINE **/
public static List<String> PREFIXGROUPS = new ArrayList<>();
private static void settings() {
PREFIXGROUPS = getList("settings.prefix-groups",
Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", "developer"));
}
public static List<String> MESSAGECOMMANDALIASES = new ArrayList<>();
public static List<String> REPLYCOMMANDALIASES = new ArrayList<>();
public static String MESSAGESENDER = "<hover:show_text:Click to reply><click:suggest_command:/msg <receiver> ><light_purple>(Me -> <gray><receiver></gray>) <message></light_purple>";
public static String MESSAGERECIEVER = "<hover:show_text:Click to reply><click:suggest_command:/msg <sender> ><light_purple>(<gray><sender></gray> on <server> -> Me) <message></light_purple>";
private static void messageCommand() {
MESSAGECOMMANDALIASES.clear();
REPLYCOMMANDALIASES.clear();
MESSAGECOMMANDALIASES = getList("commands.message.aliases", Lists.newArrayList("msg", "whisper", "tell"));
REPLYCOMMANDALIASES = getList("commands.reply.aliases", Lists.newArrayList("r"));
MESSAGESENDER = getString("commands.message.sender-message", MESSAGESENDER);
MESSAGERECIEVER = getString("commands.message.reciever-message", MESSAGERECIEVER);
}
public static List<String> GCCOMMANDALIASES = new ArrayList<>();
public static String GCFORMAT = "<white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message></gray></white>";
public static String GCPERMISSION = "proxy.globalchat";
private static void globalChat() {
MESSAGERECIEVER = getString("commands.globalchat.format", MESSAGERECIEVER);
GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION);
GCCOMMANDALIASES = getList("commands.globalchat.aliases", Lists.newArrayList("gc"));
}
public static List<String> GACECOMMANDALIASES = new ArrayList<>();
public static String GACFORMAT = "<hover:show_text:Click to reply><click:suggest_command:/msg <sender> ><yellow>(<sender> on <server> -> Team) <message></yellow>";
private static void globalAdminChat() {
GACECOMMANDALIASES = getList("commands.globaladminchat.aliases", Lists.newArrayList("acg"));
GACFORMAT = getString("commands.globaladminchat.format", GACFORMAT);
}
}

View File

@ -1,6 +1,7 @@
package com.alttd.chat.handlers;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.api.PrivateMessageEvent;
import com.alttd.chat.config.Config;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
@ -46,6 +47,35 @@ public class ChatHandler {
return Collections.unmodifiableList(chatPlayers);
}
public void privateMessage(PrivateMessageEvent event) {
String senderName;
String receiverName;
CommandSource commandSource = event.getSender();
if (commandSource instanceof Player) {
Player sender = (Player) event.getSender();
senderName = sender.getUsername();
//plugin.getChatHandler().getChatPlayer(sender.getUniqueId()).setReplyTarget(event.getRecipient().getUniqueId()); // TODO this needs to be cleaner
} else {
senderName = "Console"; // TODO console name from config
}
receiverName = event.getRecipient().getUsername();
MiniMessage miniMessage = MiniMessage.get();
Map<String, String> map = new HashMap<>();
map.put("sender", senderName);
map.put("receiver", receiverName);
map.put("message", event.getMessage());
map.put("server", event.getRecipient().getCurrentServer().isPresent() ? event.getRecipient().getCurrentServer().get().getServerInfo().getName() : "Altitude");
Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, map);
Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, map);
event.getSender().sendMessage(senderMessage);
event.getRecipient().sendMessage(receiverMessage);
}
public void globalChat(CommandSource source, String message) {
String senderName, serverName, prefix;
Map<String, String> map = new HashMap<>();

View File

@ -1,8 +1,8 @@
package com.alttd.chat.listeners;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.api.GlobalStaffChatEvent;
import com.alttd.chat.api.MessageEvent;
import com.alttd.chat.api.GlobalAdminChatEvent;
import com.alttd.chat.api.PrivateMessageEvent;
import com.alttd.chat.config.Config;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.PostOrder;
@ -24,37 +24,12 @@ public class ChatListener {
}
@Subscribe(order = PostOrder.FIRST)
public void onMessage(MessageEvent event) {
String senderName;
String receiverName;
CommandSource commandSource = event.getSender();
if (commandSource instanceof Player) {
Player sender = (Player) event.getSender();
senderName = sender.getUsername();
plugin.getChatHandler().getChatPlayer(sender.getUniqueId()).setReplyTarget(event.getRecipient().getUniqueId()); // TODO this needs to be cleaner
} else {
senderName = "Console"; // TODO console name from config
}
receiverName = event.getRecipient().getUsername();
MiniMessage miniMessage = MiniMessage.get();
Map<String, String> map = new HashMap<>();
map.put("sender", senderName);
map.put("receiver", receiverName);
map.put("message", event.getMessage());
map.put("server", event.getRecipient().getCurrentServer().isPresent() ? event.getRecipient().getCurrentServer().get().getServerInfo().getName() : "Altitude");
Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, map);
Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, map);
event.getSender().sendMessage(senderMessage);
event.getRecipient().sendMessage(receiverMessage);
public void onMessage(PrivateMessageEvent event) {
// TODO check muted, etc
}
@Subscribe(order = PostOrder.FIRST)
public void onGlobalStaffChat(GlobalStaffChatEvent event) {
public void onGlobalStaffChat(GlobalAdminChatEvent event) {
String senderName;
String serverName;
CommandSource commandSource = event.getSender();