Browse Source

stack resources

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

65
src/engine/objects/CharacterItemManager.java

@ -2274,7 +2274,6 @@ public class CharacterItemManager { @@ -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 { @@ -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 { @@ -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<ItemBase,ArrayList<Item>> resources = new HashMap<>();
//populate resources hashmap with items currently in inventory
ArrayList<Item> resources = new ArrayList<>();
HashMap<Integer,Integer> 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<>());
}
}
}
//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(),
Enum.OwnerType.PlayerCharacter, (byte) 0, (byte) 0, (short) resourceBase.getDurability(), (short) resourceBase.getDurability(),
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());
}
}
}
//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());
}
}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();
}
}

Loading…
Cancel
Save