IJMinecraftLevel.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // IJMinecraftLevel.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/7/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJMinecraftLevel.h"
  9. #import "IJInventoryItem.h"
  10. @implementation IJMinecraftLevel
  11. - (NBTContainer *)containerWithName:(NSString *)theName inArray:(NSArray *)array
  12. {
  13. for (NBTContainer *container in array)
  14. {
  15. if ([container.name isEqual:theName])
  16. return container;
  17. }
  18. return nil;
  19. }
  20. - (NBTContainer *)inventoryList
  21. {
  22. // Inventory is found in:
  23. // - compound "Data"
  24. // - compound "Player"
  25. // - list "Inventory"
  26. // *
  27. NBTContainer *dataCompound = [self childNamed:@"Data"];
  28. NBTContainer *playerCompound = [dataCompound childNamed:@"Player"];
  29. NBTContainer *inventoryList = [playerCompound childNamed:@"Inventory"];
  30. // TODO: Check for error conditions here.
  31. return inventoryList;
  32. }
  33. - (NSArray *)inventory
  34. {
  35. NSMutableArray *output = [NSMutableArray array];
  36. for (NSArray *listItems in [self inventoryList].children)
  37. {
  38. IJInventoryItem *invItem = [[IJInventoryItem alloc] init];
  39. invItem.itemId = [[self containerWithName:@"id" inArray:listItems].numberValue shortValue];
  40. invItem.count = [[self containerWithName:@"Count" inArray:listItems].numberValue unsignedCharValue];
  41. invItem.damage = [[self containerWithName:@"Damage" inArray:listItems].numberValue shortValue];
  42. invItem.slot = [[self containerWithName:@"Slot" inArray:listItems].numberValue unsignedCharValue];
  43. [output addObject:invItem];
  44. [invItem release];
  45. }
  46. return output;
  47. }
  48. - (void)setInventory:(NSArray *)newInventory
  49. {
  50. NSMutableArray *newChildren = [NSMutableArray array];
  51. NBTContainer *inventoryList = [self inventoryList];
  52. for (IJInventoryItem *invItem in newInventory)
  53. {
  54. NSArray *listItems = [NSArray arrayWithObjects:
  55. [NBTContainer containerWithName:@"id" type:NBTTypeShort numberValue:[NSNumber numberWithShort:invItem.itemId]],
  56. [NBTContainer containerWithName:@"Damage" type:NBTTypeShort numberValue:[NSNumber numberWithShort:invItem.damage]],
  57. [NBTContainer containerWithName:@"Count" type:NBTTypeByte numberValue:[NSNumber numberWithShort:invItem.count]],
  58. [NBTContainer containerWithName:@"Slot" type:NBTTypeByte numberValue:[NSNumber numberWithShort:invItem.slot]],
  59. nil];
  60. [newChildren addObject:listItems];
  61. }
  62. inventoryList.children = newChildren;
  63. }
  64. @end