Change backen for loading world, now use folder path as index instead of slot id.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
@interface IJInventoryWindowController : NSWindowController <NSWindowDelegate, IJInventoryViewDelegate> {
|
||||
IJMinecraftLevel *level;
|
||||
IJMinecraftLevel *player; /***< SMP Player.dat file use same format as level.dat */
|
||||
NSArray *inventory;
|
||||
|
||||
NSSegmentedControl *worldSelectionControl;
|
||||
@@ -42,8 +43,9 @@
|
||||
|
||||
// Document
|
||||
int64_t sessionLockValue;
|
||||
int loadedWorldIndex;
|
||||
int attemptedLoadWorldIndex;
|
||||
int loadedWorldIndex;
|
||||
NSString *loadedWorldFolder;
|
||||
NSString *attemptedLoadWorldFolder;
|
||||
}
|
||||
|
||||
@property (nonatomic, assign) IBOutlet NSSegmentedControl *worldSelectionControl;
|
||||
@@ -56,6 +58,7 @@
|
||||
|
||||
@property (nonatomic, retain) NSNumber *worldTime;
|
||||
|
||||
- (IBAction)menuSelectWorldFromPath:(id)sender;
|
||||
- (IBAction)menuSelectWorld:(id)sender;
|
||||
- (IBAction)worldSelectionChanged:(id)sender;
|
||||
- (IBAction)updateItemSearchFilter:(id)sender;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
@interface IJInventoryWindowController ()
|
||||
- (void)saveWorld;
|
||||
- (void)loadWorldAtIndex:(int)worldIndex;
|
||||
- (void)loadWorldAtFolder:(NSString *)worldFolder;
|
||||
- (BOOL)isDocumentEdited;
|
||||
@end
|
||||
|
||||
@@ -77,20 +78,29 @@
|
||||
if (returnCode == NSAlertDefaultReturn) // Save
|
||||
{
|
||||
[self saveWorld];
|
||||
[self loadWorldAtIndex:attemptedLoadWorldIndex];
|
||||
[self loadWorldAtFolder:attemptedLoadWorldFolder];
|
||||
}
|
||||
else if (returnCode == NSAlertAlternateReturn) // Don't save
|
||||
{
|
||||
[self setDocumentEdited:NO]; // Slightly hacky -- prevent the alert from being put up again.
|
||||
[self loadWorldAtIndex:attemptedLoadWorldIndex];
|
||||
[self loadWorldAtFolder:attemptedLoadWorldFolder];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)loadWorldAtIndex:(int)worldIndex
|
||||
- (void)loadWorldPlayerInventory:(NSString *)PlayerName
|
||||
{
|
||||
/*
|
||||
* If passing NULL to PlayerName, we will use level.dat instead of
|
||||
* Players/PlayerName.dat file
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
- (void)loadWorldAtFolder:(NSString *)worldPath
|
||||
{
|
||||
if ([self isDocumentEdited])
|
||||
{
|
||||
attemptedLoadWorldIndex = worldIndex;
|
||||
attemptedLoadWorldFolder = worldPath;
|
||||
NSBeginInformationalAlertSheet(@"Do you want to save the changes you made in this world?", @"Save", @"Don't Save", @"Cancel", self.window, self, @selector(dirtyLoadSheetDidEnd:returnCode:contextInfo:), nil, nil, @"Your changes will be lost if you do not save them.");
|
||||
return;
|
||||
}
|
||||
@@ -111,22 +121,9 @@
|
||||
[self didChangeValueForKey:@"worldTime"];
|
||||
|
||||
statusTextField.stringValue = @"No world loaded.";
|
||||
|
||||
if (![IJMinecraftLevel worldExistsAtIndex:worldIndex])
|
||||
{
|
||||
NSBeginCriticalAlertSheet(@"No world exists in that slot.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Please create a new single player world in this slot using Minecraft and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
sessionLockValue = [IJMinecraftLevel writeToSessionLockAtIndex:worldIndex];
|
||||
if (![IJMinecraftLevel checkSessionLockAtIndex:worldIndex value:sessionLockValue])
|
||||
{
|
||||
NSBeginCriticalAlertSheet(@"Error loading world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Inside Job was unable obtain the session lock.");
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *levelPath = [IJMinecraftLevel pathForLevelDatAtIndex:worldIndex];
|
||||
|
||||
|
||||
NSString *levelPath = [IJMinecraftLevel pathForLevelDatAtFolder:worldPath];
|
||||
|
||||
NSData *fileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:levelPath]];
|
||||
|
||||
if (!fileData)
|
||||
@@ -138,8 +135,19 @@
|
||||
|
||||
[self willChangeValueForKey:@"worldTime"];
|
||||
|
||||
level = [[IJMinecraftLevel nbtContainerWithData:fileData] retain];
|
||||
inventory = [[level inventory] retain];
|
||||
/* Now search for first player .dat file (but by default try to load from level.dat */
|
||||
NSString *playerPath = [IJMinecraftLevel pathForLevelDatAtFolder:worldPath];
|
||||
NSData *playerFileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:playerPath]];
|
||||
if (!fileData)
|
||||
{
|
||||
// Error loading
|
||||
NSBeginCriticalAlertSheet(@"Error loading world.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"InsideJob was unable to load the level at %@.", levelPath);
|
||||
return;
|
||||
}
|
||||
|
||||
level = [[IJMinecraftLevel nbtContainerWithData:fileData] retain];
|
||||
player = [[IJMinecraftLevel nbtContainerWithData:playerFileData] retain];
|
||||
inventory = [[player inventory] retain];
|
||||
|
||||
[self didChangeValueForKey:@"worldTime"];
|
||||
|
||||
@@ -173,8 +181,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
// NSLog(@"normal: %@", normalInventory);
|
||||
// NSLog(@"quick: %@", quickInventory);
|
||||
// NSLog(@"normal: %@", normalInventory);
|
||||
// NSLog(@"quick: %@", quickInventory);
|
||||
|
||||
[inventoryView setItems:normalInventory];
|
||||
[quickView setItems:quickInventory];
|
||||
@@ -182,11 +190,21 @@
|
||||
|
||||
[self setDocumentEdited:NO];
|
||||
statusTextField.stringValue = @"";
|
||||
loadedWorldIndex = worldIndex;
|
||||
loadedWorldFolder = worldPath;
|
||||
}
|
||||
|
||||
- (void)loadWorldAtIndex:(int)worldIndex
|
||||
{
|
||||
NSString *worldPath;
|
||||
worldPath = [IJMinecraftLevel pathForWorldAtIndex:worldIndex];
|
||||
|
||||
[self loadWorldAtFolder: worldPath];
|
||||
}
|
||||
|
||||
|
||||
- (void)saveWorld
|
||||
{
|
||||
#if 0
|
||||
int worldIndex = loadedWorldIndex;
|
||||
if (inventory == nil)
|
||||
return; // no world loaded, nothing to save
|
||||
@@ -260,6 +278,7 @@
|
||||
|
||||
[self setDocumentEdited:NO];
|
||||
statusTextField.stringValue = @"Saved.";
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)setDocumentEdited:(BOOL)edited
|
||||
@@ -284,6 +303,34 @@
|
||||
[worldSelectionControl setSelectedSegment:worldIndex - 1];
|
||||
}
|
||||
|
||||
- (IBAction)menuSelectWorldFromPath:(id)sender
|
||||
{
|
||||
NSInteger openResult;
|
||||
/* Ask user for world folder path */
|
||||
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
||||
NSString *worldPath;
|
||||
|
||||
/* Only allow to choose a folder */
|
||||
[panel setCanChooseDirectories:YES];
|
||||
[panel setCanChooseFiles:NO];
|
||||
openResult = [panel runModal];
|
||||
|
||||
if (openResult == NSOKButton)
|
||||
{
|
||||
worldPath = [[panel directoryURL] path];
|
||||
|
||||
/* Verify for level.dat */
|
||||
if (![IJMinecraftLevel worldExistsAtFolder: worldPath])
|
||||
{
|
||||
NSBeginCriticalAlertSheet(@"No world exists in that slot.", @"Dismiss", nil, nil, self.window, nil, nil, nil, nil, @"Please create a new single player world in this slot using Minecraft and try again.");
|
||||
return;
|
||||
}
|
||||
/* Now try to open the world... */
|
||||
[self loadWorldAtFolder:[[panel directoryURL] path]];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
- (IBAction)worldSelectionChanged:(id)sender
|
||||
{
|
||||
int worldIndex = [worldSelectionControl selectedSegment] + 1;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//
|
||||
// Created by Adam Preble on 10/7/10.
|
||||
// Copyright 2010 Adam Preble. All rights reserved.
|
||||
// Changes for opening folder Copyright 2011 Manoel Trapier
|
||||
//
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
@@ -17,13 +18,14 @@
|
||||
@property (nonatomic, readonly) NBTContainer *worldTimeContainer;
|
||||
|
||||
+ (NSString *)pathForWorldAtIndex:(int)worldIndex;
|
||||
+ (NSString *)pathForLevelDatAtIndex:(int)worldIndex;
|
||||
+ (NSString *)pathForSessionLockAtIndex:(int)worldIndex;
|
||||
|
||||
+ (BOOL)worldExistsAtIndex:(int)worldIndex;
|
||||
+ (NSString *)pathForLevelDatAtFolder:(NSString *)worldPath;
|
||||
+ (NSString *)pathForSessionLockAtFolder:(NSString *)worldPath;
|
||||
|
||||
+ (int64_t)writeToSessionLockAtIndex:(int)worldIndex;
|
||||
+ (BOOL)checkSessionLockAtIndex:(int)worldIndex value:(int64_t)checkValue;
|
||||
+ (BOOL)worldExistsAtFolder:(NSString *)worldPath;
|
||||
|
||||
+ (int64_t)writeToSessionLockAtFolder:(NSString *)worldPath;
|
||||
+ (BOOL)checkSessionLockAtFolder:(NSString *)worldPath value:(int64_t)checkValue;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//
|
||||
// Created by Adam Preble on 10/7/10.
|
||||
// Copyright 2010 Adam Preble. All rights reserved.
|
||||
// Changes for opening folder Copyright 2011 Manoel Trapier
|
||||
//
|
||||
|
||||
#import "IJMinecraftLevel.h"
|
||||
@@ -96,20 +97,6 @@
|
||||
return path;
|
||||
}
|
||||
|
||||
+ (NSString *)pathForLevelDatAtIndex:(int)worldIndex
|
||||
{
|
||||
return [[[self class] pathForWorldAtIndex:worldIndex] stringByAppendingPathComponent:@"level.dat"];
|
||||
}
|
||||
+ (NSString *)pathForSessionLockAtIndex:(int)worldIndex
|
||||
{
|
||||
return [[[self class] pathForWorldAtIndex:worldIndex] stringByAppendingPathComponent:@"session.lock"];
|
||||
}
|
||||
|
||||
+ (BOOL)worldExistsAtIndex:(int)worldIndex
|
||||
{
|
||||
return [[NSFileManager defaultManager] fileExistsAtPath:[[self class] pathForLevelDatAtIndex:worldIndex]];
|
||||
}
|
||||
|
||||
+ (NSData *)dataWithInt64:(int64_t)v
|
||||
{
|
||||
NSMutableData *data = [NSMutableData data];
|
||||
@@ -128,9 +115,25 @@
|
||||
return n;
|
||||
}
|
||||
|
||||
+ (int64_t)writeToSessionLockAtIndex:(int)worldIndex
|
||||
/******************************************************************************/
|
||||
+ (NSString *)pathForLevelDatAtFolder:(NSString *)worldPath
|
||||
{
|
||||
NSString *path = [IJMinecraftLevel pathForSessionLockAtIndex:worldIndex];
|
||||
return [worldPath stringByAppendingPathComponent:@"level.dat"];
|
||||
}
|
||||
|
||||
+ (NSString *)pathForSessionLockAtFolder:(NSString *)worldPath
|
||||
{
|
||||
return [worldPath stringByAppendingPathComponent:@"session.lock"];
|
||||
}
|
||||
|
||||
+ (BOOL)worldExistsAtFolder:(NSString *)worldPath
|
||||
{
|
||||
return [[NSFileManager defaultManager] fileExistsAtPath:[[self class] pathForLevelDatAtFolder:worldPath]];
|
||||
}
|
||||
|
||||
+ (int64_t)writeToSessionLockAtFolder:(NSString *)worldPath
|
||||
{
|
||||
NSString *path = [IJMinecraftLevel pathForSessionLockAtFolder:worldPath];
|
||||
NSDate *now = [NSDate date];
|
||||
NSTimeInterval interval = [now timeIntervalSince1970];
|
||||
int64_t milliseconds = (int64_t)(interval * 1000.0);
|
||||
@@ -142,11 +145,11 @@
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
+ (BOOL)checkSessionLockAtIndex:(int)worldIndex value:(int64_t)checkValue
|
||||
+ (BOOL)checkSessionLockAtFolder:(NSString *)worldPath value:(int64_t)checkValue
|
||||
{
|
||||
NSString *path = [IJMinecraftLevel pathForSessionLockAtIndex:worldIndex];
|
||||
NSString *path = [IJMinecraftLevel pathForSessionLockAtFolder:worldPath];
|
||||
NSData *data = [NSData dataWithContentsOfFile:path];
|
||||
|
||||
|
||||
if (!data)
|
||||
{
|
||||
NSLog(@"Failed to read session lock at %@", path);
|
||||
|
||||
Reference in New Issue
Block a user