From f29fda72c5351a9d037cbdb7e67a3fb1dd7fb1ff Mon Sep 17 00:00:00 2001 From: preble Date: Thu, 14 Oct 2010 22:56:31 -0400 Subject: [PATCH] Better handle worlds that could not be loaded. --- Classes/IJInventoryView.m | 19 +++++++++++++++++-- Classes/IJInventoryWindowController.m | 11 +++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Classes/IJInventoryView.m b/Classes/IJInventoryView.m index b502db6..ef9d4a7 100644 --- a/Classes/IJInventoryView.m +++ b/Classes/IJInventoryView.m @@ -132,7 +132,11 @@ const static CGFloat cellOffset = 40; - (void)reloadItemAtIndex:(int)itemIndex { - IJInventoryItem *item = [items objectAtIndex:itemIndex]; + IJInventoryItem *item = nil; + + if (itemIndex < items.count) + item = [items objectAtIndex:itemIndex]; + CALayer *layer = [self.layer.sublayers objectAtIndex:itemIndex]; CALayer *imageLayer = [layer.sublayers objectAtIndex:0]; @@ -151,7 +155,7 @@ const static CGFloat cellOffset = 40; [theItems retain]; items = theItems; - for (int i = 0; i < items.count; i++) + for (int i = 0; i < rows * cols; i++) [self reloadItemAtIndex:i]; } @@ -201,6 +205,9 @@ const static CGFloat cellOffset = 40; // Find the IJInventoryItem: NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil]; int itemIndex = [self itemIndexForPoint:pointInView]; + if (itemIndex >= items.count) + return; + IJInventoryItem *item = [items objectAtIndex:itemIndex]; if (item.itemId == 0) return; // can't drag nothing @@ -282,6 +289,10 @@ const static CGFloat cellOffset = 40; { // TODO: Detect and ignore same slot. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]]; + + if (index >= items.count) // this could happen if this is an invalid world + return NSDragOperationNone; + [self moveHighlightToLayerAtIndex:index]; if ([[sender draggingSource] isKindOfClass:[self class]]) @@ -293,6 +304,10 @@ const static CGFloat cellOffset = 40; { // TODO: Detect and ignore same slot. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]]; + + if (index >= items.count) // this could happen if this is an invalid world + return NSDragOperationNone; + [self moveHighlightToLayerAtIndex:index]; if ([[sender draggingSource] isKindOfClass:[self class]]) diff --git a/Classes/IJInventoryWindowController.m b/Classes/IJInventoryWindowController.m index e93c41d..32c562f 100644 --- a/Classes/IJInventoryWindowController.m +++ b/Classes/IJInventoryWindowController.m @@ -74,6 +74,11 @@ inventory = nil; [self didChangeValueForKey:@"worldTime"]; + [inventoryView setItems:normalInventory]; + [quickView setItems:quickInventory]; + [armorView setItems:armorInventory]; + + statusTextField.stringValue = @"No world loaded."; if (![IJMinecraftLevel worldExistsAtIndex:worldIndex]) { @@ -149,6 +154,9 @@ - (void)saveToWorldAtIndex:(int)worldIndex { + if (inventory == nil) + return; // no world loaded, nothing to save + if (![IJMinecraftLevel checkSessionLockAtIndex:worldIndex value:sessionLockValue]) { NSBeginCriticalAlertSheet(@"Another application has modified this world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"The session lock was changed by another application."); @@ -259,6 +267,9 @@ - (BOOL)validateUserInterfaceItem:(id )anItem { + if (anItem.action == @selector(saveDocument:)) + return inventory != nil; + return YES; }