Skip to content

Commit 953bc04

Browse files
authored
GH-152 feat!: introduce new dependency injection system and rework plugin core
Thanks to @EternalCodeTeam
1 parent 46ec766 commit 953bc04

263 files changed

Lines changed: 3804 additions & 10095 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.imdmk.playtime;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.time.Duration;
6+
import java.util.concurrent.TimeUnit;
7+
8+
public record PlayTime(long millis) implements Comparable<PlayTime> {
9+
10+
private static final long MILLIS_PER_TICK = 50L;
11+
12+
public static final PlayTime ZERO = new PlayTime(0L);
13+
14+
public PlayTime {
15+
if (millis < 0L) {
16+
throw new IllegalArgumentException("PlayTime millis cannot be negative");
17+
}
18+
}
19+
20+
public static PlayTime of(@NotNull Duration duration) {
21+
return new PlayTime(duration.toMillis());
22+
}
23+
24+
public static PlayTime ofMillis(long millis) {
25+
return new PlayTime(millis);
26+
}
27+
28+
public static PlayTime ofTicks(long ticks) {
29+
return new PlayTime(Math.multiplyExact(ticks, MILLIS_PER_TICK));
30+
}
31+
32+
public long toMillis() {
33+
return millis;
34+
}
35+
36+
public Duration toDuration() {
37+
return Duration.ofMillis(millis);
38+
}
39+
40+
public long toSeconds() {
41+
return TimeUnit.MILLISECONDS.toSeconds(millis);
42+
}
43+
44+
public int toTicks() {
45+
return Math.toIntExact(millis / MILLIS_PER_TICK);
46+
}
47+
48+
public boolean isZero() {
49+
return millis == 0;
50+
}
51+
52+
public PlayTime plus(@NotNull PlayTime other) {
53+
return new PlayTime(Math.addExact(millis, other.millis));
54+
}
55+
56+
public PlayTime minus(@NotNull PlayTime other) {
57+
return new PlayTime(Math.subtractExact(millis, other.millis));
58+
}
59+
60+
@Override
61+
public int compareTo(PlayTime o) {
62+
return Long.compare(millis, o.millis);
63+
}
64+
}
Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
11
package com.github.imdmk.playtime;
22

3-
import com.github.imdmk.playtime.user.UserService;
43
import org.jetbrains.annotations.NotNull;
54

6-
/**
7-
* Central API contract for interacting with the PlayTime plugin’s core services.
8-
*
9-
* <p>This interface provides unified access to the main subsystems of the plugin:</p>
10-
*
11-
* <ul>
12-
* <li>{@link UserService} – manages player data persistence, synchronization,
13-
* and user-specific statistics.</li>
14-
* <li>{@link PlaytimeService} – handles retrieval and manipulation of player
15-
* playtime data.</li>
16-
* </ul>
17-
*
18-
* <p>External plugins can use this interface to integrate with PlayTime features
19-
* without depending on internal implementation details. The implementation is provided
20-
* automatically by the PlayTime plugin during runtime initialization.</p>
21-
*
22-
* <p><b>Usage Example:</b></p>
23-
*
24-
* <pre>{@code
25-
* PlayTimeApi api = PlayTimeApiProvider.get();
26-
*
27-
* UserService userService = api.userService();
28-
* PlaytimeService playtimeService = api.playtimeService();
29-
*
30-
* UUID uuid = player.getUniqueId();
31-
* UserTime time = playtimeService.getTime(uuid);
32-
* }</pre>
33-
*
34-
* @see PlaytimeService
35-
* @see com.github.imdmk.playtime.user.UserService
36-
* @see com.github.imdmk.playtime.user.UserTime
37-
*/
5+
import java.util.UUID;
6+
import java.util.concurrent.CompletableFuture;
7+
388
public interface PlayTimeApi {
399

40-
/**
41-
* Returns the {@link UserService}, which provides access to user-management operations
42-
* such as creating, saving, and retrieving user data including playtime,
43-
* ranks, and metadata.
44-
*
45-
* @return non-null {@link UserService} instance
46-
*/
47-
@NotNull UserService userService();
10+
CompletableFuture<PlayTime> getTime(@NotNull UUID uuid);
11+
12+
CompletableFuture<Void> setTime(@NotNull UUID uuid, @NotNull PlayTime time);
13+
CompletableFuture<Void> addTime(@NotNull UUID uuid, @NotNull PlayTime delta);
14+
15+
CompletableFuture<Void> resetTime(@NotNull UUID uuid);
4816

49-
/**
50-
* Returns the {@link PlaytimeService}, which provides high-level operations for
51-
* retrieving and modifying player playtime data.
52-
*
53-
* <p>This service acts as the bridge between the plugin’s internal user model
54-
* and the underlying storage or platform-specific systems.</p>
55-
*
56-
* @return non-null {@link PlaytimeService} instance
57-
*/
58-
@NotNull PlaytimeService playtimeService();
5917
}

playtime-api/src/main/java/com/github/imdmk/playtime/PlayTimeApiProvider.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5-
/**
6-
* Static access point for the {@link PlayTimeApi}.
7-
* <p>
8-
* Thread-safe: publication via synchronized register/unregister and a volatile reference.
9-
*/
105
public final class PlayTimeApiProvider {
116

127
private static volatile PlayTimeApi API; // visibility across threads
@@ -15,56 +10,26 @@ private PlayTimeApiProvider() {
1510
throw new UnsupportedOperationException("This class cannot be instantiated.");
1611
}
1712

18-
/**
19-
* Returns the registered {@link PlayTimeApi}.
20-
*
21-
* @return the registered API
22-
* @throws IllegalStateException if the API is not registered
23-
*/
24-
public static @NotNull PlayTimeApi get() {
25-
PlayTimeApi api = API;
13+
@NotNull
14+
public static PlayTimeApi get() {
15+
final PlayTimeApi api = API;
2616
if (api == null) {
2717
throw new IllegalStateException("PlayTimeAPI is not registered.");
2818
}
2919
return api;
3020
}
3121

32-
/**
33-
* Checks if the API is registered
34-
*
35-
* @return {@code true} if the API is registered.
36-
*/
3722
public static boolean isRegistered() {
3823
return API != null;
3924
}
4025

41-
/**
42-
* Registers the {@link PlayTimeApi} instance.
43-
*
44-
* @param api the API instance to register
45-
* @throws IllegalStateException if already registered
46-
*/
4726
static synchronized void register(@NotNull PlayTimeApi api) {
4827
if (API != null) {
4928
throw new IllegalStateException("PlayTimeAPI is already registered.");
5029
}
5130
API = api;
5231
}
5332

54-
/**
55-
* Forces registration of the {@link PlayTimeApi} instance.
56-
* <p>
57-
* Intended for tests/bootstrap only; overwrites any existing instance.
58-
*/
59-
static synchronized void forceRegister(@NotNull PlayTimeApi api) {
60-
API = api;
61-
}
62-
63-
/**
64-
* Unregisters the {@link PlayTimeApi}.
65-
*
66-
* @throws IllegalStateException if no API was registered
67-
*/
6833
static synchronized void unregister() {
6934
if (API == null) {
7035
throw new IllegalStateException("PlayTimeAPI is not registered.");

playtime-api/src/main/java/com/github/imdmk/playtime/PlaytimeService.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)