package NET.worlds.scape; import NET.worlds.console.ActionsPart; import NET.worlds.console.Console; import NET.worlds.console.TradeDialog; import NET.worlds.console.WhisperManager; import NET.worlds.core.ServerTableManager; import NET.worlds.network.URL; import java.net.MalformedURLException; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; public class InventoryManager { private static InventoryManager manager_; private Hashtable masterList_; private Hashtable inventory_ = new Hashtable(); private boolean initialized_; private Vector equipped_; public synchronized void setEquippedItems(Vector equippedItems) { this.removeEquippedItems(); this.equipped_ = equippedItems; this.equipItems(); } public synchronized Vector getEquippedItems() { return this.equipped_; } private synchronized void removeEquippedItems() { for (int i = 0; i < this.equipped_.size(); i++) { EquippableItem itemToEquip = (EquippableItem)this.equipped_.elementAt(i); Shape ownedShape = itemToEquip.getOwnedShape(); if (ownedShape != null) { ownedShape.detach(); } itemToEquip.setOwnedShape(null); } } private synchronized void equipItems() { System.out.println("Equipped Items Size: " + this.equipped_.size()); for (int i = 0; i < this.equipped_.size(); i++) { Shape itemShape = new Shape(); EquippableItem itemToEquip = (EquippableItem)this.equipped_.elementAt(i); if (itemToEquip != null) { try { itemShape.setURL(new URL(itemToEquip.getModelLocation())); } catch (MalformedURLException var10) { System.out.println("Badly formed URL for " + itemToEquip.getItemName()); continue; } float s = itemToEquip.getScale(); itemShape.scale(s, s, s); itemShape.pitch(itemToEquip.getPitch()); itemShape.roll(itemToEquip.getRoll()); itemShape.yaw(itemToEquip.getYaw()); itemShape.moveBy(itemToEquip.getXPos(), itemToEquip.getYPos(), itemToEquip.getZPos()); int properLoc = itemToEquip.getBodyLocation(); DeepEnumeration de = new DeepEnumeration(); Pilot.getActive().getChildren(de); while (de.hasMoreElements()) { Object obj = de.nextElement(); if (obj instanceof Shape) { Shape objShape = (Shape)obj; int partType = objShape.getBodPartNum(); if (partType == properLoc) { objShape.add(itemShape); itemToEquip.setOwnedShape(itemShape); break; } } } } } } public Vector getEquippableItems() { Vector retVal = new Vector(); Enumeration invEnum = this.inventory_.elements(); while (invEnum.hasMoreElements()) { InventoryItem obj = invEnum.nextElement(); if (obj instanceof EquippableItem) { retVal.addElement(obj); } } return retVal; } public Vector getInventoryAvatars() { Vector retVal = new Vector(); Enumeration invEnum = this.inventory_.elements(); while (invEnum.hasMoreElements()) { InventoryItem obj = invEnum.nextElement(); if (obj instanceof InventoryAvatar) { retVal.addElement(obj); } } return retVal; } public Hashtable getInventoryItems() { return this.inventory_; } public int checkInventoryFor(String nm) { InventoryItem it = this.inventory_.get(nm); return it != null ? it.getItemQuantity() : 0; } public Vector getInventoryActionList() { Vector retVal = new Vector(); Enumeration invEnum = this.inventory_.elements(); while (invEnum.hasMoreElements()) { InventoryItem obj = invEnum.nextElement(); if (obj instanceof InventoryAction) { retVal.addElement(obj); } } return retVal; } public void doInventoryAction(String act) { Vector actVector = this.getInventoryActionList(); for (int i = 0; i < actVector.size(); i++) { InventoryAction invAct = (InventoryAction)actVector.elementAt(i); if (invAct.getItemName().equalsIgnoreCase(act)) { invAct.doAction(); String msg = "&|+deal>trade " + invAct.getItemId() + ","; TradeDialog.sendTradeMessage(msg); } } } public void setInventory(String invString) { this.initialized_ = true; Hashtable newInv = this.parseInventoryString(invString); this.inventory_ = newInv; Enumeration e = WhisperManager.whisperManager().tradeDialogs().elements(); while (e.hasMoreElements()) { TradeDialog wd = e.nextElement(); wd.setTrading(true); } if (Console.getActive() != null) { Console a = Console.getActive(); a.inventoryChanged(); if (a.targetValid != a.isValidAv()) { a.resetAvatar(); } } ActionsPart.updateActionDialog(); } public Hashtable parseInventoryString(String invString) { Hashtable newInventory = new Hashtable(); if (invString == null) { return newInventory; } else { int len = invString.length(); int itemStart = 0; while (itemStart < len) { char ch = invString.charAt(itemStart); if (ch < 'A' || ch > 'Z') { System.out.println("Bad inventory: " + invString); return newInventory; } int descLen; for (descLen = 1; itemStart + descLen < len; descLen++) { ch = invString.charAt(itemStart + descLen); if (ch < 'a' || ch > 'z') { break; } } String shortName = invString.substring(itemStart, itemStart + descLen); int countStart = itemStart + descLen; int countLen = 0; while (true) { if (countLen + countStart < len) { ch = invString.charAt(countStart + countLen); if (ch >= '0' && ch <= '9') { countLen++; continue; } } int count = 1; if (countLen > 0) { count = Integer.parseInt(invString.substring(countStart, countStart + countLen)); } InventoryItem foundItem = this.masterList_.get(shortName); if (foundItem != null) { InventoryItem newItem = foundItem.cloneItem(); newItem.setQuantity(count); newInventory.put(shortName, newItem); } itemStart = countStart + countLen; break; } } return newInventory; } } public String properCase(String s) { return s.equals("") ? s : s.substring(0, 1).toUpperCase() + s.substring(1); } public String getSingular(String sn) { InventoryItem item = this.masterList_.get(sn); return item != null ? item.getItemName() : "unknown" + sn; } public String getPlural(String sn) { return this.getSingular(sn) + "s"; } public String itemName(String sn, int num) { return num == 1 ? "a " + this.getSingular(sn) : num + " " + this.getPlural(sn); } public String itemName(InventoryItem item) { return this.itemName(item.getItemId(), item.getItemQuantity()); } private InventoryManager() { this.masterList_ = new Hashtable(); this.initialized_ = false; this.equipped_ = new Vector(); ServerTableManager stm = ServerTableManager.instance(); int invVersion = stm.getFileVersion(); String[] invStrings = stm.getTable("invList"); String[] graphicStrings = new String[0]; if (invVersion > 1) { graphicStrings = stm.getTable("graphicList"); } URL defaultImageURL = URL.make("home:..\\default.gif"); if (invStrings != null) { int numStringsPerItem = 12; for (int i = 0; i < invStrings.length; i += numStringsPerItem) { String id = invStrings[i]; String name = invStrings[i + 2]; String model = invStrings[i + 3]; int loc = Double.valueOf(invStrings[i + 4]).intValue(); float scale = Double.valueOf(invStrings[i + 5]).floatValue(); int pitch = Double.valueOf(invStrings[i + 6]).intValue(); int roll = Double.valueOf(invStrings[i + 7]).intValue(); int yaw = Double.valueOf(invStrings[i + 8]).intValue(); float xPos = Double.valueOf(invStrings[i + 9]).floatValue(); float yPos = Double.valueOf(invStrings[i + 10]).floatValue(); float zPos = Double.valueOf(invStrings[i + 11]).floatValue(); URL graphic = null; if (invVersion > 1 && graphicStrings.length > i / 6 + 1) { String gString = graphicStrings[i / 6 + 1]; if (gString != "default") { graphic = URL.make(gString); } } if (graphic == null) { graphic = defaultImageURL; } InventoryItem newItem; if (id.charAt(0) == 'H') { newItem = InventoryAction.createAction(id, name, 1); } else if (id.charAt(0) == 'W') { newItem = new EquippableItem(id, name, 1, model, scale, loc, xPos, yPos, zPos, pitch, roll, yaw); } else if (id.charAt(0) == 'V') { newItem = new InventoryAvatar(id, name, 1); } else { newItem = new InventoryItem(id, name, 1); } newItem.setItemGraphicLocation(graphic); this.masterList_.put(id, newItem); } } } public static InventoryManager getInventoryManager() { if (manager_ == null) { manager_ = new InventoryManager(); } return manager_; } public boolean inventoryInitialized() { return this.initialized_; } }