44
55import java .io .File ;
66import java .io .IOException ;
7- import java .util .ArrayList ;
8- import java .util .List ;
7+ import java .util .Arrays ;
98
109import org .apache .logging .log4j .Level ;
11- import org .lwjgl .input .Keyboard ;
1210
1311import com .minecrafttas .mctcommon .Configuration ;
1412import com .minecrafttas .mctcommon .Configuration .ConfigOptions ;
1513import com .minecrafttas .mctcommon .KeybindManager ;
16- import com .minecrafttas .mctcommon .KeybindManager .Keybind ;
1714import com .minecrafttas .mctcommon .LanguageManager ;
1815import com .minecrafttas .mctcommon .events .EventClient .EventClientInit ;
1916import com .minecrafttas .mctcommon .events .EventClient .EventOpenGui ;
2421import com .minecrafttas .mctcommon .server .Server ;
2522import com .minecrafttas .tasmod .gui .InfoHud ;
2623import com .minecrafttas .tasmod .handlers .LoadingScreenHandler ;
27- import com .minecrafttas .tasmod .networking .TASmodBufferBuilder ;
2824import com .minecrafttas .tasmod .networking .TASmodPackets ;
2925import com .minecrafttas .tasmod .playback .PlaybackControllerClient ;
3026import com .minecrafttas .tasmod .playback .PlaybackControllerClient .TASstate ;
31- import com .minecrafttas .tasmod .playback .metadata .PlaybackMetadataRegistry ;
3227import com .minecrafttas .tasmod .playback .metadata .integrated .CreditsMetadataExtension ;
3328import com .minecrafttas .tasmod .playback .metadata .integrated .StartpositionMetadataExtension ;
3429import com .minecrafttas .tasmod .playback .tasfile .PlaybackSerialiser ;
3833import com .minecrafttas .tasmod .util .LoggerMarkers ;
3934import com .minecrafttas .tasmod .util .Scheduler ;
4035import com .minecrafttas .tasmod .util .ShieldDownloader ;
36+ import com .minecrafttas .tasmod .util .TASmodKeybinds ;
37+ import com .minecrafttas .tasmod .util .TASmodRegistry ;
4138import com .minecrafttas .tasmod .virtual .VirtualInput ;
4239import com .minecrafttas .tasmod .virtual .VirtualKeybindings ;
4340
4845import net .minecraft .client .gui .GuiMainMenu ;
4946import net .minecraft .client .gui .GuiScreen ;
5047import net .minecraft .client .multiplayer .ServerData ;
51- import net .minecraft .client .settings .KeyBinding ;
5248import 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 ;
5649
5750public class TASmodClient implements ClientModInitializer , EventClientInit , EventPlayerJoinedClientSide , EventOpenGui {
5851
@@ -115,25 +108,11 @@ public static void createSavestatesDir() {
115108 @ Override
116109 public void onInitializeClient () {
117110
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-
127111 LanguageManager .registerMod ("tasmod" );
128112
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 );
137116
138117 // Initialize InfoHud
139118 hud = new InfoHud ();
@@ -146,7 +125,29 @@ public void onInitializeClient() {
146125 // Initialize keybind manager
147126 keybindManager = new KeybindManager (VirtualKeybindings ::isKeyDownExceptTextfield );
148127
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 () {
150151 EventListenerRegistry .register (this );
151152// EventListenerRegistry.register(virtual); TODO Remove if unnecessary
152153 EventListenerRegistry .register (hud );
@@ -161,64 +162,14 @@ public void onInitializeClient() {
161162 return gui ;
162163 }));
163164 EventListenerRegistry .register (controller );
164- PlaybackMetadataRegistry .register (creditsMetadataExtension );
165165 EventListenerRegistry .register (creditsMetadataExtension );
166-
167- PlaybackMetadataRegistry .register (startpositionMetadataExtension );
168166 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-
184167 }
185168
186169 @ Override
187170 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 );
222173
223174 createTASDir ();
224175 createSavestatesDir ();
@@ -255,7 +206,7 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) {
255206 }
256207
257208
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?
259210 try {
260211 LOGGER .info ("Closing client connection: {}" , client .getRemote ());
261212 client .disconnect ();
@@ -280,34 +231,7 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) {
280231 @ Override
281232 public GuiScreen onOpenGui (GuiScreen gui ) {
282233 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 ();
311235 } else if (gui instanceof GuiControls ) {
312236 TASmodClient .controller .setTASState (TASstate .NONE ); // Set the TASState to nothing to avoid collisions
313237 if (TASmodClient .tickratechanger .ticksPerSecond == 0 ) {
@@ -322,4 +246,53 @@ public GuiScreen onOpenGui(GuiScreen gui) {
322246 }
323247 return gui ;
324248 }
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+ }
325298}
0 commit comments