KeyProto.java

jmud-0.11/src/KeyProto.java · 2.1 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Enumeration;

import util.list.Vector;
import util.file.InvalidFileFormatException;
import util.log.Log;
import misc.Separators;
import integer.IntPair;
import string.StrUtil;


class KeyProto extends ItemProto {

    Vector doorReferences = new Vector();

    // salva base da zona para permitir salvar os identificadores
    // de porta corretos (obtidos via subtracao deste valor)
    int savedZoneBase;  

    KeyProto(int zoneBase, int itemId, BufferedReader itemFile)
	throws InvalidFileFormatException {

	super(zoneBase, itemId, itemFile);
	Log.debug("Chave: " + getName());

	savedZoneBase = zoneBase;

	try {
	    for (; ; ) {
		if (!itemFile.ready())
		    throw new InvalidFileFormatException();
		String line = itemFile.readLine();
		if (line.startsWith(Separators.EOR))
		    break;
		StringTokenizer toker = new StringTokenizer(line);
		if (!toker.hasMoreTokens())
		    throw new InvalidFileFormatException();
		int room = Integer.parseInt(toker.nextToken());
		if (!toker.hasMoreTokens())
		    throw new InvalidFileFormatException();
		int dir  = Door.getDirectionByName(toker.nextToken());
		doorReferences.insert(new IntPair(room + zoneBase, dir));
	    }
	    doorReferences.trim();
	}
	catch(IOException e) {
	    throw new InvalidFileFormatException();
	}
	catch(NumberFormatException e) {
	    throw new InvalidFileFormatException();
	}
    }

    protected void finalize() {
	super.finalize();
    }

    int getProtoType() {
	return Item.T_KEY;
    }

    Item create() {
	return new Key(this);
    }
    
    /////////////
    // Sheetable:

    private String listDoors() {
	String list = "";
	for (Enumeration enum = doorReferences.elements(); enum.hasMoreElements(); ) {
	    IntPair pair = (IntPair) enum.nextElement();
	    list += Separators.NL + (pair.getValue1() - savedZoneBase) + " " + Door.getDirectionName(pair.getValue2());
	}
	return list;
    }

    public String getSheet() {
	return super.getSheet() + Separators.NL + "Portas:" + StrUtil.wrapList(listDoors(), "Nenhuma");
    }

    //
    /////////////
}

Site Stats

Snippets: 1791
Codebases: 571