-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRepairInventoryCommand.java
More file actions
138 lines (121 loc) · 5.7 KB
/
Copy pathRepairInventoryCommand.java
File metadata and controls
138 lines (121 loc) · 5.7 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package net.countercraft.movecraft.repair.commands;
import java.util.LinkedList;
import java.util.List;
import net.countercraft.movecraft.util.ChatUtils;
import net.countercraft.movecraft.util.ComponentPaginator;
import net.countercraft.movecraft.util.Pair;
import net.kyori.adventure.text.Component;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.PlayerCraft;
import net.countercraft.movecraft.repair.RepairBlobManager;
import net.countercraft.movecraft.repair.localisation.I18nSupport;
import net.countercraft.movecraft.repair.types.RepairCounter;
import net.countercraft.movecraft.repair.types.blobs.RepairBlob;
import net.countercraft.movecraft.util.Tags;
public class RepairInventoryCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s,
@NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(ChatUtils.commandPrefix().append(I18nSupport.getInternationalisedComponent("Repair - Must Be Player")));
return true;
}
if (!player.hasPermission("movecraft.repair.repaircancel")) {
player.sendMessage(ChatUtils.commandPrefix().append(net.countercraft.movecraft.localisation.I18nSupport.getInternationalisedComponent("Insufficient Permissions")));
return true;
}
PlayerCraft craft = CraftManager.getInstance().getCraftByPlayer(player);
if (craft == null) {
player.sendMessage(I18nSupport.getInternationalisedComponent("You must be piloting a craft"));
return true;
}
int page = 1; // Default page
if (args.length > 0) {
try {
page = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
sender.sendMessage(Component.empty()
.append(ChatUtils.commandPrefix())
.append(net.countercraft.movecraft.localisation.I18nSupport.getInternationalisedComponent("Paginator - Invalid page"))
.append(Component.text(" \""))
.append(Component.text(args[0]))
.append(Component.text("\"")));
return true;
}
}
Pair<RepairCounter, Integer> result = sumInventory(craft);
RepairCounter inventory = result.getLeft();
List<RepairBlob> keys = new LinkedList<>(inventory.getKeySet());
if (keys.isEmpty()) {
player.sendMessage(ChatUtils.commandPrefix().append(I18nSupport.getInternationalisedComponent("Inventory - Empty Craft")));
return true;
}
keys.sort((key1, key2) -> ((int) (inventory.get(key2) - inventory.get(key1))));
ComponentPaginator paginator = new ComponentPaginator(
I18nSupport.getInternationalisedComponent("Inventory - Inventory Header"),
pageNumber -> "/repairinventory " + pageNumber);
paginator.addLine(Component.text(String.format("EMPTY SLOTS: %,d", result.getRight())));
for (RepairBlob key : keys) {
paginator.addLine(buildLine(key, inventory.get(key)));
}
if (!paginator.isInBounds(page)) {
sender.sendMessage(Component.empty()
.append(ChatUtils.commandPrefix())
.append(net.countercraft.movecraft.localisation.I18nSupport.getInternationalisedComponent("Paginator - Invalid page"))
.append(Component.text(" \""))
.append(Component.text(args[0]))
.append(Component.text("\"")));
return true;
}
for (Component line : paginator.getPage(page)) {
player.sendMessage(line);
}
return true;
}
@NotNull
private Component buildLine(@NotNull RepairBlob key, double count) {
return Component.empty()
.append(Component.text(key.getName()))
.append(Component.text(": "))
.append(Component.text(String.format("%,.0f", count)));
}
@NotNull
private Pair<RepairCounter, Integer> sumInventory(@NotNull Craft craft) {
RepairCounter result = new RepairCounter();
int emptySlots = 0;
World world = craft.getWorld();
for (MovecraftLocation location : craft.getHitBox()) {
Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ());
if (!Tags.CHESTS.contains(block.getType()))
continue;
BlockState state = block.getState();
if (!(state instanceof Container))
continue;
Inventory inventory = ((Container) state).getInventory();
if (inventory instanceof DoubleChestInventory)
continue; // Don't take from double chests
for (ItemStack item : inventory.getContents()) {
if (item == null) {
emptySlots++;
continue;
}
result.add(RepairBlobManager.get(item.getType()), item.getAmount());
}
}
return new Pair<>(result, emptySlots);
}
}