Browse Source

Better handle worlds that could not be loaded.

preble 13 years ago
parent
commit
f29fda72c5
2 changed files with 28 additions and 2 deletions
  1. 17 2
      Classes/IJInventoryView.m
  2. 11 0
      Classes/IJInventoryWindowController.m

+ 17 - 2
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]])

+ 11 - 0
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 <NSValidatedUserInterfaceItem>)anItem
 {
+	if (anItem.action == @selector(saveDocument:))
+		return inventory != nil;
+		
 	return YES;
 }