RuneHive-Game
Loading...
Searching...
No Matches
BankVault.java
Go to the documentation of this file.
1package com.runehive.game.world.items.containers.bank;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.entity.mob.player.PlayerRight;
5import com.runehive.game.world.items.Item;
6import com.runehive.net.packet.out.SendItemOnInterface;
7import com.runehive.net.packet.out.SendMessage;
8import com.runehive.net.packet.out.SendString;
9import com.runehive.util.MessageColor;
10import com.runehive.util.Utility;
11
12import static com.runehive.game.world.items.containers.bank.VaultCurrency.BLOOD_MONEY;
13import static com.runehive.game.world.items.containers.bank.VaultCurrency.COINS;
14
15/**
16 * Handles the bank vault system.
17 *
18 * @author Daniel
19 * @since 12-1-2017.
20 */
21public class BankVault {
22
23 /** The player instance. */
24 private final Player player;
25
26 public long coinsContainer;
28
29
30 /** Constructs a new <code>BankVault</code>. */
32 this.player = player;
33 }
34
35 public void open() {
36 long coins = getContainer(COINS);
37 long bloodMoney = getContainer(BLOOD_MONEY);
38 player.send(new SendString(Utility.formatDigits(coins), 56311));
39 player.send(new SendString(Utility.formatDigits(bloodMoney), 56312));
40 player.send(new SendItemOnInterface(56314, new Item(995, coins), new Item(13307, bloodMoney)));
41 player.interfaceManager.open(56300);
42 }
43
44 private long getContainer(VaultCurrency currency) {
45 return currency == COINS ? coinsContainer : bloodMoneyContainer;
46 }
47
48 /** Sends message to player about their container value. */
49 public void value(VaultCurrency currency) {
50 long container = getContainer(currency);
51 player.send(new SendMessage("You currently have " + Utility.formatDigits(container) + " " + currency.name + " stored inside your bank vault.", MessageColor.DARK_RED));
52 }
53
54 /** Checks if player's bank container contains a certain amount. */
55 public boolean contains(VaultCurrency currency, int amount) {
56 return getContainer(currency) >= amount;
57 }
58
59 /** Adds an amount into the player's bank vault with no checks. Careful how you use this! */
60 public boolean add(VaultCurrency currency, long amount) {
61 return add(currency, amount, false);
62 }
63
64 /** Adds an amount into the player's bank vault with no checks. Careful how you use this! */
65 public boolean add(VaultCurrency currency, long amount, boolean message) {
66 long container = getContainer(currency);
67
68 if (player.right == PlayerRight.ULTIMATE_IRONMAN) {
69 player.inventory.addOrDrop(new Item(currency.id, (int) amount));
70 return true;
71 }
72
73 if (container + amount >= Long.MAX_VALUE) {
74 return false;
75 }
76
77 if (currency == COINS) {
78 coinsContainer += amount;
79 } else if (currency == BLOOD_MONEY) {
80 bloodMoneyContainer += amount;
81 }
82
83 if (message) {
84 player.send(new SendMessage(Utility.formatDigits(amount) + " " + currency.name + " have been added into your bank vault.", MessageColor.DARK_BLUE));
85 }
86 return true;
87 }
88
89 /** Removes a certain amount from the player's bank vault container. */
90 public void remove(VaultCurrency currency, int amount) {
91 if (currency == COINS) {
92 coinsContainer -= amount;
93 } else if (currency == BLOOD_MONEY) {
94 bloodMoneyContainer -= amount;
95 }
96 }
97
98 /** Deposits an an amount into the player's bank vaut. */
99 public void deposit(VaultCurrency currency, int amount) {
100 long container = getContainer(currency);
101
102 if (Long.MAX_VALUE == container) {
103 player.send(new SendMessage("Your vault is currently full and can no longer hold any more " + currency.name + "!", MessageColor.RED));
104 return;
105 }
106
107 if (Long.MAX_VALUE - container < amount) {
108 amount = (int) (Long.MAX_VALUE - container);
109 }
110
111 int invAmount = player.inventory.computeAmountForId(currency.id);
112 int bankAmount = 0;
113
114 if (invAmount < amount) {
115 bankAmount = player.bank.computeAmountForId(currency.id);
116 if (invAmount + bankAmount <= 0) {
117 player.send(new SendMessage("You don't have any " + currency.name + " you noob!", MessageColor.RED));
118 return;
119 }
120 }
121
122 if (invAmount > amount) {
123 invAmount = amount;
124 }
125
126 if (bankAmount > amount - invAmount) {
127 bankAmount = amount - invAmount;
128 }
129
130 amount = invAmount + bankAmount;
131
132 if (invAmount > 0)
133 player.inventory.remove(currency.id, invAmount);
134 if (bankAmount > 0)
135 player.bank.remove(currency.id, bankAmount);
136
137 add(currency, amount);
138 player.bank.refresh();
139 player.send(new SendMessage("You now have " + Utility.formatDigits(container) + " " + currency.name + " stored inside your bank vault.", MessageColor.DARK_RED));
140
141 if (player.interfaceManager.isInterfaceOpen(56300)) {
142 long coins = getContainer(COINS);
143 long bloodMoney = getContainer(BLOOD_MONEY);
144 player.send(new SendString(Utility.formatDigits(coins), 56311));
145 player.send(new SendString(Utility.formatDigits(bloodMoney), 56312));
146 player.send(new SendItemOnInterface(56314, new Item(995, coins), new Item(13307, bloodMoney)));
147 }
148 }
149
150 /** Withdraws a certain amount into the player's bank vault container. */
151 public void withdraw(VaultCurrency currency, long amount) {
152 long container = getContainer(currency);
153 if (amount < 0) {
154 return;
155 }
156 if (container == 0) {
157 player.send(new SendMessage("Your bank vault is currently empty.", MessageColor.RED));
158 return;
159 }
160 if (amount > Integer.MAX_VALUE) {
161 player.send(new SendMessage("You can not hold more than " + Utility.formatPrice(Integer.MAX_VALUE) + " " + currency.name + " at a time.", MessageColor.RED));
162 return;
163 }
164 if (amount > container) {
165 amount = container;
166 }
167 int contain = player.inventory.computeAmountForId(currency.id);
168 if (contain + amount > Integer.MAX_VALUE) {
169 amount = contain - amount;
170 }
171 if (player.inventory.add(new Item(currency.id, (int) amount))) {
172 remove(currency, (int) amount);
173 player.send(new SendMessage("You have successfully withdrawn " + Utility.formatDigits(amount) + " " + currency.name + "."));
174 player.bank.refresh();
175 }
176 if (player.interfaceManager.isInterfaceOpen(56300)) {
177 long coins = getContainer(COINS);
178 long bloodMoney = getContainer(BLOOD_MONEY);
179 player.send(new SendString(Utility.formatDigits(coins), 56311));
180 player.send(new SendString(Utility.formatDigits(bloodMoney), 56312));
181 player.send(new SendItemOnInterface(56314, new Item(995, (int) coins), new Item(13307, (int) bloodMoney)));
182 }
183 }
184}
This class represents a character controlled by a player.
Definition Player.java:125
The container class that represents an item that can be interacted with.
Definition Item.java:21
BankVault(Player player)
Constructs a new BankVault.
void deposit(VaultCurrency currency, int amount)
Deposits an an amount into the player's bank vaut.
void withdraw(VaultCurrency currency, long amount)
Withdraws a certain amount into the player's bank vault container.
boolean contains(VaultCurrency currency, int amount)
Checks if player's bank container contains a certain amount.
boolean add(VaultCurrency currency, long amount, boolean message)
Adds an amount into the player's bank vault with no checks.
void value(VaultCurrency currency)
Sends message to player about their container value.
boolean add(VaultCurrency currency, long amount)
Adds an amount into the player's bank vault with no checks.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String formatDigits(final int amount)
Formats digits for integers.
Definition Utility.java:41
static String formatPrice(final long amount)
Formats a price for longs.
Definition Utility.java:56
Holds an enum of colors for ease.