RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ItemBag.java
1package com.osroyale.content.bags;
2
3import com.osroyale.game.world.entity.mob.player.Player;
4import com.osroyale.game.world.items.Item;
5import com.osroyale.game.world.items.ItemDefinition;
6import com.osroyale.game.world.items.containers.ItemContainer;
7
8import java.util.Arrays;
9import java.util.function.Predicate;
10
37
38public abstract class ItemBag {
39
40 public final ItemContainer container;
41
42 public ItemBag(ItemContainer container) {
43 this.container = container;
44 }
45
46 public abstract String getItem();
47
48 public abstract String getName();
49
50 public abstract Predicate<Item> isAllowed();
51
52 public abstract String getIndication();
53
54 public void fill(Player player) {
55 player.message("You search your inventory for "+getItem()+" to put into the "+getName()+"...");
56
57 if (Arrays.stream(player.inventory.getItems()).noneMatch(isAllowed())) {
58 player.message("There "+getIndication()+" no "+getItem()+" in your inventory that can be added to the "+getName()+".");
59 return;
60 }
61
62 Arrays.stream(player.inventory.getItems()).filter(isAllowed()).forEach(item -> {
63 player.inventory.remove(item);
64 container.add(item);
65 });
66
67 player.message("You add the "+getItem()+" to your "+getName()+".");
68 player.inventory.refresh();
69 }
70
71 public void empty(Player player) {
72 if (container.getFreeSlots() == container.capacity()) {
73 player.message("The "+getName()+" is already empty.");
74 return;
75 }
76 if (player.inventory.getFreeSlots() <= 0) {
77 player.message("You don't have enough inventory space to empty the contents of this "+getName()+".");
78 return;
79 }
80 for(Item item : container.getItems()) {
81 int freeSlots = player.inventory.getFreeSlots();
82 if (freeSlots <= 0) {
83 player.inventory.refresh();
84 return;
85 }
86 if(item == null) continue;
87
88 int amount = item.getAmount();
89
90 if(amount > freeSlots)
91 amount = freeSlots;
92
93 container.remove(new Item(item.getId(), amount));
94 player.inventory.add(new Item(item.getId(), item.getAmount()));
95 }
96 player.inventory.refresh();
97 }
98
99 public void check(Player player) {
100 player.message("You look in your "+getName()+" and see:");
101
102 if (container.getFreeSlots() == container.capacity()) {
103 player.message("The "+getName()+" is empty.");
104 return;
105 }
106 for (Item item : container.getItems()) {
107 if(item == null) continue;
108 player.message(item.getAmount() + "x " + ItemDefinition.get(item.getId()).getName());
109 }
110 }
111
112}