www.pudn.com > 20063622273380188.rar > Restore.java


import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
 
import javax.microedition.rms.InvalidRecordIDException; 
import javax.microedition.rms.RecordStore; 
import javax.microedition.rms.RecordStoreException; 
import javax.microedition.rms.RecordStoreFullException; 
import javax.microedition.rms.RecordStoreNotFoundException; 
import javax.microedition.rms.RecordStoreNotOpenException; 
 
class Restore 
{ 
	private RecordStore rs; 
	private Map map; 
	private ItemManager iMgr; 
	private Creature main; 
	private boolean used[]; 
	private String name[]; 
	private int level[]; 
	private int onRecord; 
	private ByteArrayOutputStream bos; 
	private DataOutputStream dos; 
	private ByteArrayInputStream bis; 
	private DataInputStream dis; 
	private byte data[]; 
	 
	public Restore() 
	{ 
		open(); 
		refresh(); 
		close(); 
	} 
	 
	void open() 
	{ 
		try { 
			rs=RecordStore.openRecordStore("RpgGame",true); 
		} catch (RecordStoreFullException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreNotFoundException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreException e) { 
			e.printStackTrace(); 
		}	 
	} 
	 
	private void refresh() 
	{ 
		used=new boolean[3]; 
		name=new String[3]; 
		level=new int[3]; 
		try { 
			if(rs.getNumRecords()==0) 
			{ 
				bos=new ByteArrayOutputStream(); 
				dos=new DataOutputStream(bos); 
				dos.writeBoolean(false); 
				data=bos.toByteArray(); 
				for(int i=0;i<3;i++) 
				{ 
					rs.addRecord(data,0,data.length); 
					used[i]=false; 
				} 
				bos.close(); 
				dos.close(); 
			} 
			else 
			{ 
				for(int i=1;i<=3;i++) 
				{ 
					data=rs.getRecord(i); 
					bis=new ByteArrayInputStream(data); 
					dis=new DataInputStream(bis); 
					used[i-1]=dis.readBoolean(); 
					if(used[i-1]) 
					{ 
						name[i-1]=dis.readUTF(); 
						level[i-1]=dis.readInt(); 
					} 
					bis.close(); 
					dis.close(); 
				} 
			} 
		} catch (RecordStoreNotOpenException e1) { 
			e1.printStackTrace(); 
		} catch (RecordStoreFullException e1) { 
			e1.printStackTrace(); 
		} catch (InvalidRecordIDException e1) { 
			e1.printStackTrace(); 
		} catch (RecordStoreException e1) { 
			e1.printStackTrace(); 
		} catch (IOException e1) { 
			e1.printStackTrace(); 
		}  
		 
	} 
	 
	boolean[] getRecordUsed() 
	{ 
		return used; 
	} 
	 
	String[] getName() 
	{ 
		return name; 
	} 
	 
	int[] getLevel() 
	{ 
		return level; 
	} 
	 
	 
	void load(int recordID) 
	{ 
		open(); 
		try { 
			data=rs.getRecord(recordID); 
		} catch (RecordStoreNotOpenException e) { 
			e.printStackTrace(); 
		} catch (InvalidRecordIDException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreException e) { 
			e.printStackTrace(); 
		} 
		bis=new ByteArrayInputStream(data); 
		dis=new DataInputStream(bis); 
		try { 
			boolean used=dis.readBoolean(); 
			String name=dis.readUTF(); 
			int level=dis.readInt(); 
			int exp=dis.readInt(); 
			int hp=dis.readInt(); 
			int mp=dis.readInt(); 
			Item equip[]=new Item[4]; 
			for(int i=0;i<4;i++) 
				equip[i]=new Item(dis.readInt()); 
			int x=dis.readInt(); 
			int y=dis.readInt(); 
			int mapId=dis.readInt(); 
			int mapStates[]=new int[4]; 
			for(int i=0;i<4;i++) 
				mapStates[i]=dis.readInt(); 
			Item items[]=new Item[16]; 
			for(int i=0;i<16;i++) 
				items[i]=new Item(dis.readInt()); 
			int money=dis.readInt(); 
			int mission=dis.readInt(); 
			dis.close(); 
			bis.close(); 
			iMgr=new ItemManager(money,items); 
			map=new Map(mapId,mapStates[mapId],x,y); 
			main=new Creature(name,level,exp,hp,mp,mapStates,equip,mission); 
		} catch (IOException e1) { 
			e1.printStackTrace(); 
		} 
		onRecord=recordID; 
		close(); 
	} 
	 
	void save(Creature main,Map map,ItemManager iMgr) 
	{ 
		open(); 
		bos=new ByteArrayOutputStream(); 
		dos=new DataOutputStream(bos); 
		try { 
			dos.writeBoolean(true); 
			dos.writeUTF(main.name); 
			dos.writeInt(main.level);//level 
			dos.writeInt(main.exp);//exp 
			dos.writeInt(main.hp);//hp 
			dos.writeInt(main.mp);//mp 
			for(int i=0;i<4;i++) 
				dos.writeInt(main.items[i].getID());//item1 
			dos.writeInt(map.getMe().getX());//x 
			dos.writeInt(map.getMe().getY());//y 
			dos.writeInt(map.getMapId());//mapId 
			for(int i=0;i<4;i++) 
				dos.writeInt(main.mapState[i]);//mapState 
			for(int i=0;i<16;i++) 
				dos.writeInt(iMgr.getItem(i).getID());//items 
			dos.writeInt(iMgr.getMoney());//money 
			dos.writeInt(main.mission);//mission 
			data=bos.toByteArray(); 
			bos.close(); 
			dos.close(); 
		} catch (IOException e1) { 
			e1.printStackTrace(); 
		} 
		try { 
			rs.setRecord(onRecord,data,0,data.length); 
		} catch (RecordStoreNotOpenException e) { 
			e.printStackTrace(); 
		} catch (InvalidRecordIDException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreFullException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreException e) { 
			e.printStackTrace(); 
		} 
		refresh(); 
		close(); 
	} 
	 
	void init(int recordID,String name) 
	{ 
		open(); 
		bos=new ByteArrayOutputStream(); 
		dos=new DataOutputStream(bos); 
		try { 
			dos.writeBoolean(true); 
			dos.writeUTF(name); 
			dos.writeInt(0);//level 
			dos.writeInt(0);//exp 
			dos.writeInt(60);//hp 
			dos.writeInt(10);//mp 
			dos.writeInt(0);//item1 
			dos.writeInt(0);//item2 
			dos.writeInt(0);//item3 
			dos.writeInt(0);//item4 
			dos.writeInt(64);//x 
			dos.writeInt(97);//y 
			dos.writeInt(0);//mapId 
			for(int i=0;i<4;i++) 
				dos.writeInt(0);//mapState 
			dos.writeInt(18); 
			dos.writeInt(28); 
			dos.writeInt(38); 
			dos.writeInt(48); 
			dos.writeInt(1); 
			for(int i=0;i<11;i++) 
				dos.writeInt(0);//items 
			dos.writeInt(0);//money 
			dos.writeInt(0);//mission 
			data=bos.toByteArray(); 
			dos.close(); 
			bos.close(); 
		} catch (IOException e1) { 
			e1.printStackTrace(); 
		} 
		try { 
			rs.setRecord(recordID,data,0,data.length); 
		} catch (RecordStoreNotOpenException e) { 
			e.printStackTrace(); 
		} catch (InvalidRecordIDException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreFullException e) { 
			e.printStackTrace(); 
		} catch (RecordStoreException e) { 
			e.printStackTrace(); 
		} 
		Item items[]=new Item[16]; 
		for(int i=0;i