11package me .hsgamer .bettergui .menu ;
22
3- import me .hsgamer .bettergui .action .ActionApplier ;
4- import me .hsgamer .bettergui .api .menu .StandardMenu ;
53import me .hsgamer .bettergui .api .requirement .Requirement ;
6- import me .hsgamer .bettergui .argument .ArgumentHandler ;
74import me .hsgamer .bettergui .builder .InventoryBuilder ;
8- import me .hsgamer .bettergui .requirement .RequirementApplier ;
95import me .hsgamer .bettergui .util .ProcessApplierConstants ;
106import me .hsgamer .bettergui .util .StringReplacerApplier ;
117import me .hsgamer .hscore .bukkit .gui .BukkitGUIDisplay ;
128import me .hsgamer .hscore .bukkit .gui .BukkitGUIHolder ;
139import me .hsgamer .hscore .bukkit .scheduler .Scheduler ;
1410import me .hsgamer .hscore .bukkit .scheduler .Task ;
15- import me .hsgamer .hscore .bukkit .utils .MessageUtils ;
16- import me .hsgamer .hscore .bukkit .utils .PermissionUtils ;
17- import me .hsgamer .hscore .common .CollectionUtils ;
1811import me .hsgamer .hscore .common .MapUtils ;
1912import me .hsgamer .hscore .common .Pair ;
2013import me .hsgamer .hscore .common .Validate ;
2720import org .bukkit .Bukkit ;
2821import org .bukkit .entity .Player ;
2922import org .bukkit .event .inventory .InventoryType ;
30- import org .bukkit .permissions .Permission ;
3123import org .jetbrains .annotations .NotNull ;
3224
3325import java .math .BigDecimal ;
3426import java .util .*;
3527import java .util .concurrent .ConcurrentHashMap ;
3628import java .util .concurrent .ConcurrentSkipListSet ;
3729import java .util .function .Consumer ;
38- import java .util .stream .Collectors ;
3930
4031import static me .hsgamer .bettergui .BetterGUI .getInstance ;
4132
42- public abstract class BaseInventoryMenu <B extends ButtonMap > extends StandardMenu {
43- private final RequirementApplier viewRequirementApplier ;
33+ /**
34+ * A {@link BaseMenu} for menus using {@link BukkitGUIHolder}
35+ *
36+ * @param <B> the type of the {@link ButtonMap} to use in the {@link BukkitGUIHolder}
37+ */
38+ public abstract class BaseInventoryMenu <B extends ButtonMap > extends BaseMenu {
4439 private final BukkitGUIHolder guiHolder ;
4540 private final B buttonMap ;
4641 private final Set <UUID > forceClose = new ConcurrentSkipListSet <>();
4742 private final Map <UUID , Task > updateTasks = new ConcurrentHashMap <>();
4843 private final long ticks ;
49- private final List <Permission > permissions ;
50- private final ArgumentHandler argumentHandler ;
5144
5245 protected BaseInventoryMenu (Config config ) {
5346 super (config );
@@ -67,26 +60,53 @@ protected void onRemoveDisplay(@NotNull BukkitGUIDisplay display) {
6760 Optional .ofNullable (updateTasks .remove (display .getUniqueId ())).ifPresent (Task ::cancel );
6861 super .onRemoveDisplay (display );
6962 }
70- };
7163
72- List <Consumer <BukkitGUIHolder >> postInitActions = new ArrayList <>();
73- Optional .ofNullable (menuSettings .get ("open-action" ))
74- .map (o -> new ActionApplier (this , o ))
75- .ifPresent (actionApplier -> postInitActions .add (holder -> holder .addEventConsumer (OpenEvent .class , openEvent -> {
76- UUID uuid = openEvent .getViewerID ();
77- BatchRunnable batchRunnable = new BatchRunnable ();
78- batchRunnable .getTaskPool (ProcessApplierConstants .ACTION_STAGE ).addLast (process -> actionApplier .accept (uuid , process ));
79- Scheduler .current ().async ().runTask (batchRunnable );
80- })));
64+ @ Override
65+ protected void onOpen (@ NotNull OpenEvent event ) {
66+ if (!openActionApplier .isEmpty ()) {
67+ UUID uuid = event .getViewerID ();
68+ BatchRunnable batchRunnable = new BatchRunnable ();
69+ batchRunnable .getTaskPool (ProcessApplierConstants .ACTION_STAGE ).addLast (process -> openActionApplier .accept (uuid , process ));
70+ Scheduler .current ().async ().runTask (batchRunnable );
71+ }
72+ }
73+
74+ @ Override
75+ protected void onClose (@ NotNull CloseEvent event ) {
76+ UUID uuid = event .getViewerID ();
77+
78+ if (!closeActionApplier .isEmpty ()) {
79+ BatchRunnable batchRunnable = new BatchRunnable ();
80+ batchRunnable .getTaskPool (ProcessApplierConstants .ACTION_STAGE ).addLast (process -> closeActionApplier .accept (uuid , process ));
81+ Scheduler .current ().async ().runTask (batchRunnable );
82+ }
83+
84+ if (!closeRequirementApplier .isEmpty ()) {
85+ if (forceClose .contains (uuid )) {
86+ forceClose .remove (uuid );
87+ return ;
88+ }
89+ Requirement .Result result = closeRequirementApplier .getResult (uuid );
90+
91+ BatchRunnable batchRunnable = new BatchRunnable ();
92+ batchRunnable .getTaskPool (ProcessApplierConstants .REQUIREMENT_ACTION_STAGE ).addLast (process -> {
93+ result .applier .accept (uuid , process );
94+ process .next ();
95+ });
96+ Scheduler .current ().async ().runTask (batchRunnable );
8197
82- Optional .ofNullable (menuSettings .get ("close-action" ))
83- .map (o -> new ActionApplier (this , o ))
84- .ifPresent (actionApplier -> postInitActions .add (holder -> holder .addEventConsumer (CloseEvent .class , openEvent -> {
85- UUID uuid = openEvent .getViewerID ();
86- BatchRunnable batchRunnable = new BatchRunnable ();
87- batchRunnable .getTaskPool (ProcessApplierConstants .ACTION_STAGE ).addLast (process -> actionApplier .accept (uuid , process ));
88- Scheduler .current ().async ().runTask (batchRunnable );
89- })));
98+ if (!result .isSuccess ) {
99+ event .setRemoveDisplay (false );
100+ guiHolder .getDisplay (uuid ).ifPresent (display -> {
101+ Player player = Bukkit .getPlayer (uuid );
102+ if (player != null ) {
103+ Scheduler .current ().sync ().runEntityTask (player , () -> player .openInventory (display .getInventory ()));
104+ }
105+ });
106+ }
107+ }
108+ }
109+ };
90110
91111 Optional .ofNullable (MapUtils .getIfFound (menuSettings , "inventory-type" , "inventory" )).ifPresent (o -> {
92112 try {
@@ -115,66 +135,13 @@ protected void onRemoveDisplay(@NotNull BukkitGUIDisplay display) {
115135 .map (BigDecimal ::longValue )
116136 .orElse (0L );
117137
118- viewRequirementApplier = Optional .ofNullable (menuSettings .get ("view-requirement" ))
119- .flatMap (MapUtils ::castOptionalStringObjectMap )
120- .map (m -> new RequirementApplier (this , getName () + "_view" , m ))
121- .orElseGet (() -> new RequirementApplier (this , getName (), Collections .emptyMap ()));
122-
123138 Optional .ofNullable (menuSettings .get ("cached" ))
124139 .map (String ::valueOf )
125140 .map (Boolean ::parseBoolean )
126141 .ifPresent (cached -> {
127142 guiHolder .addEventConsumer (CloseEvent .class , closeEvent -> closeEvent .setRemoveDisplay (!cached ));
128143 });
129144
130- Optional .ofNullable (menuSettings .get ("close-requirement" ))
131- .flatMap (MapUtils ::castOptionalStringObjectMap )
132- .map (m -> new RequirementApplier (this , getName () + "_close" , m ))
133- .ifPresent (closeRequirementApplier -> {
134- guiHolder .addEventConsumer (CloseEvent .class , closeEvent -> {
135- UUID uuid = closeEvent .getViewerID ();
136- if (forceClose .contains (uuid )) {
137- forceClose .remove (uuid );
138- return ;
139- }
140- Requirement .Result result = closeRequirementApplier .getResult (uuid );
141-
142- BatchRunnable batchRunnable = new BatchRunnable ();
143- batchRunnable .getTaskPool (ProcessApplierConstants .REQUIREMENT_ACTION_STAGE ).addLast (process -> {
144- result .applier .accept (uuid , process );
145- process .next ();
146- });
147- Scheduler .current ().async ().runTask (batchRunnable );
148-
149- if (!result .isSuccess ) {
150- closeEvent .setRemoveDisplay (false );
151- guiHolder .getDisplay (uuid ).ifPresent (display -> {
152- Player player = Bukkit .getPlayer (uuid );
153- if (player != null ) {
154- Scheduler .current ().sync ().runEntityTask (player , () -> player .openInventory (display .getInventory ()));
155- }
156- });
157- }
158- });
159- });
160-
161- permissions = Optional .ofNullable (menuSettings .get ("permission" ))
162- .map (o -> CollectionUtils .createStringListFromObject (o , true ))
163- .map (l -> l .stream ().map (Permission ::new ).collect (Collectors .toList ()))
164- .orElseGet (() -> Collections .singletonList (new Permission (getInstance ().getName ().toLowerCase () + "." + getName ())));
165-
166- Optional .ofNullable (menuSettings .get ("command" ))
167- .map (o -> CollectionUtils .createStringListFromObject (o , true ))
168- .ifPresent (list -> {
169- for (String s : list ) {
170- if (s .contains (" " )) {
171- getInstance ().getLogger ().warning ("Illegal characters in command '" + s + "'" + "in the menu '" + getName () + "'. Ignored" );
172- } else {
173- getInstance ().getMenuCommandManager ().registerMenuCommand (s , this );
174- }
175- }
176- });
177-
178145 Optional .ofNullable (MapUtils .getIfFound (menuSettings , "name" , "title" ))
179146 .map (String ::valueOf )
180147 .ifPresent (s -> guiHolder .setTitleFunction (uuid -> StringReplacerApplier .replace (s , uuid , this )));
@@ -206,62 +173,20 @@ public void accept(ClickEvent clickEvent) {
206173 });
207174 }
208175
209- argumentHandler = Optional .ofNullable (MapUtils .getIfFound (menuSettings , "argument-processor" , "arg-processor" , "argument" , "arg" ))
210- .flatMap (MapUtils ::castOptionalStringObjectMap )
211- .map (m -> new ArgumentHandler (this , m ))
212- .orElseGet (() -> new ArgumentHandler (this , Collections .emptyMap ()));
213-
214176 buttonMap = createButtonMap ();
215177 guiHolder .setButtonMap (buttonMap );
216178
217179 guiHolder .init ();
218- postInitActions .forEach (action -> action .accept (guiHolder ));
219180 }
220181
221182 @ Override
222- public boolean create (Player player , String [] args , boolean bypass ) {
183+ protected boolean createChecked (Player player , String [] args , boolean bypass ) {
223184 UUID uuid = player .getUniqueId ();
224-
225- // Check Argument
226- if (!argumentHandler .process (uuid , args ).isPresent ()) {
227- return false ;
228- }
229-
230- // Refresh Button Map
231185 refreshButtonMapOnCreate (buttonMap , uuid );
232-
233- // Check Permission
234- if (!bypass && !PermissionUtils .hasAnyPermission (player , permissions )) {
235- MessageUtils .sendMessage (player , getInstance ().getMessageConfig ().getNoPermission ());
236- return false ;
237- }
238-
239- // Check Requirement
240- if (!bypass ) {
241- Requirement .Result result = viewRequirementApplier .getResult (uuid );
242-
243- BatchRunnable batchRunnable = new BatchRunnable ();
244- batchRunnable .getTaskPool (ProcessApplierConstants .REQUIREMENT_ACTION_STAGE ).addLast (process -> {
245- result .applier .accept (uuid , process );
246- process .next ();
247- });
248- Scheduler .current ().async ().runTask (batchRunnable );
249-
250- if (!result .isSuccess ) {
251- return false ;
252- }
253- }
254-
255- // Open Inventory
256186 guiHolder .createDisplay (uuid ).open ();
257187 return true ;
258188 }
259189
260- @ Override
261- public List <String > tabComplete (Player player , String [] args ) {
262- return argumentHandler .handleTabComplete (player .getUniqueId (), args );
263- }
264-
265190 @ Override
266191 public void update (Player player ) {
267192 guiHolder .getDisplay (player .getUniqueId ()).ifPresent (BukkitGUIDisplay ::update );
@@ -291,8 +216,4 @@ public B getButtonMap() {
291216 public BukkitGUIHolder getGUIHolder () {
292217 return guiHolder ;
293218 }
294-
295- public ArgumentHandler getArgumentHandler () {
296- return argumentHandler ;
297- }
298219}
0 commit comments