aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/common/tileentities/GTMTE_ItemServer.java
blob: cfff360ce469904d5d92babc262d2acb5835e6e1 (plain)
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
package common.tileentities;

import java.util.ArrayList;
import java.util.HashSet;

import org.lwjgl.input.Keyboard;

import common.Blocks;
import common.blocks.Block_ItemServerDrive;
import common.blocks.Block_ItemServerIOPort;
import common.blocks.Block_ItemServerRackCasing;
import gregtech.api.enums.Textures.BlockIcons;
import gregtech.api.gui.GT_GUIContainer_MultiMachine;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
import gregtech.api.objects.GT_RenderedTexture;
import kekztech.MultiItemHandler;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.common.util.ForgeDirection;
import util.MultiBlockTooltipBuilder;
import util.Vector3i;
import util.Vector3ic;

public class GTMTE_ItemServer extends GT_MetaTileEntity_MultiBlockBase {
	
	private static final int BASE_SEGMENT_ENERGY_COST = 1;
	private static final int BASE_PER_ITEM_CAPACITY = 1024;
	private static final int BASE_ITEM_TYPES_PER_SEGMENT = 4;
	
	private final Block_ItemServerDrive DRIVE = (Block_ItemServerDrive) Blocks.itemServerDrive;
	private final Block_ItemServerRackCasing CASING = (Block_ItemServerRackCasing) Blocks.itemServerRackCasing;
	private final Block_ItemServerIOPort IO_PORT = (Block_ItemServerIOPort) Blocks.itemServerIOPort;
	private final String ALU_FRAME_BOX_NAME = "gt.blockmachines";
	private final int ALU_FRAME_BOX_META = 6;
	private final int CASING_TEXTURE_ID = 176;
	
	private MultiItemHandler mih;
	private HashSet<TE_ItemServerIOPort> ioPorts = new HashSet<>(); 
	private int sliceCount = 0;
	
	public GTMTE_ItemServer(int aID, String aName, String aNameRegional) {
		super(aID, aName, aNameRegional);
	}

	public GTMTE_ItemServer(String aName) {
		super(aName);
	}
	
	@Override
	public IMetaTileEntity newMetaEntity(IGregTechTileEntity var1) {
		return new GTMTE_ItemServer(super.mName);
	}
	
	@Override
	public String[] getDescription() {
		final MultiBlockTooltipBuilder b = new MultiBlockTooltipBuilder();
		b.addInfo("[W.I.P - Probably doesn't work]")
				.addInfo("High-Tech item storage!")
				.addInfo("Variable length: Slices 2-4 can be repeated as long as the total length does not exceed 16 blocks.")
				.addInfo("Each segment offers storage for 128 item types")
				.addInfo("Storage capacity per item depends on the controller configuration.")
				.addInfo("Insert an Integrated Circuit into the controller with your desired configuration.")
				.addInfo("The base configuration (0) is 1024 items per type. For each higher level, the capacity quadruples.")
				.addInfo("Each slice also adds 1EU/t of power consumption and doubles with rising configuration values.")
				.addInfo("Valid config values are from zero to eight.")
				.addSeparator()
				.beginStructureBlock(3, 5, 4)
				.addController("Front Bottom Center")
				.addEnergyHatch("Any casing")
				.addOtherStructurePart("Front slice", "3x5x1 Item Server Rack Casing")
				.addOtherStructurePart("2nd and 3rd slice, center", "1x4x1 Aluminium Frame Box")
				.addOtherStructurePart("2nd and 3rd slice, top", "3x1x1 Item Server Rack Casing")
				.addOtherStructurePart("2nd and 3rd slice, sides", "2x 1x4x1 Item Server Drive")
				.addOtherStructurePart("Back slice", "3x5x1 Item Server Rack Casing")
				.signAndFinalize("Kekzdealer");
		if(!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
			return b.getInformation();
		} else {
			return b.getStructureInformation();
		}
	}
	
