Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save machine state #1579

Open
wants to merge 12 commits into
base: unstable
Choose a base branch
from
66 changes: 66 additions & 0 deletions src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2086,4 +2086,70 @@ private int shiftInventoryIndex(int slotIndex, int nbtVersion){
return slotIndex + indexShift;
}

/**
* Helper matrix for rotating all covers concurrently.
* First index is old facing.
* Second index is new facing.
* Third index is original cover side.
* Value is new cover side.
* Rotating between opposite horizontal facings will leave top/bottom alone, and
* rotating between top/bottom will leave north/south alone.
* 0=Up, 1=Down, 2=North, 3=South, 4=West, 5=East
*/
private final int[][][] COVER_ROTATION_MATRIX = {
{{0, 1, 2, 3, 4, 5}, // Up to Up
{1, 0, 2, 3, 5, 4}, // Up to Down
{2, 3, 1, 0, 4, 5}, // Up to North
{3, 2, 0, 1, 4, 5}, // Up to South
{4, 5, 2, 3, 1, 0}, // Up to West
{5, 4, 2, 3, 0, 1}}, // Up to East

{{1, 0, 2, 3, 5, 4}, // Down to Up
{0, 1, 2, 3, 4, 5}, // Down to Down
{3, 2, 0, 1, 4, 5}, // Down to North
{2, 3, 1, 0, 4, 5}, // Down to South
{5, 4, 2, 3, 0, 1}, // Down to West
{4, 5, 2, 3, 1, 0}}, // Down to East

{{3, 2, 0, 1, 4, 5}, // North to Up
{2, 3, 1, 0, 4, 5}, // North to Down
{0, 1, 2, 3, 4, 5}, // North to North
{0, 1, 3, 2, 5, 4}, // North to South
{0, 1, 4, 5, 3, 2}, // North to West
{0, 1, 5, 4, 2, 3}}, // North to East

{{2, 3, 1, 0, 4, 5}, // South to Up
{3, 2, 0, 1, 4, 5}, // South to Down
{0, 1, 3, 2, 5, 4}, // South to North
{0, 1, 2, 3, 4, 5}, // South to South
{0, 1, 5, 4, 2, 3}, // South to West
{0, 1, 4, 5, 3, 2}}, // South to East

{{5, 4, 2, 3, 0, 1}, // West to Up
{4, 5, 2, 3, 1, 0}, // West to Down
{0, 1, 5, 4, 2, 3}, // West to North
{0, 1, 4, 5, 3, 2}, // West to South
{0, 1, 2, 3, 4, 5}, // West to West
{0, 1, 3, 2, 5, 4}}, // West to East

{{4, 5, 2, 3, 1, 0}, // East to Up
{5, 4, 2, 3, 0, 1}, // East to Down
{0, 1, 4, 5, 3, 2}, // East to North
{0, 1, 5, 4, 2, 3}, // East to South
{0, 1, 3, 2, 5, 4}, // East to West
{0, 1, 2, 3, 4, 5}} // East to East
};

public void rotateCovers(int oldFacing, int newFacing) {
if (oldFacing < 0 || oldFacing >= 6 || newFacing < 0 || newFacing >= 6) return;
int[] tCoverSides = new int[6];
int[] tCoverData = new int[6];
for (int i = 0; i < 6; i++) {
tCoverSides[COVER_ROTATION_MATRIX[oldFacing][newFacing][i]] = mCoverSides[i];
tCoverData[COVER_ROTATION_MATRIX[oldFacing][newFacing][i]] = mCoverData[i];
}
mCoverSides = tCoverSides;
mCoverData = tCoverData;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gregtech.api.gui.GT_GUIContainer_BasicMachine;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.objects.GT_ItemStack;
import gregtech.api.objects.GT_RenderedTexture;
import gregtech.api.objects.XSTR;
Expand Down Expand Up @@ -48,6 +49,7 @@ public abstract class GT_MetaTileEntity_BasicMachine extends GT_MetaTileEntity_B
public final int mInputSlotCount, mAmperage;
public boolean mAllowInputFromOutputSide = false, mFluidTransfer = false, mItemTransfer = false, mHasBeenUpdated = false, mStuttering = false, mCharge = false, mDecharge = false;
public int mMainFacing = -1, mProgresstime = 0, mMaxProgresstime = 0, mEUt = 0, mOutputBlocked = 0;
private int mOldMainFacing = -1;
public FluidStack mOutputFluid;
public String mGUIName = "", mNEIName = "";
public GT_MetaTileEntity_MultiBlockBase mCleanroom;
Expand Down Expand Up @@ -116,7 +118,12 @@ protected boolean isValidMainFacing(byte aSide) {

public boolean setMainFacing(byte aSide){
if (!isValidMainFacing(aSide)) return false;
mOldMainFacing = mMainFacing;
mMainFacing = aSide;
if (getBaseMetaTileEntity() instanceof BaseMetaTileEntity) {
((BaseMetaTileEntity)getBaseMetaTileEntity()).rotateCovers(mOldMainFacing, mMainFacing);
getBaseMetaTileEntity().issueClientUpdate();
}
if(getBaseMetaTileEntity().getFrontFacing() == mMainFacing){
getBaseMetaTileEntity().setFrontFacing(GT_Utility.getOppositeSide(aSide));
}
Expand Down Expand Up @@ -417,6 +424,7 @@ public void loadNBTData(NBTTagCompound aNBT) {
mAllowInputFromOutputSide = aNBT.getBoolean("mAllowInputFromOutputSide");
mEUt = aNBT.getInteger("mEUt");
mMainFacing = aNBT.getInteger("mMainFacing");
if (aNBT.hasKey("mOldMainFacing")) mOldMainFacing = aNBT.getInteger("mOldMainFacing");
mProgresstime = aNBT.getInteger("mProgresstime");
mMaxProgresstime = aNBT.getInteger("mMaxProgresstime");
mOutputFluid = FluidStack.loadFluidStackFromNBT(aNBT.getCompoundTag("mOutputFluid"));
Expand All @@ -425,6 +433,15 @@ public void loadNBTData(NBTTagCompound aNBT) {
for (int i = 0; i < mOutputItems.length; i++) mOutputItems[i] = GT_Utility.loadItem(aNBT, "mOutputItem" + i);
}

@Override
public void setItemNBT(NBTTagCompound aNBT) {
super.setItemNBT(aNBT);
aNBT.setBoolean("mFluidTransfer", mFluidTransfer);
aNBT.setBoolean("mItemTransfer", mItemTransfer);
aNBT.setBoolean("mAllowInputFromOutputSide", mAllowInputFromOutputSide);
aNBT.setInteger("mOldMainFacing", mMainFacing);
}

@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
super.onPostTick(aBaseMetaTileEntity, aTick);
Expand Down Expand Up @@ -536,6 +553,11 @@ protected void doDisplayThings() {
mMainFacing = getBaseMetaTileEntity().getFrontFacing();
}
if (mMainFacing >= 2 && !mHasBeenUpdated) {
if (mOldMainFacing >= 0) {
if (getBaseMetaTileEntity() instanceof BaseMetaTileEntity) {
((BaseMetaTileEntity)getBaseMetaTileEntity()).rotateCovers(mOldMainFacing, mMainFacing);
}
}
mHasBeenUpdated = true;
getBaseMetaTileEntity().setFrontFacing(getBaseMetaTileEntity().getBackFacing());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,4 @@ public boolean allowPutStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex
protected void onEmptyingContainerWhenEmpty(){
//Do nothing
}
}
}