Skip to content

Commit ee1ad6e

Browse files
committed
feat: timer state
1 parent bc7771b commit ee1ad6e

5 files changed

Lines changed: 64 additions & 20 deletions

File tree

inventory-framework-api/src/main/java/me/devnatan/inventoryframework/ViewConfigBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Set;
99
import java.util.stream.Collectors;
1010
import me.devnatan.inventoryframework.exception.InvalidLayoutException;
11+
import me.devnatan.inventoryframework.state.TimerState;
1112
import org.jetbrains.annotations.ApiStatus;
1213
import org.jetbrains.annotations.NotNull;
1314

@@ -31,6 +32,7 @@ public final class ViewConfigBuilder {
3132
private String[] layout = null;
3233
private final Set<ViewConfig.Modifier> modifiers = new HashSet<>();
3334
private long updateIntervalInTicks, interactionDelayInMillis;
35+
private TimerState updateIntervalState;
3436
private boolean transitiveInitialData;
3537

3638
/**
@@ -200,6 +202,19 @@ public ViewConfigBuilder scheduleUpdate(long intervalInTicks) {
200202
return this;
201203
}
202204

205+
/**
206+
* Schedules the view to update every fixed interval.
207+
*
208+
* @param timerState The
209+
* @return This configuration builder.
210+
* @see <a href="https://github.com/DevNatan/inventory-framework/wiki/scheduled-updates">Scheduled Updates on Wiki</a>
211+
*/
212+
@ApiStatus.Experimental
213+
public ViewConfigBuilder scheduleUpdate(TimerState timerState) {
214+
this.updateIntervalState = timerState;
215+
return this;
216+
}
217+
203218
/**
204219
* Waits a fixed delay before any player interaction.
205220
* <p>

inventory-framework-api/src/main/java/me/devnatan/inventoryframework/internal/ElementFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import me.devnatan.inventoryframework.context.IFSlotClickContext;
1818
import me.devnatan.inventoryframework.context.IFSlotRenderContext;
1919
import me.devnatan.inventoryframework.logging.Logger;
20+
import me.devnatan.inventoryframework.state.TimerState;
2021
import org.jetbrains.annotations.NotNull;
2122
import org.jetbrains.annotations.Nullable;
2223

@@ -89,4 +90,6 @@ public abstract IFCloseContext createCloseContext(
8990
public abstract boolean worksInCurrentPlatform();
9091

9192
public abstract Job scheduleJobInterval(@NotNull RootView root, long intervalInTicks, @NotNull Runnable execution);
93+
94+
public abstract TimerState createTimerState(long stateId, long intervalInTicks);
9295
}

inventory-framework-api/src/main/java/me/devnatan/inventoryframework/state/StateAccess.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ public interface StateAccess<
173173
*/
174174
<T> MutableState<T> initialState(@NotNull String key);
175175

176+
/**
177+
* Creates a new unmodifiable timer state.
178+
*
179+
* @param intervalInTicks Ticks to schedule the timer to run.
180+
* <p>
181+
* <b><i> This API is experimental and is not subject to the general compatibility guarantees
182+
* such API may be changed or may be removed completely in any further release. </i></b>
183+
*
184+
* @return A new unmodifiable timer state.
185+
*/
186+
@ApiStatus.Experimental
187+
TimerState timerState(long intervalInTicks);
188+
176189
/**
177190
* Creates a new immutable pagination with static data source.
178191
*
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.devnatan.inventoryframework.state;
2+
3+
public interface TimerState extends State<TimerState.Timer> {
4+
5+
interface Timer {}
6+
}

inventory-framework-core/src/main/java/me/devnatan/inventoryframework/state/StateAccessImpl.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public StateAccessImpl(
3939
}
4040

4141
@Override
42-
public final <T> State<T> state(T initialValue) {
42+
public <T> State<T> state(T initialValue) {
4343
final long id = State.next();
4444
final StateValueFactory factory = (host, state) -> new ImmutableValue(state, initialValue);
4545
final State<T> state = new BaseState<>(id, factory);
@@ -49,7 +49,7 @@ public final <T> State<T> state(T initialValue) {
4949
}
5050

5151
@Override
52-
public final <T> MutableState<T> mutableState(T initialValue) {
52+
public <T> MutableState<T> mutableState(T initialValue) {
5353
final long id = State.next();
5454
final StateValueFactory factory = (host, state) -> new MutableValue(state, initialValue);
5555
final MutableState<T> state = new MutableGenericStateImpl<>(id, factory);
@@ -59,7 +59,7 @@ public final <T> MutableState<T> mutableState(T initialValue) {
5959
}
6060

6161
@Override
62-
public final MutableIntState mutableState(int initialValue) {
62+
public MutableIntState mutableState(int initialValue) {
6363
final long id = State.next();
6464
final StateValueFactory factory = (host, state) -> new MutableValue(state, initialValue);
6565
final MutableIntState state = new MutableIntStateImpl(id, factory);
@@ -69,7 +69,7 @@ public final MutableIntState mutableState(int initialValue) {
6969
}
7070

7171
@Override
72-
public final <T> State<T> computedState(@NotNull Function<Context, T> computation) {
72+
public <T> State<T> computedState(@NotNull Function<Context, T> computation) {
7373
final long id = State.next();
7474
@SuppressWarnings("unchecked")
7575
final StateValueFactory factory =
@@ -81,7 +81,7 @@ public final <T> State<T> computedState(@NotNull Function<Context, T> computatio
8181
}
8282

8383
@Override
84-
public final <T> State<T> computedState(@NotNull Supplier<T> computation) {
84+
public <T> State<T> computedState(@NotNull Supplier<T> computation) {
8585
final long id = State.next();
8686
final StateValueFactory factory = (host, state) -> new ComputedValue(state, computation);
8787
final State<T> state = new BaseState<>(id, factory);
@@ -91,7 +91,7 @@ public final <T> State<T> computedState(@NotNull Supplier<T> computation) {
9191
}
9292

9393
@Override
94-
public final <T> State<T> lazyState(@NotNull Function<Context, T> computation) {
94+
public <T> State<T> lazyState(@NotNull Function<Context, T> computation) {
9595
final long id = State.next();
9696
@SuppressWarnings("unchecked")
9797
final StateValueFactory factory =
@@ -103,7 +103,7 @@ public final <T> State<T> lazyState(@NotNull Function<Context, T> computation) {
103103
}
104104

105105
@Override
106-
public final <T> State<T> lazyState(@NotNull Supplier<T> computation) {
106+
public <T> State<T> lazyState(@NotNull Supplier<T> computation) {
107107
final long id = State.next();
108108
final StateValueFactory factory = (host, state) -> new LazyValue(state, computation);
109109
final State<T> state = new BaseState<>(id, factory);
@@ -114,13 +114,13 @@ public final <T> State<T> lazyState(@NotNull Supplier<T> computation) {
114114

115115
@SuppressWarnings("DataFlowIssue")
116116
@Override
117-
public final <T> MutableState<T> initialState() {
117+
public <T> MutableState<T> initialState() {
118118
return initialState(null);
119119
}
120120

121121
@SuppressWarnings("NullableProblems")
122122
@Override
123-
public final <T> MutableState<T> initialState(@NotNull String key) {
123+
public <T> MutableState<T> initialState(@NotNull String key) {
124124
final long id = State.next();
125125
final MutableState<T> state =
126126
new BaseMutableState<>(id, (host, valueState) -> new InitialDataStateValue(valueState, host, key));
@@ -130,7 +130,14 @@ public final <T> MutableState<T> initialState(@NotNull String key) {
130130
}
131131

132132
@Override
133-
public final <T> State<Pagination> paginationState(
133+
public TimerState timerState(long intervalInTicks) {
134+
final TimerState state = elementFactory.createTimerState(State.next(), intervalInTicks);
135+
this.stateRegistry.registerState(state, this);
136+
return state;
137+
}
138+
139+
@Override
140+
public <T> State<Pagination> paginationState(
134141
@NotNull List<? super T> sourceProvider,
135142
@NotNull PaginationValueConsumer<Context, ItemBuilder, T> elementConsumer) {
136143
return this.<T>buildPaginationState(sourceProvider)
@@ -139,7 +146,7 @@ public final <T> State<Pagination> paginationState(
139146
}
140147

141148
@Override
142-
public final <T> State<Pagination> computedPaginationState(
149+
public <T> State<Pagination> computedPaginationState(
143150
@NotNull Function<Context, List<? super T>> sourceProvider,
144151
@NotNull PaginationValueConsumer<Context, ItemBuilder, T> valueConsumer) {
145152
return this.buildComputedPaginationState(sourceProvider)
@@ -148,7 +155,7 @@ public final <T> State<Pagination> computedPaginationState(
148155
}
149156

150157
@Override
151-
public final <T> State<Pagination> computedAsyncPaginationState(
158+
public <T> State<Pagination> computedAsyncPaginationState(
152159
@NotNull Function<Context, CompletableFuture<List<T>>> sourceProvider,
153160
@NotNull PaginationValueConsumer<Context, ItemBuilder, T> valueConsumer) {
154161
return this.buildComputedAsyncPaginationState(sourceProvider)
@@ -157,7 +164,7 @@ public final <T> State<Pagination> computedAsyncPaginationState(
157164
}
158165

159166
@Override
160-
public final <T> State<Pagination> lazyPaginationState(
167+
public <T> State<Pagination> lazyPaginationState(
161168
@NotNull Function<Context, List<? super T>> sourceProvider,
162169
@NotNull PaginationValueConsumer<Context, ItemBuilder, T> valueConsumer) {
163170
return this.buildLazyPaginationState(sourceProvider)
@@ -184,48 +191,48 @@ public <T> State<Pagination> lazyAsyncPaginationState(
184191
}
185192

186193
@Override
187-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildPaginationState(
194+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildPaginationState(
188195
@NotNull List<? super T> sourceProvider) {
189196
return new PaginationStateBuilder<>(
190197
elementFactoryProvider, sourceProvider, this::createPaginationState, false, false);
191198
}
192199

193200
@Override
194-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildComputedPaginationState(
201+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildComputedPaginationState(
195202
@NotNull Function<Context, List<? super T>> sourceProvider) {
196203
return new PaginationStateBuilder<>(
197204
elementFactoryProvider, sourceProvider, this::createPaginationState, false, true);
198205
}
199206

200207
@Override
201-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildComputedAsyncPaginationState(
208+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildComputedAsyncPaginationState(
202209
@NotNull Function<Context, CompletableFuture<List<T>>> sourceProvider) {
203210
return new PaginationStateBuilder<>(
204211
elementFactoryProvider, sourceProvider, this::createPaginationState, true, true);
205212
}
206213

207214
@Override
208-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyPaginationState(
215+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyPaginationState(
209216
@NotNull Supplier<List<? super T>> sourceProvider) {
210217
return new PaginationStateBuilder<>(
211218
elementFactoryProvider, sourceProvider, this::createPaginationState, false, false);
212219
}
213220

214221
@Override
215-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyPaginationState(
222+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyPaginationState(
216223
@NotNull Function<Context, List<? super T>> sourceProvider) {
217224
return new PaginationStateBuilder<>(
218225
elementFactoryProvider, sourceProvider, this::createPaginationState, false, false);
219226
}
220227

221228
@Override
222-
public final <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyAsyncPaginationState(
229+
public <T> PaginationStateBuilder<Context, ItemBuilder, T> buildLazyAsyncPaginationState(
223230
@NotNull Function<Context, CompletableFuture<List<T>>> sourceProvider) {
224231
return new PaginationStateBuilder<>(
225232
elementFactoryProvider, sourceProvider, this::createPaginationState, true, false);
226233
}
227234

228-
protected final <V> State<Pagination> createPaginationState(
235+
private <V> State<Pagination> createPaginationState(
229236
@NotNull PaginationStateBuilder<Context, ItemBuilder, V> builder) {
230237
final long id = State.next();
231238
@SuppressWarnings({"unchecked", "rawtypes"})

0 commit comments

Comments
 (0)