-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSideUtil.java
More file actions
70 lines (63 loc) · 2.38 KB
/
Copy pathSideUtil.java
File metadata and controls
70 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package net.modificationstation.stationapi.api.util;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import java.util.function.Supplier;
/**
* Utility class to work with side-dependent objects and processes.
* @author mine_diver
*/
public class SideUtil {
/**
* Side-dependent object choice.
* @param clientObject the object to be returned on the client side.
* @param serverObject the object to be returned on the server side.
* @param <T> the type of the object to be chosen.
* @return the object corresponding to the current side.
*/
public static <T> T choose(T clientObject, T serverObject) {
return switch (FabricLoader.getInstance().getEnvironmentType()) {
case CLIENT -> clientObject;
case SERVER -> serverObject;
};
}
/**
* Side-dependent supplier execution.
* @param clientSupplier the supplier that should be executed on client side.
* @param serverSupplier the supplier that should be executed on server side.
* @param <T> the supplier return type.
* @return the supplier result.
*/
@API
public static <T> T get(Supplier<T> clientSupplier, Supplier<T> serverSupplier) {
return choose(clientSupplier, serverSupplier).get();
}
/**
* Side-dependent runnable execution.
* @param clientRunnable the runnable that should be executed on client side.
* @param serverRunnable the runnable that should be executed on server side.
*/
@API
public static void run(Runnable clientRunnable, Runnable serverRunnable) {
choose(clientRunnable, serverRunnable).run();
}
/**
* Side-dependent runnable execution which only runs on the client side.
* @param clientRunnable the runnable that should be executed on client side.
*/
@API
public static void runClient(Runnable clientRunnable) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
clientRunnable.run();
}
}
/**
* Side-dependent runnable execution which only runs on the server side.
* @param serverRunnable the runnable that should be executed on server side.
*/
@API
public static void runServer(Runnable serverRunnable) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
serverRunnable.run();
}
}
}