IJInventoryItem.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // IJInventoryItem.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/7/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJInventoryItem.h"
  9. @implementation IJInventoryItem
  10. @synthesize itemId, slot, damage, count;
  11. + (id)emptyItemWithSlot:(uint8_t)slot
  12. {
  13. IJInventoryItem *obj = [[[[self class] alloc] init] autorelease];
  14. obj.slot = slot;
  15. return obj;
  16. }
  17. - (NSString *)itemName
  18. {
  19. NSString *name = [[IJInventoryItem itemIdLookup] objectForKey:[NSNumber numberWithShort:self.itemId]];
  20. if (name)
  21. return name;
  22. else
  23. return [NSString stringWithFormat:@"%d", self.itemId];
  24. }
  25. + (NSDictionary *)itemIdLookup
  26. {
  27. static NSDictionary *lookup = nil;
  28. if (!lookup)
  29. {
  30. NSError *error = nil;
  31. NSString *lines = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Items" withExtension:@"csv"]
  32. encoding:NSUTF8StringEncoding
  33. error:&error];
  34. NSMutableDictionary *building = [NSMutableDictionary dictionary];
  35. [lines enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
  36. NSArray *components = [line componentsSeparatedByString:@","];
  37. NSNumber *itemId = [NSNumber numberWithShort:[[components objectAtIndex:0] intValue]];
  38. NSString *name = [components objectAtIndex:1];
  39. [building setObject:name forKey:itemId];
  40. }];
  41. lookup = [[NSDictionary alloc] initWithDictionary:building];
  42. }
  43. return lookup;
  44. }
  45. @end