parent
e3e5f58f32
commit
7598aded28
12 changed files with 283 additions and 82 deletions
139
src/main/java/eu/m724/tweaks/chat/ChatManager.java
Normal file
139
src/main/java/eu/m724/tweaks/chat/ChatManager.java
Normal file
|
@ -0,0 +1,139 @@
|
|||
package eu.m724.tweaks.chat;
|
||||
|
||||
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 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");
|
||||
}
|
||||
|
||||
public void init() {
|
||||
getById("global");
|
||||
plugin.getServer().getPluginManager().registerEvents(new ChatListener(this), plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
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);
|
||||
oldRoom.players.remove(player);
|
||||
|
||||
player.getPersistentDataContainer().set(chatRoomKey, PersistentDataType.STRING, chatRoom.id);
|
||||
playerMap.put(player, chatRoom);
|
||||
chatRoom.players.add(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = "global";
|
||||
chatRoom = getById(id);
|
||||
if (chatRoom == null) chatRoom = getById("global");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue