Browse Source

Resource Auto Stacking

lakebane-master
FatBoy-DOTC 6 months ago
parent
commit
b8abe94a8c
  1. 14
      src/engine/net/client/ClientMessagePump.java
  2. 75
      src/engine/objects/CharacterItemManager.java
  3. 2
      src/engine/objects/Warehouse.java

14
src/engine/net/client/ClientMessagePump.java

@ -446,6 +446,7 @@ public class ClientMessagePump implements NetMsgHandler {
dispatch = Dispatch.borrow(player, msg); dispatch = Dispatch.borrow(player, msg);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
itemManager.StackResources();
} }
private static void transferItemFromVaultToInventory(TransferItemFromVaultToInventoryMsg msg, ClientConnection origin) { private static void transferItemFromVaultToInventory(TransferItemFromVaultToInventoryMsg msg, ClientConnection origin) {
@ -460,7 +461,7 @@ public class ClientMessagePump implements NetMsgHandler {
return; return;
player.getAccount().transferItemFromVaultToInventory(msg, origin); player.getAccount().transferItemFromVaultToInventory(msg, origin);
player.getCharItemManager().StackResources();
} }
//call this if the transfer fails server side to kick the item back to inventory from vault //call this if the transfer fails server side to kick the item back to inventory from vault
@ -489,7 +490,6 @@ public class ClientMessagePump implements NetMsgHandler {
if (player.getAccount() == null) if (player.getAccount() == null)
return; return;
player.getAccount().transferItemFromInventoryToVault(msg, origin);
} }
@ -507,6 +507,7 @@ public class ClientMessagePump implements NetMsgHandler {
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
ChatManager.chatInfoError(player, "Can't transfer to vault: " + reason); ChatManager.chatInfoError(player, "Can't transfer to vault: " + reason);
player.getAccount().transferItemFromInventoryToVault(msg, origin);
} }
private static void transferGoldFromVaultToInventory(TransferGoldFromVaultToInventoryMsg msg, ClientConnection origin) { private static void transferGoldFromVaultToInventory(TransferGoldFromVaultToInventoryMsg msg, ClientConnection origin) {
@ -992,6 +993,11 @@ public class ClientMessagePump implements NetMsgHandler {
String text = player.getFirstName() + " has looted " + name + '.'; String text = player.getFirstName() + " has looted " + name + '.';
ChatManager.chatGroupInfoCanSee(player, text); ChatManager.chatGroupInfoCanSee(player, text);
} }
if(item.getItemBase().getType().equals(ItemType.RESOURCE)){
player.getCharItemManager().StackResources();
}
} catch (Exception e) { } catch (Exception e) {
Logger.info(e.getMessage()); Logger.info(e.getMessage());
} finally { } finally {
@ -1608,7 +1614,9 @@ public class ClientMessagePump implements NetMsgHandler {
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
itemMan.updateInventory(); itemMan.updateInventory();
} }
if(buy.getItemBase().getType().equals(ItemType.RESOURCE)){
itemMan.StackResources();
}
} finally { } finally {
origin.buyLock.unlock(); origin.buyLock.unlock();
} }

75
src/engine/objects/CharacterItemManager.java

@ -12,6 +12,7 @@ package engine.objects;
import engine.Enum; import engine.Enum;
import engine.Enum.GameObjectType; import engine.Enum.GameObjectType;
import engine.Enum.ItemType; import engine.Enum.ItemType;
import engine.devcmd.cmds.MakeItemCmd;
import engine.gameManager.*; import engine.gameManager.*;
import engine.math.Vector3fImmutable; import engine.math.Vector3fImmutable;
import engine.net.Dispatch; import engine.net.Dispatch;
@ -23,10 +24,7 @@ import engine.powers.poweractions.AbstractPowerAction;
import engine.server.MBServerStatics; import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -778,6 +776,8 @@ public class CharacterItemManager {
this.commitTrade(); this.commitTrade();
this.closeTradeWindow(ctwm1, false); this.closeTradeWindow(ctwm1, false);
other.getCharItemManager().closeTradeWindow(ctwm2, false); other.getCharItemManager().closeTradeWindow(ctwm2, false);
other.getCharItemManager().StackResources();
this.StackResources();
} }
return true; return true;
} }
@ -1254,18 +1254,12 @@ public class CharacterItemManager {
} }
return false; return false;
} }
} //else if (i.getItemBase().getType().equals(ItemType.RESOURCE)) { }
// if(this.inventoryContainsResource(i)){
// Item resource = getResource(i);
// if(resource != null){
// resource.setNumOfItems(resource.getNumOfItems() + i.getNumOfItems());
// return true;
// }
//}
// }
this.inventory.add(i); this.inventory.add(i);
this.itemIDtoType.put(i.getObjectUUID(), i.getObjectType().ordinal()); this.itemIDtoType.put(i.getObjectUUID(), i.getObjectType().ordinal());
if(i.getItemBase().getType().equals(ItemType.RESOURCE)){
this.StackResources();
}
return true; return true;
} }
@ -2626,4 +2620,57 @@ public class CharacterItemManager {
} }
public void StackResources(){
if(this.getOwner().getObjectType().equals(GameObjectType.PlayerCharacter) == false)
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
for(Item item : this.getInventory()){
if(Warehouse.getMaxResources().get(item.getItemBaseID()) != null && Warehouse.getMaxResources().get(item.getItemBaseID()) != 0){
if(resources.containsKey(item.getItemBaseID())){
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(),
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 = DbManager.ItemQueries.ADD_ITEM(item);
if(item != null){
this.addItemToInventory(item);
this.updateInventory();
}
} catch (Exception e) {
ChatManager.chatSystemError((PlayerCharacter)this.getOwner(), "Resource Stack Error: Unable to create item. " + e.getMessage());
}
}
for (Item purged : purgeList){
this.delete(purged);
}
this.updateInventory();
}
} }

2
src/engine/objects/Warehouse.java

@ -217,6 +217,8 @@ public class Warehouse extends AbstractWorldObject {
vrm.configure(); vrm.configure();
dispatch = Dispatch.borrow(player, vrm); dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
player.getCharItemManager().StackResources();
} }
public static void warehouseLock(MerchantMsg msg, PlayerCharacter player, NPC npc, ClientConnection origin) { public static void warehouseLock(MerchantMsg msg, PlayerCharacter player, NPC npc, ClientConnection origin) {

Loading…
Cancel
Save