-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathGhostCircuitItemStackHandler.java
More file actions
186 lines (160 loc) · 6.47 KB
/
GhostCircuitItemStackHandler.java
File metadata and controls
186 lines (160 loc) · 6.47 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
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
package gregtech.api.capability.impl;
import gregtech.api.capability.INotifiableHandler;
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.recipes.ingredients.IntCircuitIngredient;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.IItemHandlerModifiable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class GhostCircuitItemStackHandler extends GTItemStackHandler
implements IItemHandlerModifiable, INotifiableHandler {
/**
* Special circuit value indicating no circuit value is set.
*/
public static final int NO_CONFIG = -1;
private final List<MetaTileEntity> notifiableEntities = new ArrayList<>();
private int circuitValue = NO_CONFIG;
private ItemStack circuitStack = ItemStack.EMPTY;
public GhostCircuitItemStackHandler(@NotNull MetaTileEntity metaTileEntity) {
this(metaTileEntity, metaTileEntity);
}
public GhostCircuitItemStackHandler(@NotNull MetaTileEntity metaTileEntity, @Nullable MetaTileEntity mteToNotify) {
super(metaTileEntity);
if (mteToNotify != null) {
notifiableEntities.add(mteToNotify);
}
}
/**
* Return the circuit value, or {@link GhostCircuitItemStackHandler#NO_CONFIG} if
* no circuit value is set.
*
* @return A valid circuit value if present, otherwise {@link GhostCircuitItemStackHandler#NO_CONFIG}
*/
public int getCircuitValue() {
return this.circuitValue;
}
/**
* Returns whether this instance contains valid circuit value.
*
* @return Whether this instance contains valid circuit value.
*/
public boolean hasCircuitValue() {
return this.circuitValue != NO_CONFIG;
}
/**
* Set the circuit value of this inventory to given value and update the item. The item is set to circuit item
* with corresponding int value, or an empty itemstack if {@link GhostCircuitItemStackHandler#NO_CONFIG} is given.
* <p>
* The value is expected to be either a valid circuit value
* ({@link IntCircuitIngredient#CIRCUIT_MIN} ~ {@link IntCircuitIngredient#CIRCUIT_MAX}, both inclusive)
* or {@link GhostCircuitItemStackHandler#NO_CONFIG}; any other value will produce IllegalArgumentException.
*
* @param config New config value
* @throws IllegalArgumentException On invalid input
*/
public void setCircuitValue(int config) {
if (config == NO_CONFIG) {
this.circuitValue = NO_CONFIG;
this.circuitStack = ItemStack.EMPTY;
} else if (config >= IntCircuitIngredient.CIRCUIT_MIN && config <= IntCircuitIngredient.CIRCUIT_MAX) {
this.circuitValue = config;
this.circuitStack = IntCircuitIngredient.getIntegratedCircuit(config);
} else {
throw new IllegalArgumentException("Circuit value out of range: " + config);
}
for (MetaTileEntity mte : notifiableEntities) {
if (mte != null && mte.isValid()) {
addToNotifiedList(mte, this, false);
}
}
}
/**
* Set the circuit value of this inventory from given item. Circuit value is set to valid circuit value only if
* the supplied item is int circuit; providing any other item will set the circuit value to {@link
* GhostCircuitItemStackHandler#NO_CONFIG}.
*
* @param stack Item stack to read circuit value from
*/
public void setCircuitValueFromStack(@NotNull ItemStack stack) {
setCircuitValue(!stack.isEmpty() && IntCircuitIngredient.isIntegratedCircuit(stack) ?
IntCircuitIngredient.getCircuitConfiguration(stack) : NO_CONFIG);
}
/**
* Add given value to preexisting circuit value. The resulting value is capped in range of valid circuit value.
* If there was no circuit value present, this method does nothing.
*
* @param configDelta Amount of circuit value to add, can be negative
*/
public void addCircuitValue(int configDelta) {
if (hasCircuitValue()) {
setCircuitValue(MathHelper.clamp(getCircuitValue() + configDelta,
IntCircuitIngredient.CIRCUIT_MIN, IntCircuitIngredient.CIRCUIT_MAX));
}
}
@Override
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
validateSlot(slot);
setCircuitValueFromStack(stack);
}
@Override
public int getSlots() {
return 1;
}
@NotNull
@Override
public ItemStack getStackInSlot(int slot) {
validateSlot(slot);
return this.circuitStack;
}
@NotNull
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
validateSlot(slot);
return stack; // reject all item insertions
}
@NotNull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (amount <= 0) return ItemStack.EMPTY;
validateSlot(slot);
if (!simulate) {
setCircuitValue(NO_CONFIG);
}
return this.circuitStack;
}
@Override
public int getSlotLimit(int slot) {
validateSlot(slot);
return 1;
}
protected void validateSlot(int slot) {
if (slot != 0) throw new IndexOutOfBoundsException("Slot index out of bounds: " + slot);
}
@Override
public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
if (metaTileEntity == null) return;
this.notifiableEntities.add(metaTileEntity);
}
@Override
public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
this.notifiableEntities.remove(metaTileEntity);
}
public void write(@NotNull NBTTagCompound tag) {
if (this.circuitValue != NO_CONFIG) {
tag.setByte("GhostCircuit", (byte) this.circuitValue);
}
}
public void read(@NotNull NBTTagCompound tag) {
int circuitValue = tag.hasKey("GhostCircuit", Constants.NBT.TAG_ANY_NUMERIC) ? tag.getInteger("GhostCircuit") :
NO_CONFIG;
if (circuitValue < IntCircuitIngredient.CIRCUIT_MIN || circuitValue > IntCircuitIngredient.CIRCUIT_MAX)
circuitValue = NO_CONFIG;
setCircuitValue(circuitValue);
}
}