IJInventoryWindowController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // IJInventoryWindowController.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/7/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJInventoryWindowController.h"
  9. #import "IJMinecraftLevel.h"
  10. #import "IJInventoryItem.h"
  11. #import "IJItemPickerWindowController.h"
  12. @implementation IJInventoryWindowController
  13. @synthesize outlineView;
  14. @synthesize worldPopup;
  15. @synthesize statusTextField;
  16. - (void)awakeFromNib
  17. {
  18. armorItem = [NSMutableArray array];
  19. quickItem = [NSMutableArray array];
  20. inventoryItem = [NSMutableArray array];
  21. rootItems = [[NSArray alloc] initWithObjects:armorItem, quickItem, inventoryItem, nil];
  22. statusTextField.stringValue = @"";
  23. }
  24. - (void)dealloc
  25. {
  26. [inventory release];
  27. [rootItems release];
  28. [level release];
  29. [super dealloc];
  30. }
  31. #pragma mark -
  32. #pragma mark World Selection
  33. - (void)loadWorldAtIndex:(int)worldIndex
  34. {
  35. [armorItem removeAllObjects];
  36. [quickItem removeAllObjects];
  37. [inventoryItem removeAllObjects];
  38. sessionLockValue = [IJMinecraftLevel writeToSessionLockAtIndex:worldIndex];
  39. if (![IJMinecraftLevel checkSessionLockAtIndex:worldIndex value:sessionLockValue])
  40. {
  41. NSBeginCriticalAlertSheet(@"Error loading world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable obtain the session lock.");
  42. return;
  43. }
  44. NSString *levelPath = [IJMinecraftLevel pathForLevelDatAtIndex:worldIndex];
  45. NSData *fileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:levelPath]];
  46. if (!fileData)
  47. {
  48. // Error loading
  49. [outlineView reloadData];
  50. NSBeginCriticalAlertSheet(@"Error loading world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"InsideJob was unable to load the level at %@.", levelPath);
  51. return;
  52. }
  53. [level release];
  54. level = [[IJMinecraftLevel nbtContainerWithData:fileData] retain];
  55. inventory = [[level inventory] retain];
  56. // Add placeholder inventory items:
  57. for (int i = 0; i < IJInventorySlotQuickLast + 1 - IJInventorySlotQuickFirst; i++)
  58. [quickItem addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotQuickFirst + i]];
  59. for (int i = 0; i < IJInventorySlotNormalLast + 1 - IJInventorySlotNormalFirst; i++)
  60. [inventoryItem addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotNormalFirst + i]];
  61. for (int i = 0; i < IJInventorySlotArmorLast + 1 - IJInventorySlotArmorFirst; i++)
  62. [armorItem addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotArmorFirst + i]];
  63. // Overwrite the placeholders with actual inventory:
  64. for (IJInventoryItem *item in inventory)
  65. {
  66. if (IJInventorySlotQuickFirst <= item.slot && item.slot <= IJInventorySlotQuickLast)
  67. {
  68. [quickItem replaceObjectAtIndex:item.slot - IJInventorySlotQuickFirst withObject:item];
  69. }
  70. else if (IJInventorySlotNormalFirst <= item.slot && item.slot <= IJInventorySlotNormalLast)
  71. {
  72. [inventoryItem replaceObjectAtIndex:item.slot - IJInventorySlotNormalFirst withObject:item];
  73. }
  74. else if (IJInventorySlotArmorFirst <= item.slot && item.slot <= IJInventorySlotArmorLast)
  75. {
  76. [armorItem replaceObjectAtIndex:item.slot - IJInventorySlotArmorFirst withObject:item];
  77. }
  78. }
  79. [outlineView reloadData];
  80. [outlineView expandItem:nil expandChildren:YES];
  81. dirty = NO;
  82. statusTextField.stringValue = @"";
  83. }
  84. - (void)saveToWorldAtIndex:(int)worldIndex
  85. {
  86. if (![IJMinecraftLevel checkSessionLockAtIndex:worldIndex value:sessionLockValue])
  87. {
  88. NSBeginCriticalAlertSheet(@"Another application has modified this world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"The session lock was changed by another application.");
  89. return;
  90. }
  91. NSString *levelPath = [IJMinecraftLevel pathForLevelDatAtIndex:worldIndex];
  92. NSMutableArray *newInventory = [NSMutableArray array];
  93. for (NSArray *items in rootItems)
  94. {
  95. for (IJInventoryItem *item in items)
  96. {
  97. if (item.count > 0 && item.itemId > 0)
  98. [newInventory addObject:item];
  99. }
  100. }
  101. [level setInventory:newInventory];
  102. NSString *backupPath = [levelPath stringByAppendingPathExtension:@".insidejobbackup"];
  103. BOOL success;
  104. NSError *error = nil;
  105. success = [[NSFileManager defaultManager] removeItemAtPath:backupPath error:&error];
  106. success = [[NSFileManager defaultManager] copyItemAtPath:levelPath
  107. toPath:backupPath
  108. error:&error];
  109. if (!success)
  110. {
  111. 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.");
  112. return;
  113. }
  114. [[level writeData] writeToURL:[NSURL fileURLWithPath:levelPath] atomically:NO];
  115. dirty = NO;
  116. statusTextField.stringValue = @"Saved.";
  117. }
  118. - (void)markDirty
  119. {
  120. dirty = YES;
  121. statusTextField.stringValue = @"World has unsaved changes.";
  122. }
  123. #pragma mark -
  124. #pragma mark Actions
  125. - (IBAction)worldSelectionChanged:(id)sender
  126. {
  127. int worldIndex = [[worldPopup selectedItem] tag];
  128. [self loadWorldAtIndex:worldIndex];
  129. }
  130. - (void)saveDocument:(id)sender
  131. {
  132. int worldIndex = [[worldPopup selectedItem] tag];
  133. [self saveToWorldAtIndex:worldIndex];
  134. }
  135. - (void)delete:(id)sender
  136. {
  137. IJInventoryItem *item = [outlineView itemAtRow:[outlineView selectedRow]];
  138. item.count = 0;
  139. item.itemId = 0;
  140. item.damage = 0;
  141. [self markDirty];
  142. [outlineView reloadItem:item];
  143. }
  144. - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
  145. {
  146. if (anItem.action == @selector(delete:))
  147. {
  148. return [outlineView selectedRow] != -1 && ![rootItems containsObject:[outlineView itemAtRow:[outlineView selectedRow]]];
  149. }
  150. return YES;
  151. }
  152. #pragma mark -
  153. #pragma mark Inventory Outline View
  154. - (id)outlineView:(NSOutlineView *)theOutlineView child:(NSInteger)index ofItem:(id)item
  155. {
  156. if (item == nil)
  157. {
  158. return [rootItems objectAtIndex:index];
  159. }
  160. else
  161. {
  162. return [item objectAtIndex:index];
  163. }
  164. }
  165. - (BOOL)outlineView:(NSOutlineView *)theOutlineView isItemExpandable:(id)item
  166. {
  167. return item == nil || [rootItems containsObject:item];
  168. }
  169. - (NSInteger)outlineView:(NSOutlineView *)theOutlineView numberOfChildrenOfItem:(id)item
  170. {
  171. if (item == nil)
  172. {
  173. return 3;
  174. }
  175. else if ([rootItems containsObject:item])
  176. {
  177. return [(NSArray *)item count];
  178. }
  179. else
  180. {
  181. return 0;
  182. }
  183. }
  184. - (id)outlineView:(NSOutlineView *)theOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  185. {
  186. if ([rootItems containsObject:item])
  187. {
  188. if ([tableColumn.identifier isEqual:@"slot"])
  189. {
  190. if (item == armorItem)
  191. return @"Armor";
  192. else if (item == quickItem)
  193. return @"Quick Inventory";
  194. else if (item == inventoryItem)
  195. return @"Inventory";
  196. }
  197. else
  198. {
  199. return nil;
  200. }
  201. }
  202. IJInventoryItem *invItem = item;
  203. if ([tableColumn.identifier isEqual:@"slot"])
  204. {
  205. return [NSString stringWithFormat:@"%d", invItem.slot];
  206. }
  207. else if ([tableColumn.identifier isEqual:@"id"])
  208. {
  209. if (invItem.itemId)
  210. return [NSNumber numberWithShort:invItem.itemId];
  211. else
  212. return nil;
  213. }
  214. else if ([tableColumn.identifier isEqual:@"item"])
  215. {
  216. if (invItem.itemId)
  217. return invItem.itemName;
  218. else
  219. return @"";
  220. }
  221. else if ([tableColumn.identifier isEqual:@"count"])
  222. {
  223. if (invItem.count)
  224. return [NSNumber numberWithUnsignedChar:invItem.count];
  225. else
  226. return nil;
  227. }
  228. else if ([tableColumn.identifier isEqual:@"damage"])
  229. {
  230. if (invItem.damage)
  231. return [NSNumber numberWithShort:invItem.damage];
  232. else
  233. return nil;
  234. }
  235. return nil;
  236. }
  237. - (void)outlineView:(NSOutlineView *)theOutlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  238. {
  239. IJInventoryItem *invItem = item;
  240. if ([tableColumn.identifier isEqual:@"id"])
  241. {
  242. invItem.itemId = [object shortValue];
  243. [self markDirty];
  244. }
  245. else if ([tableColumn.identifier isEqual:@"count"])
  246. {
  247. invItem.count = [object unsignedCharValue];
  248. if (invItem.count > 64)
  249. invItem.count = 64;
  250. [self markDirty];
  251. }
  252. else if ([tableColumn.identifier isEqual:@"damage"])
  253. {
  254. invItem.damage = [object shortValue];
  255. [self markDirty];
  256. }
  257. }
  258. - (BOOL)outlineView:(NSOutlineView *)theOutlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
  259. {
  260. if ([rootItems containsObject:item])
  261. return NO;
  262. else if ([tableColumn.identifier isEqual:@"item"])
  263. {
  264. IJInventoryItem *invItem = item;
  265. [[IJItemPickerWindowController sharedController] showPickerWithInitialItemId:invItem.itemId completionBlock:^(uint16_t itemId) {
  266. invItem.itemId = itemId;
  267. [outlineView reloadItem:item];
  268. [self markDirty];
  269. }];
  270. return NO;
  271. }
  272. else
  273. {
  274. return [tableColumn.identifier isEqual:@"slot"] == NO;
  275. }
  276. }
  277. @end