Skip to content

Commit 08a4298

Browse files
committed
Added JSR305 annotations. Fixed some minor logic issues and problems in Javadocs
1 parent 71c79ec commit 08a4298

17 files changed

+283
-193
lines changed

pom.xml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,17 @@
4343
<version>1.12-R0.1-SNAPSHOT</version>
4444
<scope>provided</scope>
4545
</dependency>
46-
<dependency>
47-
<groupId>org.bukkit</groupId>
48-
<artifactId>bukkit</artifactId>
49-
<version>1.12-R0.1-SNAPSHOT</version>
50-
<scope>provided</scope>
51-
</dependency>
52-
<dependency>
53-
<groupId>org.bukkit</groupId>
54-
<artifactId>craftbukkit</artifactId>
55-
<version>1.12-R0.1-SNAPSHOT</version>
56-
<scope>provided</scope>
57-
</dependency>
5846
<dependency>
5947
<groupId>org.spigotmc</groupId>
6048
<artifactId>spigot</artifactId>
6149
<version>1.12-R0.1-SNAPSHOT</version>
6250
<scope>provided</scope>
6351
</dependency>
52+
<dependency>
53+
<groupId>com.google.code.findbugs</groupId>
54+
<artifactId>jsr305</artifactId>
55+
<version>3.0.2</version>
56+
</dependency>
6457
</dependencies>
6558

6659
<repositories>

src/main/java/me/tom/sparse/spigot/chat/menu/ChatMenu.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
import net.md_5.bungee.api.chat.BaseComponent;
99
import org.bukkit.entity.Player;
1010

11+
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
1113
import java.util.*;
1214

