-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Inventory.java
128 lines (111 loc) · 3.71 KB
/
Inventory.java
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
package net.citizensnpcs.api.trait.trait;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.ItemStorage;
/**
* Represents an NPC's inventory.
*/
@TraitName("inventory")
public class Inventory extends Trait {
private ItemStack[] contents;
private org.bukkit.inventory.Inventory view;
public Inventory() {
super("inventory");
contents = new ItemStack[72];
}
/**
* Gets the contents of an NPC's inventory.
*
* @return ItemStack array of an NPC's inventory contents
*/
public ItemStack[] getContents() {
return contents;
}
public org.bukkit.inventory.Inventory getInventoryView() {
return view;
}
@EventHandler
public void inventoryEvent(InventoryEvent event) {
if (view != null && event.getInventory().equals(view)) {
contents = event.getInventory().getContents();
}
}
@Override
public void load(DataKey key) throws NPCLoadException {
contents = parseContents(key);
}
@Override
public void onSpawn() {
setContents(contents);
view = Bukkit.createInventory(
npc.getEntity() instanceof InventoryHolder ? ((InventoryHolder) npc.getEntity()) : null,
contents.length);
}
private ItemStack[] parseContents(DataKey key) throws NPCLoadException {
ItemStack[] contents = new ItemStack[72];
for (DataKey slotKey : key.getIntegerSubKeys()) {
contents[Integer.parseInt(slotKey.name())] = ItemStorage.loadItemStack(slotKey);
}
return contents;
}
@Override
public void run() {
if (npc.getEntity() instanceof Player) {
contents = ((Player) npc.getEntity()).getInventory().getContents();
}
}
@Override
public void save(DataKey key) {
int slot = 0;
for (ItemStack item : contents) {
// Clear previous items to avoid conflicts
key.removeKey(String.valueOf(slot));
if (item != null) {
ItemStorage.saveItem(key.getRelative(String.valueOf(slot)), item);
}
slot++;
}
}
/**
* Sets the contents of an NPC's inventory.
*
* @param contents
* ItemStack array to set as the contents of an NPC's inventory
*/
public void setContents(ItemStack[] contents) {
this.contents = Arrays.copyOf(contents, 72);
org.bukkit.inventory.Inventory dest = null;
int maxCopySize = -1;
if (npc.getEntity() instanceof Player) {
dest = ((Player) npc.getEntity()).getInventory();
maxCopySize = 36;
} else if (npc.getEntity() instanceof StorageMinecart) {
dest = ((StorageMinecart) npc.getEntity()).getInventory();
} else if (npc.getEntity() instanceof Horse) {
dest = ((Horse) npc.getEntity()).getInventory();
}
if (dest == null)
return;
if (maxCopySize == -1) {
maxCopySize = dest.getSize();
}
for (int i = 0; i < maxCopySize; i++) {
dest.setItem(i, contents[i]);
}
}
@Override
public String toString() {
return "Inventory{" + Arrays.toString(contents) + "}";
}
}