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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
package gregtech.api.metatileentity.implementations;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.IAlignment;
import com.gtnewhorizon.structurelib.alignment.IAlignmentLimits;
import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider;
import com.gtnewhorizon.structurelib.alignment.constructable.IConstructable;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
import com.gtnewhorizon.structurelib.alignment.enumerable.Flip;
import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;
import com.gtnewhorizon.structurelib.structure.IItemSource;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment;
import cpw.mods.fml.common.network.NetworkRegistry;
import gregtech.api.GregTechAPI;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.MultiblockTooltipBuilder;
import gregtech.api.util.shutdown.ShutDownReasonRegistry;
/**
* Enhanced multiblock base class, featuring following improvement over {@link MTEMultiBlockBase}
* <p>
* 1. TecTech style declarative structure check utilizing StructureLib. 2. Arbitrarily rotating the whole structure, if
* allowed to.
*
* @param <T> type of this
*/
public abstract class MTEEnhancedMultiBlockBase<T extends MTEEnhancedMultiBlockBase<T>> extends MTETooltipMultiBlockBase
implements IAlignment, IConstructable {
private ExtendedFacing mExtendedFacing = ExtendedFacing.DEFAULT;
private IAlignmentLimits mLimits = getInitialAlignmentLimits();
protected MTEEnhancedMultiBlockBase(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
protected MTEEnhancedMultiBlockBase(String aName) {
super(aName);
}
@Override
public ExtendedFacing getExtendedFacing() {
return mExtendedFacing;
}
@Override
public void setExtendedFacing(ExtendedFacing newExtendedFacing) {
if (mExtendedFacing != newExtendedFacing) {
if (mMachine) stopMachine(ShutDownReasonRegistry.STRUCTURE_INCOMPLETE);
mExtendedFacing = newExtendedFacing;
final IGregTechTileEntity base = getBaseMetaTileEntity();
mMachine = false;
mUpdated = false;
mUpdate = 50;
if (getBaseMetaTileEntity().isServerSide()
&& !GregTechAPI.isDummyWorld(getBaseMetaTileEntity().getWorld())) {
StructureLibAPI.sendAlignment(
(IAlignmentProvider) base,
new NetworkRegistry.TargetPoint(
base.getWorld().provider.dimensionId,
base.getXCoord(),
base.getYCoord(),
base.getZCoord(),
512));
} else {
base.issueTextureUpdate();
}
}
}
@Override
public final boolean isFacingValid(ForgeDirection facing) {
return canSetToDirectionAny(facing);
}
@Override
public boolean onWrenchRightClick(ForgeDirection side, ForgeDirection wrenchingSide, EntityPlayer entityPlayer,
float aX, float aY, float aZ) {
if (wrenchingSide != getBaseMetaTileEntity().getFrontFacing())
return super.onWrenchRightClick(side, wrenchingSide, entityPlayer, aX, aY, aZ);
if (entityPlayer.isSneaking()) {
if (isFlipChangeAllowed()) {
toolSetFlip(getFlip().isHorizontallyFlipped() ? Flip.NONE : Flip.HORIZONTAL);
} else {
return false;
}
} else {
if (isRotationChangeAllowed()) {
toolSetRotation(null);
} else {
return false;
}
}
return true;
}
@Override
public void onFacingChange() {
toolSetDirection(getBaseMetaTileEntity().getFrontFacing());
}
@Override
public IAlignmentLimits getAlignmentLimits() {
return mLimits;
}
protected void setAlignmentLimits(IAlignmentLimits mLimits) {
this.mLimits = mLimits;
}
/**
* Due to limitation of Java type system, you might need to do an unchecked cast. HOWEVER, the returned
* IStructureDefinition is expected to be evaluated against current instance only, and should not be used against
* other instances, even for those of the same class.
*/
public abstract IStructureDefinition<T> getStructureDefinition();
protected abstract MultiblockTooltipBuilder createTooltip();
@Override
public String[] getStructureDescription(ItemStack stackSize) {
return getTooltip().getStructureHint();
}
protected IAlignmentLimits getInitialAlignmentLimits() {
return (d, r, f) -> !f.isVerticallyFliped();
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
aNBT.setByte(
"eRotation",
(byte) mExtendedFacing.getRotation()
.getIndex());
aNBT.setByte(
"eFlip",
(byte) mExtendedFacing.getFlip()
.getIndex());
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
mExtendedFacing = ExtendedFacing.of(
getBaseMetaTileEntity().getFrontFacing(),
Rotation.byIndex(aNBT.getByte("eRotation")),
Flip.byIndex(aNBT.getByte("eFlip")));
if (!getAlignmentLimits().isNewExtendedFacingValid(mExtendedFacing)) {
mExtendedFacing = getCorrectedAlignment(mExtendedFacing);
}
}
/**
* When an old {@link ExtendedFacing} is loaded upon MTE deserialization, and the loaded facing cannot pass test of
* current {@link #getAlignmentLimits()}, this method will be called to retrieve a corrected version. This method
* is currently only intended to be used as a mean to migrate alignment limits, so if you never change the alignment
* limit then you can probably just use the default implementation.
* <p>
* The returned new facing must be able to pass the test of {@link #isNewExtendedFacingValid(ExtendedFacing)}
*/
protected ExtendedFacing getCorrectedAlignment(ExtendedFacing aOldFacing) {
if (isNewExtendedFacingValid(ExtendedFacing.DEFAULT)) {
return ExtendedFacing.DEFAULT;
}
for (ExtendedFacing facing : ExtendedFacing.VALUES) {
if (facing.getFlip()
.isVerticallyFliped()) {
continue;
}
if (isNewExtendedFacingValid(facing)) {
return facing;
}
}
throw new AssertionError("No ExtendedFacing can pass the test of isNewExtendedFacingValid");
}
@SuppressWarnings("unchecked")
private IStructureDefinition<MTEEnhancedMultiBlockBase<T>> getCastedStructureDefinition() {
return (IStructureDefinition<MTEEnhancedMultiBlockBase<T>>) getStructureDefinition();
}
/**
* Explanation of the world coordinate these offset means:
* <p>
* Imagine you stand in front of the controller, with controller facing towards you not rotated or flipped.
* <p>
* The horizontalOffset would be the number of blocks on the left side of the controller, not counting controller
* itself. The verticalOffset would be the number of blocks on the top side of the controller, not counting
* controller itself. The depthOffset would be the number of blocks between you and controller, not counting
* controller itself.
* <p>
* All these offsets can be negative.
*/
protected final boolean checkPiece(String piece, int horizontalOffset, int verticalOffset, int depthOffset) {
final IGregTechTileEntity tTile = getBaseMetaTileEntity();
return getCastedStructureDefinition().check(
this,
piece,
tTile.getWorld(),
getExtendedFacing(),
tTile.getXCoord(),
tTile.getYCoord(),
tTile.getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
!mMachine);
}
protected final boolean buildPiece(String piece, ItemStack trigger, boolean hintOnly, int horizontalOffset,
int verticalOffset, int depthOffset) {
final IGregTechTileEntity tTile = getBaseMetaTileEntity();
return getCastedStructureDefinition().buildOrHints(
this,
trigger,
piece,
tTile.getWorld(),
getExtendedFacing(),
tTile.getXCoord(),
tTile.getYCoord(),
tTile.getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
hintOnly);
}
@Deprecated
protected final int survivialBuildPiece(String piece, ItemStack trigger, int horizontalOffset, int verticalOffset,
int depthOffset, int elementsBudget, IItemSource source, EntityPlayerMP actor, boolean check) {
final IGregTechTileEntity tTile = getBaseMetaTileEntity();
return getCastedStructureDefinition().survivalBuild(
this,
trigger,
piece,
tTile.getWorld(),
getExtendedFacing(),
tTile.getXCoord(),
tTile.getYCoord(),
tTile.getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
elementsBudget,
source,
actor,
check);
}
protected final int survivialBuildPiece(String piece, ItemStack trigger, int horizontalOffset, int verticalOffset,
int depthOffset, int elementsBudget, ISurvivalBuildEnvironment env, boolean check) {
final IGregTechTileEntity tTile = getBaseMetaTileEntity();
return getCastedStructureDefinition().survivalBuild(
this,
trigger,
piece,
tTile.getWorld(),
getExtendedFacing(),
tTile.getXCoord(),
tTile.getYCoord(),
tTile.getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
elementsBudget,
env,
check);
}
@Deprecated
protected final int survivialBuildPiece(String piece, ItemStack trigger, int horizontalOffset, int verticalOffset,
int depthOffset, int elementsBudget, IItemSource source, EntityPlayerMP actor, boolean check,
boolean checkIfPlaced) {
int built = survivialBuildPiece(
piece,
trigger,
horizontalOffset,
verticalOffset,
depthOffset,
elementsBudget,
source,
actor,
check);
if (checkIfPlaced && built > 0) checkStructure(true, getBaseMetaTileEntity());
return built;
}
protected final int survivialBuildPiece(String piece, ItemStack trigger, int horizontalOffset, int verticalOffset,
int depthOffset, int elementsBudget, ISurvivalBuildEnvironment env, boolean check, boolean checkIfPlaced) {
int built = survivialBuildPiece(
piece,
trigger,
horizontalOffset,
verticalOffset,
depthOffset,
elementsBudget,
env,
check);
if (checkIfPlaced && built > 0) checkStructure(true, getBaseMetaTileEntity());
return built;
}
@Override
public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) {
super.onFirstTick(aBaseMetaTileEntity);
if (aBaseMetaTileEntity.isClientSide())
StructureLibAPI.queryAlignment((IAlignmentProvider) aBaseMetaTileEntity);
}
}
|