4
4
5
5
import java .io .File ;
6
6
import java .io .IOException ;
7
- import java .util .ArrayList ;
8
- import java .util .List ;
7
+ import java .util .Arrays ;
9
8
10
9
import org .apache .logging .log4j .Level ;
11
- import org .lwjgl .input .Keyboard ;
12
10
13
11
import com .minecrafttas .mctcommon .Configuration ;
14
12
import com .minecrafttas .mctcommon .Configuration .ConfigOptions ;
15
13
import com .minecrafttas .mctcommon .KeybindManager ;
16
- import com .minecrafttas .mctcommon .KeybindManager .Keybind ;
17
14
import com .minecrafttas .mctcommon .LanguageManager ;
18
15
import com .minecrafttas .mctcommon .events .EventClient .EventClientInit ;
19
16
import com .minecrafttas .mctcommon .events .EventClient .EventOpenGui ;
24
21
import com .minecrafttas .mctcommon .server .Server ;
25
22
import com .minecrafttas .tasmod .gui .InfoHud ;
26
23
import com .minecrafttas .tasmod .handlers .LoadingScreenHandler ;
27
- import com .minecrafttas .tasmod .networking .TASmodBufferBuilder ;
28
24
import com .minecrafttas .tasmod .networking .TASmodPackets ;
29
25
import com .minecrafttas .tasmod .playback .PlaybackControllerClient ;
30
26
import com .minecrafttas .tasmod .playback .PlaybackControllerClient .TASstate ;
31
- import com .minecrafttas .tasmod .playback .metadata .PlaybackMetadataRegistry ;
32
27
import com .minecrafttas .tasmod .playback .metadata .integrated .CreditsMetadataExtension ;
33
28
import com .minecrafttas .tasmod .playback .metadata .integrated .StartpositionMetadataExtension ;
34
29
import com .minecrafttas .tasmod .playback .tasfile .PlaybackSerialiser ;
38
33
import com .minecrafttas .tasmod .util .LoggerMarkers ;
39
34
import com .minecrafttas .tasmod .util .Scheduler ;
40
35
import com .minecrafttas .tasmod .util .ShieldDownloader ;
36
+ import com .minecrafttas .tasmod .util .TASmodKeybinds ;
37
+ import com .minecrafttas .tasmod .util .TASmodRegistry ;
41
38
import com .minecrafttas .tasmod .virtual .VirtualInput ;
42
39
import com .minecrafttas .tasmod .virtual .VirtualKeybindings ;
43
40
48
45
import net .minecraft .client .gui .GuiMainMenu ;
49
46
import net .minecraft .client .gui .GuiScreen ;
50
47
import net .minecraft .client .multiplayer .ServerData ;
51
- import net .minecraft .client .settings .KeyBinding ;
52
48
import net .minecraft .server .MinecraftServer ;
53
- import net .minecraft .util .text .ChatType ;
54
- import net .minecraft .util .text .TextComponentString ;
55
- import net .minecraft .util .text .TextFormatting ;
56
49
57
50
public class TASmodClient implements ClientModInitializer , EventClientInit , EventPlayerJoinedClientSide , EventOpenGui {
58
51
@@ -115,25 +108,11 @@ public static void createSavestatesDir() {
115
108
@ Override
116
109
public void onInitializeClient () {
117
110
118
- // Load config
119
- Minecraft mc = Minecraft .getMinecraft ();
120
-
121
- File configDir = new File (mc .mcDataDir , "config" );
122
- if (!configDir .exists ()) {
123
- configDir .mkdir ();
124
- }
125
- config = new Configuration ("TASmod configuration" , new File (configDir , "tasmod.cfg" ));
126
-
127
111
LanguageManager .registerMod ("tasmod" );
128
112
129
- // Execute /restartandplay. Load the file to start from the config. If it exists load the playback file on start.
130
- String fileOnStart = config .get (ConfigOptions .FileToOpen );
131
- if (fileOnStart .isEmpty ()) {
132
- fileOnStart = null ;
133
- } else {
134
- config .reset (ConfigOptions .FileToOpen );
135
- }
136
- virtual =new VirtualInput (LOGGER ); //TODO Move fileOnStart to PlaybackController
113
+ loadConfig (Minecraft .getMinecraft ());
114
+
115
+ virtual =new VirtualInput (LOGGER );
137
116
138
117
// Initialize InfoHud
139
118
hud = new InfoHud ();
@@ -146,7 +125,29 @@ public void onInitializeClient() {
146
125
// Initialize keybind manager
147
126
keybindManager = new KeybindManager (VirtualKeybindings ::isKeyDownExceptTextfield );
148
127
149
- // Register event listeners
128
+ registerEventListeners ();
129
+
130
+ registerNetworkPacketHandlers ();
131
+
132
+ // Starting local server instance
133
+ try {
134
+ TASmod .server = new Server (TASmod .networkingport -1 , TASmodPackets .values ());
135
+ } catch (Exception e ) {
136
+ LOGGER .error ("Unable to launch TASmod server: {}" , e .getMessage ());
137
+ }
138
+
139
+ }
140
+
141
+ private void registerNetworkPacketHandlers () {
142
+ // Register packet handlers
143
+ LOGGER .info (LoggerMarkers .Networking , "Registering network handlers on client" );
144
+ PacketHandlerRegistry .register (controller );
145
+ PacketHandlerRegistry .register (ticksyncClient );
146
+ PacketHandlerRegistry .register (tickratechanger );
147
+ PacketHandlerRegistry .register (savestateHandlerClient );
148
+ }
149
+
150
+ private void registerEventListeners () {
150
151
EventListenerRegistry .register (this );
151
152
// EventListenerRegistry.register(virtual); TODO Remove if unnecessary
152
153
EventListenerRegistry .register (hud );
@@ -161,64 +162,14 @@ public void onInitializeClient() {
161
162
return gui ;
162
163
}));
163
164
EventListenerRegistry .register (controller );
164
- PlaybackMetadataRegistry .register (creditsMetadataExtension );
165
165
EventListenerRegistry .register (creditsMetadataExtension );
166
-
167
- PlaybackMetadataRegistry .register (startpositionMetadataExtension );
168
166
EventListenerRegistry .register (startpositionMetadataExtension );
169
-
170
- // Register packet handlers
171
- LOGGER .info (LoggerMarkers .Networking , "Registering network handlers on client" );
172
- PacketHandlerRegistry .register (controller );
173
- PacketHandlerRegistry .register (ticksyncClient );
174
- PacketHandlerRegistry .register (tickratechanger );
175
- PacketHandlerRegistry .register (savestateHandlerClient );
176
-
177
- // Starting local server instance
178
- try {
179
- TASmod .server = new Server (TASmod .networkingport -1 , TASmodPackets .values ());
180
- } catch (Exception e ) {
181
- LOGGER .error ("Unable to launch TASmod server: {}" , e .getMessage ());
182
- }
183
-
184
167
}
185
168
186
169
@ Override
187
170
public void onClientInit (Minecraft mc ) {
188
- // initialize keybindings
189
- List <KeyBinding > blockedKeybindings = new ArrayList <>();
190
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Tickrate 0 Key" , "TASmod" , Keyboard .KEY_F8 , () -> TASmodClient .tickratechanger .togglePause (), VirtualKeybindings ::isKeyDown )));
191
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Advance Tick" , "TASmod" , Keyboard .KEY_F9 , () -> TASmodClient .tickratechanger .advanceTick (), VirtualKeybindings ::isKeyDown )));
192
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Recording/Playback Stop" , "TASmod" , Keyboard .KEY_F10 , () -> TASmodClient .controller .setTASState (TASstate .NONE ), VirtualKeybindings ::isKeyDown )));
193
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Create Savestate" , "TASmod" , Keyboard .KEY_J , () -> {
194
- Minecraft .getMinecraft ().ingameGUI .addChatMessage (ChatType .CHAT , new TextComponentString ("Savestates might not work correctly at the moment... rewriting a lot of core features, which might break this..." ));
195
- try {
196
- TASmodClient .client .send (new TASmodBufferBuilder (TASmodPackets .SAVESTATE_SAVE ).writeInt (-1 ));
197
- } catch (Exception e ) {
198
- e .printStackTrace ();
199
- }
200
- })));
201
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Load Latest Savestate" , "TASmod" , Keyboard .KEY_K , () -> {
202
- Minecraft .getMinecraft ().ingameGUI .addChatMessage (ChatType .CHAT , new TextComponentString (TextFormatting .RED +"Savestates might not work correctly at the moment... rewriting a lot of core features, which might break this..." ));
203
- try {
204
- TASmodClient .client .send (new TASmodBufferBuilder (TASmodPackets .SAVESTATE_LOAD ).writeInt (-1 ));
205
- } catch (Exception e ) {
206
- e .printStackTrace ();
207
- }
208
- })));
209
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Open InfoGui Editor" , "TASmod" , Keyboard .KEY_F6 , () -> Minecraft .getMinecraft ().displayGuiScreen (TASmodClient .hud ))));
210
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Various Testing" , "TASmod" , Keyboard .KEY_F12 , () -> {
211
- controller .setTASState (TASstate .RECORDING );
212
- }, VirtualKeybindings ::isKeyDown )));
213
- blockedKeybindings .add (keybindManager .registerKeybind (new Keybind ("Various Testing2" , "TASmod" , Keyboard .KEY_F7 , () -> {
214
- // try {
215
- // TASmodClient.client = new Client("localhost", TASmod.networkingport-1, TASmodPackets.values(), mc.getSession().getProfile().getName(), true);
216
- // } catch (Exception e) {
217
- // e.printStackTrace();
218
- // }
219
- controller .setTASState (TASstate .PLAYBACK );
220
- }, VirtualKeybindings ::isKeyDown )));
221
- blockedKeybindings .forEach (VirtualKeybindings ::registerBlockedKeyBinding );
171
+ registerKeybindings (mc );
172
+ registerPlaybackMetadata (mc );
222
173
223
174
createTASDir ();
224
175
createSavestatesDir ();
@@ -255,7 +206,7 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) {
255
206
}
256
207
257
208
258
- if (!(ip +":" +port ).equals (connectedIP )) {
209
+ if (!(ip +":" +port ).equals (connectedIP )) { // TODO Clean this up. Make TASmodNetworkHandler out of this... Maybe with Permission system?
259
210
try {
260
211
LOGGER .info ("Closing client connection: {}" , client .getRemote ());
261
212
client .disconnect ();
@@ -280,34 +231,7 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) {
280
231
@ Override
281
232
public GuiScreen onOpenGui (GuiScreen gui ) {
282
233
if (gui instanceof GuiMainMenu ) {
283
- if (client == null ) {
284
- Minecraft mc = Minecraft .getMinecraft ();
285
-
286
- String IP = "localhost" ;
287
- int PORT = TASmod .networkingport - 1 ;
288
-
289
- // Get the connection on startup from config
290
- String configAddress = config .get (ConfigOptions .ServerConnection );
291
- if (configAddress != null && !configAddress .isEmpty ()) {
292
- String [] ipSplit = configAddress .split (":" );
293
- IP = ipSplit [0 ];
294
- try {
295
- PORT = Integer .parseInt (ipSplit [1 ]);
296
- } catch (Exception e ) {
297
- LOGGER .catching (Level .ERROR , e );
298
- IP = "localhost" ;
299
- PORT = TASmod .networkingport - 1 ;
300
- }
301
- }
302
-
303
- try {
304
- // connect to server and authenticate
305
- client = new Client (IP , PORT , TASmodPackets .values (), mc .getSession ().getUsername (), true );
306
- } catch (Exception e ) {
307
- LOGGER .error ("Unable to connect TASmod client: {}" , e );
308
- }
309
- ticksyncClient .setEnabled (true );
310
- }
234
+ initializeCustomPacketHandler ();
311
235
} else if (gui instanceof GuiControls ) {
312
236
TASmodClient .controller .setTASState (TASstate .NONE ); // Set the TASState to nothing to avoid collisions
313
237
if (TASmodClient .tickratechanger .ticksPerSecond == 0 ) {
@@ -322,4 +246,53 @@ public GuiScreen onOpenGui(GuiScreen gui) {
322
246
}
323
247
return gui ;
324
248
}
249
+
250
+ private void initializeCustomPacketHandler () {
251
+ if (client == null ) {
252
+ Minecraft mc = Minecraft .getMinecraft ();
253
+
254
+ String IP = "localhost" ;
255
+ int PORT = TASmod .networkingport - 1 ;
256
+
257
+ // Get the connection on startup from config
258
+ String configAddress = config .get (ConfigOptions .ServerConnection );
259
+ if (configAddress != null && !configAddress .isEmpty ()) {
260
+ String [] ipSplit = configAddress .split (":" );
261
+ IP = ipSplit [0 ];
262
+ try {
263
+ PORT = Integer .parseInt (ipSplit [1 ]);
264
+ } catch (Exception e ) {
265
+ LOGGER .catching (Level .ERROR , e );
266
+ IP = "localhost" ;
267
+ PORT = TASmod .networkingport - 1 ;
268
+ }
269
+ }
270
+
271
+ try {
272
+ // connect to server and authenticate
273
+ client = new Client (IP , PORT , TASmodPackets .values (), mc .getSession ().getUsername (), true );
274
+ } catch (Exception e ) {
275
+ LOGGER .error ("Unable to connect TASmod client: {}" , e );
276
+ }
277
+ ticksyncClient .setEnabled (true );
278
+ }
279
+ }
280
+
281
+ private void registerKeybindings (Minecraft mc ) {
282
+ Arrays .stream (TASmodKeybinds .valuesKeybind ()).forEach (keybindManager ::registerKeybind );
283
+ Arrays .stream (TASmodKeybinds .valuesVanillaKeybind ()).forEach (VirtualKeybindings ::registerBlockedKeyBinding );
284
+ }
285
+
286
+ private void registerPlaybackMetadata (Minecraft mc ) {
287
+ TASmodRegistry .PLAYBACK_METADATA .register (creditsMetadataExtension );
288
+ TASmodRegistry .PLAYBACK_METADATA .register (startpositionMetadataExtension );
289
+ }
290
+
291
+ private void loadConfig (Minecraft mc ) {
292
+ File configDir = new File (mc .mcDataDir , "config" );
293
+ if (!configDir .exists ()) {
294
+ configDir .mkdir ();
295
+ }
296
+ config = new Configuration ("TASmod configuration" , new File (configDir , "tasmod.cfg" ));
297
+ }
325
298
}
0 commit comments