Browse Source

stack resources

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

61
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())){
} else{ newStacks.put(item.getItemBaseID(),newStacks.get(item.getItemBaseID()) + item.getNumOfItems());
resources.put(item.getItemBase(),new ArrayList<>()); }else{
newStacks.put(item.getItemBaseID(),item.getNumOfItems());
} }
} }
} }
//create a purge list to remove form inventory after we compile all the stacks together //do stacking here
ArrayList<Item> purgeList = new ArrayList<>(); PlayerCharacter pc = PlayerCharacter.getFromCache(this.getOwner().getObjectUUID());
ArrayList<Item> newStacks = new ArrayList<>(); for(Integer id : newStacks.keySet()){
//now we have all our resources sorted, lets combine them into one single stack for each type ItemBase ib = ItemBase.getItemBase(id);
for(ItemBase resourceBase : resources.keySet()){ Item item = new Item(ib, pc.getObjectUUID(),
int count = 0; Enum.OwnerType.PlayerCharacter, (byte) 0, (byte) 0, (short) ib.getDurability(), (short) ib.getDurability(),
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(),
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); try{
}
//create new stacks in database, add to inventory and update window
for(Item item : newStacks){
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){
} ChatManager.chatSystemError(pc, "Failed To Stack Resources");
} catch (Exception e) {
ChatManager.chatSystemError((PlayerCharacter)this.getOwner(), "Resource Stack Error: Unable to create item. " + e.getMessage());
} }
}
for (Item purged : purgeList){
this.delete(purged);
} }
for(Item item : resources)
this.delete(item);
//this.updateInventory(); this.updateInventory();
} }
} }

Loading…
Cancel
Save