RuneHive-Game
Loading...
Searching...
No Matches
ItemBag.java
Go to the documentation of this file.
1package com.runehive.content.bags;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.items.Item;
5import com.runehive.game.world.items.ItemDefinition;
6import com.runehive.game.world.items.containers.ItemContainer;
7
8import java.util.Arrays;
9import java.util.function.Predicate;
10
11public abstract class ItemBag {
12
14
16 this.container = container;
17 }
18
19 public abstract String getItem();
20
21 public abstract String getName();
22
23 public abstract Predicate<Item> isAllowed();
24
25 public abstract String getIndication();
26
27 public void fill(Player player) {
28 player.message("You search your inventory for "+getItem()+" to put into the "+getName()+"...");
29
30 if (Arrays.stream(player.inventory.getItems()).noneMatch(isAllowed())) {
31 player.message("There "+getIndication()+" no "+getItem()+" in your inventory that can be added to the "+getName()+".");
32 return;
33 }
34
35 Arrays.stream(player.inventory.getItems()).filter(isAllowed()).forEach(item -> {
36 player.inventory.remove(item);
37 container.add(item);
38 });
39
40 player.message("You add the "+getItem()+" to your "+getName()+".");
41 player.inventory.refresh();
42 }
43
44 public void empty(Player player) {
45 if (container.getFreeSlots() == container.capacity()) {
46 player.message("The "+getName()+" is already empty.");
47 return;
48 }
49 if (player.inventory.getFreeSlots() <= 0) {
50 player.message("You don't have enough inventory space to empty the contents of this "+getName()+".");
51 return;
52 }
53 for(Item item : container.getItems()) {
54 int freeSlots = player.inventory.getFreeSlots();
55 if (freeSlots <= 0) {
56 player.inventory.refresh();
57 return;
58 }
59 if(item == null) continue;
60
61 int amount = item.getAmount();
62
63 if(amount > freeSlots)
64 amount = freeSlots;
65
66 container.remove(new Item(item.getId(), amount));
67 player.inventory.add(new Item(item.getId(), item.getAmount()));
68 }
69 player.inventory.refresh();
70 }
71
72 public void check(Player player) {
73 player.message("You look in your "+getName()+" and see:");
74
75 if (container.getFreeSlots() == container.capacity()) {
76 player.message("The "+getName()+" is empty.");
77 return;
78 }
79 for (Item item : container.getItems()) {
80 if(item == null) continue;
81 player.message(item.getAmount() + "x " + ItemDefinition.get(item.getId()).getName());
82 }
83 }
84
85}
ItemBag(ItemContainer container)
Definition ItemBag.java:15
void empty(Player player)
Definition ItemBag.java:44
final ItemContainer container
Definition ItemBag.java:13
void check(Player player)
Definition ItemBag.java:72
abstract Predicate< Item > isAllowed()
This class represents a character controlled by a player.
Definition Player.java:125
Represents all of an in-game Item's attributes.
static ItemDefinition get(int id)
Gets an item definition.
The container class that represents an item that can be interacted with.
Definition Item.java:21
An abstraction game representing a group of Items.
boolean remove(Item item)
Attempts to withdraw item from this container.
boolean add(Item item)
Attempts to deposit item into this container.
void refresh()
Refreshes the players inventory.