use of gregapi.tileentity.logistics.ITileEntityLogistics in project gregtech6 by GregTech6.
the class MultiTileEntityLogisticsCore method onServerTickPre.
@Override
@SuppressWarnings("unchecked")
public void onServerTickPre(boolean aFirst) {
// Sync up with other Stuff that happens to check for visual Updates every 20 Ticks to reduce Lag.
if (SYNC_SECOND) {
int tCPU_Logic = oCPU_Logic, tCPU_Control = oCPU_Control, tCPU_Storage = oCPU_Storage, tCPU_Conversion = oCPU_Conversion;
oCPU_Logic = 0;
oCPU_Control = 0;
oCPU_Storage = 0;
oCPU_Conversion = 0;
if (checkStructure(F) && mEnergy >= 128L + mCPU_Logic * 64L * mCPU_Conversion) {
int tX = getOffsetXN(mFacing, 2), tY = getOffsetYN(mFacing, 2), tZ = getOffsetZN(mFacing, 2);
ItemStackSet<ItemStackContainer> tFilteredFor = new ItemStackSet<>();
final List<LogisticsData> tStackImportsGeneric = new ArrayListNoNulls<>(), tStackImportsSemi = new ArrayListNoNulls<>(), tStackImportsFiltered = new ArrayListNoNulls<>(), tStackExportsGeneric = new ArrayListNoNulls<>(), tStackExportsSemi = new ArrayListNoNulls<>(), tStackExportsFiltered = new ArrayListNoNulls<>(), tStackStorageGeneric = new ArrayListNoNulls<>(), tStackStorageSemi = new ArrayListNoNulls<>(), tStackStorageFiltered = new ArrayListNoNulls<>(), tStackDumps = new ArrayListNoNulls<>(), tFluidImportsGeneric = new ArrayListNoNulls<>(), tFluidImportsSemi = new ArrayListNoNulls<>(), tFluidImportsFiltered = new ArrayListNoNulls<>(), tFluidExportsGeneric = new ArrayListNoNulls<>(), tFluidExportsSemi = new ArrayListNoNulls<>(), tFluidExportsFiltered = new ArrayListNoNulls<>(), tFluidStorageGeneric = new ArrayListNoNulls<>(), tFluidStorageSemi = new ArrayListNoNulls<>(), tFluidStorageFiltered = new ArrayListNoNulls<>(), tExports1[][] = new List[][] { { tFluidExportsFiltered, tStackExportsFiltered }, { tFluidExportsSemi, tStackExportsSemi }, { tFluidExportsGeneric, tStackExportsGeneric }, { tFluidStorageFiltered, tStackStorageFiltered }, { tFluidStorageSemi, tStackStorageSemi }, { tFluidStorageGeneric, tStackStorageGeneric } }, tExports2[][] = new List[][] { { tFluidExportsFiltered, tStackExportsFiltered }, { tFluidExportsSemi, tStackExportsSemi }, { tFluidExportsGeneric, tStackExportsGeneric } }, tImports1[][] = new List[][] { { tFluidImportsGeneric, tStackImportsGeneric }, { tFluidImportsSemi, tStackImportsSemi }, { tFluidImportsFiltered, tStackImportsFiltered } }, tImports2[][] = new List[][] { { tFluidStorageGeneric, tStackStorageGeneric }, { tFluidStorageSemi, tStackStorageSemi }, { tFluidStorageFiltered, tStackStorageFiltered } };
Set<ITileEntityLogistics> tScanning = new HashSetNoNulls<>(), tScanningNext = new HashSetNoNulls<>();
Set<TileEntity> tScanned = new HashSetNoNulls<>();
for (int i = -2; i <= 2; i++) for (int j = -2; j <= 2; j++) for (int k = -2; k <= 2; k++) {
TileEntity tTileEntity = WD.te(worldObj, tX + i, tY + j, tZ + k, T);
if (tScanned.add(tTileEntity) && tTileEntity instanceof ITileEntityLogistics)
tScanning.add((ITileEntityLogistics) tTileEntity);
}
while (!tScanning.isEmpty()) {
for (ITileEntityLogistics tLogistics : tScanning) {
if (tLogistics instanceof ITileEntityLogisticsStorage) {
tFilteredFor.add(((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterItem());
if (tLogistics instanceof ITileEntityLogisticsSemiFilteredItem) {
tFilteredFor.addAll(((ITileEntityLogisticsSemiFilteredItem) tLogistics).getLogisticsFilter(SIDE_ANY));
}
switch(((ITileEntityLogisticsStorage) tLogistics).getLogisticsPriorityFluid()) {
case 1:
tFluidStorageGeneric.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterFluid()));
break;
case 2:
tFluidStorageSemi.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterFluid()));
break;
case 3:
tFluidStorageFiltered.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterFluid()));
break;
}
switch(((ITileEntityLogisticsStorage) tLogistics).getLogisticsPriorityItem()) {
case 1:
tStackStorageGeneric.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterItem()));
break;
case 2:
tStackStorageSemi.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterItem()));
break;
case 3:
tStackStorageFiltered.add(new LogisticsData(new DelegatorTileEntity<>((TileEntity) tLogistics, SIDE_ANY), ((ITileEntityLogisticsStorage) tLogistics).getLogisticsFilterItem()));
break;
}
}
CoverData tCovers = tLogistics.getCoverData();
if (tCovers != null && !tCovers.mStopped) {
for (byte tSide : ALL_SIDES_VALID) if (tCovers.mBehaviours[tSide] instanceof AbstractCoverAttachmentLogistics) {
if (tCovers.mBehaviours[tSide] == CoverLogisticsDisplayCPULogic.INSTANCE) {
tCovers.value(tSide, (short) (tCPU_Logic <= 0 ? 0 : tCPU_Logic >= mCPU_Logic ? 15 : 14 - (int) Math.max(0, Math.min(13, ((mCPU_Logic - tCPU_Logic) * 14L) / mCPU_Logic))), T);
tCovers.visual(tSide, (short) (tCPU_Logic <= 0 ? 0 : tCPU_Logic >= mCPU_Logic ? 10 : 9 - (int) Math.max(0, Math.min(8, ((mCPU_Logic - tCPU_Logic) * 9L) / mCPU_Logic))));
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsDisplayCPUControl.INSTANCE) {
tCovers.value(tSide, (short) (tCPU_Control <= 0 ? 0 : tCPU_Control >= mCPU_Control ? 15 : 14 - (int) Math.max(0, Math.min(13, ((mCPU_Control - tCPU_Control) * 14L) / mCPU_Control))), T);
tCovers.visual(tSide, (short) (tCPU_Control <= 0 ? 0 : tCPU_Control >= mCPU_Control ? 10 : 9 - (int) Math.max(0, Math.min(8, ((mCPU_Control - tCPU_Control) * 9L) / mCPU_Control))));
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsDisplayCPUStorage.INSTANCE) {
tCovers.value(tSide, (short) (tCPU_Storage <= 0 ? 0 : tCPU_Storage >= mCPU_Storage ? 15 : 14 - (int) Math.max(0, Math.min(13, ((mCPU_Storage - tCPU_Storage) * 14L) / mCPU_Storage))), T);
tCovers.visual(tSide, (short) (tCPU_Storage <= 0 ? 0 : tCPU_Storage >= mCPU_Storage ? 10 : 9 - (int) Math.max(0, Math.min(8, ((mCPU_Storage - tCPU_Storage) * 9L) / mCPU_Storage))));
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsDisplayCPUConversion.INSTANCE) {
tCovers.value(tSide, (short) (tCPU_Conversion <= 0 ? 0 : tCPU_Conversion >= mCPU_Conversion ? 15 : 14 - (int) Math.max(0, Math.min(13, ((mCPU_Conversion - tCPU_Conversion) * 14L) / mCPU_Conversion))), T);
tCovers.visual(tSide, (short) (tCPU_Conversion <= 0 ? 0 : tCPU_Conversion >= mCPU_Conversion ? 10 : 9 - (int) Math.max(0, Math.min(8, ((mCPU_Conversion - tCPU_Conversion) * 9L) / mCPU_Conversion))));
continue;
}
DelegatorTileEntity<TileEntity> tAdjacent = tLogistics.getAdjacentTileEntity(tSide);
if (tAdjacent.mTileEntity instanceof ITileEntityLogistics && ((ITileEntityLogistics) tAdjacent.mTileEntity).canLogistics(SIDE_ANY)) {
// Ignore those ones to reduce likelihood of infinite Loops.
} else {
if (tCovers.mBehaviours[tSide] == CoverLogisticsFluidExport.INSTANCE) {
FluidStack tFluid = FL.load(tCovers.mNBTs[tSide], "gt.filter.fluid");
if (tFluid != null && tFluid.getFluid() != null) {
switch(tCovers.mValues[tSide] & 3) {
case 1:
tFluidExportsGeneric.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
case 2:
tFluidExportsSemi.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
default:
tFluidExportsFiltered.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
}
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsFluidImport.INSTANCE) {
FluidStack tFluid = FL.load(tCovers.mNBTs[tSide], "gt.filter.fluid");
if (tFluid != null && tFluid.getFluid() != null) {
switch(tCovers.mValues[tSide] & 3) {
case 1:
tFluidImportsGeneric.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
case 2:
tFluidImportsSemi.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
default:
tFluidImportsFiltered.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
}
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsFluidStorage.INSTANCE) {
FluidStack tFluid = FL.load(tCovers.mNBTs[tSide], "gt.filter.fluid");
if (tFluid != null && tFluid.getFluid() != null) {
switch(tCovers.mValues[tSide] & 3) {
case 1:
tFluidStorageGeneric.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
case 2:
tFluidStorageSemi.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
default:
tFluidStorageFiltered.add(new LogisticsData(tAdjacent, tFluid.getFluid()));
break;
}
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsItemExport.INSTANCE) {
ItemStack tStack = ST.load(tCovers.mNBTs[tSide], "gt.filter.item");
if (ST.valid(tStack)) {
tFilteredFor.add(tStack);
switch(tCovers.mValues[tSide] & 3) {
case 1:
tStackExportsGeneric.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
case 2:
tStackExportsSemi.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
default:
tStackExportsFiltered.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
}
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsItemImport.INSTANCE) {
ItemStack tStack = ST.load(tCovers.mNBTs[tSide], "gt.filter.item");
if (ST.valid(tStack)) {
tFilteredFor.add(tStack);
switch(tCovers.mValues[tSide] & 3) {
case 1:
tStackImportsGeneric.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
case 2:
tStackImportsSemi.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
default:
tStackImportsFiltered.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
}
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsItemStorage.INSTANCE) {
ItemStack tStack = ST.load(tCovers.mNBTs[tSide], "gt.filter.item");
if (ST.valid(tStack)) {
tFilteredFor.add(tStack);
switch(tCovers.mValues[tSide] & 3) {
case 1:
tStackStorageGeneric.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
case 2:
tStackStorageSemi.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
default:
tStackStorageFiltered.add(new LogisticsData(tAdjacent, tStack, (tCovers.mValues[tSide] >> 2) & 127));
break;
}
}
continue;
}
LogisticsData tTarget = new LogisticsData(tAdjacent, (tCovers.mValues[tSide] >> 2) & 127);
if (tCovers.mBehaviours[tSide] == CoverLogisticsGenericDump.INSTANCE) {
tStackDumps.add(tTarget);
continue;
}
int tDefault = (tCovers.mValues[tSide] & 3);
boolean aAllowFluids = T;
if (tAdjacent.mTileEntity instanceof ITileEntityLogisticsSemiFilteredItem) {
aAllowFluids = F;
ItemStackSet<ItemStackContainer> tFilter = ((ITileEntityLogisticsSemiFilteredItem) tAdjacent.mTileEntity).getLogisticsFilter(tAdjacent.mSideOfTileEntity);
if (tFilter != null) {
tFilteredFor.addAll(tFilter);
if (tDefault == 0)
tDefault = 2;
}
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsGenericExport.INSTANCE) {
switch(tDefault) {
default:
if (aAllowFluids)
tFluidExportsGeneric.add(tTarget);
tStackExportsGeneric.add(tTarget);
break;
case 2:
if (aAllowFluids)
tFluidExportsSemi.add(tTarget);
tStackExportsSemi.add(tTarget);
break;
case 3:
if (aAllowFluids)
tFluidExportsFiltered.add(tTarget);
tStackExportsFiltered.add(tTarget);
break;
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsGenericImport.INSTANCE) {
switch(tDefault) {
default:
if (aAllowFluids)
tFluidImportsGeneric.add(tTarget);
tStackImportsGeneric.add(tTarget);
break;
case 2:
if (aAllowFluids)
tFluidImportsSemi.add(tTarget);
tStackImportsSemi.add(tTarget);
break;
case 3:
if (aAllowFluids)
tFluidImportsFiltered.add(tTarget);
tStackImportsFiltered.add(tTarget);
break;
}
continue;
}
if (tCovers.mBehaviours[tSide] == CoverLogisticsGenericStorage.INSTANCE) {
switch(tDefault) {
default:
if (aAllowFluids)
tFluidStorageGeneric.add(tTarget);
tStackStorageGeneric.add(tTarget);
break;
case 2:
if (aAllowFluids)
tFluidStorageSemi.add(tTarget);
tStackStorageSemi.add(tTarget);
break;
case 3:
if (aAllowFluids)
tFluidStorageFiltered.add(tTarget);
tStackStorageFiltered.add(tTarget);
break;
}
continue;
}
}
}
}
if (tLogistics.getWorld() == worldObj)
for (byte tSide : ALL_SIDES_VALID) if (tLogistics.canLogistics(tSide)) {
int tMaxDistance = Math.max(Math.abs(tLogistics.getOffsetX(tSide) - tX), Math.max(Math.abs(tLogistics.getOffsetY(tSide) - tY), Math.abs(tLogistics.getOffsetZ(tSide) - tZ)));
if (tMaxDistance <= mCPU_Control + 2) {
oCPU_Control = Math.max(oCPU_Control, tMaxDistance - 2);
DelegatorTileEntity<TileEntity> tAdjacent = tLogistics.getAdjacentTileEntity(tSide);
if (tAdjacent.mTileEntity instanceof ITileEntityLogistics && ((ITileEntityLogistics) tAdjacent.mTileEntity).canLogistics(tAdjacent.mSideOfTileEntity) && tScanned.add(tAdjacent.mTileEntity))
tScanningNext.add((ITileEntityLogistics) tAdjacent.mTileEntity);
}
}
}
tScanning.clear();
tScanning.addAll(tScanningNext);
tScanningNext.clear();
}
while (++oCPU_Logic <= mCPU_Logic) {
boolean tBreak = F;
// Import Export Business
for (List<LogisticsData>[] tExports : tExports1) {
for (List<LogisticsData>[] tImports : tImports1) {
if (moveFluids(tImports[0], tExports[0])) {
tBreak = T;
break;
}
if (moveStacks(tImports[1], tExports[1])) {
tBreak = T;
break;
}
}
if (tBreak)
break;
}
if (tBreak)
continue;
for (List<LogisticsData>[] tExports : tExports2) {
for (List<LogisticsData>[] tImports : tImports2) {
if (moveFluids(tImports[0], tExports[0])) {
tBreak = T;
break;
}
if (moveStacks(tImports[1], tExports[1])) {
tBreak = T;
break;
}
}
if (tBreak)
break;
}
if (tBreak)
continue;
// Defragmentation
if (moveFluids(tFluidStorageGeneric, tFluidStorageFiltered))
continue;
if (moveStacks(tStackStorageGeneric, tStackStorageFiltered))
continue;
if (moveFluids(tFluidStorageGeneric, tFluidStorageSemi))
continue;
if (moveStacks(tStackStorageGeneric, tStackStorageSemi))
continue;
// Dump
for (LogisticsData tImport : tStackStorageGeneric) {
for (LogisticsData tExport : tStackDumps) {
for (int j = 0; j < mCPU_Conversion; j++) {
long tMoved = ST.move(tImport.mTarget, tExport.mTarget, tFilteredFor, F, F, T, F, 64, 1, 64, 1);
if (tMoved > 0) {
oCPU_Conversion = Math.max(oCPU_Conversion, j + 1);
mEnergy -= tMoved;
tBreak = T;
continue;
}
break;
}
if (tBreak)
break;
}
if (tBreak)
break;
}
if (tBreak)
continue;
// Core didn't actually get used.
oCPU_Logic--;
break;
}
}
}
mEnergy -= 20 + mCPU_Logic + mCPU_Control + mCPU_Storage + mCPU_Conversion;
if (mEnergy < 0)
mEnergy = 0;
}
Aggregations