|
5 | 5 | */ |
6 | 6 | package tak; |
7 | 7 |
|
8 | | -import java.util.concurrent.ConcurrentHashMap; |
9 | 8 | import tak.utils.BadWordFilter; |
10 | 9 | import tak.utils.ConcurrentHashSet; |
11 | | -import java.util.concurrent.locks.*; |
| 10 | + |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.concurrent.locks.Lock; |
| 13 | +import java.util.concurrent.locks.ReentrantLock; |
12 | 14 |
|
13 | 15 | /** |
14 | 16 | * |
15 | 17 | * @author chaitu |
16 | 18 | */ |
17 | 19 | public class ChatRoom { |
18 | 20 | static final ConcurrentHashMap<String, ChatRoom> chatRooms = new ConcurrentHashMap<>(); |
19 | | - static Lock roommaplock=new ReentrantLock(); |
20 | | - |
| 21 | + static Lock roommaplock = new ReentrantLock(); |
| 22 | + |
21 | 23 | ConcurrentHashSet<Client> members; |
22 | | - |
| 24 | + |
23 | 25 | ChatRoom() { |
24 | | - members = new ConcurrentHashSet<Client>(); |
| 26 | + members = new ConcurrentHashSet<>(); |
25 | 27 | } |
26 | | - |
27 | | - public static ChatRoom joinRoom(String name, Client client){ |
| 28 | + |
| 29 | + public static ChatRoom joinRoom(String name, Client client) { |
28 | 30 | roommaplock.lock(); |
29 | | - try{ |
30 | | - ChatRoom room=chatRooms.get(name); |
31 | | - if(room==null){ |
| 31 | + try { |
| 32 | + ChatRoom room = chatRooms.get(name); |
| 33 | + if (room == null) { |
32 | 34 | room = new ChatRoom(); |
33 | 35 | chatRooms.put(name, room); |
34 | 36 | } |
35 | 37 | room.members.add(client); |
36 | 38 | return room; |
37 | | - } |
38 | | - finally{ |
| 39 | + } finally { |
39 | 40 | roommaplock.unlock(); |
40 | 41 | } |
41 | 42 | } |
42 | | - |
43 | | - public static void shout(String name, Client client, String msg) { |
44 | | - ChatRoom room=chatRooms.get(name); |
45 | | - if(room!=null){ |
46 | | - String compiledmessage="ShoutRoom "+name+" <"+client.player.getName()+"> "+ BadWordFilter.filterText(msg); |
47 | | - if(!client.player.isGagged()) { |
| 43 | + |
| 44 | + public static void shout(String name, Client client, String msg) { |
| 45 | + ChatRoom room = chatRooms.get(name); |
| 46 | + if (room != null) { |
| 47 | + String compiledmessage = "ShoutRoom " + name + " <" + client.player.getName() + "> " + BadWordFilter.filterText(msg); |
| 48 | + if (!client.player.isGagged()) { |
48 | 49 | for (Client cc : room.members) { |
49 | 50 | cc.sendWithoutLogging(compiledmessage); |
50 | 51 | } |
51 | 52 | } else { |
52 | | - client.sendWithoutLogging("ShoutRoom "+name+" <"+client.player.getName()+"> <Server: You have been muted for inappropriate chat behavior.>"); |
| 53 | + client.sendWithoutLogging("ShoutRoom " + name + " <" + client.player.getName() + "> <Server: You have been muted for inappropriate chat behavior.>"); |
53 | 54 | } |
54 | 55 | } |
55 | 56 | } |
56 | | - |
57 | | - public static void leaveRoom(String name, Client client){ |
| 57 | + |
| 58 | + public static void leaveRoom(String name, Client client) { |
58 | 59 | roommaplock.lock(); |
59 | | - try{ |
60 | | - ChatRoom room=chatRooms.get(name); |
61 | | - if(room!=null){ |
| 60 | + try { |
| 61 | + ChatRoom room = chatRooms.get(name); |
| 62 | + if (room != null) { |
62 | 63 | room.members.remove(client); |
63 | | - if(room.members.isEmpty()){ |
| 64 | + if (room.members.isEmpty()) { |
64 | 65 | chatRooms.remove(name); |
65 | 66 | } |
66 | 67 | } |
67 | | - } |
68 | | - finally{ |
| 68 | + } finally { |
69 | 69 | roommaplock.unlock(); |
70 | 70 | } |
71 | 71 | } |
|
0 commit comments