Skip to content

Commit df8abb4

Browse files
authored
Update docs, simplify MenuManager and remove its generics (#18)
* refactor(menus): move MenuManager out of its own package It's redundant for now to have a package for a single class. This is a breaking change. * fix(menus): remove generics from MenuManager It's hard to figure out how those generics should even be used, and they're preventing parameterized MenuManager instances from being compiled successfully when used in AbstractMenu.open methods. * feat(github): add library installation guide to README.md * fix(ci/cd): use groovy language in release draft Gradle code block * docs(github,maven): upgrade library version
1 parent 9faea04 commit df8abb4

File tree

7 files changed

+119
-33
lines changed

7 files changed

+119
-33
lines changed

.github/workflows/release-drafter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
6666
#### Gradle
6767
68-
```gradle
68+
```groovy
6969
dependencyResolutionManagement {
7070
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
7171
repositories {

README.md

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ A library with some common utilities for ease of developing Spigot plugins.
44

55
# Features
66

7-
- menu library for custom menus and handling of all their events
7+
- central menu module for custom menus and handling of all their events
88
- `ItemBuilder` for less headache with `ItemMeta` and `ItemStack` modifications
99
- cooldowns that manage themselves
1010
- plugin file handling
11-
- miscellaneous utilities
1211

1312
## The menu library
1413

@@ -19,10 +18,102 @@ overriden by the developer; it also comes with some pre-made classes that can be
1918

2019
To use the library, a `MenuManager` needs to be instantiated, which will manage all the menus that are passed to it.
2120

22-
## How to use `plugin-utilities` (WIP)
21+
## How to use `plugin-utilities`
2322

24-
1. Add the library as a JitPack dependency via a build automation that you're using, such as Maven, Gradle, Ant, etc.
25-
2. Shade the dependency, possibly with the JAR minimization option to exclude the utilities you're not using, which will
26-
lower the file size impact of the dependency even though it's currently rather small.
27-
3. Write your code. The menu library, for example, requires you to instantiate a `MenuManager` with your `Plugin`
28-
instance to use it.
23+
1. Add the library as a JitPack dependency via a build automation tool that you're using, such as Maven, Gradle, Ant,
24+
etc.
25+
26+
```xml
27+
<project>
28+
<repositories>
29+
<repository>
30+
<id>jitpack.io</id>
31+
<url>https://jitpack.io</url>
32+
</repository>
33+
</repositories>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>com.github.CosimoTiger</groupId>
38+
<artifactId>plugin-utilities</artifactId>
39+
<version>1.0.0-alpha.2</version>
40+
</dependency>
41+
</dependencies>
42+
</project>
43+
```
44+
45+
```groovy
46+
dependencyResolutionManagement {
47+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
48+
repositories {
49+
mavenCentral()
50+
maven { url 'https://jitpack.io' }
51+
}
52+
}
53+
54+
dependencies {
55+
implementation 'com.github.CosimoTiger:plugin-utilities:1.0.0-alpha.2'
56+
}
57+
```
58+
59+
2. **Optional step:** you can shade the dependency library using JAR minimization to exclude unused features from being
60+
compiled with your plugin, which will decrease the file size impact of the dependency even though it's currently very
61+
small and lightweight. Here's one way of doing it:
62+
63+
Inside `<plugins>...</plugins>` add:
64+
65+
```xml
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-shade-plugin</artifactId>
69+
<version>3.6.0</version>
70+
<executions>
71+
<execution>
72+
<phase>package</phase>
73+
<goals>
74+
<goal>shade</goal>
75+
</goals>
76+
<configuration>
77+
<minimizeJar>true</minimizeJar>
78+
</configuration>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
```
83+
84+
Beware of what these options do – if you're using Java Reflection, databases or possibly dependency injection, those
85+
dependencies may be affected, but don't fret because you
86+
can [exclude them from the process](https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html).
87+
88+
3. Start writing your code. Here's the typical plugin initialization code example:
89+
90+
```java
91+
import com.cosimo.utilities.file.YamlFile;
92+
import com.cosimo.utilities.menu.AbstractMenu;
93+
import com.cosimo.utilities.menu.manager.MenuManager;
94+
import org.bukkit.plugin.java.JavaPlugin;
95+
96+
public class ExamplePlugin extends JavaPlugin {
97+
98+
private static MenuManager menuManager = null;
99+
private static ExamplePlugin instance = null;
100+
101+
@Override
102+
public void onEnable() {
103+
menuManager = new MenuManager(this);
104+
instance = this;
105+
106+
final var config = new YamlFile(this, "config.yml").reloadFile().getMemory();
107+
108+
getCommand("example").setExecutor(new ExampleCommand(config.getConfigurationSection("commands.example")));
109+
}
110+
111+
public static MenuManager getMenuManager() {
112+
return menuManager;
113+
}
114+
115+
public static ExamplePlugin getInstance() {
116+
return instance;
117+
}
118+
}
119+
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.cosimo</groupId>
88
<artifactId>plugin-utilities</artifactId>
9-
<version>1.0.0-alpha.1</version>
9+
<version>1.0.0-alpha.2</version>
1010
<name>PluginUtilities</name>
1111
<url>https://github.com/CosimoTiger/plugin-utilities</url>
1212
<description>A library with many utilities that help with developing Spigot plugins.</description>

src/main/java/com/cosimo/utilities/menu/AbstractMenu.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cosimo.utilities.menu;
22

3-
import com.cosimo.utilities.menu.manager.MenuManager;
43
import com.cosimo.utilities.menu.type.Menu;
54
import com.cosimo.utilities.menu.type.PropertyMenu;
65
import com.google.common.base.Preconditions;
@@ -98,7 +97,7 @@ public void onOpen(@NonNull InventoryOpenEvent event) {
9897
* @throws NullPointerException If a {@link HumanEntity} is null
9998
*/
10099
@NonNull
101-
public Self open(@NonNull MenuManager<AbstractMenu<?>> menuManager,
100+
public Self open(@NonNull MenuManager menuManager,
102101
@NonNull Iterable<@NonNull ? extends HumanEntity> viewers) {
103102
menuManager.registerMenu(this);
104103

@@ -127,7 +126,7 @@ public Self open(@NonNull MenuManager<AbstractMenu<?>> menuManager,
127126
* @throws NullPointerException If a {@link HumanEntity} is null
128127
*/
129128
@NonNull
130-
public Self open(@NonNull MenuManager<AbstractMenu<?>> menuManager, @NonNull HumanEntity @NonNull ... viewers) {
129+
public Self open(@NonNull MenuManager menuManager, @NonNull HumanEntity @NonNull ... viewers) {
131130
return this.open(menuManager, List.of(viewers));
132131
}
133132

src/main/java/com/cosimo/utilities/menu/IMenu.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cosimo.utilities.menu;
22

3-
import com.cosimo.utilities.menu.manager.MenuManager;
43
import lombok.NonNull;
54
import org.bukkit.entity.HumanEntity;
65
import org.bukkit.event.inventory.InventoryAction;

src/main/java/com/cosimo/utilities/menu/InventoryListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public interface InventoryListener {
1616
* such.
1717
*
1818
* @param event {@link InventoryClickEvent}
19-
* @param external Whether the clicked slot is outside the inventory menu detected by
20-
* {@link com.cosimo.utilities.menu.manager.MenuManager}
19+
* @param external Whether the clicked slot is outside the inventory menu detected by {@link MenuManager}
2120
*/
2221
void onClick(@NonNull InventoryClickEvent event, boolean external);
2322

src/main/java/com/cosimo/utilities/menu/manager/MenuManager.java renamed to src/main/java/com/cosimo/utilities/menu/MenuManager.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.cosimo.utilities.menu.manager;
1+
package com.cosimo.utilities.menu;
22

3-
import com.cosimo.utilities.menu.IMenu;
43
import com.google.common.base.Preconditions;
54
import lombok.NonNull;
65
import org.bukkit.Bukkit;
@@ -24,9 +23,10 @@
2423
import java.util.logging.Level;
2524

2625
/**
27-
* A {@link Listener} that filters {@link org.bukkit.event.inventory.InventoryEvent}s to registered {@link Inventory}
28-
* {@link IMenu}s. Menu inventory listening is delegated and centralised in one {@link MenuManager} because of the
29-
* assumption that constantly registering and unregistering new {@link Listener}s for each {@link IMenu} is slow.
26+
* A {@link Listener} that filters and dispatches {@link org.bukkit.event.inventory.InventoryEvent}s to registered
27+
* {@link Inventory} {@link IMenu}s. Menu inventory listening is delegated and centralised in one {@link MenuManager}
28+
* because of the assumption that constantly registering and unregistering new {@link Listener}s for each {@link IMenu}
29+
* is slow.
3030
*
3131
* <p><strong>Note:</strong> {@link IMenu}s should be unregistered from their {@link MenuManager} when they're
3232
* not in use anymore, such as when the last viewer closes an {@link IMenu}. The unfollowing of this rule will cause a
@@ -35,18 +35,16 @@
3535
* <p><strong>Warning:</strong> {@link IMenu}s in multiple {@link MenuManager} instances may cause
3636
* duplicate event handler calls.
3737
*
38-
* @param <E> {@link IMenu} subclass restriction that's always expected to be received or added to this
39-
* {@link MenuManager}
4038
* @author CosimoTiger
4139
*/
42-
public class MenuManager<E extends IMenu> implements Listener {
40+
public class MenuManager implements Listener {
4341

4442
// TODO: WeakHashMap? Inventories might be referenced only by their Bukkit viewers.
4543
// WeakHashMap<Inventory, WeakReference<IMenu>> = new WeakHashMap<>(4);
4644
/**
4745
* Stores {@link IMenu} associations to their {@link Inventory} instances for quick access.
4846
*/
49-
private final Map<Inventory, E> menus;
47+
private final Map<Inventory, IMenu> menus;
5048
private final Plugin provider;
5149

5250
/**
@@ -57,7 +55,7 @@ public class MenuManager<E extends IMenu> implements Listener {
5755
* @throws IllegalArgumentException If the {@link Plugin} argument is null
5856
* @throws IllegalStateException If the {@link Plugin} argument is not enabled
5957
*/
60-
protected MenuManager(@NonNull Plugin provider, @NonNull Map<Inventory, E> mapImpl) {
58+
protected MenuManager(@NonNull Plugin provider, @NonNull Map<Inventory, IMenu> mapImpl) {
6159
Preconditions.checkState(provider.isEnabled(), "Plugin provider argument can't be disabled");
6260
Bukkit.getPluginManager().registerEvents(this, this.provider = provider);
6361
this.menus = mapImpl;
@@ -82,7 +80,7 @@ public MenuManager(@NonNull Plugin provider) {
8280
* @throws IllegalArgumentException If the {@link Inventory} argument is null
8381
*/
8482
@NonNull
85-
public Optional<E> unregisterMenu(@NonNull Inventory inventory) {
83+
public Optional<IMenu> unregisterMenu(@NonNull Inventory inventory) {
8684
return Optional.ofNullable(this.menus.remove(inventory));
8785
}
8886

@@ -94,7 +92,7 @@ public Optional<E> unregisterMenu(@NonNull Inventory inventory) {
9492
* @throws IllegalArgumentException If the {@link IMenu} argument is null
9593
*/
9694
@NonNull
97-
public Optional<E> unregisterMenu(@NonNull E menu) {
95+
public Optional<IMenu> unregisterMenu(@NonNull IMenu menu) {
9896
return Optional.ofNullable(this.menus.remove(menu.getInventory()));
9997
}
10098

@@ -107,7 +105,7 @@ public Optional<E> unregisterMenu(@NonNull E menu) {
107105
* @throws IllegalArgumentException If the {@link IMenu} argument is null
108106
*/
109107
@NonNull
110-
public Optional<E> registerMenu(@NonNull E menu) {
108+
public Optional<IMenu> registerMenu(@NonNull IMenu menu) {
111109
return Optional.ofNullable(this.menus.put(menu.getInventory(), menu));
112110
}
113111

@@ -120,7 +118,7 @@ public Optional<E> registerMenu(@NonNull E menu) {
120118
* @return This instance, useful for chaining
121119
*/
122120
@NonNull
123-
public MenuManager<E> closeMenus() {
121+
public MenuManager closeMenus() {
124122
/*
125123
* Closing each menu calls an InventoryCloseEvent which may unregister a menu from the MenuManager Map of menus.
126124
* Therefore, a ConcurrentModificationException needs to be avoided using an Iterator.
@@ -141,7 +139,7 @@ public MenuManager<E> closeMenus() {
141139
* @throws IllegalArgumentException If the {@link Inventory} argument is null
142140
*/
143141
@NonNull
144-
public Optional<E> getMenu(@NonNull Inventory inventory) {
142+
public Optional<IMenu> getMenu(@NonNull Inventory inventory) {
145143
return Optional.ofNullable(this.menus.get(inventory));
146144
}
147145

@@ -154,7 +152,7 @@ public Optional<E> getMenu(@NonNull Inventory inventory) {
154152
* @throws IllegalArgumentException If the viewer argument is null
155153
*/
156154
@NonNull
157-
public Optional<E> getMenu(@NonNull HumanEntity viewer) {
155+
public Optional<IMenu> getMenu(@NonNull HumanEntity viewer) {
158156
return this.getMenu(viewer.getOpenInventory().getTopInventory());
159157
}
160158

@@ -166,7 +164,7 @@ public Optional<E> getMenu(@NonNull HumanEntity viewer) {
166164
*/
167165
@NonNull
168166
@UnmodifiableView
169-
public Map<Inventory, E> getMap() {
167+
public Map<Inventory, IMenu> getMap() {
170168
return Collections.unmodifiableMap(this.menus);
171169
}
172170

0 commit comments

Comments
 (0)