forked from MLG-Fortress/AutomaticInventory
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutoRefillHotBarTask.java
More file actions
67 lines (57 loc) · 1.9 KB
/
Copy pathAutoRefillHotBarTask.java
File metadata and controls
67 lines (57 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dev.chaws.automaticinventory.tasks;
import dev.chaws.automaticinventory.configuration.PlayerConfig;
import dev.chaws.automaticinventory.messaging.Messages;
import dev.chaws.automaticinventory.utilities.Chat;
import dev.chaws.automaticinventory.utilities.Level;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
public class AutoRefillHotBarTask implements Runnable {
private final Player player;
private final PlayerInventory targetInventory;
private final int slotToRefill;
private final ItemStack stackToReplace;
public AutoRefillHotBarTask(Player player, PlayerInventory targetInventory, int slotToRefill, ItemStack stackToReplace) {
this.player = player;
this.targetInventory = targetInventory;
this.slotToRefill = slotToRefill;
this.stackToReplace = stackToReplace;
}
@Override
public void run() {
var currentStack = this.targetInventory.getItem(this.slotToRefill);
if (currentStack != null) {
return;
}
ItemStack bestMatchStack = null;
var bestMatchSlot = -1;
var bestMatchStackSize = Integer.MAX_VALUE;
for (var i = 0; i < 36; i++) {
var itemInSlot = this.targetInventory.getItem(i);
if (itemInSlot == null) {
continue;
}
if (itemInSlot.isSimilar(this.stackToReplace)) {
var stackSize = itemInSlot.getAmount();
if (stackSize < bestMatchStackSize) {
bestMatchStack = itemInSlot;
bestMatchSlot = i;
bestMatchStackSize = stackSize;
}
if (bestMatchStackSize == 1) {
break;
}
}
}
if (bestMatchStack == null) {
return;
}
this.targetInventory.setItem(this.slotToRefill, bestMatchStack);
this.targetInventory.clear(bestMatchSlot);
var playerConfig = PlayerConfig.fromPlayer(player);
if (!playerConfig.hasReceivedRestackInfo()) {
Chat.sendMessage(player, Level.Info, Messages.AutoRefillEducation);
playerConfig.setReceivedRestackInfo(true);
}
}
}