Skip to content

Commit 56391d3

Browse files
committed
1.3.0
1 parent 4bc5832 commit 56391d3

File tree

5 files changed

+39
-49
lines changed

5 files changed

+39
-49
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "one.tranic"
12-
version = "1.2.9"
12+
version = "1.3.0"
1313

1414
repositories {
1515
mavenCentral()

src/main/java/one/tranic/t/base/TBase.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import one.tranic.t.thread.T2hread;
66
import org.jetbrains.annotations.Nullable;
77

8+
import java.util.ServiceLoader;
89
import java.util.UUID;
910
import java.util.concurrent.CompletableFuture;
1011
import java.util.concurrent.ExecutorService;
@@ -13,14 +14,31 @@
1314
@SuppressWarnings("unused")
1415
public class TBase {
1516
public final static ExecutorService executor = T2hread.getExecutor();
17+
public final static TInterface INSTANCE;
1618
private final static Operator operator = new Operator("Console", UUID.fromString("05b11eee-24db-4a21-ba9d-e12e8df9a92f"));
1719
private static final String packageName;
1820
//public final static List<String> EMPTY_LIST = Collections.newUnmodifiableList();
1921
private static SystemCommandSource<?, ?> CONSOLE_SOURCE;
20-
private static Supplier<SystemCommandSource<?, ?>> getConsoleSourceSupplier;
2122

2223
static {
2324
packageName = getRootPath();
25+
INSTANCE = load();
26+
}
27+
28+
@SuppressWarnings("all")
29+
private static TInterface load() {
30+
var loader = ServiceLoader.load(TInterface.class);
31+
var i = loader.iterator();
32+
for (; i.hasNext(); ) {
33+
var service = i.next();
34+
var platforms = service.getSupportedPlatforms();
35+
for (var platform : platforms) {
36+
if (platform.is() && !platform.isMixinServer()) {
37+
return service;
38+
}
39+
}
40+
}
41+
throw new NullPointerException("Failed to load service for " + TInterface.class.getName());
2442
}
2543

2644
/**
@@ -56,22 +74,6 @@ public static Operator console() {
5674
return operator;
5775
}
5876

59-
/**
60-
* Retrieves the console as a {@link SystemCommandSource}, an abstraction representing
61-
* the source of command execution or interaction.
62-
*
63-
* @return a {@link SystemCommandSource} instance representing the console source, typically used
64-
* for administrative or automated command execution.
65-
*/
66-
public static @Nullable SystemCommandSource<?, ?> getConsoleSource() {
67-
if (CONSOLE_SOURCE != null) return CONSOLE_SOURCE;
68-
if (getConsoleSourceSupplier != null) {
69-
CONSOLE_SOURCE = getConsoleSourceSupplier.get();
70-
return CONSOLE_SOURCE;
71-
}
72-
return null;
73-
}
74-
7577
/**
7678
* Executes the given {@code Runnable} asynchronously using a pre-configured executor.
7779
*
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
package one.tranic.t.base.player;
1+
package one.tranic.t.base;
22

3+
import one.tranic.t.base.command.source.SystemCommandSource;
4+
import one.tranic.t.base.player.Player;
35
import one.tranic.t.utils.Collections;
6+
import one.tranic.t.utils.minecraft.Platform;
47
import org.jetbrains.annotations.NotNull;
58
import org.jetbrains.annotations.Nullable;
69

710
import java.util.List;
811
import java.util.Objects;
912
import java.util.UUID;
10-
import java.util.function.Function;
11-
import java.util.function.Supplier;
1213

13-
@SuppressWarnings("unused")
14-
public class Players {
15-
// Ignore the "never allocated" warning here, it will be set by the common package via reflection.
16-
private static Function<String, Player<?>> getPlayerWithStringMethod;
17-
private static Function<UUID, Player<?>> getPlayerWithUUIDMethod;
18-
private static Supplier<List<Player<?>>> getOnlinePlayersMethod;
19-
private static Supplier<List<?>> getPlatformOnlinePlayersMethod;
20-
private static Supplier<List<String>> getOnlinePlayersNameMethod;
14+
public interface TInterface {
15+
Platform[] getSupportedPlatforms();
16+
17+
SystemCommandSource<?, ?> getConsoleSource();
2118

2219
/**
2320
* Retrieves a player instance based on their player name.
2421
*
2522
* @param name the name of the player to search for; must not be null
2623
* @return the {@code Player} instance corresponding to the given name, or {@code null} if no player is found
2724
*/
28-
public static @Nullable Player<?> getPlayer(@NotNull String name) {
29-
return getPlayerWithStringMethod.apply(name);
30-
}
25+
@Nullable Player<?> getPlayer(@NotNull String name);
3126

3227
/**
3328
* Retrieves a player using their unique identifier (UUID).
@@ -36,19 +31,15 @@ public class Players {
3631
* @return a {@code Player<?>} instance if a player with the given UUID exists;
3732
* otherwise, {@code null}
3833
*/
39-
public static @Nullable Player<?> getPlayer(@NotNull UUID uuid) {
40-
return getPlayerWithUUIDMethod.apply(uuid);
41-
}
34+
@Nullable Player<?> getPlayer(@NotNull UUID uuid);
4235

4336
/**
4437
* Retrieves a list of all online players currently connected to the server.
4538
*
4639
* @return a {@code List} of {@code Player<?>} representing each online player;
4740
* the list is guaranteed to be non-null.
4841
*/
49-
public static @NotNull List<Player<?>> getOnlinePlayers() {
50-
return getOnlinePlayersMethod.get();
51-
}
42+
@NotNull List<Player<?>> getOnlinePlayers();
5243

5344
/**
5445
* Retrieves a list of alternative players who are connected to the same host
@@ -67,7 +58,7 @@ public class Players {
6758
* the list is guaranteed to be non-null but may be empty if no other
6859
* players share the same connected host
6960
*/
70-
public static @NotNull List<Player<?>> getAltPlayers(@NotNull Player<?> player) {
61+
default @NotNull List<Player<?>> getAltPlayers(@NotNull Player<?> player) {
7162
final List<Player<?>> end = Collections.newArrayList();
7263
final List<Player<?>> players = getOnlinePlayers();
7364
for (Player<?> p : players) {
@@ -85,17 +76,13 @@ public class Players {
8576
* @return a {@code List<?>} representing the platform-specific online players;
8677
* the list is guaranteed to be non-null.
8778
*/
88-
public static @NotNull List<?> getPlatformOnlinePlayers() {
89-
return getPlatformOnlinePlayersMethod.get();
90-
}
79+
@NotNull List<?> getPlatformOnlinePlayers();
9180

9281
/**
9382
* Retrieves a list of the names of all online players currently connected to the server.
9483
*
9584
* @return a {@code List} of {@code String} containing the names of online players;
9685
* the list is guaranteed to be non-null.
9786
*/
98-
public static @NotNull List<String> getOnlinePlayersName() {
99-
return getOnlinePlayersNameMethod.get();
100-
}
87+
@NotNull List<String> getOnlinePlayersName();
10188
}

src/main/java/one/tranic/t/base/command/simple/SimpleCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void sendResult(C source, Component msg) {
4444
*/
4545
public void sendResult(C source, Component msg, boolean withConsole) {
4646
if (source.isPlayer()) source.sendMessage(msg);
47-
if (withConsole) TBase.getConsoleSource().sendMessage(msg);
47+
if (withConsole) TBase.INSTANCE.getConsoleSource().sendMessage(msg);
4848
}
4949

5050
/**
@@ -66,7 +66,7 @@ public void sendResult(C source, String msg) {
6666
*/
6767
public void sendResult(C source, String msg, boolean withConsole) {
6868
if (source.isPlayer()) source.sendMessage(msg);
69-
if (withConsole) TBase.getConsoleSource().sendMessage(msg);
69+
if (withConsole) TBase.INSTANCE.getConsoleSource().sendMessage(msg);
7070
}
7171

7272
/**

src/main/java/one/tranic/t/base/player/Player.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package one.tranic.t.base.player;
22

33
import net.kyori.adventure.text.Component;
4+
import one.tranic.t.base.TBase;
45
import one.tranic.t.utils.minecraft.ProtocolVersion;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
@@ -11,11 +12,11 @@
1112
@SuppressWarnings("unused")
1213
public interface Player<C> {
1314
static @Nullable Player<?> getPlayer(@NotNull String name) {
14-
return Players.getPlayer(name);
15+
return TBase.INSTANCE.getPlayer(name);
1516
}
1617

1718
static @Nullable Player<?> getPlayer(@NotNull UUID uuid) {
18-
return Players.getPlayer(uuid);
19+
return TBase.INSTANCE.getPlayer(uuid);
1920
}
2021

2122
/**

0 commit comments

Comments
 (0)