-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathMetaTileEntityAssemblyLine.java
More file actions
394 lines (346 loc) · 16.8 KB
/
MetaTileEntityAssemblyLine.java
File metadata and controls
394 lines (346 loc) · 16.8 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package gregtech.common.metatileentities.multi.electric;
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.IDataAccessHatch;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.IMultiblockPart;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.metatileentity.multiblock.RecipeMapMultiblockController;
import gregtech.api.pattern.BlockPattern;
import gregtech.api.pattern.FactoryBlockPattern;
import gregtech.api.pattern.TraceabilityPredicate;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMaps;
import gregtech.api.recipes.ingredients.GTRecipeInput;
import gregtech.api.recipes.properties.impl.ResearchProperty;
import gregtech.api.util.GTUtility;
import gregtech.api.util.RelativeDirection;
import gregtech.client.particle.GTLaserBeamParticle;
import gregtech.client.particle.GTParticleManager;
import gregtech.client.renderer.ICubeRenderer;
import gregtech.client.renderer.texture.Textures;
import gregtech.common.ConfigHolder;
import gregtech.common.blocks.BlockGlassCasing;
import gregtech.common.blocks.BlockMetalCasing;
import gregtech.common.blocks.BlockMultiblockCasing;
import gregtech.common.blocks.MetaBlocks;
import gregtech.common.metatileentities.MetaTileEntities;
import gregtech.common.metatileentities.multi.multiblockpart.MetaTileEntityMultiFluidHatch;
import gregtech.core.sound.GTSoundEvents;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.IItemHandlerModifiable;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import codechicken.lib.vec.Vector3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Function;
import static gregtech.api.util.RelativeDirection.*;
public class MetaTileEntityAssemblyLine extends RecipeMapMultiblockController {
private static final ResourceLocation LASER_LOCATION = GTUtility.gregtechId("textures/fx/laser/laser.png");
private static final ResourceLocation LASER_HEAD_LOCATION = GTUtility
.gregtechId("textures/fx/laser/laser_start.png");
@SideOnly(Side.CLIENT)
private GTLaserBeamParticle[][] beamParticles;
private int beamCount;
private int beamTime;
public MetaTileEntityAssemblyLine(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, RecipeMaps.ASSEMBLY_LINE_RECIPES);
}
@Override
public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {
return new MetaTileEntityAssemblyLine(metaTileEntityId);
}
@NotNull
@Override
protected BlockPattern createStructurePattern() {
FactoryBlockPattern pattern = FactoryBlockPattern.start(FRONT, UP, RIGHT)
.aisle("FIF", "RTR", "SAG", " Y ")
.aisle("FIF", "RTR", "DAG", " Y ").setRepeatable(3, 15)
.aisle("FOF", "RTR", "DAG", " Y ")
.where('S', selfPredicate())
.where('F', states(getCasingState())
.or(autoAbilities(false, true, false, false, false, false, false))
.or(fluidInputPredicate()))
.where('O', abilities(MultiblockAbility.EXPORT_ITEMS)
.addTooltips("gregtech.multiblock.pattern.location_end"))
.where('Y', states(getCasingState())
.or(abilities(MultiblockAbility.INPUT_ENERGY)
.setMinGlobalLimited(1)
.setMaxGlobalLimited(1)))
.where('I', metaTileEntities(MetaTileEntities.ITEM_IMPORT_BUS[GTValues.ULV]))
.where('G', states(getGrateState()))
.where('A',
states(MetaBlocks.MULTIBLOCK_CASING
.getState(BlockMultiblockCasing.MultiblockCasingType.ASSEMBLY_CONTROL)))
.where('R', states(MetaBlocks.TRANSPARENT_CASING.getState(BlockGlassCasing.CasingType.LAMINATED_GLASS)))
.where('T',
states(MetaBlocks.MULTIBLOCK_CASING
.getState(BlockMultiblockCasing.MultiblockCasingType.ASSEMBLY_LINE_CASING)))
.where('D', dataHatchPredicate())
.where(' ', any());
return pattern.build();
}
@NotNull
protected static IBlockState getCasingState() {
return MetaBlocks.METAL_CASING.getState(BlockMetalCasing.MetalCasingType.STEEL_SOLID);
}
@NotNull
protected static IBlockState getGrateState() {
return MetaBlocks.MULTIBLOCK_CASING.getState(BlockMultiblockCasing.MultiblockCasingType.GRATE_CASING);
}
@NotNull
protected static TraceabilityPredicate fluidInputPredicate() {
// block multi-fluid hatches if ordered fluids is enabled
if (ConfigHolder.machines.orderedFluidAssembly) {
return metaTileEntities(MultiblockAbility.REGISTRY.get(MultiblockAbility.IMPORT_FLUIDS).stream()
.filter(mte -> !(mte instanceof MetaTileEntityMultiFluidHatch))
.toArray(MetaTileEntity[]::new))
.setMaxGlobalLimited(4);
}
return abilities(MultiblockAbility.IMPORT_FLUIDS);
}
@NotNull
protected static TraceabilityPredicate dataHatchPredicate() {
// if research is enabled, require the data hatch, otherwise use a grate instead
if (ConfigHolder.machines.enableResearch) {
return abilities(MultiblockAbility.DATA_ACCESS_HATCH, MultiblockAbility.OPTICAL_DATA_RECEPTION)
.setExactLimit(1)
.or(states(getGrateState()));
}
return states(getGrateState());
}
@Override
protected Function<BlockPos, Integer> multiblockPartSorter() {
// player's right when looking at the controller, but the controller's left
return RelativeDirection.LEFT.getSorter(getFrontFacing(), getUpwardsFacing(), isFlipped());
}
@SideOnly(Side.CLIENT)
@Override
public ICubeRenderer getBaseTexture(IMultiblockPart sourcePart) {
if (sourcePart != null) {
// part rendering
if (sourcePart instanceof IDataAccessHatch) {
return Textures.GRATE_CASING_STEEL_FRONT;
} else {
return Textures.SOLID_STEEL_CASING;
}
} else {
// controller rendering
if (isStructureFormed()) {
return Textures.GRATE_CASING_STEEL_FRONT;
} else {
return Textures.SOLID_STEEL_CASING;
}
}
}
@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
super.renderMetaTileEntity(renderState, translation, pipeline);
getFrontOverlay().renderOrientedState(renderState, translation, pipeline, getFrontFacing(),
recipeMapWorkable.isActive(), recipeMapWorkable.isWorkingEnabled());
}
@Override
public SoundEvent getBreakdownSound() {
return GTSoundEvents.BREAKDOWN_MECHANICAL;
}
@Override
public void update() {
super.update();
if (ConfigHolder.client.shader.assemblyLineParticles) {
if (getRecipeMapWorkable().isWorking()) {
int maxBeams = getAbilities(MultiblockAbility.IMPORT_ITEMS).size() + 1;
int maxProgress = getRecipeMapWorkable().getMaxProgress();
// Each beam should be visible for an equal amount of time, which is derived from the maximum number of
// beams and the maximum progress in the recipe.
int beamTime = Math.max(1, maxProgress / maxBeams);
int beamCount = Math.min(maxBeams, getRecipeMapWorkable().getProgress() / beamTime + 1);
if (beamCount != this.beamCount) {
if (beamCount < this.beamCount) {
// if beam count decreases, the last beam in the queue needs to be removed for the sake of fade
// time.
this.beamCount = Math.max(0, beamCount - 1);
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
this.beamTime = beamTime;
this.beamCount = beamCount;
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
} else if (beamCount != 0) {
this.beamTime = 0;
this.beamCount = 0;
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
}
}
@Override
public void writeInitialSyncData(PacketBuffer buf) {
super.writeInitialSyncData(buf);
writeParticles(buf);
}
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
super.receiveInitialSyncData(buf);
readParticles(buf);
}
@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
if (dataId == GregtechDataCodes.UPDATE_PARTICLE) {
readParticles(buf);
} else {
super.receiveCustomData(dataId, buf);
}
}
@Override
public void onRemoval() {
super.onRemoval();
if (getWorld().isRemote && beamParticles != null) {
for (GTLaserBeamParticle[] particle : beamParticles) {
if (particle[0] != null) {
particle[0].setExpired();
particle[1].setExpired();
}
}
beamParticles = null;
}
}
private void writeParticles(@NotNull PacketBuffer buf) {
buf.writeVarInt(beamCount);
buf.writeVarInt(beamTime);
}
@SideOnly(Side.CLIENT)
private void readParticles(@NotNull PacketBuffer buf) {
beamCount = buf.readVarInt();
beamTime = buf.readVarInt();
if (beamParticles == null) {
beamParticles = new GTLaserBeamParticle[17][2];
}
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(getPos());
EnumFacing relativeUp = RelativeDirection.UP.getRelativeFacing(getFrontFacing(), getUpwardsFacing(),
isFlipped());
EnumFacing relativeLeft = RelativeDirection.LEFT.getRelativeFacing(getFrontFacing(), getUpwardsFacing(),
isFlipped());
boolean negativeUp = relativeUp.getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE;
for (int i = 0; i < beamParticles.length; i++) {
GTLaserBeamParticle particle = beamParticles[i][0];
if (i < beamCount && particle == null) {
pos.setPos(getPos());
if (negativeUp) {
// correct for the position of the block corresponding to its negative side
pos.move(relativeUp.getOpposite());
}
Vector3 startPos = new Vector3()
.add(pos.move(relativeLeft, i))
.add( // offset by 0.5 in both non-"upwards" directions
relativeUp.getAxis() == EnumFacing.Axis.X ? 0 : 0.5,
relativeUp.getAxis() == EnumFacing.Axis.Y ? 0 : 0.5,
relativeUp.getAxis() == EnumFacing.Axis.Z ? 0 : 0.5);
Vector3 endPos = startPos.copy()
.subtract(relativeUp.getXOffset(), relativeUp.getYOffset(), relativeUp.getZOffset());
beamParticles[i][0] = createALParticles(startPos, endPos);
pos.setPos(getPos());
if (negativeUp) {
pos.move(relativeUp.getOpposite());
}
startPos = new Vector3()
.add(pos.move(relativeLeft, i)
.move(getFrontFacing().getOpposite(), 2))
.add( // offset by 0.5 in both non-"upwards" directions
relativeUp.getAxis() == EnumFacing.Axis.X ? 0 : 0.5,
relativeUp.getAxis() == EnumFacing.Axis.Y ? 0 : 0.5,
relativeUp.getAxis() == EnumFacing.Axis.Z ? 0 : 0.5);
endPos = startPos.copy()
.subtract(relativeUp.getXOffset(), relativeUp.getYOffset(), relativeUp.getZOffset());
beamParticles[i][1] = createALParticles(startPos, endPos);
// Don't forget to add particles
GTParticleManager.INSTANCE.addEffect(beamParticles[i][0]);
GTParticleManager.INSTANCE.addEffect(beamParticles[i][1]);
} else if (i >= beamCount && particle != null) {
particle.setExpired();
beamParticles[i][0] = null;
beamParticles[i][1].setExpired();
beamParticles[i][1] = null;
}
}
}
@NotNull
@SideOnly(Side.CLIENT)
private GTLaserBeamParticle createALParticles(Vector3 startPos, Vector3 endPos) {
return new GTLaserBeamParticle(this, startPos, endPos, beamTime)
.setBody(LASER_LOCATION)
.setBeamHeight(0.125f)
// Try commenting or adjusting on the next four lines to see what happens
.setDoubleVertical(true)
.setHead(LASER_HEAD_LOCATION)
.setHeadWidth(0.1f)
.setEmit(0.2f);
}
@Override
public boolean checkRecipe(@NotNull Recipe recipe, boolean consumeIfSuccess) {
if (consumeIfSuccess) return true; // don't check twice
// check ordered items
if (ConfigHolder.machines.orderedAssembly) {
List<GTRecipeInput> inputs = recipe.getInputs();
List<IItemHandlerModifiable> itemInputInventory = getAbilities(MultiblockAbility.IMPORT_ITEMS);
// slot count is not enough, so don't try to match it
if (itemInputInventory.size() < inputs.size()) return false;
for (int i = 0; i < inputs.size(); i++) {
if (!inputs.get(i).acceptsStack(itemInputInventory.get(i).getStackInSlot(0))) {
return false;
}
}
// check ordered fluids
if (ConfigHolder.machines.orderedFluidAssembly) {
inputs = recipe.getFluidInputs();
List<IFluidTank> fluidInputInventory = getAbilities(MultiblockAbility.IMPORT_FLUIDS);
// slot count is not enough, so don't try to match it
if (fluidInputInventory.size() < inputs.size()) return false;
for (int i = 0; i < inputs.size(); i++) {
if (!inputs.get(i).acceptsFluid(fluidInputInventory.get(i).getFluid())) {
return false;
}
}
}
}
if (!ConfigHolder.machines.enableResearch || !recipe.hasProperty(ResearchProperty.getInstance())) {
return super.checkRecipe(recipe, consumeIfSuccess);
}
return isRecipeAvailable(getAbilities(MultiblockAbility.DATA_ACCESS_HATCH), recipe) ||
isRecipeAvailable(getAbilities(MultiblockAbility.OPTICAL_DATA_RECEPTION), recipe);
}
private static boolean isRecipeAvailable(@NotNull Iterable<? extends IDataAccessHatch> hatches,
@NotNull Recipe recipe) {
for (IDataAccessHatch hatch : hatches) {
// creative hatches do not need to check, they always have the recipe
if (hatch.isCreative()) return true;
// hatches need to have the recipe available
if (hatch.isRecipeAvailable(recipe)) return true;
}
return false;
}
@Override
public void addInformation(ItemStack stack, @Nullable World world, @NotNull List<String> tooltip,
boolean advanced) {
if (ConfigHolder.machines.orderedAssembly && ConfigHolder.machines.orderedFluidAssembly) {
tooltip.add(I18n.format("gregtech.machine.assembly_line.tooltip_ordered_both"));
} else if (ConfigHolder.machines.orderedAssembly) {
tooltip.add(I18n.format("gregtech.machine.assembly_line.tooltip_ordered_items"));
} else if (ConfigHolder.machines.orderedFluidAssembly) {
tooltip.add(I18n.format("gregtech.machine.assembly_line.tooltip_ordered_fluids"));
}
}
}