196 lines
6.4 KiB
Java
196 lines
6.4 KiB
Java
/*
|
|
* Copyright (C) 2024 Minecon724
|
|
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
|
|
* in the project root for the full license text.
|
|
*/
|
|
|
|
package eu.m724.tweaks.chat;
|
|
|
|
import eu.m724.tweaks.TweaksConfig;
|
|
import net.md_5.bungee.api.ChatColor;
|
|
import net.md_5.bungee.api.chat.ComponentBuilder;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.OfflinePlayer;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class ChatManager {
|
|
private final Plugin plugin;
|
|
private final NamespacedKey chatRoomKey;
|
|
private final String defaultRoom;
|
|
|
|
private final Map<Player, ChatRoom> playerMap = new HashMap<>();
|
|
private final Map<String, ChatRoom> roomIdMap = new HashMap<>();
|
|
|
|
public ChatManager(Plugin plugin) {
|
|
this.plugin = plugin;
|
|
this.chatRoomKey = new NamespacedKey(plugin, "chatRoom");
|
|
this.defaultRoom = TweaksConfig.getConfig().chatDefaultName();
|
|
}
|
|
|
|
public void init() {
|
|
if (plugin.getServer().isEnforcingSecureProfiles()) {
|
|
throw new RuntimeException("Please disable enforce-secure-profile in server.properties to use chatrooms");
|
|
}
|
|
|
|
getById(defaultRoom);
|
|
plugin.getServer().getPluginManager().registerEvents(new ChatListener(this), plugin);
|
|
}
|
|
|
|
/**
|
|
* removes player from the server and their chatroom
|
|
* @param player the player
|
|
* @return the chatroom the player was removed from
|
|
*/
|
|
public ChatRoom removePlayer(Player player) {
|
|
ChatRoom chatRoom = playerMap.remove(player);
|
|
chatRoom.players.remove(player);
|
|
|
|
return chatRoom;
|
|
}
|
|
|
|
/**
|
|
* Get a chat room by id.<br>
|
|
* If the chat room is not loaded, it's loaded.
|
|
*
|
|
* @param id the id of the chat room
|
|
* @return the chat room
|
|
*/
|
|
public ChatRoom getById(String id) {
|
|
id = id.toLowerCase();
|
|
ChatRoom chatRoom = roomIdMap.get(id);
|
|
|
|
if (chatRoom == null) {
|
|
if (id.equals(defaultRoom)) {
|
|
chatRoom = new ChatRoom(defaultRoom, null, null);
|
|
} else {
|
|
chatRoom = ChatRoomLoader.load(plugin, id);
|
|
}
|
|
roomIdMap.put(id, chatRoom);
|
|
}
|
|
|
|
|
|
return chatRoom;
|
|
}
|
|
|
|
/**
|
|
* Adds a player to a chat room and leaves the previous one.
|
|
*
|
|
* @param chatRoom the chat room to add the player to
|
|
* @param player the player joining the chat room
|
|
*/
|
|
public void setPlayerChatRoom(ChatRoom chatRoom, Player player) {
|
|
ChatRoom oldRoom = getPlayerChatRoom(player);
|
|
|
|
if (chatRoom.equals(oldRoom)) return; // no change if changing to the same room
|
|
|
|
oldRoom.players.remove(player);
|
|
|
|
player.getPersistentDataContainer().set(chatRoomKey, PersistentDataType.STRING, chatRoom.id);
|
|
playerMap.put(player, chatRoom);
|
|
chatRoom.players.add(player);
|
|
|
|
chatRoom.broadcast(
|
|
new ComponentBuilder()
|
|
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom))
|
|
.append(ChatFormatUtils.formatPlayer(player))
|
|
.append(" has left the chat room").color(ChatColor.RED)
|
|
.create()
|
|
);
|
|
|
|
chatRoom.broadcast(
|
|
new ComponentBuilder()
|
|
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom))
|
|
.append(ChatFormatUtils.formatPlayer(player))
|
|
.append(" has joined the chat room").color(ChatColor.GREEN)
|
|
.create()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the chat room of a player<br>
|
|
* If not loaded, it's loaded and added to the chat room
|
|
*
|
|
* @param player the player
|
|
* @return The chat room of the player
|
|
*/
|
|
public ChatRoom getPlayerChatRoom(Player player) {
|
|
ChatRoom chatRoom = playerMap.get(player);
|
|
|
|
if (chatRoom == null) {
|
|
String id = player.getPersistentDataContainer().get(chatRoomKey, PersistentDataType.STRING);
|
|
|
|
if (id == null) id = defaultRoom;
|
|
chatRoom = getById(id);
|
|
if (chatRoom == null) chatRoom = getById(defaultRoom);
|
|
|
|
chatRoom.players.add(player);
|
|
playerMap.put(player, chatRoom);
|
|
}
|
|
|
|
return chatRoom;
|
|
}
|
|
|
|
/**
|
|
* Create a chat room and save it.
|
|
*
|
|
* @param id the id of the chat room, it will be validated
|
|
* @param password password of the chat room, may be null
|
|
* @param owner the owner of the chat room
|
|
* @return the created chat room
|
|
*
|
|
* @throws InvalidIdException if id is invalid
|
|
* @throws ChatRoomExistsException if chat room already exists
|
|
* @throws IOException if failed to save
|
|
*/
|
|
public ChatRoom createChatRoom(String id, String password, OfflinePlayer owner) throws InvalidIdException, ChatRoomExistsException, IOException {
|
|
id = id.toLowerCase();
|
|
|
|
switch (ChatRoomLoader.validateId(id)) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
throw new InvalidIdException("ID is too short, make it at least 2 chars");
|
|
case 2:
|
|
throw new InvalidIdException("ID is too long, make it 20 chars or shorter");
|
|
case 4:
|
|
throw new InvalidIdException("ID must be composed from characters a-z and numbers 0-9");
|
|
}
|
|
|
|
if (getById(id) != null)
|
|
throw new ChatRoomExistsException();
|
|
|
|
ChatRoom chatRoom = new ChatRoom(id, password, owner);
|
|
ChatRoomLoader.save(plugin, chatRoom);
|
|
return chatRoom;
|
|
}
|
|
|
|
void saveChatRoom(ChatRoom chatRoom) throws IOException {
|
|
ChatRoomLoader.save(plugin, chatRoom);
|
|
}
|
|
|
|
public void deleteChatRoom(ChatRoom chatRoom) {
|
|
roomIdMap.remove(chatRoom.id);
|
|
ChatRoomLoader.getFile(plugin, chatRoom.id).delete();
|
|
chatRoom.players.forEach(player -> setPlayerChatRoom(getById(defaultRoom), player));
|
|
}
|
|
|
|
/**
|
|
* If an ID is too short, too long, wrong composition, etc.
|
|
*/
|
|
public static class InvalidIdException extends Exception {
|
|
public InvalidIdException(String message) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If a chat room with the given ID already exists
|
|
*/
|
|
public static class ChatRoomExistsException extends Exception {}
|
|
}
|