1315
public class ChatMenu implements IElementContainer
1416
{
17+
@Nonnull
1518
protected final String id;
1619
protected boolean registered;
17-
20+
21+
@Nonnull
1822
protected List<Element> elements;
19-
23+
24+
@Nonnull
2025
protected Set<Player> viewers = new ConcurrentSet<>();
2126
protected boolean pauseChat = false;
2227

@@ -27,7 +32,7 @@ public class ChatMenu implements IElementContainer
2732
*
2833
* @param elements the elements to start the menu with.
2934
*/
30-
public ChatMenu(Element... elements)
35+
public ChatMenu(@Nonnull Element... elements)
3136
{
3237
this(Arrays.asList(elements));
3338
}
@@ -37,7 +42,7 @@ public ChatMenu(Element... elements)
3742
*
3843
* @param elements the elements to start the menu with.
3944
*/
40-
public ChatMenu(Collection<Element> elements)
45+
public ChatMenu(@Nonnull Collection<Element> elements)
4146
{
4247
this.elements = new ArrayList<>();
4348
this.elements.addAll(elements);
@@ -80,7 +85,7 @@ public void unregister()
8085
* @throws IllegalArgumentException if the element is null
8186
*/
8287
@Deprecated
83-
public void addElement(Element element)
88+
public void addElement(@Nonnull Element element)
8489
{
8590
add(element);
8691
}
@@ -92,7 +97,7 @@ public void addElement(Element element)
9297
* @param <T> the type of element
9398
* @return the element added
9499
*/
95-
public <T extends Element> T add(T t)
100+
public <T extends Element> T add(@Nonnull T t)
96101
{
97102
Objects.requireNonNull(t);
98103
elements.add(t);
@@ -106,14 +111,15 @@ public <T extends Element> T add(T t)
106111
* @param element the element to remove
107112
* @return true if the element was removed
108113
*/
109-
public boolean remove(Element element)
114+
public boolean remove(@Nonnull Element element)
110115
{
111116
return elements.remove(element);
112117
}
113118

114119
/**
115120
* @return an unmodifiable list of all the elements in this menu.
116121
*/
122+
@Nonnull
117123
public List<Element> getElements()
118124
{
119125
return Collections.unmodifiableList(elements);
@@ -126,7 +132,7 @@ public List<Element> getElements()
126132
* @param elementIndex the index of the element that was edited
127133
* @param args the data to be parsed by the element
128134
*/
129-
public void edit(Player player, int elementIndex, String[] args)
135+
public void edit(@Nonnull Player player, int elementIndex, @Nonnull String[] args)
130136
{
131137
if(elementIndex < 0 || elementIndex >= elements.size())
132138
return;
@@ -142,7 +148,7 @@ public void edit(Player player, int elementIndex, String[] args)
142148
*
143149
* @param player the player to send the menu to.
144150
*/
145-
public void openFor(Player player)
151+
public void openFor(@Nonnull Player player)
146152
{
147153
PlayerChatIntercept chat = ChatMenuAPI.getChatIntercept(player);
148154
if(viewers.add(player) && pauseChat)
@@ -151,7 +157,8 @@ public void openFor(Player player)
151157
chat.sendMessage(line);
152158
ChatMenuAPI.setCurrentMenu(player, this);
153159
}
154-
160+
161+
@Nonnull
155162
public List<BaseComponent[]> build()
156163
{
157164
Element overlapping = findOverlap();
@@ -203,10 +210,12 @@ else if(text.getWidth() > 320)
203210
}
204211
return result;
205212
}
206-
213+
214+
//FIXME: Javadocs unclear. says would return true, but return type isn't a boolean
207215
/**
208216
* @return true if any elements overlap
209217
*/
218+
@Nullable
210219
public Element findOverlap()
211220
{
212221
return elements.stream().filter(Element::isVisible).filter(a -> elements.stream().filter(Element::isVisible).anyMatch(b -> a != b && a.overlaps(b))).findFirst().orElse(null);
@@ -217,7 +226,7 @@ public Element findOverlap()
217226
*
218227
* @param player the player that closed the menu
219228
*/
220-
public void close(Player player)
229+
public void close(@Nonnull Player player)
221230
{
222231
if(viewers.remove(player))
223232
{
@@ -228,7 +237,7 @@ public void close(Player player)
228237
unregister();
229238
}
230239

231-
void onClosed(Player player)
240+
void onClosed(@Nonnull Player player)
232241
{
233242
if(viewers.remove(player))
234243
ChatMenuAPI.getChatIntercept(player).resume();
@@ -237,6 +246,7 @@ void onClosed(Player player)
237246
/**
238247
* @return the command used to interact with this menu
239248
*/
249+
@Nonnull
240250
public String getCommand()
241251
{
242252
if(!isRegistered())
@@ -248,7 +258,8 @@ public String getCommand()
248258
* @param element the element to interact with
249259
* @return the command used to interact with the provided element
250260
*/
251-
public String getCommand(Element element)
261+
@Nonnull
262+
public String getCommand(@Nonnull Element element)
252263
{
253264
return getCommand() + elements.indexOf(element)+" ";
254265
}
@@ -274,6 +285,7 @@ public boolean doesPauseChat()
274285
* Makes this menu pause chat when it is opened
275286
* @return this
276287
*/
288+
@Nonnull
277289
public ChatMenu pauseChat()
278290
{
279291
setPauseChat(true);
@@ -288,7 +300,8 @@ public ChatMenu pauseChat()
288300
* @param text the text of the close button
289301
* @return this
290302
*/
291-
public ChatMenu pauseChat(int x, int y, String text)
303+
@Nonnull
304+
public ChatMenu pauseChat(int x, int y, @Nonnull String text)
292305
{
293306
setPauseChat(true);
294307
add(ButtonElement.createCloseButton(x, y, text, this));
@@ -318,7 +331,8 @@ public boolean equals(Object o)
318331

319332
return id.equals(chatMenu.id);
320333
}
321-
334+
335+
//FIXME: Redundant
322336
public ChatMenu getMenu()
323337
{
324338
return this;

src/main/java/me/tom/sparse/spigot/chat/menu/ChatMenuAPI.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.bukkit.map.MinecraftFont;
99
import org.bukkit.plugin.Plugin;
1010

11+
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
1113
import java.util.Map;
1214
import java.util.concurrent.ConcurrentHashMap;
1315
import java.util.concurrent.ThreadLocalRandom;
@@ -23,37 +25,41 @@ public final class ChatMenuAPI
2325
private ChatMenuAPI() {}
2426

2527
/**
26-
* @param player the player whose current menu should be returned
28+
* @param player the player whose getCurrent menu should be returned
2729
* @return the menu the player currently has open, or {@code null} if no menu is open.
2830
*/
29-
public static ChatMenu getCurrentMenu(Player player)
31+
@Nullable
32+
public static ChatMenu getCurrentMenu(@Nonnull Player player)
3033
{
3134
return OPENED_MENUS.get(player);
3235
}
3336

3437
/**
35-
* @param player the player whose current menu should be set
36-
* @param menu the menu to set as current, or {@code null} if you want to close the current menu.
38+
* @param player the player whose getCurrent menu should be setCurrent
39+
* @param menu the menu to setCurrent as getCurrent, or {@code null} if you want to close the getCurrent menu.
3740
*/
38-
public static void setCurrentMenu(Player player, ChatMenu menu)
41+
@Nullable
42+
public static void setCurrentMenu(@Nonnull Player player, @Nonnull ChatMenu menu)
3943
{
4044
ChatMenu old = OPENED_MENUS.remove(player);
4145
if(old != null && old != menu) old.onClosed(player);
4246
if(menu != null) OPENED_MENUS.put(player, menu);
4347
}
44-
48+
49+
@Nonnull
4550
static String registerMenu(ChatMenu menu)
4651
{
4752
String id = generateIdentifier();
4853
MENUS.put(id, menu);
4954
return id;
5055
}
51-
52-
static void unregisterMenu(ChatMenu menu)
56+
57+
static void unregisterMenu(@Nonnull ChatMenu menu)
5358
{
5459
MENUS.values().remove(menu);
5560
}
56-
61+
62+
@Nonnull
5763
private static String generateIdentifier()
5864
{
5965
String result = null;
@@ -67,13 +73,14 @@ private static String generateIdentifier()
6773
}
6874

6975
/**
70-
* Gets the current {@link PlayerChatIntercept} associated with the provided player.
76+
* Gets the getCurrent {@link PlayerChatIntercept} associated with the provided player.
7177
* If the player does not have one, it will be created.
7278
*
7379
* @param player the player to get/create the {@link PlayerChatIntercept} for
7480
* @return the {@link PlayerChatIntercept} associated with the provided player
7581
*/
76-
public static PlayerChatIntercept getChatIntercept(Player player)
82+
@Nonnull
83+
public static PlayerChatIntercept getChatIntercept(@Nonnull Player player)
7784
{
7885
return interceptor.getChat(player);
7986
}
@@ -86,7 +93,7 @@ public static PlayerChatIntercept getChatIntercept(Player player)
8693
* @param text the text to calculate the width for
8794
* @return the number of pixels in chat the text takes up
8895
*/
89-
public static int getWidth(String text)
96+
public static int getWidth(@Nonnull String text)
9097
{
9198
if(text.contains("\n"))
9299
throw new IllegalArgumentException("Cannot get width of text containing newline");
@@ -169,7 +176,7 @@ static ChatMenu getMenu(String id)
169176
*
170177
* @param plugin the plugin to initialize everything with, including listeners and scheduled tasks
171178
*/
172-
public static void init(Plugin plugin)
179+
public static void init(@Nonnull Plugin plugin)
173180
{
174181
if(ChatMenuAPI.plugin != null)
175182
return;

0 commit comments

Comments
 (0)