5
5
import org .bukkit .entity .Player ;
6
6
import org .bukkit .inventory .Inventory ;
7
7
import org .bukkit .inventory .ItemStack ;
8
+ import org .bukkit .inventory .PlayerInventory ;
8
9
9
10
public class InventoryUtils {
10
11
12
+ public static boolean hasItem (Player player , ItemStack item , int amount ) {
13
+ return hasItem (player .getInventory (), item , amount );
14
+ }
15
+
11
16
public static boolean hasItem (Inventory inv , ItemStack item , int amount ) {
12
17
return inv .containsAtLeast (item , amount );
13
18
}
14
19
20
+ public static boolean addItem (Player player , ItemStack item ) {
21
+ return addItem (player .getInventory (), item .clone (), item .getAmount ());
22
+ }
23
+
24
+ public static boolean addItem (Inventory inventory , ItemStack item ) {
25
+ return addItem (inventory , item .clone (), item .getAmount ());
26
+ }
27
+
28
+ private static boolean addItem (Inventory inventory , ItemStack item , int amount ) {
29
+ ItemStack [] items = new ItemStack [inventory .getSize ()];
30
+ for (int i = 0 ; i < inventory .getSize (); i ++) {
31
+ if (i >= 36 && i <= 39 && inventory instanceof PlayerInventory ) {
32
+ items [i ] = null ;
33
+ continue ;
34
+ }
35
+ if (inventory .getItem (i ) != null && inventory .getItem (i ).getType () != Material .AIR ) {
36
+ items [i ] = inventory .getItem (i ).clone ();
37
+ } else {
38
+ items [i ] = new ItemStack (Material .AIR );
39
+ }
40
+ }
41
+ boolean success = false ;
42
+ for (int slot = 0 ; slot < items .length ; slot ++) {
43
+ ItemStack tmp = items [slot ];
44
+ if (tmp == null ) {
45
+ continue ;
46
+ }
47
+ if (item .isSimilar (tmp ) && tmp .getAmount () < item .getMaxStackSize ()) {
48
+ if ((tmp .getAmount () + amount ) <= item .getMaxStackSize ()) {
49
+ tmp .setAmount (amount + tmp .getAmount ());
50
+ items [slot ] = tmp ;
51
+ success = true ;
52
+ break ;
53
+ } else {
54
+ amount = amount - (item .getMaxStackSize () - tmp .getAmount ());
55
+ tmp .setAmount (item .getMaxStackSize ());
56
+ items [slot ] = tmp ;
57
+ continue ;
58
+ }
59
+ }
60
+ }
61
+ if (!success ) {
62
+ for (int i = 0 ; i < items .length ; i ++) {
63
+ if (items [i ] != null && items [i ].getType () == Material .AIR ) {
64
+ item .setAmount (amount );
65
+ items [i ] = item ;
66
+ success = true ;
67
+ break ;
68
+ }
69
+ }
70
+ }
71
+ if (success ) {
72
+ for (int i = 0 ; i < items .length ; i ++) {
73
+ if (items [i ] != null && !items [i ].equals (inventory .getItem (i ))) {
74
+ inventory .setItem (i , items [i ]);
75
+ }
76
+ }
77
+ return true ;
78
+ }
79
+ return false ;
80
+ }
81
+
15
82
public static boolean removeItem (Player player , ItemStack item , int amount ) {
16
- Inventory inv = player .getInventory ();
17
- ItemStack [] items = new ItemStack [inv .getSize ()];
18
- for (int i = 0 ; i < inv .getSize (); i ++) {
19
- if (inv .getItem (i ) != null &&
20
- inv .getItem (i ).getType () != Material .AIR ) {
21
- items [i ] = inv .getItem (i ).clone ();
83
+ return removeItem (player .getInventory (), item , amount );
84
+ }
85
+
86
+ public static boolean removeItem (Inventory inventory , ItemStack item , int amount ) {
87
+ ItemStack [] items = new ItemStack [inventory .getSize ()];
88
+ for (int i = 0 ; i < inventory .getSize (); i ++) {
89
+ if (inventory .getItem (i ) != null &&
90
+ inventory .getItem (i ).getType () != Material .AIR ) {
91
+ items [i ] = inventory .getItem (i ).clone ();
22
92
} else {
23
93
items [i ] = new ItemStack (Material .AIR );
24
94
}
@@ -45,23 +115,55 @@ public static boolean removeItem(Player player, ItemStack item, int amount) {
45
115
}
46
116
}
47
117
if (success ) {
48
- player .getInventory ().setContents (items );
118
+ for (int i = 0 ; i < items .length ; i ++) {
119
+ if (!items [i ].equals (inventory .getItem (i ))) {
120
+ inventory .setItem (i , items [i ]);
121
+ }
122
+ }
49
123
return true ;
50
124
}
51
125
return false ;
52
126
}
53
127
54
128
public static int getAmount (Player p , ItemStack item ) {
129
+ return getAmount (p .getInventory (), item );
130
+ }
131
+
132
+ public static int getAmount (Inventory inventory , ItemStack item ) {
55
133
int amount = 0 ;
56
- Inventory inv = p .getInventory ();
57
- for (int i = 0 ; i < inv .getSize (); i ++) {
58
- if (inv .getItem (i ) != null &&
59
- inv .getItem (i ).getType () != Material .AIR &&
60
- inv .getItem (i ).isSimilar (item )) {
61
- amount += inv .getItem (i ).getAmount ();
134
+ for (int i = 0 ; i < inventory .getSize (); i ++) {
135
+ if (inventory .getItem (i ) != null &&
136
+ inventory .getItem (i ).getType () != Material .AIR &&
137
+ inventory .getItem (i ).isSimilar (item )) {
138
+ amount += inventory .getItem (i ).getAmount ();
62
139
}
63
140
}
64
141
return amount ;
65
142
}
66
143
144
+ public static boolean hasEnoughSpace (Player player , ItemStack item , int amount ) {
145
+ return hasEnoughSpace (player .getInventory (), item , amount );
146
+ }
147
+
148
+ public static boolean hasEnoughSpace (Inventory inventory , ItemStack item ) {
149
+ return hasEnoughSpace (inventory , item , item .getAmount ());
150
+ }
151
+
152
+ public static boolean hasEnoughSpace (Inventory inventory , ItemStack item , int amount ) {
153
+ for (int i = 0 ; i < inventory .getSize (); i ++) {
154
+ if (i >= 36 && i <= 39 && inventory instanceof PlayerInventory ) {
155
+ continue ;
156
+ }
157
+ if (inventory .getItem (i ) != null && item .isSimilar (inventory .getItem (i )) &&
158
+ inventory .getItem (i ).getAmount () < item .getMaxStackSize ()) {
159
+ amount -= item .getMaxStackSize () - inventory .getItem (i ).getAmount ();
160
+ } else if (inventory .getItem (i ) == null || inventory .getItem (i ).getType () == Material .AIR ) {
161
+ amount = 0 ;
162
+ }
163
+ if (amount < 1 ) {
164
+ return true ;
165
+ }
166
+ }
167
+ return false ;
168
+ }
67
169
}
0 commit comments