Files
prestonbane/src/engine/net/client/handlers/TransferGoldToFromBuildingMsgHandler.java
T

115 lines
3.5 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
package engine.net.client.handlers;
import engine.gameManager.BuildingManager;
import engine.gameManager.DispatchManager;
2022-04-30 09:41:17 -04:00
import engine.gameManager.SessionManager;
2024-05-12 11:55:12 -04:00
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
2022-04-30 09:41:17 -04:00
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.PlaceAssetMsg;
import engine.net.client.msg.TransferGoldToFromBuildingMsg;
import engine.net.client.msg.UpdateGoldMsg;
2024-03-24 09:42:27 -04:00
import engine.objects.Building;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
import engine.objects.PlayerCharacter;
2022-04-30 09:41:17 -04:00
import org.pmw.tinylog.Logger;
/*
* @Author:
* @Summary: Processes application protocol message which transfers
* gold between a building's strongbox and a player character.
*/
public class TransferGoldToFromBuildingMsgHandler extends AbstractClientMsgHandler {
2023-07-15 09:23:48 -04:00
public TransferGoldToFromBuildingMsgHandler() {
2024-05-12 11:55:12 -04:00
super();
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
2024-05-12 13:42:11 -04:00
protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PlayerCharacter player;
Building building;
CharacterItemManager itemMan;
Item goldItem;
TransferGoldToFromBuildingMsg msg;
Dispatch dispatch;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
player = SessionManager.getPlayerCharacter(origin);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (player == null)
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
msg = (TransferGoldToFromBuildingMsg) baseMsg;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
building = BuildingManager.getBuildingFromCache(msg.getObjectID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (building == null)
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (msg.getDirection() == 2) {
2022-04-30 09:41:17 -04:00
if (!BuildingManager.playerCanManage(player, building))
2023-07-15 09:23:48 -04:00
return true;
if (building.setReserve(msg.getUnknown01(), player)) {
dispatch = Dispatch.borrow(player, msg);
DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return true;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// if (building.getTimeStamp(MBServerStatics.STRONGBOX_DELAY_STRING) > System.currentTimeMillis()){
// ErrorPopupMsg.sendErrorMsg(player, MBServerStatics.STRONGBOX_DELAY_OUTPUT);
// return true;
// }
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//building.getTimestamps().put(MBServerStatics.STRONGBOX_DELAY_STRING, System.currentTimeMillis() + MBServerStatics.ONE_MINUTE);
2022-04-30 09:41:17 -04:00
2024-03-18 10:01:29 -04:00
itemMan = player.charItemManager;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
goldItem = itemMan.getGoldInventory();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (goldItem == null) {
Logger.error("Could not access gold item");
return true;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Update in-game gold values for player and building
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (!itemMan.transferGoldToFromBuilding(msg.getAmount(), building))
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
UpdateGoldMsg ugm = new UpdateGoldMsg(player);
ugm.configure();
dispatch = Dispatch.borrow(player, ugm);
DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Refresh the player's inventory if it's currently open
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Refresh the tree's window to update strongbox
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
msg.setAmount(building.getStrongboxValue());
dispatch = Dispatch.borrow(player, msg);
DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (Exception e) {
PlaceAssetMsg.sendPlaceAssetError(player.getClientConnection(), 1, "A Serious error has occurred. Please post details for to ensure transaction integrity");
}
return true;
}
2022-04-30 09:41:17 -04:00
}