1717import java .util .Optional ;
1818import java .util .function .Consumer ;
1919import java .util .function .Function ;
20+ import java .util .logging .Level ;
2021
2122public class InventoryAPI implements Listener {
2223
@@ -26,6 +27,7 @@ public class InventoryAPI implements Listener {
2627 private List <ItemAPI > items ;
2728 private Consumer <InventoryAPI > function ;
2829 private boolean refreshed ;
30+ private boolean build ;
2931
3032 private JavaPlugin plugin ;
3133
@@ -45,6 +47,10 @@ public static InventoryAPI create(final JavaPlugin plugin) {
4547 }
4648
4749 public InventoryAPI setSize (final int size ) {
50+ if (build ) {
51+ this .plugin .getLogger ().log (Level .WARNING , "Can't edit \" size\" option in InventoryAPI 'cause the inventory is already built" );
52+ return this ;
53+ }
4854 if (this .size <= 0 || this .size % 9 != 0 || this .size >= 54 ) {
4955 plugin .getLogger ().severe ("This inventory can't have a size of " + size );
5056 return this ;
@@ -58,6 +64,10 @@ public InventoryAPI setSize(final int size) {
5864 }
5965
6066 public InventoryAPI setTitle (final String title ) {
67+ if (build ) {
68+ this .plugin .getLogger ().log (Level .WARNING , "Can't edit \" title\" option in InventoryAPI 'cause the inventory is already built" );
69+ return this ;
70+ }
6171 if (this .inventory != null && !this .title .equals (title )) {
6272 this .inventory .clear ();
6373 this .inventory = Bukkit .createInventory (null , this .size , title );
@@ -67,11 +77,19 @@ public InventoryAPI setTitle(final String title) {
6777 }
6878
6979 public InventoryAPI setRefresh (final boolean refreshed ) {
80+ if (build ) {
81+ this .plugin .getLogger ().log (Level .WARNING , "Can't edit \" refresh\" option in InventoryAPI 'cause the inventory is already built" );
82+ return this ;
83+ }
7084 this .refreshed = refreshed ;
7185 return this ;
7286 }
7387
7488 public InventoryAPI setFunction (Consumer <InventoryAPI > function ) {
89+ if (build ) {
90+ this .plugin .getLogger ().log (Level .WARNING , "Can't edit \" function\" option in InventoryAPI 'cause the inventory is already built" );
91+ return this ;
92+ }
7593 this .function = function ;
7694 return this ;
7795 }
@@ -152,6 +170,7 @@ public InventoryAPI addItem(final ItemAPI itemAPI) {
152170 }
153171
154172 public void build (final Player player ) {
173+ this .build = true ;
155174 if (this .inventory == null ) {
156175 this .inventory = Bukkit .createInventory (player , this .size , this .title );
157176 if (this .function != null )
@@ -163,7 +182,8 @@ public void build(final Player player) {
163182 this .inventory .setItem (itemAPI .getSlot (), itemAPI .getItem ());
164183 });
165184 player .openInventory (this .inventory );
166- Scheduler .getInstance ().add (this );
185+ if (this .refreshed )
186+ Scheduler .getInstance ().add (this );
167187 plugin .getServer ().getPluginManager ().registerEvents (this , this .plugin );
168188 }
169189 else {
@@ -180,7 +200,8 @@ public void build(final Player player) {
180200
181201 public void stop () {
182202 HandlerList .unregisterAll (this );
183- Scheduler .getInstance ().remove (this );
203+ if (this .refreshed )
204+ Scheduler .getInstance ().remove (this );
184205 this .inventory = null ;
185206 }
186207
0 commit comments