Browse Source

stack resources

lakebane-master
FatBoy-DOTC 6 months ago
parent
commit
9e85c1e361
  1. 53
      src/engine/objects/CharacterItemManager.java

53
src/engine/objects/CharacterItemManager.java

@ -2274,7 +2274,6 @@ public class CharacterItemManager {
if (this.absCharacter.getObjectType().equals(GameObjectType.PlayerCharacter) == false) if (this.absCharacter.getObjectType().equals(GameObjectType.PlayerCharacter) == false)
return; return;
PlayerCharacter pc = (PlayerCharacter) this.absCharacter; PlayerCharacter pc = (PlayerCharacter) this.absCharacter;
UpdateInventoryMsg updateInventoryMsg = new UpdateInventoryMsg(inventory, this.getBank(), this.getGoldInventory(), add); 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. * Update the player's inventory window by resending the entire contents.
*/ */
public void updateInventory() { public void updateInventory() {
this.StackResources();
this.updateInventory(this.getInventory(), true); this.updateInventory(this.getInventory(), true);
} }
@ -2617,55 +2615,42 @@ public class CharacterItemManager {
} }
public void StackResources(){ public void StackResources(){
if(this.getOwner().getObjectType().equals(GameObjectType.PlayerCharacter) == false) if(!this.getOwner().getObjectType().equals(GameObjectType.PlayerCharacter))
return; return;
//create hashmap of all resources in inventory sorted by type ArrayList<Item> resources = new ArrayList<>();
HashMap<ItemBase,ArrayList<Item>> resources = new HashMap<>(); HashMap<Integer,Integer> newStacks = new HashMap<>();
//populate resources hashmap with items currently in inventory
for(Item item : this.getInventory()){ for(Item item : this.getInventory()){
if(Warehouse.getMaxResources().get(item.getItemBaseID()) != null && Warehouse.getMaxResources().get(item.getItemBaseID()) != 0){ if(item.getItemBase().getType().equals(ItemType.RESOURCE)){
if(resources.containsKey(item.getItemBase())){ resources.add(item);
resources.get(item.getItemBase()).add(item); if(newStacks.containsKey(item.getItemBaseID())){
newStacks.put(item.getItemBaseID(),newStacks.get(item.getItemBaseID()) + item.getNumOfItems());
}else{ }else{
resources.put(item.getItemBase(),new ArrayList<>()); newStacks.put(item.getItemBaseID(),item.getNumOfItems());
}
} }
} }
//create a purge list to remove form inventory after we compile all the stacks together
ArrayList<Item> purgeList = new ArrayList<>();
ArrayList<Item> 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(), //do stacking here
Enum.OwnerType.PlayerCharacter, (byte) 0, (byte) 0, (short) resourceBase.getDurability(), (short) resourceBase.getDurability(), 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, true, false, Enum.ItemContainerType.INVENTORY, (byte) 0,
new ArrayList<>(), ""); new ArrayList<>(), "");
item.setNumOfItems(count); item.setNumOfItems(newStacks.get(id));
newStacks.add(item);
}
//create new stacks in database, add to inventory and update window
for(Item item : newStacks){
try{ try{
item = DbManager.ItemQueries.ADD_ITEM(item); item = DbManager.ItemQueries.ADD_ITEM(item);
if(item != null){
this.addItemToInventory(item); this.addItemToInventory(item);
}
}catch(Exception e){ }catch(Exception e){
ChatManager.chatSystemError((PlayerCharacter)this.getOwner(), "Resource Stack Error: Unable to create item. " + e.getMessage()); 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();
} }
} }

Loading…
Cancel
Save