Skip to content

Commit 03918ed

Browse files
committed
Add simple Platform API
1 parent 8f07cd2 commit 03918ed

9 files changed

Lines changed: 63 additions & 32 deletions

File tree

api/src/main/java/net/earthmc/mycelium/api/Mycelium.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.earthmc.mycelium.api.event.Events;
44
import net.earthmc.mycelium.api.messaging.MessagingRegistrar;
55
import net.earthmc.mycelium.api.network.Network;
6+
import net.earthmc.mycelium.api.platform.Platform;
67
import net.earthmc.mycelium.api.store.Store;
78

89
/**
@@ -35,4 +36,9 @@ static Mycelium api() {
3536
* {@return the event manager}
3637
*/
3738
Events events();
39+
40+
/**
41+
* {@return the current running platform}
42+
*/
43+
Platform platform();
3844
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.earthmc.mycelium.api.platform;
2+
3+
/**
4+
* Represents the current platform Mycelium is running on.
5+
*/
6+
public interface Platform {
7+
/**
8+
* {@return the identifier of the platform}
9+
*/
10+
String id();
11+
12+
/**
13+
* {@return the current platform's type}
14+
*/
15+
PlatformType type();
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.earthmc.mycelium.api.platform;
2+
3+
/**
4+
* All possible platform types where Mycelium can be ran.
5+
*/
6+
public enum PlatformType {
7+
STANDALONE,
8+
SERVER,
9+
PROXY
10+
}

client/src/main/java/net/earthmc/mycelium/client/Platform.java renamed to client/src/main/java/net/earthmc/mycelium/client/AbstractPlatform.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package net.earthmc.mycelium.client;
22

3+
import net.earthmc.mycelium.api.platform.Platform;
4+
import net.earthmc.mycelium.api.platform.PlatformType;
35
import net.earthmc.mycelium.client.util.Property;
6+
import org.jspecify.annotations.NullMarked;
47
import org.jspecify.annotations.Nullable;
58

69
import java.nio.file.Path;
710
import java.util.Locale;
811

9-
public abstract class Platform {
12+
@NullMarked
13+
public abstract class AbstractPlatform implements Platform {
1014
protected static final String UNKNOWN_ID = "unknown";
1115

1216
private final String environment = Property.property("mycelium.environment", "prod");
@@ -21,6 +25,7 @@ public String environment() {
2125
return this.environment;
2226
}
2327

28+
@Override
2429
public String id() {
2530
return id;
2631
}
@@ -36,15 +41,9 @@ public String platformIdentifier() {
3641
*
3742
* @return The type of platform.
3843
*/
39-
public abstract Type type();
44+
public abstract PlatformType type();
4045

4146
public @Nullable Path dataDirectory() {
4247
return null;
4348
}
44-
45-
public enum Type {
46-
STANDALONE,
47-
SERVER,
48-
PROXY
49-
}
5049
}

client/src/main/java/net/earthmc/mycelium/client/MyceliumClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public class MyceliumClient implements Mycelium, Closeable {
4242
private final MessagingRegistrarImpl messagingRegistrar;
4343
private final CallbackProvider callbackProvider;
4444
private final NetworkImpl network;
45-
private final Platform platform;
45+
private final AbstractPlatform platform;
4646
private final Store storage;
4747
private final EventsImpl events;
4848

4949
private final String clientId = UUID.randomUUID().toString();
5050

51-
protected MyceliumClient(final UnifiedJedis client, final Platform platform) {
51+
protected MyceliumClient(final UnifiedJedis client, final AbstractPlatform platform) {
5252
this.redisClient = client;
5353
this.platform = platform;
5454

@@ -71,7 +71,7 @@ public static Builder standalone() {
7171
return new Builder(new StandalonePlatform());
7272
}
7373

74-
public static Builder forPlatform(final Platform platform) {
74+
public static Builder forPlatform(final AbstractPlatform platform) {
7575
return new Builder(platform);
7676
}
7777

@@ -95,7 +95,7 @@ public Events events() {
9595
return this.events;
9696
}
9797

98-
public Platform platform() {
98+
public AbstractPlatform platform() {
9999
return this.platform;
100100
}
101101

@@ -116,7 +116,7 @@ public Logger logger() {
116116
}
117117

118118
public static class Builder {
119-
private final Platform platform;
119+
private final AbstractPlatform platform;
120120

121121
private boolean registerInstance = false;
122122
private Function<MyceliumClient, @Nullable Server> nativeServer = client -> null;
@@ -127,7 +127,7 @@ public static class Builder {
127127
private @Nullable String redisUsername;
128128
private @Nullable String redisPassword;
129129

130-
private Builder(final Platform platform) {
130+
private Builder(final AbstractPlatform platform) {
131131
this.platform = platform;
132132

133133
// read defaults if settings file is supplied
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package net.earthmc.mycelium.client;
22

3-
public class StandalonePlatform extends Platform {
4-
@Override
5-
public String platformIdentifier() {
6-
return "standalone";
7-
}
3+
import net.earthmc.mycelium.api.platform.PlatformType;
84

5+
public class StandalonePlatform extends AbstractPlatform {
96
@Override
10-
public Type type() {
11-
return Type.STANDALONE;
7+
public PlatformType type() {
8+
return PlatformType.STANDALONE;
129
}
1310
}

client/src/main/java/net/earthmc/mycelium/client/impl/messaging/MessagingRegistrarImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import net.earthmc.mycelium.api.messaging.MessageSender;
77
import net.earthmc.mycelium.api.messaging.MessagingRegistrar;
88
import net.earthmc.mycelium.api.messaging.OutgoingMessageBuilder;
9-
import net.earthmc.mycelium.client.Platform;
9+
import net.earthmc.mycelium.api.platform.PlatformType;
10+
import net.earthmc.mycelium.client.AbstractPlatform;
1011
import net.earthmc.mycelium.api.serialization.JsonCodec;
1112
import net.earthmc.mycelium.client.MyceliumClient;
1213
import net.earthmc.mycelium.client.redis.RedisKey;
@@ -106,9 +107,9 @@ public <T> ChannelIdentifier.Bound<T> bind(ChannelIdentifier identifier, JsonCod
106107

107108
@Override
108109
public <T> Listener registerPlatformChannel(ChannelIdentifier.Bound<T> identifier, Consumer<IncomingMessage<T>> receiver) {
109-
final Platform platform = this.client.platform();
110+
final AbstractPlatform platform = this.client.platform();
110111

111-
if (platform.type() == Platform.Type.STANDALONE) {
112+
if (platform.type() == PlatformType.STANDALONE) {
112113
throw new IllegalStateException("Cannot register a platform-relative channel on a standalone platform.");
113114
}
114115

platform/paper/src/main/java/net/earthmc/mycelium/platform/paper/PaperPlatform.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import net.earthmc.mycelium.api.messaging.ChannelIdentifier;
44
import net.earthmc.mycelium.api.messaging.MessagingRegistrar;
5-
import net.earthmc.mycelium.client.Platform;
5+
import net.earthmc.mycelium.api.platform.PlatformType;
6+
import net.earthmc.mycelium.client.AbstractPlatform;
67
import net.earthmc.mycelium.api.network.command.ConsoleCommand;
78
import net.earthmc.mycelium.client.MyceliumClient;
89
import net.earthmc.mycelium.client.impl.model.PlayerCommandRequest;
@@ -17,7 +18,7 @@
1718

1819
import java.nio.file.Path;
1920

20-
public class PaperPlatform extends Platform implements Listener {
21+
public class PaperPlatform extends AbstractPlatform implements Listener {
2122
private final PaperLoader loader;
2223
private final Logger logger;
2324
private final Server server;
@@ -88,8 +89,8 @@ public UnifiedJedis redis() {
8889
}
8990

9091
@Override
91-
public Type type() {
92-
return Type.SERVER;
92+
public PlatformType type() {
93+
return PlatformType.SERVER;
9394
}
9495

9596
@Override

platform/velocity/src/main/java/net/earthmc/mycelium/platform/velocity/VelocityPlatform.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import com.velocitypowered.api.proxy.server.RegisteredServer;
1818
import net.earthmc.mycelium.api.messaging.ChannelIdentifier;
1919
import net.earthmc.mycelium.api.messaging.MessagingRegistrar;
20-
import net.earthmc.mycelium.client.Platform;
20+
import net.earthmc.mycelium.api.platform.PlatformType;
21+
import net.earthmc.mycelium.client.AbstractPlatform;
2122
import net.earthmc.mycelium.api.network.Server;
2223
import net.earthmc.mycelium.api.network.command.ConsoleCommand;
2324
import net.earthmc.mycelium.client.MyceliumClient;
@@ -45,7 +46,7 @@
4546
import java.util.function.Supplier;
4647

4748
@Plugin(name = "Mycelium", id = "mycelium", version = "0.0.1", authors = "Warriorrr")
48-
public class VelocityPlatform extends Platform {
49+
public class VelocityPlatform extends AbstractPlatform {
4950
@Inject
5051
public ProxyServer proxy;
5152

@@ -302,8 +303,8 @@ private void cleanupStalePlayersOnProxy(final boolean shuttingDown) {
302303
}
303304

304305
@Override
305-
public Type type() {
306-
return Type.PROXY;
306+
public PlatformType type() {
307+
return PlatformType.PROXY;
307308
}
308309

309310
@Override

0 commit comments

Comments
 (0)