RuneHive-Game
Loading...
Searching...
No Matches
Inventory.java
Go to the documentation of this file.
1package com.runehive.game.world.items.containers.inventory;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.items.Item;
5import com.runehive.game.world.items.containers.ItemContainer;
6import com.runehive.game.world.items.containers.ItemContainerAdapter;
7import com.runehive.game.world.items.ground.GroundItem;
8import com.runehive.net.packet.out.SendItemOnInterface;
9import com.runehive.net.packet.out.SendItemOnInterfaceSlot;
10import com.runehive.net.packet.out.SendMessage;
11
12import java.util.Arrays;
13import java.util.List;
14import java.util.Optional;
15import java.util.function.Consumer;
16
17/**
18 * An {@link ItemContainer} implementation that manages the inventory for a
19 * {@link Player}.
20 *
21 * @author lare96 <http://github.com/lare96>
22 */
23public final class Inventory extends ItemContainer {
24
25 /** The size of all equipment instances. */
26 public static final int SIZE = 28;
27
28 /** The inventory item display widget identifier. */
29 public static final int INVENTORY_DISPLAY_ID = 3214;
30
31 /** The player instance for which this inventory applies to. */
32 private final Player player;
33
34 /** Creates a new {@link Inventory}. */
38// addListener(new ItemWeightListener(player));TODO
39 this.player = player;
40 }
41
42 /** Refreshes the players inventory. */
43 public void refresh() {
45 }
46
47 /**
48 * Attempts to deposit the {@code items} to the inventory, if inventory is
49 * full it'll execute the {@code action} for the remaining items that were
50 * not added.
51 */
52 public void addOrExecute(Consumer<Item> action, Optional<String> message, List<Item> items) {
53 boolean val = false;
54 for (Item item : items) {
55 if (item == null)
56 continue;
57 if (hasCapacityFor(item)) {
58 player.inventory.add(item);
59 } else {
60 action.accept(item);
61 val = true;
62 }
63 }
64 if (val) {
65 message.ifPresent(m -> player.send(new SendMessage(m)));
66 }
67 }
68
69 /**
70 * Attempts to deposit the {@code items} to the inventory, if inventory is
71 * full it'll execute the {@code action} for the remaining items that were
72 * not added.
73 */
74 public void addOrExecute(Consumer<Item> action, Optional<String> message, Item... items) {
75 addOrExecute(action, message, Arrays.asList(items));
76 }
77
78 /**
79 * Attempts to deposit the {@code items} to the inventory, if inventory is
80 * full it'll execute the {@code action} for the remaining items that were
81 * not added.
82 */
83 public void addOrExecute(Consumer<Item> action, String message, Item... items) {
84 addOrExecute(action, Optional.of(message), Arrays.asList(items));
85 }
86
87 /**
88 * Attempts to deposit the {@code items} to the inventory, if inventory is
89 * full it'll execute the {@code action} for the remaining items that were
90 * not added.
91 */
92 public void addOrExecute(Consumer<Item> action, String message, List<Item> items) {
93 addOrExecute(action, Optional.of(message), items);
94 }
95
96 /**
97 * Attempts to deposit the {@code items} to the inventory, if inventory is
98 * full it'll execute the {@code action} for the remaining items that were
99 * not added.
100 */
101 public void addOrExecute(Consumer<Item> action, List<Item> items) {
102 addOrExecute(action, Optional.empty(), items);
103 }
104
105 /**
106 * Attempts to deposit the {@code items} to the inventory, if inventory is
107 * full it'll execute the {@code action} for the remaining items that were
108 * not added.
109 */
110 public void addOrExecute(Consumer<Item> action, Item... items) {
111 addOrExecute(action, Arrays.asList(items));
112 }
113
114 /**
115 * Attempts to deposit an item to the players inventory, if there is no
116 * space it'll bank the item instead.
117 */
118 public void addOrDrop(List<Item> items) {
119 addOrExecute(t -> GroundItem.create(player, t), "@red@Some of the items were dropped since you have no room in your inventory.", items);
120 }
121
122 /**
123 * Attempts to deposit an item to the players inventory, if there is no
124 * space it'll bank the item instead.
125 */
126 public void addOrDrop(Item... items) {
127 addOrDrop(Arrays.asList(items));
128 }
129
130 /**
131 * Attempts to deposit an item to the players inventory, if there is no
132 * space it'll bank the item instead.
133 */
134 public void addOrBank(List<Item> items) {
135 addOrExecute(t -> player.bank.depositFromNothing(t, 0), "@red@Some of the items were banked since you have no room in your inventory.", items);
136 }
137
138 /**
139 * Attempts to deposit an item to the players inventory, if there is no
140 * space it'll bank the item instead.
141 */
142 public void addOrBank(Item... items) {
143 addOrBank(Arrays.asList(items));
144 }
145
146 /** Refreshes the inventory container. */
147 @Override
148 public void refresh(Player player, int widget) {
149 player.send(new SendItemOnInterface(widget, toArray()));
150 }
151
152 /**
153 * An {@link ItemContainerAdapter} implementation that listens for changes
154 * to the inventory.
155 */
156 private final class InventoryListener extends ItemContainerAdapter {
157
158 /** Creates a new {@link InventoryListener}. */
160 super(player);
161 }
162
163 @Override
164 public void itemUpdated(ItemContainer container, Optional<Item> oldItem, Optional<Item> newItem, int index, boolean refresh, boolean login) {
165 if (refresh)
166 player.send(new SendItemOnInterfaceSlot(getWidgetId(), newItem.orElse(null), index));
167 }
168
169 @Override
170 public int getWidgetId() {
172 }
173
174 @Override
175 public String getCapacityExceededMsg() {
176 return "You do not have enough space in your inventory.";
177 }
178 }
179}
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
ItemContainerAdapter(Player player)
Creates a new ItemContainerAdapter.
ItemContainer(int capacity, StackPolicy policy, Item[] items)
Creates a new ItemContainer.
Item[] items
The Items within this container.
final boolean hasCapacityFor(Item... item)
Determines if this container has the capacity for item.
final boolean addListener(ItemContainerListener listener)
Adds an ItemContainerListener to this container.
final Item[] toArray()
Returns a shallow copy of the backing array.
An ItemContainerAdapter implementation that listens for changes to the inventory.
void itemUpdated(ItemContainer container, Optional< Item > oldItem, Optional< Item > newItem, int index, boolean refresh, boolean login)
Fired when an Item is added, removed, or replaced.
void addOrExecute(Consumer< Item > action, Item... items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
void addOrDrop(Item... items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
final Player player
The player instance for which this inventory applies to.
void addOrBank(List< Item > items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
static final int SIZE
The size of all equipment instances.
Inventory(Player player)
Creates a new Inventory.
void refresh()
Refreshes the players inventory.
void addOrExecute(Consumer< Item > action, String message, List< Item > items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
void addOrDrop(List< Item > items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
void addOrBank(Item... items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
void addOrExecute(Consumer< Item > action, String message, Item... items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
void addOrExecute(Consumer< Item > action, Optional< String > message, Item... items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
static final int INVENTORY_DISPLAY_ID
The inventory item display widget identifier.
void addOrExecute(Consumer< Item > action, Optional< String > message, List< Item > items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
void addOrExecute(Consumer< Item > action, List< Item > items)
Attempts to deposit the items to the inventory, if inventory is full it'll execute the action for the...
void refresh(Player player, int widget)
Refreshes the inventory container.
Represents a single Ground item on the world map.
static GroundItem create(Player player, Item item)
Creates a new GroundItem object for a player and an item.
The OutgoingPacket that sends a message to a Players chatbox in the client.
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 ...