	@Override
	public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, byte aSide, byte aFacing, byte aColorIndex,
			boolean aActive, boolean aRedstone) {
		return aSide == aFacing
				? new ITexture[]{BlockIcons.casingTexturePages[1][48],
						new GT_RenderedTexture(aActive
								? BlockIcons.OVERLAY_FRONT_LARGE_CHEMICAL_REACTOR_ACTIVE
								: BlockIcons.OVERLAY_FRONT_LARGE_CHEMICAL_REACTOR)}
				: new ITexture[]{BlockIcons.casingTexturePages[1][48]};
	}

	public Object getClientGUI(int aID, InventoryPlayer aPlayerInventory, IGregTechTileEntity aBaseMetaTileEntity) {
		return new GT_GUIContainer_MultiMachine(aPlayerInventory, aBaseMetaTileEntity, this.getLocalName(),
				"MultiblockDisplay.png");
	}

	@Override
	public boolean isCorrectMachinePart(ItemStack var1) {
		return true;
	}
	
	@Override
	public boolean checkRecipe(ItemStack guiSlotItem) {
		final int config = (guiSlotItem != null && guiSlotItem.getUnlocalizedName().equals("gt.integrated_circuit")) 
				? Math.min(8, guiSlotItem.getItemDamage()) : 0;
				
		this.mEfficiency = 10000 - (this.getIdealStatus() - this.getRepairStatus()) * 1000;
		this.mEfficiencyIncrease = 10000;
		this.mEUt = (int) -(BASE_SEGMENT_ENERGY_COST * sliceCount * Math.pow(2, config));
		super.mMaxProgresstime = 20;
		
		mih.setPerTypeCapacity((int) (BASE_PER_ITEM_CAPACITY * Math.pow(4, config)));
		
		return true;
	}
	
	@Override
	public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
		super.onPostTick(aBaseMetaTileEntity, aTick);
		
		if(mih != null) {
			mih.setLock(!super.getBaseMetaTileEntity().isActive());
		}
	}
	
	public Vector3ic rotateOffsetVector(Vector3ic forgeDirection, int x, int y, int z) {
		final Vector3i offset = new Vector3i();
		
		// either direction on z-axis
		if(forgeDirection.x() == 0 && forgeDirection.z() == -1) {
			offset.x = x;
			offset.y = y;
			offset.z = z;
		}
		if(forgeDirection.x() == 0 && forgeDirection.z() == 1) {
			offset.x = -x;
			offset.y = y;
			offset.z = -z;
		}
		// either direction on x-axis
		if(forgeDirection.x() == -1 && forgeDirection.z() == 0) {
			offset.x = z;
			offset.y = y;
			offset.z = -x;
		}
		if(forgeDirection.x() == 1 && forgeDirection.z() == 0) {
			offset.x = -z;
			offset.y = y;
			offset.z = x;
		}
		
		return offset;
	}
	
	@Override
	public boolean checkMachine(IGregTechTileEntity thisController, ItemStack guiSlotItem) {
		// Figure out the vector for the direction the back face of the controller is facing
		final Vector3ic forgeDirection = new Vector3i(
				ForgeDirection.getOrientation(thisController.getBackFacing()).offsetX,
				ForgeDirection.getOrientation(thisController.getBackFacing()).offsetY,
				ForgeDirection.getOrientation(thisController.getBackFacing()).offsetZ
				); 
		boolean formationChecklist = true;
		
		// Front slice
		for(int X = -1; X <= 1; X++) {
			for(int Y = 0; Y <= 4; Y++) {
				if(X == 0 && Y == 0) {
					continue; // is controller
				}
				
				// Get next TE
				final Vector3ic offset = rotateOffsetVector(forgeDirection, X, Y, 0);
				IGregTechTileEntity currentTE = 
						thisController.getIGregTechTileEntityOffset(offset.x(), offset.y(), offset.z());
				
				if(!super.addMaintenanceToMachineList(currentTE, CASING_TEXTURE_ID)
						&& !super.addEnergyInputToMachineList(currentTE, CASING_TEXTURE_ID)) {
					
					// Is casing or IO port?
					if(thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == CASING) {
						// Is casing, but there's no casing requirements
					} else if (thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == IO_PORT) {
						final TE_ItemServerIOPort port = 
								(TE_ItemServerIOPort) thisController.getWorld().getTileEntity(
										thisController.getXCoord() + offset.x(), 
										thisController.getYCoord() + offset.y(),
										thisController.getZCoord() + offset.z());
						ioPorts.add(port);
					} else {
						formationChecklist = false;
					}
				}
			}
		}
		
		// Check slices
		int segmentsFound = 0;
		int zOffset = -1; // -1 is the first slice after the front one. It goes in negative direction.
		
		while(segmentsFound < 5) {
			if(checkSegment(thisController, forgeDirection, zOffset)) {
				segmentsFound++;
				zOffset -= 3; // Each segment is 3 blocks long, so progress Z by -3
				
				System.out.println("Item Server segment approved: " + segmentsFound);
			} else {
				System.out.println("Item Server segment rejected: " + (segmentsFound + 1));
				break;
			}
		}
		
		if(segmentsFound < 1) {
			System.out.println("At least one slice required for storage");
			formationChecklist = false;
		}
		
		if(this.mEnergyHatches.size() < 1) {
			System.out.println("At least one energy hatch is required!");
			formationChecklist = false;
		}
		
		if(this.mMaintenanceHatches.size() < 1) {
			System.out.println("You need a maintenance hatch to do maintenance.");
			formationChecklist = false;
		}
		
		if(formationChecklist) {
			sliceCount = segmentsFound;
			
			if(mih == null) {
				mih = new MultiItemHandler();
				mih.setItemTypeCapacity(segmentsFound * BASE_ITEM_TYPES_PER_SEGMENT);
			}
			System.out.println("Configuring " + ioPorts.size() + " ports");
			for(TE_ItemServerIOPort port : ioPorts) {
				port.setMultiItemHandler(mih);
			}
		}
		
		return formationChecklist;
	}
	
	public boolean checkSegment(IGregTechTileEntity thisController, Vector3ic forgeDirection, int zOffset) {
		boolean formationChecklist = true;
		// Slice by slice
		for(int Z = 0; Z >= -2; Z--) {
			// Is not back slice
			if(Z != -2) {
				// Left to right
				for(int X = -1; X <= 1; X++) {
					// Bottom to top
					for(int Y = 0; Y <= 4; Y++) {
						final Vector3ic offset = rotateOffsetVector(forgeDirection, X, Y, zOffset + Z);
						
						// Server rack roof
						if(Y == 4) {
							final IGregTechTileEntity currentTE = 
									thisController.getIGregTechTileEntityOffset(offset.x(), offset.y(), offset.z());
							
							if(!super.addMaintenanceToMachineList(currentTE, CASING_TEXTURE_ID)
									&& !super.addEnergyInputToMachineList(currentTE, CASING_TEXTURE_ID)) {
								// Is casing or IO port?
								if(thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == CASING) {
									// Is casing, but there's no casing requirements
								} else if (thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == IO_PORT) {
									final TE_ItemServerIOPort port = 
											(TE_ItemServerIOPort) thisController.getWorld().getTileEntity(
													thisController.getXCoord() + offset.x(), 
													thisController.getYCoord() + offset.y(),
													thisController.getZCoord() + offset.z());
									ioPorts.add(port);
								} else {
									formationChecklist = false;
								}
							}
						}
						
						// Middle wall is aluminium frame boxes
						else if(Y <= 3 && X == 0) {
							if(!(thisController.getBlockOffset(offset.x(), offset.y(), offset.z()).getUnlocalizedName().equals(ALU_FRAME_BOX_NAME))
									|| !(thisController.getMetaIDOffset(offset.x(), offset.y(), offset.z()) == ALU_FRAME_BOX_META)) {
								formationChecklist = false;
							}
						}
						
						// Side walls are item server drives
						else if(Y <= 3 && X != 0) {
							if(!(thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == DRIVE)) {
								formationChecklist = false;
							}
						}
					}
				}
			} else {
				// Back slice
				for(int X = -1; X <= 1; X++) {
					for(int Y = 0; Y <= 4; Y++) {
						
						final Vector3ic offset = rotateOffsetVector(forgeDirection, X, Y, zOffset + Z);
						IGregTechTileEntity currentTE = 
								thisController.getIGregTechTileEntityOffset(offset.x(), offset.y(), offset.z());
						
						if(!super.addMaintenanceToMachineList(currentTE, CASING_TEXTURE_ID)
								&& !super.addEnergyInputToMachineList(currentTE, CASING_TEXTURE_ID)) {
							// Is casing or IO port?
							if(thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == CASING) {
								// Is casing, but there's no casing requirements
							} else if (thisController.getBlockOffset(offset.x(), offset.y(), offset.z()) == IO_PORT) {
								final TE_ItemServerIOPort port = 
										(TE_ItemServerIOPort) thisController.getWorld().getTileEntity(
												thisController.getXCoord() + offset.x(), 
												thisController.getYCoord() + offset.y(),
												thisController.getZCoord() + offset.z());
								ioPorts.add(port);
							} else {
								formationChecklist = false;
							}
						}
					}
				}
			}
		}
		
		
		return formationChecklist;
	}
	
	@Override
	public String[] getInfoData() {
		final ArrayList<String> ll = new ArrayList<>();//mfh.getInfoData();
		
		ll.add(EnumChatFormatting.YELLOW + "Operational Data:" + EnumChatFormatting.RESET);
		ll.add("Per-Item Capacity: " + mih.getPerTypeCapacity());
		ll.add("Item-Type Capacity: " + BASE_ITEM_TYPES_PER_SEGMENT * sliceCount);
		ll.add("Running Cost: "
				// mEUt does not naturally reflect efficiency status. Do that here.
				+ ((-super.mEUt) * 10000 / Math.max(1000, super.mEfficiency)) + "EU/t");
		ll.add("Maintenance Status: " + ((super.getRepairStatus() == super.getIdealStatus()) 
				? EnumChatFormatting.GREEN + "Working perfectly" + EnumChatFormatting.RESET 
						: EnumChatFormatting.RED + "Has Problems" + EnumChatFormatting.RESET));
		ll.add("---------------------------------------------");
		
		final String[] a = new String[ll.size()];
		return ll.toArray(a);
	}
	
	@Override
	public void saveNBTData(NBTTagCompound nbt) {
		nbt = (nbt == null) ? new NBTTagCompound() : nbt;
		
		super.saveNBTData(nbt);
	}
	
	@Override
	public void loadNBTData(NBTTagCompound nbt) {
		nbt = (nbt == null) ? new NBTTagCompound() : nbt;
		
		super.loadNBTData(nbt);
	}
	
	@Override
	public boolean isGivingInformation() {
		return true;
	}

	@Override
	public int getMaxEfficiency(ItemStack var1) {
		return 10000;
	}

	@Override
	public int getPollutionPerTick(ItemStack var1) {
		return 0;
	}

	@Override
	public int getDamageToComponent(ItemStack var1) {
		return 0;
	}

	@Override
	public boolean explodesOnComponentBreak(ItemStack var1) {
		return false;
	}
}