ItemProto.java

jmud-0.11/src/ItemProto.java · 5.2 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.NoSuchElementException;

import string.InvalidFlagException;
import string.StrUtil;
import util.file.InvalidFileFormatException;
import misc.Separators;
import util.bit.Bit;
import util.nice.Niceable;
import util.nice.NiceKey;
import util.log.Log;

class ItemProto implements Sheetable, Niceable {

  int        theId;
  String     theName        = null;
  String     theAliases     = null;
  String     theBrief       = null;
  String     theDescription = null;

  int        theLevel;
  int        theLoad;
  int        theCost;

  int        properties;
  int        wearVector;

  int        numberOfItems = 0;

  static     private int numberOfProtos = 0;

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

    theId = itemId + zoneBase;

    StringTokenizer tok = null;
    try {
      theName = itemFile.readLine();
      
      Log.log("Item: " + theName);
      
      theAliases = StrUtil.loadAliases(itemFile);
      theBrief   = itemFile.readLine();
      
      theDescription = StrUtil.readDescription(itemFile);
      
      tok = new StringTokenizer(itemFile.readLine());
    }
    catch(IOException e) {
      throw new InvalidFileFormatException();
    }

    // level
    if (!tok.hasMoreTokens())
      throw new InvalidFileFormatException();
    theLevel = Integer.parseInt(tok.nextToken());

    // load
    if (!tok.hasMoreTokens())
      throw new InvalidFileFormatException();
    theLoad = Integer.parseInt(tok.nextToken());

    // cost
    if (!tok.hasMoreTokens())
      throw new InvalidFileFormatException();
    theCost = Integer.parseInt(tok.nextToken());

    // properties
    if (!tok.hasMoreTokens())
      throw new InvalidFileFormatException();
    try {
      properties = StrUtil.parseFlags(tok.nextToken());
    }
    catch(InvalidFlagException e) {
      throw new InvalidFileFormatException();
    }

    // wearing
    if (!tok.hasMoreTokens())
      throw new InvalidFileFormatException();
    try {
      wearVector = StrUtil.parseFlags(tok.nextToken());
    }
    catch(InvalidFlagException e) {
      throw new InvalidFileFormatException();
    }

    ++numberOfProtos;
  }

  ////////////
  // Niceable:

  public boolean lessThan(Niceable n) {
    return getId() < ((ItemProto) n).getId();
  }

  public boolean hasKey(NiceKey key) {
    boolean result = false;
    switch(key.getKeyType()) {
    case NiceKey.K_INT:
      result = hasId(key.getIntKey());
      break;
    case NiceKey.K_STR: 
      result = hasName(key.getStrKey());
      break;
    default:
      Log.err("Tipo inválido de chave em ItemProto.hasKey(...): " + key.getKeyType());
    }
    return result;
  }

  public boolean keyLessThan(NiceKey key) {
    boolean result = false;
    switch(key.getKeyType()) {
    case NiceKey.K_INT:
      result = getId() < key.getIntKey();
      break;
    default:
      Log.err("Tipo inválido de chave em ItemProto.keyLessThan(...): " + key.getKeyType());
    }
    return result;
  }

  //
  ////////////

  protected void finalize() {
    --numberOfProtos;
  }

  int getNumberOfItems() {
    return numberOfItems;
  }

  Item create() {
    return new Item(this);
  }

  int getId() {
    return theId;
  }

  boolean hasId(int id) {
    return getId() == id;
  }

  String getName() {
    return theName;
  }

  boolean hasName(String name) {
    return StrUtil.findAlias(name, theAliases);
  }

    int getProtoType() {
	return Item.T_NONE;
    }

  String getItemProtoType() {
    return Item.typeLabels[getProtoType()];
  }

  /////////////
  // Sheetable:

  public String getSheet() {

    return "ITEM - ID: [" + theId + "]  Nome: [" + theName + "]  Tipo: [" + getItemProtoType() + "]" + Separators.NL +
      "Sinônimos: " + theAliases + Separators.NL +
      "Nível: [" + theLevel + "]  Peso: [" + theLoad + "]  Valor: [" + theCost + "]" + Separators.NL +
	"Propriedades: " + StrUtil.makeFlags(properties) + Separators.NL +
      "Posições: " + StrUtil.makeFlags(wearVector) + Separators.NL +
	"Quantidade: [" + numberOfItems + "]";
  }

    // usado por Item.load(...)
    static ItemProto load(int itemId, int zoneBase, BufferedReader itemFile) 
	throws InvalidFileFormatException {

	String itemType = null;
	try {
	    itemType = itemFile.readLine();
	}
	catch(IOException e) {
	    throw new InvalidFileFormatException();
	}
	switch(Integer.parseInt(itemType)) {
	case Item.T_WEAPON:    return new WeaponProto(zoneBase, itemId, itemFile);
	case Item.T_ARMOR:     return new ArmorProto(zoneBase, itemId, itemFile);
	case Item.T_CONTAINER: return new ContainerProto(zoneBase, itemId, itemFile);
	case Item.T_BOARD:     return new BoardItemProto(zoneBase, itemId, itemFile);
	case Item.T_KEY:       return new KeyProto(zoneBase, itemId, itemFile);
	case Item.T_CURRENCY:  return new CurrencyProto(zoneBase, itemId, itemFile);
	default: throw new InvalidFileFormatException();
	}
    }

    //////////////////////////////////////
    // Estes metodos sao identicos em Item

    boolean isVolatile() {
	return Bit.isSet(properties, Item.PM_VOLATILE);
    }

    boolean isStatic() {
	return Bit.isSet(properties, Item.PM_STATIC);
    }

    //
    //////////////////////////////////////

}

Site Stats

Snippets: 1791
Codebases: 571