RuneHive-Game
Loading...
Searching...
No Matches
LootingBag.java
Go to the documentation of this file.
1package com.runehive.game.world.items.containers.impl;
2
3import com.runehive.Config;
4import com.runehive.game.world.InterfaceConstants;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.game.world.items.Item;
7import com.runehive.game.world.items.containers.ItemContainer;
8import com.runehive.game.world.items.containers.ItemContainerAdapter;
9import com.runehive.game.world.items.containers.pricechecker.PriceType;
10import com.runehive.game.world.position.Area;
11import com.runehive.net.packet.out.*;
12import com.runehive.util.Utility;
13import plugin.itemon.item.LootingBagPlugin;
14
15/**
16 * Handles the looting bag container.
17 *
18 * @author Daniel.
19 */
20public class LootingBag extends ItemContainer {
21
22 /**
23 * The player instance.
24 */
25 private final Player player;
26
27 /**
28 * Constructs a new <code>LootingBag</code>.
29 */
31 super(28, StackPolicy.STANDARD);
33 this.player = player;
34 }
35
36 public void open() {
37 onRefresh();
38 player.interfaceManager.setSidebar(Config.INVENTORY_TAB, 26700);
40 }
41
42 /**
43 * Handles closing the looting bag.
44 */
45 public void close() {
46 if (player.attributes.get("BANK_KEY", Boolean.class)) {
47 player.interfaceManager.openInventory(60000, InterfaceConstants.INVENTORY_STORE - 1);
48 return;
49 }
50 player.interfaceManager.setSidebar(Config.INVENTORY_TAB, 3213);
51 }
52
53 /**
54 * Checks if the player is allowed to deposit items into the looting bag.
55 */
56 private boolean allowed(Item item) {
57 if (!Area.inWilderness(player)) {
58 player.send(new SendMessage("You can't put items in the bag unless you're in the Wilderness."));
59 return false;
60 }
61 if (!item.isTradeable()) {
62 player.send(new SendMessage("You can't deposit un-tradeable items into the looting bag."));
63 return false;
64 }
65 if (!player.inventory.contains(item)) {
66 player.send(new SendMessage("You can not deposit an item that you do not have!"));
67 return false;
68 }
69 return true;
70 }
71
72 public Item[] getDeathItems() {
73 if (!player.inventory.contains(LootingBagPlugin.OPENED_ID) && !player.inventory.contains(LootingBagPlugin.CLOSED_ID)) {
74 return null;
75 }
76 return toNonNullArray();
77 }
78
79 /**
80 * Handles opening the looting bag menu.
81 */
82 public void depositMenu(Item item) {
83 player.dialogueFactory.sendOption(
84 "Deposit 1", () -> deposit(item, 1),
85 "Deposit 5", () -> deposit(item, 5),
86 "Deposit All", () -> deposit(item, item.getAmount()),
87 "Deposit X", () ->
88 player.dialogueFactory.onAction(() -> player.send(new SendInputMessage("Enter the amount you would like to deposit into the looting bag:", 12, input -> {
89 player.dialogueFactory.clear();
90 deposit(item, Integer.parseInt(input));
91 })))).execute();
92 }
93
94 /**
95 * Handles the actual depositing of looting bag.
96 */
97 public boolean deposit(Item item, int amount) {
98 if (!allowed(item)) {
99 return false;
100 }
101 int contain = player.inventory.computeAmountForId(item.getId());
102 if (contain < amount) {
103 amount = contain;
104 }
105 Item current = new Item(item.getId(), amount);
106 if (!add(current)) {
107 return false;
108 }
109 player.inventory.remove(current);
110 player.send(new SendMessage("You have deposited " + amount + " " + current.getName() + " into the looting bag."));
111 onRefresh();
112 return true;
113 }
114
115 /**
116 * Handles the actual depositing of looting bag.
117 */
118 public void withdrawBank(Item item, int slot) {
119 int amount = computeAmountForId(item.getId());
120 if (item.getAmount() > amount) {
121 item = item.createWithAmount(amount);
122 }
123 int removed = 0;
124 if (item.isStackable()) {
125 removed = player.bank.depositFromNothing(item, player.bank.bankTab);
126 } else {
127 while (removed < item.getAmount()) {
128 int deposited = player.bank.depositFromNothing(item, player.bank.bankTab);
129 if (deposited == 0) {
130 break;
131 }
132 removed += deposited;
133 }
134 }
135 if (removed > 0) {
136 remove(item.createWithAmount(removed), slot, false);
137 }
138 player.bank.refresh();
139 onRefresh();
140 }
141
142 @Override
143 public void onRefresh() {
144 player.send(new SendItemOnInterface(26706, toArray()));
145 player.send(new SendString("Value: " + Utility.formatDigits(containerValue(PriceType.VALUE)) + " coins", 26707));
146 }
147
148 /**
149 * An {@link ItemContainerAdapter} implementation that listens for changes to the looting bag.
150 */
151 private static final class LootingBagListener extends ItemContainerAdapter {
152
156
157 @Override
158 public int getWidgetId() {
159 return 26706;
160 }
161
162 @Override
163 public String getCapacityExceededMsg() {
164 return "You do not have enough space in your looting bag.";
165 }
166 }
167}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int INVENTORY_TAB
Definition Config.java:194
The class that contains helpful information on interfaces.
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
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
Item createWithAmount(int newAmount)
Creates a new item with newAmount and the same identifier as this instance.
Definition Item.java:158
ItemContainerAdapter(Player player)
Creates a new ItemContainerAdapter.
ItemContainer(int capacity, StackPolicy policy, Item[] items)
Creates a new ItemContainer.
final int computeAmountForId(int id)
Computes the total quantity of the Items in this container with id.
boolean add(Item item)
Attempts to deposit item into this container.
final boolean addListener(ItemContainerListener listener)
Adds an ItemContainerListener to this container.
long containerValue(PriceType type)
Gets the total worth of the container using the item's values.
final Item[] toArray()
Returns a shallow copy of the backing array.
An ItemContainerAdapter implementation that listens for changes to the looting bag.
void onRefresh()
Any functionality that should occur when refreshed.
void withdrawBank(Item item, int slot)
Handles the actual depositing of looting bag.
boolean allowed(Item item)
Checks if the player is allowed to deposit items into the looting bag.
void close()
Handles closing the looting bag.
boolean deposit(Item item, int amount)
Handles the actual depositing of looting bag.
LootingBag(Player player)
Constructs a new LootingBag.
void depositMenu(Item item)
Handles opening the looting bag menu.
Handles checking if mobs are in a certain area.
Definition Area.java:13
static boolean inWilderness(Position position)
Definition Area.java:272
Sends a chatbox input prompt that accepts full text with spaces.
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
An enumerated type defining policies for stackable Items.
STANDARD
The STANDARD policy, items are only stacked if they are defined as stackable in their ItemDefinition ...