diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/InventoryManager.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/scape/InventoryManager.java')
| -rw-r--r-- | NET/worlds/scape/InventoryManager.java | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/NET/worlds/scape/InventoryManager.java b/NET/worlds/scape/InventoryManager.java new file mode 100644 index 0000000..641619a --- /dev/null +++ b/NET/worlds/scape/InventoryManager.java @@ -0,0 +1,315 @@ +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<String, InventoryItem> masterList_; + private Hashtable<String, InventoryItem> inventory_ = new Hashtable<String, InventoryItem>(); + private boolean initialized_; + private Vector<InventoryItem> equipped_; + + public synchronized void setEquippedItems(Vector<InventoryItem> equippedItems) { + this.removeEquippedItems(); + this.equipped_ = equippedItems; + this.equipItems(); + } + + public synchronized Vector<InventoryItem> 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<InventoryItem> getEquippableItems() { + Vector<InventoryItem> retVal = new Vector<InventoryItem>(); + Enumeration<InventoryItem> invEnum = this.inventory_.elements(); + + while (invEnum.hasMoreElements()) { + InventoryItem obj = invEnum.nextElement(); + if (obj instanceof EquippableItem) { + retVal.addElement(obj); + } + } + + return retVal; + } + + public Vector<InventoryItem> getInventoryAvatars() { + Vector<InventoryItem> retVal = new Vector<InventoryItem>(); + Enumeration<InventoryItem> invEnum = this.inventory_.elements(); + + while (invEnum.hasMoreElements()) { + InventoryItem obj = invEnum.nextElement(); + if (obj instanceof InventoryAvatar) { + retVal.addElement(obj); + } + } + + return retVal; + } + + public Hashtable<String, InventoryItem> getInventoryItems() { + return this.inventory_; + } + + public int checkInventoryFor(String nm) { + InventoryItem it = this.inventory_.get(nm); + return it != null ? it.getItemQuantity() : 0; + } + + public Vector<InventoryItem> getInventoryActionList() { + Vector<InventoryItem> retVal = new Vector<InventoryItem>(); + Enumeration<InventoryItem> 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<InventoryItem> 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<String, InventoryItem> newInv = this.parseInventoryString(invString); + this.inventory_ = newInv; + Enumeration<TradeDialog> 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<String, InventoryItem> parseInventoryString(String invString) { + Hashtable<String, InventoryItem> newInventory = new Hashtable<String, InventoryItem>(); + 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<String, InventoryItem>(); + this.initialized_ = false; + this.equipped_ = new Vector<InventoryItem>(); + 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_; + } +} |