diff options
Diffstat (limited to 'src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java')
-rw-r--r-- | src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java | 216 |
1 files changed, 120 insertions, 96 deletions
diff --git a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java index a782663ed2..d44ada526f 100644 --- a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java @@ -27,6 +27,7 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.EnumChatFormatting; import net.minecraft.world.EnumSkyBlock; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -38,10 +39,10 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Random; import static gregtech.api.enums.GT_Values.NW; import static gregtech.api.enums.GT_Values.V; +import static gregtech.api.objects.XSTR.XSTR_INSTANCE; /** * NEVER INCLUDE THIS FILE IN YOUR MOD!!! @@ -54,19 +55,19 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE protected long mStoredEnergy = 0, mStoredSteam = 0; protected int mAverageEUInputIndex = 0, mAverageEUOutputIndex = 0; protected boolean mReleaseEnergy = false; - protected int[] mAverageEUInput = new int[11], mAverageEUOutput = new int[11]; + protected int[] mAverageEUInput = new int[]{0, 0, 0, 0, 0}, mAverageEUOutput = new int[]{0, 0, 0, 0, 0}; + private boolean mEnergyStateReady = false; private boolean[] mActiveEUInputs = new boolean[]{false, false, false, false, false, false}, mActiveEUOutputs = new boolean[]{false, false, false, false, false, false}; private byte[] mSidedRedstone = new byte[]{15, 15, 15, 15, 15, 15}; private int[] mCoverSides = new int[]{0, 0, 0, 0, 0, 0}, mCoverData = new int[]{0, 0, 0, 0, 0, 0}, mTimeStatistics = new int[GregTech_API.TICKS_FOR_LAG_AVERAGING]; private boolean mHasEnoughEnergy = true, mRunningThroughTick = false, mInputDisabled = false, mOutputDisabled = false, mMuffler = false, mLockUpgrade = false, mActive = false, mRedstone = false, mWorkUpdate = false, mSteamConverter = false, mInventoryChanged = false, mWorks = true, mNeedsUpdate = true, mNeedsBlockUpdate = true, mSendClientData = false, oRedstone = false; - private boolean mEnergyStateReady = false; private byte mColor = 0, oColor = 0, mStrongRedstone = 0, oRedstoneData = 63, oTextureData = 0, oUpdateData = 0, oTexturePage=0, oLightValueClient = -1, oLightValue = -1, mLightValue = 0, mOtherUpgrades = 0, mFacing = 0, oFacing = 0, mWorkData = 0; private int mDisplayErrorCode = 0, oX = 0, oY = 0, oZ = 0, mTimeStatisticsIndex = 0, mLagWarningCount = 0; private short mID = 0; private long mTickTimer = 0, oOutput = 0, mAcceptedAmperes = Long.MAX_VALUE; private String mOwnerName = ""; private NBTTagCompound mRecipeStuff = new NBTTagCompound(); - + private static final Field ENTITY_ITEM_HEALTH_FIELD; static { @@ -95,7 +96,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { super.writeToNBT(aNBT); } catch (Throwable e) { - GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } try { @@ -123,7 +124,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE aNBT.setTag("GT.CraftingComponents", mRecipeStuff); aNBT.setInteger("nbtVersion", GT_Mod.TOTAL_VERSION); } catch (Throwable e) { - GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } try { @@ -143,12 +144,12 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { mMetaTileEntity.saveNBTData(aNBT); } catch (Throwable e) { - GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } } } catch (Throwable e) { - GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered CRITICAL ERROR while saving MetaTileEntity, the Chunk whould've been corrupted by now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } } @@ -191,7 +192,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE mSidedRedstone = aNBT.getByteArray("mRedstoneSided"); mRecipeStuff = aNBT.getCompoundTag("GT.CraftingComponents"); int nbtVersion = aNBT.getInteger("nbtVersion"); - + if (mCoverData.length != 6) mCoverData = new int[]{0, 0, 0, 0, 0, 0}; if (mCoverSides.length != 6) mCoverSides = new int[]{0, 0, 0, 0, 0, 0}; if (mSidedRedstone.length != 6) @@ -215,7 +216,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { mMetaTileEntity.loadNBTData(aNBT); } catch (Throwable e) { - GT_Log.err.println("Encountered Exception while loading MetaTileEntity, the Server should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered Exception while loading MetaTileEntity, the Server should've crashed now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } } @@ -455,6 +456,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE } } } + // We're ready to tell about our energy state - Only used server side mEnergyStateReady = true; } @@ -642,7 +644,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { mMetaTileEntity.receiveClientEvent((byte) aEventID, (byte) aValue); } catch (Throwable e) { - GT_Log.err.println("Encountered Exception while receiving Data from the Server, the Client should've been crashed by now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered Exception while receiving Data from the Server, the Client should've been crashed by now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } } @@ -699,25 +701,25 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE public ArrayList<String> getDebugInfo(EntityPlayer aPlayer, int aLogLevel) { ArrayList<String> tList = new ArrayList<String>(); if (aLogLevel > 2) { - tList.add("Meta-ID: " + mID + (canAccessData() ? " valid" : " invalid") + (mMetaTileEntity == null ? " MetaTileEntity == null!" : " ")); + tList.add("Meta-ID: " +EnumChatFormatting.BLUE+ mID +EnumChatFormatting.RESET + (canAccessData() ? EnumChatFormatting.GREEN+" valid"+EnumChatFormatting.RESET : EnumChatFormatting.RED+" invalid"+EnumChatFormatting.RESET) + (mMetaTileEntity == null ? EnumChatFormatting.RED+" MetaTileEntity == null!"+EnumChatFormatting.RESET : " ")); } if (aLogLevel > 1) { if (mTimeStatistics.length > 0) { double tAverageTime = 0; for (int tTime : mTimeStatistics) tAverageTime += tTime; - tList.add("This particular TileEntity has caused an average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ms over the last " + mTimeStatistics.length + " ticks."); + tList.add("Average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ms since " + mTimeStatistics.length + " ticks."); } if (mLagWarningCount > 0) { - tList.add("This TileEntity has also caused " + (mLagWarningCount >= 10 ? "more than 10" : mLagWarningCount) + " Lag Spike Warnings (anything taking longer than " + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + "ms) on the Server."); + tList.add("Caused " + (mLagWarningCount >= 10 ? "more than 10" : mLagWarningCount) + " Lag Spike Warnings (anything taking longer than " + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + "ms) on the Server."); } - tList.add("Is" + (mMetaTileEntity.isAccessAllowed(aPlayer) ? " " : " not ") + "accessible for you"); + tList.add("Is" + (mMetaTileEntity.isAccessAllowed(aPlayer) ? " " : EnumChatFormatting.RED+" not "+EnumChatFormatting.RESET) + "accessible for you"); } if (aLogLevel > 0) { if (getSteamCapacity() > 0 && hasSteamEngineUpgrade()) tList.add(getStoredSteam() + " of " + getSteamCapacity() + " Steam"); - tList.add("Machine is " + (mActive ? "active" : "inactive")); + tList.add("Machine is " + (mActive ? EnumChatFormatting.GREEN+"active"+EnumChatFormatting.RESET : EnumChatFormatting.RED+"inactive"+EnumChatFormatting.RESET)); if (!mHasEnoughEnergy) - tList.add("ATTENTION: This Device consumes Energy at a higher Rate than you input. You could insert more to speed up the process."); + tList.add(EnumChatFormatting.RED+"ATTENTION: This Device needs more power."+EnumChatFormatting.RESET); } return mMetaTileEntity.getSpecialDebugInfo(this, aPlayer, aLogLevel, tList); } @@ -970,6 +972,12 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE } @Override + public boolean energyStateReady() { + if (!isServerSide()) return true; + else return mEnergyStateReady; + } + + @Override public boolean inputEnergyFrom(byte aSide) { if (aSide == 6) return true; if (isServerSide()) return (aSide >= 0 && aSide < 6 ? mActiveEUInputs[aSide] : false) && !mReleaseEnergy; @@ -1202,14 +1210,13 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE public void dropItems(ItemStack tItem){ if(tItem==null)return; - Random tRandom = new Random(); - EntityItem tItemEntity = new EntityItem(this.worldObj, this.xCoord + tRandom.nextFloat() * 0.8F + 0.1F, this.yCoord + tRandom.nextFloat() * 0.8F + 0.1F, this.zCoord + tRandom.nextFloat() * 0.8F + 0.1F, new ItemStack(tItem.getItem(), tItem.stackSize, tItem.getItemDamage())); + EntityItem tItemEntity = new EntityItem(this.worldObj, this.xCoord + XSTR_INSTANCE.nextFloat() * 0.8F + 0.1F, this.yCoord + XSTR_INSTANCE.nextFloat() * 0.8F + 0.1F, this.zCoord + XSTR_INSTANCE.nextFloat() * 0.8F + 0.1F, new ItemStack(tItem.getItem(), tItem.stackSize, tItem.getItemDamage())); if (tItem.hasTagCompound()) { tItemEntity.getEntityItem().setTagCompound((NBTTagCompound) tItem.getTagCompound().copy()); } - tItemEntity.motionX = (tRandom.nextGaussian() * 0.0500000007450581D); - tItemEntity.motionY = (tRandom.nextGaussian() * 0.0500000007450581D + 0.2000000029802322D); - tItemEntity.motionZ = (tRandom.nextGaussian() * 0.0500000007450581D); + tItemEntity.motionX = (XSTR_INSTANCE.nextGaussian() * 0.0500000007450581D); + tItemEntity.motionY = (XSTR_INSTANCE.nextGaussian() * 0.0500000007450581D + 0.2000000029802322D); + tItemEntity.motionZ = (XSTR_INSTANCE.nextGaussian() * 0.0500000007450581D); tItemEntity.hurtResistantTime = 999999; tItemEntity.lifespan = 60000; try { @@ -1296,11 +1303,13 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sSoftHammerList)) { if (GT_ModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) { if (mWorks) disableWorking(); - else enableWorking();{ - String tChat = trans("090","Machine Processing: ") + (isAllowedToWork() ? trans("088","Enabled") : trans("087","Disabled")); - if(getMetaTileEntity() !=null && getMetaTileEntity().hasAlternativeModeText()) - tChat = getMetaTileEntity().getAlternativeModeText(); - GT_Utility.sendChatToPlayer(aPlayer, tChat);} + else enableWorking(); + { + String tChat = trans("090", "Machine Processing: ") + (isAllowedToWork() ? trans("088", "Enabled") : trans("087", "Disabled")); + if (getMetaTileEntity() != null && getMetaTileEntity().hasAlternativeModeText()) + tChat = getMetaTileEntity().getAlternativeModeText(); + GT_Utility.sendChatToPlayer(aPlayer, tChat); + } GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(101), 1.0F, -1, xCoord, yCoord, zCoord); } return true; @@ -1308,10 +1317,22 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sSolderingToolList)) { byte tSide = GT_Utility.determineWrenchingSide(aSide, aX, aY, aZ); - if (GT_ModHandler.useSolderingIron(tCurrentItem, aPlayer)) { + if (mMetaTileEntity.onSolderingToolRightClick(aSide, tSide, aPlayer, aX, aY, aZ)) { + //logic handled internally + GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(103), 1.0F, -1, xCoord, yCoord, zCoord); + } else if (GT_ModHandler.useSolderingIron(tCurrentItem, aPlayer)) { mStrongRedstone ^= (1 << tSide); GT_Utility.sendChatToPlayer(aPlayer, trans("091","Redstone Output at Side ") + tSide + trans("092"," set to: ") + ((mStrongRedstone & (1 << tSide)) != 0 ? trans("093","Strong") : trans("094","Weak"))); GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(103), 3.0F, -1, xCoord, yCoord, zCoord); + } + return true; + } + + if (GT_Utility.isStackInList(tCurrentItem, GregTech_API.sWireCutterList)) { + byte tSide = GT_Utility.determineWrenchingSide(aSide, aX, aY, aZ); + if (mMetaTileEntity.onWireCutterRightClick(aSide, tSide, aPlayer, aX, aY, aZ)) { + //logic handled internally + GT_Utility.sendSoundToPlayers(worldObj, GregTech_API.sSoundList.get(100), 1.0F, -1, xCoord, yCoord, zCoord); } return true; } @@ -1373,7 +1394,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { if (hasValidMetaTileEntity()) return mMetaTileEntity.onRightclick(this, aPlayer, aSide, aX, aY, aZ); } catch (Throwable e) { - GT_Log.err.println("Encountered Exception while rightclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered Exception while rightclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } @@ -1385,7 +1406,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE try { if (aPlayer != null && hasValidMetaTileEntity()) mMetaTileEntity.onLeftclick(this, aPlayer); } catch (Throwable e) { - GT_Log.err.println("Encountered Exception while leftclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immidietly to GregTech Intergalactical!!!"); + GT_Log.err.println("Encountered Exception while leftclicking TileEntity, the Game should've crashed now, but I prevented that. Please report immediately to GregTech Intergalactical!!!"); e.printStackTrace(GT_Log.err); } } @@ -1620,21 +1641,15 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE @Override public long getAverageElectricInput() { int rEU = 0; - for (int i = 0; i < mAverageEUInput.length; i++) { - if (i != mAverageEUInputIndex) - rEU += mAverageEUInput[i]; - } - return rEU / (mAverageEUInput.length - 1); + for (int tEU : mAverageEUInput) rEU += tEU; + return rEU / mAverageEUInput.length; } @Override public long getAverageElectricOutput() { int rEU = 0; - for (int i = 0; i < mAverageEUOutput.length; i++) { - if (i != mAverageEUOutputIndex) - rEU += mAverageEUOutput[i]; - } - return rEU / (mAverageEUOutput.length - 1); + for (int tEU : mAverageEUOutput) rEU += tEU; + return rEU / mAverageEUOutput.length; } @Override @@ -1959,60 +1974,69 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE public void onEntityCollidedWithBlock(World aWorld, int aX, int aY, int aZ, Entity collider) { mMetaTileEntity.onEntityCollidedWithBlock(aWorld, aX, aY, aZ, collider); } - - /** - * Shifts the machine Inventory index according to the change in Input/Output Slots. - * This is NOT done automatically. If you want to change slot count for a machine this method needs to be adapted. - * Currently this method only works for GT_MetaTileEntity_BasicMachine - * @param slotIndex The original Inventory index - * @param nbtVersion The GregTech version in which the original Inventory Index was saved. - * @return The corrected Inventory index - */ - private int shiftInventoryIndex(int slotIndex, int nbtVersion){ - int oldInputSize, newInputSize, oldOutputSize, newOutputSize; - int chemistryUpdateVersion = GT_Mod.calculateTotalGTVersion(509, 31); - if (mID >= 211 && mID <= 218) {//Assembler - if (nbtVersion < chemistryUpdateVersion) { - oldInputSize = 2; - oldOutputSize = 1; - } else { - return slotIndex; - } - newInputSize = 6; - newOutputSize = 1; - } else if (mID >= 421 && mID <= 428){//Chemical Reactor - if (nbtVersion < chemistryUpdateVersion) { - oldInputSize = 2; - oldOutputSize = 1; - } else { - return slotIndex; - } - newInputSize = 2; - newOutputSize = 2; - } else if (mID >= 531 && mID <= 538) {//Distillery - if (nbtVersion < chemistryUpdateVersion) { - oldInputSize = 1; - oldOutputSize = 0; - } else { - return slotIndex; - } - newInputSize = 1; - newOutputSize = 1; - } else { - return slotIndex; - } - int indexShift = 0; - if (slotIndex >= GT_MetaTileEntity_BasicMachine.OTHER_SLOT_COUNT + oldInputSize) { - indexShift += newInputSize - oldInputSize; - } - if (slotIndex >= GT_MetaTileEntity_BasicMachine.OTHER_SLOT_COUNT + oldInputSize + oldOutputSize) { - indexShift += newOutputSize - oldOutputSize; - } - return slotIndex + indexShift; - } - - @Override - public boolean energyStateReady() { - return isClientSide() || mEnergyStateReady; - } + + /** + * Shifts the machine Inventory index according to the change in Input/Output Slots. + * This is NOT done automatically. If you want to change slot count for a machine this method needs to be adapted. + * Currently this method only works for GT_MetaTileEntity_BasicMachine + * @param slotIndex The original Inventory index + * @param nbtVersion The GregTech version in which the original Inventory Index was saved. + * @return The corrected Inventory index + */ + private int shiftInventoryIndex(int slotIndex, int nbtVersion){ + int oldInputSize, newInputSize, oldOutputSize, newOutputSize; + int chemistryUpdateVersion = GT_Mod.calculateTotalGTVersion(509, 31); + if (mID >= 211 && mID <= 218) {//Assembler + if (nbtVersion < chemistryUpdateVersion) { + oldInputSize = 2; + oldOutputSize = 1; + } else { + return slotIndex; + } + newInputSize = 6; + newOutputSize = 1; + + + } else if (mID >= 421 && mID <= 428){//Chemical Reactor + if (nbtVersion < chemistryUpdateVersion) { + oldInputSize = 2; + oldOutputSize = 1; + } else { + return slotIndex; + } + newInputSize = 2; + newOutputSize = 2; + + } else if (mID >= 531 && mID <= 538) {//Distillery + if (nbtVersion < chemistryUpdateVersion) { + oldInputSize = 1; + oldOutputSize = 0; + } else { + return slotIndex; + } + newInputSize = 1; + newOutputSize = 1; + } else if (mID >= 581 && mID <=588){//Mixer + if (nbtVersion < chemistryUpdateVersion) { + oldInputSize = 4; + oldOutputSize = 1; + }else{ + return slotIndex; + } + newInputSize = 6; + newOutputSize = 1; + + } else { + return slotIndex; + } + + int indexShift = 0; + if (slotIndex >= GT_MetaTileEntity_BasicMachine.OTHER_SLOT_COUNT + oldInputSize) {indexShift += newInputSize - oldInputSize; + } + if (slotIndex >= GT_MetaTileEntity_BasicMachine.OTHER_SLOT_COUNT + oldInputSize + oldOutputSize) { + indexShift += newOutputSize - oldOutputSize; + } + return slotIndex + indexShift; + } } + |