Skip to content

Commit f710e7a

Browse files
Non-freezing animations
1 parent 48af12e commit f710e7a

5 files changed

Lines changed: 48 additions & 24 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public sealed abstract class AbstractGui
6464
}
6565

6666
public void handleClick(int slot, Click click) {
67-
// ignore all clicks if the gui is frozen or an animation is running
68-
if (frozen || animation != null)
67+
// ignore all clicks if the gui is frozen
68+
if (isFrozen())
6969
return;
7070

7171
SlotElement slotElement = slotElements[slot];
@@ -78,8 +78,8 @@ public void handleClick(int slot, Click click) {
7878
}
7979

8080
public void handleBundleSelect(Player player, int slot, int bundleSlot) {
81-
// ignore all clicks if the gui is frozen or an animation is running
82-
if (frozen || animation != null)
81+
// ignore all clicks if the gui is frozen
82+
if (isFrozen())
8383
return;
8484

8585
SlotElement slotElement = slotElements[slot];
@@ -691,7 +691,7 @@ public void setFrozen(boolean frozen) {
691691

692692
@Override
693693
public boolean isFrozen() {
694-
return frozen;
694+
return frozen || (animation != null && animation.isFreezing());
695695
}
696696

697697
@Override

invui/src/main/java/xyz/xenondevs/invui/gui/Animation.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ sealed interface Builder permits AnimationImpl.BuilderImpl {
156156
* Sets the delay between invocations of the slot selector.
157157
*
158158
* @param tickDelay The delay between invocations of the slot selector.
159-
* @return This {@link Builder Animation builder}
159+
* @return This {@link Builder}
160160
*/
161161
Builder setTickDelay(int tickDelay);
162162

163163
/**
164164
* Adds a filter defining which slots are part of the animation.
165165
*
166166
* @param filter The filter defining which slots are part of the animation.
167-
* @return This {@link Builder Animation builder}
167+
* @return This {@link Builder}
168168
*/
169169
Builder addSlotFilter(BiPredicate<? super Gui, ? super Slot> filter);
170170

171171
/**
172172
* Adds a {@link #addSlotFilter(BiPredicate) filter} that ignores all empty slots.
173173
*
174-
* @return This {@link Builder Animation builder}
174+
* @return This {@link Builder}
175175
*/
176176
default Builder filterNonEmptySlots() {
177177
return addSlotFilter((gui, slot) -> gui.getSlotElement(slot.x(), slot.y()) != null);
@@ -183,7 +183,7 @@ default Builder filterNonEmptySlots() {
183183
*
184184
* @param key The key to filter for
185185
* @param keys Additional keys to filter for
186-
* @return This {@link Builder Animation builder}
186+
* @return This {@link Builder}
187187
* @see Gui#applyStructure(Structure)
188188
*/
189189
default Builder filterTaggedSlots(char key, char... keys) {
@@ -197,7 +197,7 @@ default Builder filterTaggedSlots(char key, char... keys) {
197197
* @param sound The sound effect type
198198
* @param volume The volume of the sound
199199
* @param pitch The pitch of the sound
200-
* @return This {@link Builder Animation builder}
200+
* @return This {@link Builder}
201201
*/
202202
default Builder addSoundEffect(Sound sound, float volume, float pitch) {
203203
return addShowHandler((animation, slot) ->
@@ -213,15 +213,15 @@ default Builder addSoundEffect(Sound sound, float volume, float pitch) {
213213
* Slot selectors need to show all slots of {@link State#getRemainingSlots()} in order to complete the animation.
214214
*
215215
* @param selector The slot selector for the animation.
216-
* @return This {@link Builder Animation builder}
216+
* @return This {@link Builder}
217217
*/
218218
Builder setSlotSelector(Function<? super State, ? extends Set<? extends Slot>> selector);
219219

220220
/**
221221
* Sets the intermediary {@link ItemProvider} that is displayed before the slots pop in.
222222
*
223223
* @param provider The intermediary {@link ItemProvider}.
224-
* @return This {@link Builder Animation builder}
224+
* @return This {@link Builder}
225225
*/
226226
default Builder setIntermediary(ItemProvider provider) {
227227
return setIntermediary(Item.simple(provider));
@@ -231,7 +231,7 @@ default Builder setIntermediary(ItemProvider provider) {
231231
* Sets the intermediary {@link Item} that is displayed before the slots pop in.
232232
*
233233
* @param item The intermediary {@link Item}.
234-
* @return This {@link Builder Animation builder}
234+
* @return This {@link Builder}
235235
*/
236236
default Builder setIntermediary(Item item) {
237237
var element = new SlotElement.Item(item);
@@ -243,7 +243,7 @@ default Builder setIntermediary(Item item) {
243243
* before the {@link Gui Gui's} slots pop in.
244244
*
245245
* @param intermediaryGenerator The generator function that creates intermediary {@link ItemProvider ItemProviders}.
246-
* @return This {@link Builder Animation builder}
246+
* @return This {@link Builder}
247247
*/
248248
default Builder setIntermediaryGenerator(Function<? super Slot, ? extends @Nullable ItemProvider> intermediaryGenerator) {
249249
return setIntermediaryElementGenerator(slot -> {
@@ -257,26 +257,35 @@ default Builder setIntermediaryGenerator(Function<? super Slot, ? extends @Nulla
257257
* before the {@link Gui Gui's} slots pop in.
258258
*
259259
* @param intermediaryGenerator The generator function that creates intermediary {@link SlotElement SlotElements}.
260-
* @return This {@link Builder Animation builder}
260+
* @return This {@link Builder}
261261
*/
262262
Builder setIntermediaryElementGenerator(Function<? super Slot, ? extends @Nullable SlotElement> intermediaryGenerator);
263263

264264
/**
265265
* Adds a handler that is called when slot(s) are shown.
266266
*
267267
* @param showHandler The handler that is called when slot(s) are shown.
268-
* @return This {@link Builder Animation builder}
268+
* @return This {@link Builder}
269269
*/
270270
Builder addShowHandler(BiConsumer<? super State, ? super Set<? extends Slot>> showHandler);
271271

272272
/**
273273
* Adds a handler that is called when the animation is finished.
274274
*
275275
* @param finishHandler The handler that is called when the animation is finished.
276-
* @return This {@link Builder Animation builder}
276+
* @return This {@link Builder}
277277
*/
278278
Builder addFinishHandler(Consumer<? super State> finishHandler);
279279

280+
/**
281+
* Sets whether the {@link Gui} that plays the animation should be frozen (i.e. not allow any interactions)
282+
* during it. Defaults to true.
283+
*
284+
* @param freezing Whether the {@link Gui} should be frozen during the animation.
285+
* @return This {@link Builder}
286+
*/
287+
Builder setFreezing(boolean freezing);
288+
280289
/**
281290
* Builds the animation.
282291
*

invui/src/main/java/xyz/xenondevs/invui/gui/AnimationImpl.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package xyz.xenondevs.invui.gui;
22

33
import org.bukkit.Bukkit;
4-
import org.bukkit.scheduler.BukkitScheduler;
54
import org.bukkit.scheduler.BukkitTask;
65
import org.jspecify.annotations.Nullable;
76
import xyz.xenondevs.invui.InvUI;
@@ -23,21 +22,24 @@ final class AnimationImpl implements Animation {
2322
private final Function<? super Slot, ? extends @Nullable SlotElement> intermediaryGenerator;
2423
private final BiConsumer<? super State, ? super Set<? extends Slot>> showHandler;
2524
private final Consumer<? super State> finishHandler;
25+
private final boolean freezing;
2626

2727
public AnimationImpl(
2828
int tickDelay,
2929
BiPredicate<? super Gui, ? super Slot> slotFilter,
3030
Function<? super State, ? extends Set<? extends Slot>> slotSelector,
3131
Function<? super Slot, ? extends @Nullable SlotElement> intermediaryGenerator,
3232
BiConsumer<? super State, ? super Set<? extends Slot>> showHandler,
33-
Consumer<? super State> finishHandler
33+
Consumer<? super State> finishHandler,
34+
boolean freezing
3435
) {
3536
this.tickDelay = tickDelay;
3637
this.slotFilter = slotFilter;
3738
this.slotSelector = slotSelector;
3839
this.intermediaryGenerator = intermediaryGenerator;
3940
this.showHandler = showHandler;
4041
this.finishHandler = finishHandler;
42+
this.freezing = freezing;
4143
}
4244

4345
final class StateImpl implements State {
@@ -70,7 +72,7 @@ public StateImpl(
7072
}
7173

7274
/**
73-
* Starts the animation task timer.
75+
* Starts the animation task timer;
7476
*/
7577
public void start() {
7678
if (isFinished())
@@ -105,6 +107,10 @@ public void cancel() {
105107
return intermediaryGenerator.apply(slot);
106108
}
107109

110+
public boolean isFreezing() {
111+
return freezing;
112+
}
113+
108114
@Override
109115
public Gui getGui() {
110116
return gui;
@@ -135,6 +141,7 @@ static final class BuilderImpl implements Animation.Builder {
135141
private Function<? super Slot, ? extends @Nullable SlotElement> intermediaryGenerator = slot -> null;
136142
private BiConsumer<State, Set<? extends Slot>> showHandler = (state, slot) -> {};
137143
private Consumer<State> finishHandler = gui -> {};
144+
private boolean freezing = true;
138145

139146
@Override
140147
public Builder setTickDelay(int tickDelay) {
@@ -172,12 +179,18 @@ public Builder addSlotFilter(BiPredicate<? super Gui, ? super Slot> filter) {
172179
return this;
173180
}
174181

182+
@Override
183+
public Builder setFreezing(boolean freezing) {
184+
this.freezing = freezing;
185+
return this;
186+
}
187+
175188
@Override
176189
public Animation build() {
177190
if (slotSelector == null)
178191
throw new IllegalStateException("SlotSelector needs to be set");
179192

180-
return new AnimationImpl(tickDelay, slotFilter, slotSelector, intermediaryGenerator, showHandler, finishHandler);
193+
return new AnimationImpl(tickDelay, slotFilter, slotSelector, intermediaryGenerator, showHandler, finishHandler, freezing);
181194
}
182195

183196
}

invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,16 @@ default void setGui(char key, Gui gui) {
586586

587587
/**
588588
* Freezes or unfreezes the {@link Gui}.
589-
* A frozen {@link Gui} will not allow any clicks.
589+
* A frozen {@link Gui} will not allow any interactions.
590590
*
591591
* @param frozen If the {@link Gui} should be frozen or not.
592592
*/
593593
void setFrozen(boolean frozen);
594594

595595
/**
596-
* Gets if the {@link Gui} is frozen.
596+
* Gets if the {@link Gui} is frozen, i.e. does not allow any interactions.
597+
* A {@link Gui} can become frozen either due to a call to {@link #setFrozen(boolean)} or
598+
* because a freezing {@link Animation} is currently running.
597599
*
598600
* @return If the {@link Gui} is frozen.
599601
*/

invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private List<InventorySlot> getActiveInventorySlots(IntSet slots) {
295295
// skip frozen and animating slots
296296
for (SlotElement element : path) {
297297
if (element instanceof SlotElement.GuiLink guiLink) {
298-
if (guiLink.gui().isFrozen() || guiLink.gui().isAnimationRunning())
298+
if (guiLink.gui().isFrozen())
299299
continue slotLoop;
300300
}
301301
}

0 commit comments

Comments
 (0)