From 9e85c1e3613e3b10da693c1a57846a309253ddf9 Mon Sep 17 00:00:00 2001 From: FatBoy-DOTC Date: Tue, 14 May 2024 21:55:20 -0500 Subject: [PATCH] stack resources --- src/engine/objects/CharacterItemManager.java | 61 ++++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/src/engine/objects/CharacterItemManager.java b/src/engine/objects/CharacterItemManager.java index fbf554fd..567496e9 100644 --- a/src/engine/objects/CharacterItemManager.java +++ b/src/engine/objects/CharacterItemManager.java @@ -2274,7 +2274,6 @@ public class CharacterItemManager { if (this.absCharacter.getObjectType().equals(GameObjectType.PlayerCharacter) == false) return; - PlayerCharacter pc = (PlayerCharacter) this.absCharacter; UpdateInventoryMsg updateInventoryMsg = new UpdateInventoryMsg(inventory, this.getBank(), this.getGoldInventory(), add); @@ -2312,7 +2311,6 @@ public class CharacterItemManager { * Update the player's inventory window by resending the entire contents. */ public void updateInventory() { - this.StackResources(); this.updateInventory(this.getInventory(), true); } @@ -2617,55 +2615,42 @@ public class CharacterItemManager { } public void StackResources(){ - if(this.getOwner().getObjectType().equals(GameObjectType.PlayerCharacter) == false) + if(!this.getOwner().getObjectType().equals(GameObjectType.PlayerCharacter)) return; - //create hashmap of all resources in inventory sorted by type - HashMap> resources = new HashMap<>(); - //populate resources hashmap with items currently in inventory + ArrayList resources = new ArrayList<>(); + HashMap newStacks = new HashMap<>(); for(Item item : this.getInventory()){ - if(Warehouse.getMaxResources().get(item.getItemBaseID()) != null && Warehouse.getMaxResources().get(item.getItemBaseID()) != 0){ - if(resources.containsKey(item.getItemBase())){ - resources.get(item.getItemBase()).add(item); - } else{ - resources.put(item.getItemBase(),new ArrayList<>()); + if(item.getItemBase().getType().equals(ItemType.RESOURCE)){ + resources.add(item); + if(newStacks.containsKey(item.getItemBaseID())){ + newStacks.put(item.getItemBaseID(),newStacks.get(item.getItemBaseID()) + item.getNumOfItems()); + }else{ + newStacks.put(item.getItemBaseID(),item.getNumOfItems()); } } } - //create a purge list to remove form inventory after we compile all the stacks together - ArrayList purgeList = new ArrayList<>(); - ArrayList newStacks = new ArrayList<>(); - //now we have all our resources sorted, lets combine them into one single stack for each type - for(ItemBase resourceBase : resources.keySet()){ - int count = 0; - for(Item resource : resources.get(resourceBase)){ - count += resource.getNumOfItems(); - purgeList.add(resource); - } - Item item = new Item(resourceBase, this.getOwner().getObjectUUID(), - Enum.OwnerType.PlayerCharacter, (byte) 0, (byte) 0, (short) resourceBase.getDurability(), (short) resourceBase.getDurability(), + //do stacking here + PlayerCharacter pc = PlayerCharacter.getFromCache(this.getOwner().getObjectUUID()); + for(Integer id : newStacks.keySet()){ + ItemBase ib = ItemBase.getItemBase(id); + Item item = new Item(ib, pc.getObjectUUID(), + Enum.OwnerType.PlayerCharacter, (byte) 0, (byte) 0, (short) ib.getDurability(), (short) ib.getDurability(), true, false, Enum.ItemContainerType.INVENTORY, (byte) 0, new ArrayList<>(), ""); - item.setNumOfItems(count); - newStacks.add(item); - } - //create new stacks in database, add to inventory and update window - for(Item item : newStacks){ - try { + item.setNumOfItems(newStacks.get(id)); + try{ item = DbManager.ItemQueries.ADD_ITEM(item); - if(item != null){ - this.addItemToInventory(item); - } - } catch (Exception e) { - ChatManager.chatSystemError((PlayerCharacter)this.getOwner(), "Resource Stack Error: Unable to create item. " + e.getMessage()); + this.addItemToInventory(item); + }catch(Exception e){ + ChatManager.chatSystemError(pc, "Failed To Stack Resources"); } - } - for (Item purged : purgeList){ - this.delete(purged); } + for(Item item : resources) + this.delete(item); - //this.updateInventory(); + this.updateInventory(); } }