-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathChatMenuAPI.java
More file actions
212 lines (182 loc) · 5.17 KB
/
ChatMenuAPI.java
File metadata and controls
212 lines (182 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package me.tom.sparse.spigot.chat.menu;
import me.tom.sparse.spigot.chat.protocol.ChatPacketInterceptor;
import me.tom.sparse.spigot.chat.protocol.PlayerChatIntercept;
import me.tom.sparse.spigot.chat.util.LogFilter;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.map.MapFont;
import org.bukkit.map.MinecraftFont;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
public final class ChatMenuAPI
{
private static final Map<String, ChatMenu> MENUS = new ConcurrentHashMap<>();
private static final Map<Player, ChatMenu> OPENED_MENUS = new ConcurrentHashMap<>();
private static Plugin plugin;
private static ChatPacketInterceptor interceptor;
private ChatMenuAPI() {}
/**
* @param player the player whose current menu should be returned
* @return the menu the player currently has open, or {@code null} if no menu is open.
*/
@Nullable
public static ChatMenu getCurrentMenu(@NotNull Player player)
{
return OPENED_MENUS.get(player);
}
/**
* @param player the player whose current menu should be returned
* @param menu the menu to set as current, or {@code null} if you want to close the current menu.
*/
public static void setCurrentMenu(@NotNull Player player, @Nullable ChatMenu menu)
{
ChatMenu old = OPENED_MENUS.remove(player);
if(old != null && old != menu) old.onClosed(player);
if(menu != null) OPENED_MENUS.put(player, menu);
}
@NotNull
static String registerMenu(ChatMenu menu)
{
String id = generateIdentifier();
MENUS.put(id, menu);
return id;
}
static void unregisterMenu(@NotNull ChatMenu menu)
{
MENUS.values().remove(menu);
}
@NotNull
private static String generateIdentifier()
{
String result = null;
while(result == null || MENUS.containsKey(result))
{
int[] ints = ThreadLocalRandom.current().ints(4, 0x2700, 0x27BF).toArray();
result = new String(ints, 0, ints.length);
}
return result;
}
/**
* Gets the current {@link PlayerChatIntercept} associated with the provided player.
* If the player does not have one, it will be created.
*
* @param player the player to get/create the {@link PlayerChatIntercept} for
* @return the {@link PlayerChatIntercept} associated with the provided player
*/
@NotNull
public static PlayerChatIntercept getChatIntercept(@NotNull Player player)
{
return interceptor.getChat(player);
}
/**
* Calculates the width of the provided text.
* <br>
* Works with formatting codes such as bold.
*
* @param text the text to calculate the width for
* @return the number of pixels in chat the text takes up
*/
public static int getWidth(@NotNull String text)
{
if(text.contains("\n"))
throw new IllegalArgumentException("Cannot get width of text containing newline");
int width = 0;
boolean isBold = false;
char[] chars = text.toCharArray();
for(int i = 0; i < chars.length; i++)
{
char c = chars[i];
int charWidth = getCharacterWidth(c);
if(c == ChatColor.COLOR_CHAR && i < chars.length - 1)
{
c = chars[++i];
if(c != 'l' && c != 'L')
{
if(c == 'r' || c == 'R')
{
isBold = false;
}
}else
{
isBold = true;
}
charWidth = 0;
}
if(isBold && c != ' ' && charWidth > 0)
{
width++;
}
width += charWidth;
}
return width;
}
/**
* @param c the character to get the width of
* @return the width of the provided character in pixels
*/
public static int getCharacterWidth(char c)
{
if(c >= '\u2588' && c <= '\u258F')
{
return ('\u258F' - c) + 2;
}
switch(c)
{
case ' ':
return 4;
case '\u2714':
return 8;
case '\u2718':
return 7;
default:
MapFont.CharacterSprite mcChar = MinecraftFont.Font.getChar(c);
if(mcChar != null)
return mcChar.getWidth() + 1;
return 0;
}
}
static ChatMenu getMenu(String id)
{
return MENUS.get(id);
}
/**
* <b>This method should only be called by you if you're including this API inside your plugin.</b>
* <br>
* Initializes all the necessary things for the ChatMenuAPI to function. This method can only be called once.
*
* @param plugin the plugin to initialize everything with, including listeners and scheduled tasks
*/
public static void init(@NotNull Plugin plugin)
{
if(ChatMenuAPI.plugin != null)
return;
ChatMenuAPI.plugin = plugin;
// Bukkit.getPluginCommand("cmapi").setExecutor(new CMCommand());
new CMListener(plugin);
new LogFilter();
try
{
interceptor = new ChatPacketInterceptor(plugin);
}catch(ReflectiveOperationException e)
{
plugin.getLogger().severe("Unable to create ChatPacketInterceptor! The ChatMenuAPI will not function properly!");
e.printStackTrace();
}
}
/**
* <b>This method should only be called by you if you're including this API inside your plugin.</b>
* <br>
* Disables everything necessary for this API to be reloaded properly without restarting.
*/
public static void disable()
{
if(plugin == null)
return;
plugin = null;
interceptor.disable();
}
}