Skip to content

Commit dee3225

Browse files
author
tom2208
committed
Refill works now with stacks form the hotbar except the item which you
are holding
1 parent f54ce9d commit dee3225

1 file changed

Lines changed: 36 additions & 31 deletions

File tree

src/chestcleaner/listeners/RefillListener.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,16 @@ private boolean isOnBlackList(ItemStack item) {
200200
private int getRefillStack(Material material, Player player) {
201201

202202
ItemStack[] items = InventoryDetector.getFullInventory(player.getInventory());
203-
for (int i = 9; i < 36; i++) {
203+
for (int i = 0; i < 36; i++) {
204+
if (i != player.getInventory().getHeldItemSlot() && i != 40) {
205+
if (items[i] != null) {
204206

205-
if (items[i] != null) {
207+
if (items[i].getType().equals(material)) {
208+
return i;
209+
}
206210

207-
if (items[i].getType().equals(material)) {
208-
return i;
209211
}
210-
211212
}
212-
213213
}
214214
return -1;
215215

@@ -296,6 +296,10 @@ private boolean playerOffHandHeldItemMaterialEquals(ItemStack item, Player playe
296296
return player.getInventory().getItemInOffHand().getType().equals(item.getType());
297297
}
298298

299+
private boolean isViableSlot(int i, Player player) {
300+
return i != player.getInventory().getHeldItemSlot() && i != 40;
301+
}
302+
299303
/**
300304
* Searches through the main inventory (slots 9 - 35) taking the first ItemStack
301305
* with the same type, an amount bigger than 1 (bigger than 0 would work but
@@ -312,23 +316,24 @@ private void refillConsumableInSlot(int hand, Player player, ItemStack conItem)
312316
public void run() {
313317

314318
ItemStack[] items = InventoryDetector.getFullInventory(player.getInventory());
315-
for (int i = 9; i < 36; i++) {
319+
for (int i = 0; i < 36; i++) {
316320
if (items[i] != null) {
317-
if (items[i].getType().equals(conItem.getType())) {
318-
if (hand > -999) {
319-
player.getInventory().setItem(hand, items[i]);
320-
player.getInventory().setItem(i, null);
321-
break;
322-
} else {
323-
player.getInventory().setItemInOffHand(items[i]);
324-
player.getInventory().setItem(i, null);
325-
break;
321+
if (isViableSlot(i, player)) {
322+
if (items[i].getType().equals(conItem.getType())) {
323+
if (hand > -999) {
324+
player.getInventory().setItem(hand, items[i]);
325+
player.getInventory().setItem(i, null);
326+
break;
327+
} else {
328+
player.getInventory().setItemInOffHand(items[i]);
329+
player.getInventory().setItem(i, null);
330+
break;
331+
}
332+
326333
}
327334

328335
}
329-
330336
}
331-
332337
}
333338

334339
}
@@ -346,24 +351,24 @@ public void run() {
346351
private void refillBlockInSlot(Player player, Material material, EquipmentSlot hand) {
347352
ItemStack[] items = InventoryDetector.getFullInventory(player.getInventory());
348353

349-
for (int i = 9; i < 36; i++) {
354+
for (int i = 0; i < 36; i++) {
350355

351356
if (items[i] != null) {
357+
if (isViableSlot(i, player)) {
358+
if (items[i].getType().equals(material)) {
359+
360+
if (hand.equals(EquipmentSlot.HAND)) {
361+
player.getInventory().setItemInMainHand(items[i]);
362+
player.getInventory().setItem(i, null);
363+
break;
364+
} else if (hand.equals(EquipmentSlot.OFF_HAND)) {
365+
player.getInventory().setItemInOffHand(items[i]);
366+
player.getInventory().setItem(i, null);
367+
break;
368+
}
352369

353-
if (items[i].getType().equals(material)) {
354-
355-
if (hand.equals(EquipmentSlot.HAND)) {
356-
player.getInventory().setItemInMainHand(items[i]);
357-
player.getInventory().setItem(i, null);
358-
break;
359-
} else if (hand.equals(EquipmentSlot.OFF_HAND)) {
360-
player.getInventory().setItemInOffHand(items[i]);
361-
player.getInventory().setItem(i, null);
362-
break;
363370
}
364-
365371
}
366-
367372
}
368373

369374
}

0 commit comments

Comments
 (0)