-
Notifications
You must be signed in to change notification settings - Fork 755
/
InventoryModifier.java
196 lines (179 loc) · 7.23 KB
/
InventoryModifier.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package slimeknights.tconstruct.library.modifiers.impl;
import lombok.RequiredArgsConstructor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.build.ModifierRemovalHook;
import slimeknights.tconstruct.library.modifiers.hook.build.ValidateModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.build.VolatileDataModifierHook;
import slimeknights.tconstruct.library.module.ModuleHookMap.Builder;
import slimeknights.tconstruct.library.tools.capability.ToolInventoryCapability;
import slimeknights.tconstruct.library.tools.capability.ToolInventoryCapability.InventoryModifierHook;
import slimeknights.tconstruct.library.tools.nbt.IModDataView;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import javax.annotation.Nullable;
import java.util.BitSet;
import java.util.function.BiFunction;
/**
* Modifier that has an inventory
* TODO: migrate to a modifier module
*/
@RequiredArgsConstructor
public class InventoryModifier extends Modifier implements InventoryModifierHook, VolatileDataModifierHook, ValidateModifierHook, ModifierRemovalHook {
/** Mod Data NBT mapper to get a compound list */
protected static final BiFunction<CompoundTag,String,ListTag> GET_COMPOUND_LIST = (nbt, name) -> nbt.getList(name, Tag.TAG_COMPOUND);
/** Error for if the container has items preventing modifier removal */
private static final Component HAS_ITEMS = TConstruct.makeTranslation("modifier", "inventory_cannot_remove");
/** NBT key to store the slot for a stack */
protected static final String TAG_SLOT = "Slot";
/** Persistent data key for the inventory storage, if null uses the modifier ID */
@Nullable
private final ResourceLocation inventoryKey;
/** Number of slots to add per modifier level */
protected final int slotsPerLevel;
public InventoryModifier(int slotsPerLevel) {
this(null, slotsPerLevel);
}
@Override
protected void registerHooks(Builder hookBuilder) {
super.registerHooks(hookBuilder);
hookBuilder.addHook(this, ToolInventoryCapability.HOOK, ModifierHooks.VOLATILE_DATA, ModifierHooks.REMOVE);
}
/** Gets the inventory key used for NBT serializing */
protected ResourceLocation getInventoryKey() {
return inventoryKey == null ? getId() : inventoryKey;
}
@Override
public void addVolatileData(IToolContext context, ModifierEntry modifier, ModDataNBT volatileData) {
ToolInventoryCapability.addSlots(volatileData, getSlots(context, modifier.getLevel()));
}
/**
* Same as {@link ValidateModifierHook#validate(IToolStackView, ModifierEntry)} but allows passing in a max slots count.
* Allows the subclass to validate on a different max slots if needed
* @param tool Tool to check
* @param maxSlots Max slots to use in the check
* @return True if the number of slots is valid
*/
@Nullable
protected Component validateForMaxSlots(IToolStackView tool, int maxSlots) {
IModDataView persistentData = tool.getPersistentData();
ResourceLocation key = getInventoryKey();
if (persistentData.contains(key, Tag.TAG_LIST)) {
ListTag listNBT = persistentData.get(key, GET_COMPOUND_LIST);
if (!listNBT.isEmpty()) {
if (maxSlots == 0) {
return HAS_ITEMS;
}
// first, see whether we have any available slots
BitSet freeSlots = new BitSet(maxSlots);
freeSlots.set(0, maxSlots-1, true);
for (int i = 0; i < listNBT.size(); i++) {
freeSlots.set(listNBT.getCompound(i).getInt(TAG_SLOT), false);
}
for (int i = 0; i < listNBT.size(); i++) {
CompoundTag compoundNBT = listNBT.getCompound(i);
if (compoundNBT.getInt(TAG_SLOT) >= maxSlots) {
int free = freeSlots.stream().findFirst().orElse(-1);
if (free == -1) {
return HAS_ITEMS;
} else {
freeSlots.set(free, false);
compoundNBT.putInt(TAG_SLOT, free);
}
}
}
}
}
return null;
}
@Nullable
@Override
public Component validate(IToolStackView tool, ModifierEntry modifier) {
return validateForMaxSlots(tool, getSlots(tool, modifier.getLevel()));
}
@Nullable
@Override
public Component onRemoved(IToolStackView tool, Modifier modifier) {
Component component = validateForMaxSlots(tool, 0);
if (component != null) {
return component;
}
tool.getPersistentData().remove(getInventoryKey());
return null;
}
@Override
public ItemStack getStack(IToolStackView tool, ModifierEntry modifier, int slot) {
IModDataView modData = tool.getPersistentData();
ResourceLocation key = getInventoryKey();
if (slot < getSlots(tool, modifier) && modData.contains(key, Tag.TAG_LIST)) {
ListTag list = tool.getPersistentData().get(key, GET_COMPOUND_LIST);
for (int i = 0; i < list.size(); i++) {
CompoundTag compound = list.getCompound(i);
if (compound.getInt(TAG_SLOT) == slot) {
return ItemStack.of(compound);
}
}
}
return ItemStack.EMPTY;
}
@Override
public void setStack(IToolStackView tool, ModifierEntry modifier, int slot, ItemStack stack) {
if (slot < getSlots(tool, modifier)) {
ListTag list;
ModDataNBT modData = tool.getPersistentData();
// if the tag exists, fetch it
ResourceLocation key = getInventoryKey();
if (modData.contains(key, Tag.TAG_LIST)) {
list = modData.get(key, GET_COMPOUND_LIST);
// first, try to find an existing stack in the slot
for (int i = 0; i < list.size(); i++) {
CompoundTag compound = list.getCompound(i);
if (compound.getInt(TAG_SLOT) == slot) {
if (stack.isEmpty()) {
list.remove(i);
} else {
compound.getAllKeys().clear();
stack.save(compound);
compound.putInt(TAG_SLOT, slot);
}
return;
}
}
} else if (stack.isEmpty()) {
// nothing to do if empty
return;
} else {
list = new ListTag();
modData.put(key, list);
}
// list did not contain the slot, so add it
if (!stack.isEmpty()) {
list.add(write(stack, slot));
}
}
}
/** Gets the number of slots for this modifier */
public int getSlots(IToolContext tool, int level) {
return level * slotsPerLevel;
}
@Override
public final int getSlots(IToolStackView tool, ModifierEntry modifier) {
return getSlots(tool, modifier.getLevel());
}
/** Writes a stack to NBT, including the slot */
protected static CompoundTag write(ItemStack stack, int slot) {
CompoundTag compound = new CompoundTag();
stack.save(compound);
compound.putInt(TAG_SLOT, slot);
return compound;
}
}