2022-04-14 20:44:09 +02:00
|
|
|
/*
|
|
|
|
This file is a part of the SteamWar software.
|
|
|
|
|
|
|
|
Copyright (C) 2022 SteamWar.de-Serverteam
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.steamwar.messages;
|
|
|
|
|
2022-04-22 17:10:32 +02:00
|
|
|
import de.steamwar.bungeecore.*;
|
2022-04-14 22:24:36 +02:00
|
|
|
import de.steamwar.bungeecore.bot.listeners.DiscordChatListener;
|
2023-02-21 18:22:42 +01:00
|
|
|
import de.steamwar.sql.SteamwarUser;
|
2023-08-29 17:47:49 +02:00
|
|
|
import de.steamwar.sql.UserPerm;
|
2022-04-14 20:44:09 +02:00
|
|
|
import net.md_5.bungee.api.ChatMessageType;
|
|
|
|
import net.md_5.bungee.api.CommandSender;
|
2022-04-14 22:24:36 +02:00
|
|
|
import net.md_5.bungee.api.ProxyServer;
|
2022-04-14 20:44:09 +02:00
|
|
|
import net.md_5.bungee.api.chat.BaseComponent;
|
|
|
|
import net.md_5.bungee.api.chat.ClickEvent;
|
|
|
|
import net.md_5.bungee.api.chat.HoverEvent;
|
|
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
|
|
import net.md_5.bungee.api.chat.hover.content.Text;
|
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
2022-04-19 20:32:02 +02:00
|
|
|
import net.md_5.bungee.api.event.LoginEvent;
|
2022-04-14 20:44:09 +02:00
|
|
|
import net.md_5.bungee.command.ConsoleCommandSender;
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.ResourceBundle;
|
2022-04-19 20:32:02 +02:00
|
|
|
import java.util.function.BiConsumer;
|
2022-04-14 20:44:09 +02:00
|
|
|
import java.util.logging.Level;
|
2022-04-14 22:24:36 +02:00
|
|
|
import java.util.stream.Stream;
|
2022-04-14 20:44:09 +02:00
|
|
|
|
|
|
|
public interface ChatSender {
|
|
|
|
|
2022-04-14 22:24:36 +02:00
|
|
|
static Stream<ProxiedPlayer> all() {
|
|
|
|
return ProxyServer.getInstance().getPlayers().stream();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<ChatSender> allReceivers() {
|
|
|
|
return all().map(ChatSender::of);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<ChatSender> globalReceivers() {
|
|
|
|
return all().filter(player -> {
|
|
|
|
Subserver subserver = Subserver.getSubserver(player);
|
2022-04-22 17:10:32 +02:00
|
|
|
return subserver == null || !(subserver.getType() == Servertype.ARENA && subserver.getServer() == player.getServer().getInfo());
|
2022-04-14 22:24:36 +02:00
|
|
|
}).map(ChatSender::of);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<ChatSender> serverteamReceivers() {
|
2023-08-29 17:47:49 +02:00
|
|
|
return allReceivers().filter(player -> player.user().hasPerm(UserPerm.TEAM));
|
2022-04-14 22:24:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:44:09 +02:00
|
|
|
SteamwarUser user();
|
|
|
|
|
|
|
|
Locale getLocale();
|
|
|
|
boolean chatShown();
|
|
|
|
void sendMessage(ChatMessageType type, BaseComponent... msg);
|
|
|
|
|
|
|
|
default void chat(Message message) {
|
|
|
|
send(false, ChatMessageType.CHAT, null, null, message);
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default void system(String format, Object... params) {
|
|
|
|
system(new Message(format, params));
|
2022-04-14 20:44:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default void system(Message message) {
|
|
|
|
send(true, ChatMessageType.SYSTEM, null, null, message);
|
2022-04-14 20:44:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default void system(String format, Message onHover, ClickEvent onClick, Object... params) {
|
|
|
|
send(true, ChatMessageType.SYSTEM, onHover, onClick, new Message(format, params));
|
2022-04-14 20:44:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default void prefixless(String format, Object... params) {
|
|
|
|
prefixless(format, null, null, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
default void prefixless(String format, Message onHover, ClickEvent onClick, Object... params) {
|
|
|
|
send(false, ChatMessageType.SYSTEM, onHover, onClick, new Message(format, params));
|
2022-04-14 22:24:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:44:09 +02:00
|
|
|
default void send(boolean prefixed, ChatMessageType type, Message onHover, ClickEvent onClick, Message message) {
|
2022-04-19 20:32:02 +02:00
|
|
|
TextComponent msg = parseToComponent(prefixed, message);
|
2022-04-14 20:44:09 +02:00
|
|
|
if(onHover != null)
|
|
|
|
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(parse(false, onHover))));
|
|
|
|
if(onClick != null)
|
|
|
|
msg.setClickEvent(onClick);
|
|
|
|
sendMessage(type, msg);
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default TextComponent parseToComponent(boolean prefixed, Message message) {
|
|
|
|
return new TextComponent(parse(prefixed, message));
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:08:00 +02:00
|
|
|
default String parseToPlain(String format, Object... params) {
|
|
|
|
return parseToPlain(new Message(format, params));
|
|
|
|
}
|
|
|
|
|
|
|
|
default String parseToPlain(Message message) {
|
|
|
|
return parseToComponent(false, message).toPlainText();
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
default String parseToLegacy(String format, Object... params) {
|
|
|
|
return parseToLegacy(new Message(format, params));
|
|
|
|
}
|
|
|
|
|
|
|
|
default String parseToLegacy(Message message) {
|
|
|
|
return parseToComponent(false, message).toLegacyText();
|
|
|
|
}
|
|
|
|
|
2022-06-17 20:43:36 +02:00
|
|
|
default BaseComponent[] parse(boolean prefixed, String format, Object... params) {
|
|
|
|
return parse(prefixed, new Message(format, params));
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:44:09 +02:00
|
|
|
default BaseComponent[] parse(boolean prefixed, Message message) {
|
|
|
|
Locale locale = getLocale();
|
|
|
|
ResourceBundle resourceBundle = SteamwarResourceBundle.getResourceBundle(locale);
|
|
|
|
String pattern = "";
|
|
|
|
if(prefixed)
|
|
|
|
pattern = resourceBundle.getObject("PREFIX") + " ";
|
2022-04-14 22:24:36 +02:00
|
|
|
pattern += (String)resourceBundle.getObject(message.getFormat());
|
2022-04-14 20:44:09 +02:00
|
|
|
|
|
|
|
MessageFormat format = new MessageFormat(pattern, locale);
|
|
|
|
Object[] params = message.getParams();
|
|
|
|
for (int i = 0; i < params.length; i++) {
|
|
|
|
if(params[i] instanceof Message) {
|
2022-04-19 20:32:02 +02:00
|
|
|
params[i] = parseToLegacy((Message) params[i]);
|
2022-04-14 20:44:09 +02:00
|
|
|
} else if(params[i] instanceof Date) {
|
|
|
|
params[i] = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale).format((Date) params[i]);
|
|
|
|
} else if(params[i] instanceof SteamwarUser) {
|
|
|
|
params[i] = ((SteamwarUser) params[i]).getUserName();
|
|
|
|
} else if(params[i] instanceof ProxiedPlayer) {
|
|
|
|
params[i] = ((ProxiedPlayer) params[i]).getName();
|
|
|
|
} else if(params[i] instanceof ChatSender) {
|
|
|
|
params[i] = ((ChatSender) params[i]).user().getUserName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TextComponent.fromLegacyText(format.format(params));
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
static ChatSender ofProxiedPlayer(ProxiedPlayer player, BiConsumer<ChatMessageType, BaseComponent[]> sendMessage) {
|
2022-04-14 20:44:09 +02:00
|
|
|
return new ChatSender() {
|
|
|
|
@Override
|
|
|
|
public SteamwarUser user() {
|
|
|
|
return SteamwarUser.get(player.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Locale getLocale() {
|
2022-05-24 10:11:10 +02:00
|
|
|
return user().getLocale();
|
2022-04-14 20:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chatShown() {
|
|
|
|
return player.getChatMode() == ProxiedPlayer.ChatMode.SHOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(ChatMessageType type, BaseComponent... msg) {
|
2022-04-19 20:32:02 +02:00
|
|
|
sendMessage.accept(type, msg);
|
2022-04-14 20:44:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
static ChatSender of(ProxiedPlayer player) {
|
|
|
|
return ofProxiedPlayer(player, player::sendMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChatSender disconnect(ProxiedPlayer player) {
|
|
|
|
return ofProxiedPlayer(player, (type, msg) -> player.disconnect(msg));
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:44:09 +02:00
|
|
|
static ChatSender of(CommandSender sender) {
|
|
|
|
if(sender instanceof ProxiedPlayer)
|
|
|
|
return of((ProxiedPlayer) sender);
|
|
|
|
|
|
|
|
//Console
|
|
|
|
return new ChatSender() {
|
|
|
|
@Override
|
|
|
|
public SteamwarUser user() {
|
|
|
|
return SteamwarUser.get(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Locale getLocale() {
|
|
|
|
return Locale.getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chatShown() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(ChatMessageType type, BaseComponent... msg) {
|
|
|
|
sender.sendMessage(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChatSender console() {
|
|
|
|
return of(ConsoleCommandSender.getInstance());
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:32:02 +02:00
|
|
|
static ChatSender of(LoginEvent event) {
|
|
|
|
return new ChatSender() {
|
|
|
|
@Override
|
|
|
|
public SteamwarUser user() {
|
|
|
|
return SteamwarUser.get(event.getConnection().getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Locale getLocale() {
|
|
|
|
return Locale.getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chatShown() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(ChatMessageType type, BaseComponent... msg) {
|
|
|
|
event.setCancelReason(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-14 22:24:36 +02:00
|
|
|
static ChatSender discordChannel(DiscordChatListener channel) {
|
2022-04-14 20:44:09 +02:00
|
|
|
return new ChatSender() {
|
|
|
|
@Override
|
|
|
|
public SteamwarUser user() {
|
|
|
|
return SteamwarUser.get(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Locale getLocale() {
|
|
|
|
return Locale.getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chatShown() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(ChatMessageType type, BaseComponent... msg) {
|
|
|
|
try {
|
2022-04-14 22:24:36 +02:00
|
|
|
channel.send("<t:" + (System.currentTimeMillis() / 1000) + "> " + new TextComponent(msg).toPlainText());
|
2022-04-14 20:44:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
BungeeCore.get().getLogger().log(Level.SEVERE, "Could not send chat message to discord", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChatSender of(net.dv8tion.jda.api.entities.Message message, SteamwarUser user) {
|
|
|
|
return new ChatSender() {
|
|
|
|
@Override
|
|
|
|
public SteamwarUser user() {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Locale getLocale() {
|
|
|
|
return Locale.getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chatShown() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(ChatMessageType type, BaseComponent... msg) {
|
|
|
|
try {
|
2022-04-14 22:24:36 +02:00
|
|
|
message.delete().queue();
|
2022-04-14 20:44:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
BungeeCore.get().getLogger().log(Level.SEVERE, "Could not send chat reply to discord user", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|