Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 9f204c5

Browse files
committed
Move Client API initialization into ClientAPI class
1 parent d5ee87f commit 9f204c5

4 files changed

Lines changed: 67 additions & 46 deletions

File tree

src/main/java/clientapi/ClientAPI.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616

1717
package clientapi;
1818

19+
import clientapi.config.ClientConfiguration;
20+
import clientapi.config.JsonConfiguration;
1921
import clientapi.event.ClientAPIEventManager;
22+
import clientapi.event.handle.KeybindEventHandler;
23+
import clientapi.load.ClientInitException;
24+
import clientapi.util.render.gl.GLUtils;
25+
import net.minecraft.client.Minecraft;
2026
import org.apache.logging.log4j.LogManager;
2127
import org.apache.logging.log4j.Logger;
2228

29+
import java.io.InputStream;
30+
import java.lang.reflect.Constructor;
31+
import java.lang.reflect.InvocationTargetException;
32+
2333
/**
2434
* Contains some constants that are used throughout the API.
2535
*
@@ -42,4 +52,55 @@ private ClientAPI() {}
4252
* Instance of the API logger.
4353
*/
4454
public static final Logger LOGGER = LogManager.getLogger("ClientAPI");
55+
56+
/**
57+
* Called after the game has initialized. This method is responsible
58+
* for initializing any core Client API classes and the discovered
59+
* Client API mod.
60+
*
61+
* @see Minecraft#init()
62+
*/
63+
public static void start() {
64+
InputStream stream = ClientAPI.class.getResourceAsStream("/client.json");
65+
if (stream == null) {
66+
throw new ClientInitException("Unable to locate Client Configuration");
67+
}
68+
69+
// Construct a ClientConfiguration object from the client json using GSON
70+
ClientConfiguration clientConfig = JsonConfiguration.loadConfiguration(stream, ClientConfiguration.class);
71+
if (clientConfig == null) {
72+
throw new ClientInitException("Unable to create Client Configuration from client.json");
73+
}
74+
75+
// Attempt to instantiate the main class from the client configuration
76+
Client client;
77+
try {
78+
Class<?> clientClass = Class.forName(clientConfig.getMainClass());
79+
if (clientClass == null) {
80+
throw new ClientInitException("Unable to find the main class (%s)", clientConfig.getMainClass());
81+
}
82+
83+
if (!clientClass.getSuperclass().equals(Client.class)) {
84+
throw new ClientInitException("Main class does not inheit clientapi.Client");
85+
}
86+
87+
Constructor<?> constructor = clientClass.getConstructor(ClientConfiguration.class);
88+
89+
client = (Client) constructor.newInstance(clientConfig);
90+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
91+
throw new ClientInitException("Unable to instantiate main client class", e);
92+
} catch (ClassNotFoundException e) {
93+
throw new ClientInitException("Unable to find client class");
94+
} catch (NoSuchMethodException e) {
95+
throw new ClientInitException("Unable to find a suitable main class constructor");
96+
}
97+
98+
// Init GLUtils
99+
GLUtils.init();
100+
101+
// Init client
102+
client.init();
103+
104+
ClientAPI.EVENT_BUS.subscribe(KeybindEventHandler.INSTANCE);
105+
}
45106
}

src/main/java/clientapi/event/handle/ClientHandler.java renamed to src/main/java/clientapi/event/handle/KeybindEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @author Brady
3636
* @since 2/9/2017
3737
*/
38-
public enum ClientHandler implements Listenable, MinecraftAccessible {
38+
public enum KeybindEventHandler implements Listenable, MinecraftAccessible {
3939

4040
INSTANCE;
4141

src/main/java/clientapi/load/ClientInitException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public ClientInitException(String message) {
3232
public ClientInitException(String message, Object... args) {
3333
this(String.format(message, args));
3434
}
35+
36+
public ClientInitException(String message, Throwable cause) {
37+
super(message, cause);
38+
}
3539
}

src/main/java/clientapi/load/mixin/MixinMinecraft.java

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@
1616

1717
package clientapi.load.mixin;
1818

19-
import clientapi.Client;
2019
import clientapi.ClientAPI;
21-
import clientapi.config.ClientConfiguration;
22-
import clientapi.config.JsonConfiguration;
2320
import clientapi.event.defaults.game.core.*;
2421
import clientapi.event.defaults.game.render.GuiDisplayEvent;
2522
import clientapi.event.defaults.game.world.WorldEvent;
26-
import clientapi.event.handle.ClientHandler;
27-
import clientapi.load.ClientInitException;
2823
import clientapi.load.mixin.extension.IMinecraft;
2924
import clientapi.util.io.MouseKeyTracker;
30-
import clientapi.util.render.gl.GLUtils;
3125
import me.zero.alpine.event.EventState;
3226
import net.minecraft.client.Minecraft;
3327
import net.minecraft.client.gui.GuiScreen;
@@ -45,9 +39,6 @@
4539
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
4640

4741
import javax.annotation.Nullable;
48-
import java.io.InputStream;
49-
import java.lang.reflect.Constructor;
50-
import java.lang.reflect.InvocationTargetException;
5142

5243
import static clientapi.event.defaults.game.core.ClickEvent.MouseButton.*;
5344

@@ -152,42 +143,7 @@ private void onMouseEvent(CallbackInfo info) {
152143
at = @At("RETURN")
153144
)
154145
private void init(CallbackInfo ci) {
155-
InputStream stream = this.getClass().getResourceAsStream("/client.json");
156-
157-
if (stream == null)
158-
throw new ClientInitException("Unable to locate Client Configuration");
159-
160-
// Construct a ClientConfiguration object from the client json using GSON
161-
ClientConfiguration clientConfig = JsonConfiguration.loadConfiguration(stream, ClientConfiguration.class);
162-
163-
if (clientConfig == null)
164-
throw new ClientInitException("Unable to create Client Configuration from client.json");
165-
166-
// Attempt to instantiate the specified class from the client configuration
167-
Client client;
168-
try {
169-
Class<?> clientClass = Class.forName(clientConfig.getMainClass());
170-
Constructor<?> constructor;
171-
if (clientClass != null && clientClass.getSuperclass().equals(Client.class) && (constructor = clientClass.getConstructor(ClientConfiguration.class)) != null) {
172-
client = (Client) constructor.newInstance(clientConfig);
173-
} else {
174-
throw new ClientInitException("Client class is null or the superclass is not Client type");
175-
}
176-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
177-
throw new ClientInitException("Unable to instantiate main client class");
178-
} catch (ClassNotFoundException e) {
179-
throw new ClientInitException("Unable to find client class");
180-
} catch (NoSuchMethodException e) {
181-
throw new ClientInitException("Unable to find constructor with valid parameters");
182-
}
183-
184-
// Init GLUtils
185-
GLUtils.init();
186-
187-
// Init client
188-
client.init();
189-
190-
ClientAPI.EVENT_BUS.subscribe(ClientHandler.INSTANCE);
146+
ClientAPI.start();
191147
}
192148

193149
@ModifyVariable(

0 commit comments

Comments
 (0)