From 46ade83b0378cacd06495e53dbb93acae9d1eb01 Mon Sep 17 00:00:00 2001 From: Manoel Trapier Date: Fri, 11 Mar 2011 02:41:52 +0100 Subject: [PATCH] We can now select player from SMP world. "Default player" is currently a placeholder, and player file are not filtered. --- Classes/IJInventoryWindowController.h | 5 +- Classes/IJInventoryWindowController.m | 249 ++++++++++++++++++++------ Classes/IJMinecraftLevel.m | 2 +- English.lproj/MainMenu.xib | 156 +++++++++++++++- 4 files changed, 353 insertions(+), 59 deletions(-) diff --git a/Classes/IJInventoryWindowController.h b/Classes/IJInventoryWindowController.h index 433463e..b5ef814 100644 --- a/Classes/IJInventoryWindowController.h +++ b/Classes/IJInventoryWindowController.h @@ -47,6 +47,7 @@ NSString *loadedWorldFolder; NSString *attemptedLoadWorldFolder; NSString *loadedPlayer; + NSPopUpButton *playerSelectionControl; } @property (nonatomic, assign) IBOutlet NSPopUpButton *worldSelectionControl; @@ -56,9 +57,8 @@ @property (nonatomic, assign) IBOutlet IJInventoryView *armorView; @property (nonatomic, assign) IBOutlet NSSearchField *itemSearchField; @property (nonatomic, assign) IBOutlet NSTableView *itemTableView; - @property (nonatomic, retain) NSNumber *worldTime; -@property (nonatomic, retain) NSString *playerName; +@property (nonatomic, assign) IBOutlet NSPopUpButton *playerSelectionControl; - (IBAction)menuSelectWorldFromPath:(id)sender; - (IBAction)menuSelectWorld:(id)sender; @@ -75,5 +75,6 @@ - (IBAction)emptyInventory:(id)sender; - (IBAction)saveInventoryItems:(id)sender; - (IBAction)loadInventoryItems:(id)sender; +- (IBAction)playerSelectionChanged:(id)sender; @end diff --git a/Classes/IJInventoryWindowController.m b/Classes/IJInventoryWindowController.m index fec0aa0..d82559a 100644 --- a/Classes/IJInventoryWindowController.m +++ b/Classes/IJInventoryWindowController.m @@ -20,9 +20,11 @@ - (BOOL)isDocumentEdited; - (void)loadWorldAtFolder:(NSString *)worldFolder; - (void)loadWorldSelectionControl; +- (void)loadPlayerSelectionControl; @end @implementation IJInventoryWindowController +@synthesize playerSelectionControl; @synthesize worldSelectionControl; @synthesize statusTextField; @@ -98,6 +100,78 @@ * Players/PlayerName.dat file */ + [armorInventory removeAllObjects]; + [quickInventory removeAllObjects]; + [normalInventory removeAllObjects]; + + [inventoryView setItems:normalInventory]; + [quickView setItems:quickInventory]; + [armorView setItems:armorInventory]; + + [player release]; + player = nil; + [inventory release]; + inventory = nil; + + loadedPlayer = nil; + + NSLog(@"Player name: %@",PlayerName); + + NSString *playerPath = [IJMinecraftLevel pathForPlayer:PlayerName withWorld: loadedWorldFolder]; + + NSLog(@"Path: %@", playerPath); + + NSData *playerFileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:playerPath]]; + if (!playerFileData) + { + // Error loading + NSBeginCriticalAlertSheet(@"Error loading player.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"InsideJob was unable to load the level at %@.", playerPath); + return; + } + + player = [[IJMinecraftLevel nbtContainerWithData:playerFileData] retain]; + inventory = [[player inventory] retain]; + + // Add placeholder inventory items: + + for (int i = 0; i < IJInventorySlotQuickLast + 1 - IJInventorySlotQuickFirst; i++) + [quickInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotQuickFirst + i]]; + + for (int i = 0; i < IJInventorySlotNormalLast + 1 - IJInventorySlotNormalFirst; i++) + [normalInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotNormalFirst + i]]; + + for (int i = 0; i < IJInventorySlotArmorLast + 1 - IJInventorySlotArmorFirst; i++) + [armorInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotArmorFirst + i]]; + + + // Overwrite the placeholders with actual inventory: + + for (IJInventoryItem *item in inventory) + { + if (IJInventorySlotQuickFirst <= item.slot && item.slot <= IJInventorySlotQuickLast) + { + [quickInventory replaceObjectAtIndex:item.slot - IJInventorySlotQuickFirst withObject:item]; + } + else if (IJInventorySlotNormalFirst <= item.slot && item.slot <= IJInventorySlotNormalLast) + { + [normalInventory replaceObjectAtIndex:item.slot - IJInventorySlotNormalFirst withObject:item]; + } + else if (IJInventorySlotArmorFirst <= item.slot && item.slot <= IJInventorySlotArmorLast) + { + [armorInventory replaceObjectAtIndex:item.slot - IJInventorySlotArmorFirst withObject:item]; + } + } + + // NSLog(@"normal: %@", normalInventory); + // NSLog(@"quick: %@", quickInventory); + + [inventoryView setItems:normalInventory]; + [quickView setItems:quickInventory]; + [armorView setItems:armorInventory]; + + [self setDocumentEdited:NO]; + statusTextField.stringValue = @"Player loaded!"; + loadedPlayer = [PlayerName retain]; } - (void)loadWorldAtFolder:(NSString *)worldPath @@ -109,6 +183,11 @@ return; } + NSFileManager *filemgr; + NSArray *filelist; + NSError *fileError; + int count, i; + [armorInventory removeAllObjects]; [quickInventory removeAllObjects]; [normalInventory removeAllObjects]; @@ -156,13 +235,31 @@ [self willChangeValueForKey:@"worldTime"]; + [playerSelectionControl setHidden: YES]; /* Now search for first player .dat file (but by default try to load from level.dat */ -#if 1 + /* Verify that a "Player" folder exist. If not do nt show the player list */ + filemgr = [NSFileManager defaultManager]; + + filelist = [filemgr contentsOfDirectoryAtPath:worldPath error:&fileError]; + + count = [filelist count]; + + for (i = 0; i < count; i++) + { + NSLog (@"File in world/ %@", [filelist objectAtIndex: i]); + if ([[filelist objectAtIndex: i] isEqualTo: @"players"]) + { + [playerSelectionControl setHidden: NO]; + [self loadPlayerSelectionControl]; + break; + } + } + loadedPlayer = nil; NSString *playerPath = [IJMinecraftLevel pathForPlayer:loadedPlayer withWorld:worldPath]; -#else - NSString *playerPath = [worldPath stringByAppendingString:@"/players/Godzil.dat"]; -#endif + + /* Now load level.dat as if i is not a SMP. */ + NSData *playerFileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:playerPath]]; if (!playerFileData) { @@ -215,11 +312,11 @@ [armorView setItems:armorInventory]; [self setDocumentEdited:NO]; - statusTextField.stringValue = @""; + statusTextField.stringValue = @"World loaded!"; loadedWorldFolder = [worldPath retain]; - - NSLog(@"%@",loadedWorldFolder); - NSLog(@"%@",worldPath); + + NSLog(@"%@",loadedWorldFolder); + NSLog(@"%@",worldPath); } @@ -278,17 +375,6 @@ return; } } - // Remove a previously-created .insidejobbackup, if it exists: - if ([[NSFileManager defaultManager] fileExistsAtPath:backupLevelPath]) - { - success = [[NSFileManager defaultManager] removeItemAtPath:backupPlayerPath error:&error]; - if (!success) - { - NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [error localizedDescription]); - NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to remove the prior backup of this level file:\n%@", [error localizedDescription]); - return; - } - } // Create the backup: success = [[NSFileManager defaultManager] copyItemAtPath:levelPath toPath:backupLevelPath error:&error]; @@ -299,15 +385,7 @@ return; } - success = [[NSFileManager defaultManager] copyItemAtPath:playerPath toPath:backupPlayerPath error:&error]; - if (!success) - { - NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [error localizedDescription]); - NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to create a backup of the existing level file:\n%@", [error localizedDescription]); - return; - } - - // Write the new level.dat out: + // Write the new level.dat out: success = [[player writeData] writeToURL:[NSURL fileURLWithPath:levelPath] options:0 error:&error]; if (!success) @@ -321,20 +399,58 @@ NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [restoreError localizedDescription]); NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing level file, and the backup could not be restored.\n%@\n%@", [error localizedDescription], [restoreError localizedDescription]); } - - success = [[NSFileManager defaultManager] copyItemAtPath:backupPlayerPath toPath:playerPath error:&restoreError]; - if (!success) - { - NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [restoreError localizedDescription]); - NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing level file, and the backup could not be restored.\n%@\n%@", [error localizedDescription], [restoreError localizedDescription]); - } else - { - NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing level file, and the backup was successfully restored.\n%@", [error localizedDescription]); - } + { + NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing level file, and the backup was successfully restored.\n%@", [error localizedDescription]); + } return; } - + + if (playerPath != levelPath) + { + + // Remove a previously-created .insidejobbackup, if it exists: + if ([[NSFileManager defaultManager] fileExistsAtPath:backupPlayerPath]) + { + success = [[NSFileManager defaultManager] removeItemAtPath:backupPlayerPath error:&error]; + if (!success) + { + NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [error localizedDescription]); + NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to remove the prior backup of this player file:\n%@", [error localizedDescription]); + return; + } + } + + success = [[NSFileManager defaultManager] copyItemAtPath:playerPath toPath:backupPlayerPath error:&error]; + if (!success) + { + NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [error localizedDescription]); + NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to create a backup of the existing player file:\n%@", [error localizedDescription]); + return; + } + + // Write the new player.dat out: + success = [[player writeData] writeToURL:[NSURL fileURLWithPath:playerPath] options:0 error:&error]; + + if (!success) + { + NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [error localizedDescription]); + + NSError *restoreError = nil; + + success = [[NSFileManager defaultManager] copyItemAtPath:backupPlayerPath toPath:playerPath error:&restoreError]; + if (!success) + { + NSLog(@"%s:%d %@", __PRETTY_FUNCTION__, __LINE__, [restoreError localizedDescription]); + NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing player file, and the backup could not be restored.\n%@\n%@", [error localizedDescription], [restoreError localizedDescription]); + } + else + { + NSBeginCriticalAlertSheet(@"An error occurred while saving.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable to save to the existing player file, and the backup was successfully restored.\n%@", [error localizedDescription]); + } + return; + } + } [self setDocumentEdited:NO]; statusTextField.stringValue = @"Saved."; } @@ -399,14 +515,14 @@ path = [path stringByAppendingPathComponent:@"saves"]; - NSString* worldName = [worldSelectionControl titleOfSelectedItem]; + NSString* worldName = [worldSelectionControl titleOfSelectedItem]; NSString* worldPath = [path stringByAppendingPathComponent:worldName]; - NSLog(@"loadedWorldFolder: %@",loadedWorldFolder); - NSLog(@"worldName: %@",worldName); - NSLog(@"worldPath: %@",worldPath); + NSLog(@"loadedWorldFolder: %@",loadedWorldFolder); + NSLog(@"worldName: %@",worldName); + NSLog(@"worldPath: %@",worldPath); - [self loadWorldAtFolder:worldPath]; + [self loadWorldAtFolder:worldPath]; } - (void)loadWorldSelectionControl @@ -442,6 +558,37 @@ } +- (void)loadPlayerSelectionControl +{ + NSString *playerPath = loadedWorldFolder; + playerPath = [playerPath stringByAppendingPathComponent:@"players"]; + + NSFileManager *filemgr; + NSArray *filelist; + NSError *fileError; + int count; + int i; + + filemgr = [NSFileManager defaultManager]; + + filelist = [filemgr contentsOfDirectoryAtPath:playerPath error:&fileError]; + + count = [filelist count]; + + [playerSelectionControl removeAllItems]; + + [playerSelectionControl addItemWithTitle:@"World default"]; + + for (i = 0; i < count; i++) + { + NSLog (@"%@", [filelist objectAtIndex: i]); + [playerSelectionControl addItemWithTitle:[[filelist objectAtIndex: i] stringByDeletingPathExtension]]; + } + + [filemgr release]; + +} + - (void)saveDocument:(id)sender { [self saveWorld]; @@ -484,13 +631,6 @@ [self setDocumentEdited:YES]; } -- (NSString *)playerName -{ - return @"Godzil"; -} -- (void)setPlayerName:(NSString *)playerName -{ -} - (void)calcTimePoints:(int)number { @@ -622,6 +762,13 @@ [self loadInventory]; } +- (IBAction)playerSelectionChanged:(id)sender +{ + [self loadWorldPlayerInventory: [playerSelectionControl titleOfSelectedItem]]; + + // [self loadWorldAtFolder:worldPath]; +} + #pragma mark - #pragma mark IJInventoryViewDelegate diff --git a/Classes/IJMinecraftLevel.m b/Classes/IJMinecraftLevel.m index d30d9fc..828dbfe 100644 --- a/Classes/IJMinecraftLevel.m +++ b/Classes/IJMinecraftLevel.m @@ -143,7 +143,7 @@ if (loadedPlayer == nil) return [self pathForLevelDatAtFolder:worldPath]; - return [[[worldPath stringByAppendingPathComponent:@"players"] stringByAppendingPathComponent:loadedPlayer] stringByAppendingPathExtension:@".dat"]; + return [[[worldPath stringByAppendingPathComponent:@"players"] stringByAppendingPathComponent:loadedPlayer] stringByAppendingPathExtension:@"dat"]; } + (BOOL)worldExistsAtFolder:(NSString *)worldPath diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index 8a0123a..c6bf252 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -560,7 +560,7 @@ 266 - {{211, 369}, {366, 14}} + {{308, 369}, {269, 14}} @@ -1068,10 +1068,10 @@ 268 - {{11, 363}, {172, 25}} + {{11, 363}, {175, 25}} - + YES -2080244160 @@ -1086,7 +1086,7 @@ 75 - Item 1 + World 1048576 2147483647 @@ -1133,6 +1133,73 @@ 2 + + + 268 + {{194, 363}, {109, 25}} + + + YES + + -2080244160 + 2048 + + + -2038284033 + 163 + + + 400 + 75 + + + Player + + 1048576 + 2147483647 + 1 + + + _popUpItemAction: + + + YES + + OtherViews + + YES + + + + Item 2 + + 1048576 + 2147483647 + + + _popUpItemAction: + + + + + Item 3 + + 1048576 + 2147483647 + + + _popUpItemAction: + + + + + + 2 + YES + YES + 2 + + {{7, 11}, {585, 396}} @@ -1516,6 +1583,22 @@ 741 + + + playerSelectionControl + + + + 755 + + + + playerSelectionChanged: + + + + 756 + @@ -1791,6 +1874,7 @@ + @@ -2185,6 +2269,50 @@ + + 749 + + + YES + + + + + + 750 + + + YES + + + + + + 751 + + + YES + + + + + + + + 752 + + + + + 753 + + + + + 754 + + + @@ -2333,8 +2461,10 @@ 730.IBPluginDependency 732.IBPluginDependency 733.IBPluginDependency + 749.IBPluginDependency 75.IBPluginDependency 75.ImportedFromIB2 + 750.IBPluginDependency 81.IBEditorWindowLastContentRect 81.IBPluginDependency 81.ImportedFromIB2 @@ -2510,7 +2640,9 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin {{483, 773}, {178, 63}} com.apple.InterfaceBuilder.CocoaPlugin @@ -2533,7 +2665,7 @@ - 741 + 756 @@ -2559,6 +2691,7 @@ makeSearchFieldFirstResponder: menuSelectWorld: menuSelectWorldFromPath: + playerSelectionChanged: saveInventoryItems: setNextDay: setNextMidnight: @@ -2582,6 +2715,7 @@ id id id + id @@ -2594,6 +2728,7 @@ makeSearchFieldFirstResponder: menuSelectWorld: menuSelectWorldFromPath: + playerSelectionChanged: saveInventoryItems: setNextDay: setNextMidnight: @@ -2628,6 +2763,10 @@ menuSelectWorldFromPath: id + + playerSelectionChanged: + id + saveInventoryItems: id @@ -2666,6 +2805,7 @@ inventoryView itemSearchField itemTableView + playerSelectionControl quickView statusTextField worldSelectionControl @@ -2676,6 +2816,7 @@ IJInventoryView NSSearchField NSTableView + NSPopUpButton IJInventoryView NSTextField NSPopUpButton @@ -2689,6 +2830,7 @@ inventoryView itemSearchField itemTableView + playerSelectionControl quickView statusTextField worldSelectionControl @@ -2711,6 +2853,10 @@ itemTableView NSTableView + + playerSelectionControl + NSPopUpButton + quickView IJInventoryView